aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java')
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java257
1 files changed, 0 insertions, 257 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java
deleted file mode 100644
index af891fec..00000000
--- a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java
+++ /dev/null
@@ -1,257 +0,0 @@
-/*
- * The MIT License (MIT)
- *
- * Copyright (c) 2012-2014 lacolaco.net
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package net.lacolaco.smileessence.activity;
-
-import android.app.Activity;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.net.Uri;
-import android.os.Bundle;
-import android.support.annotation.NonNull;
-import android.support.v4.app.ActivityCompat;
-import android.support.v4.content.ContextCompat;
-import android.view.Menu;
-import android.view.MenuItem;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.*;
-import com.android.volley.toolbox.NetworkImageView;
-import net.lacolaco.smileessence.Application;
-import net.lacolaco.smileessence.R;
-import net.lacolaco.smileessence.World;
-import net.lacolaco.smileessence.data.Account;
-import net.lacolaco.smileessence.data.ImageCache;
-import net.lacolaco.smileessence.logging.Logger;
-import net.lacolaco.smileessence.twitter.OAuthSession;
-import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class ManageAccountsActivity extends Activity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
- public static final String INTENT_KEY_NOINIT = "noInit";
- private static final int REQUEST_OAUTH = 10;
- private static final int REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION = 11;
- private EditAccountsAdapter adapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- // Check if it is initiated from launcher
- if (!getIntent().getBooleanExtra(INTENT_KEY_NOINIT, false)) {
- int wextPermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
- if (wextPermission != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION);
- }
-
- // Skip this activity if app is already started
- World currentWorld = Application.getCurrentWorld();
- if (currentWorld != null) {
- goToWorld(currentWorld);
- return;
- }
- }
-
- setContentView(R.layout.layout_edit_list);
- adapter = new EditAccountsAdapter();
- ListView listView = (ListView) findViewById(R.id.listview_edit_list);
- listView.setAdapter(adapter);
- listView.setOnItemClickListener(this);
- listView.setOnItemLongClickListener(this);
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
- switch (requestCode) {
- case REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION: {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // OK
- World currentWorld = Application.getCurrentWorld();
- if (currentWorld != null) {
- goToWorld(currentWorld);
- }
- } else {
- Application.toast(R.string.notice_error_storage_permission);
- // TODO: Kill Process?
- finish();
- }
- break;
- }
- }
- }
-
- private void goToWorld(World world) {
- // Continue the existing MainActivity
- Intent intent = new Intent(this, MainActivity.class);
- intent.setData(Uri.parse("smileessence://mainactivity/?user_id=" + world.getAccount().getUserId()));
- finish();
- startActivity(intent);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuItem add = menu.add(Menu.NONE, R.id.menu_edit_list_add, Menu.NONE, "");
- add.setIcon(android.R.drawable.ic_menu_add);
- add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
- return true;
- }
-
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
- Account account = adapter.getItem(i);
- goToWorld(Application.getWorld(account.getUserId()));
- }
-
- @Override
- public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
- if (adapter.getCount() > 1) {
- // remove account from application
- Account account = adapter.getItem(i);
- ConfirmDialogFragment.show(this, getString(R.string.dialog_confirm_clear_account, account.getUser().getScreenName()), () -> {
- adapter.removeAt(i);
- Account.unregister(account.getUserId());
- }, false);
- return true;
- } else {
- Application.toast(R.string.notice_cant_remove_last_account);
- return false;
- }
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_edit_list_add: {
- startActivityForResult(new Intent(this, OAuthActivity.class), REQUEST_OAUTH);
- break;
- }
- case android.R.id.home: {
- safeFinish();
- }
- }
- return true;
- }
-
- @Override
- public void onBackPressed() {
- safeFinish();
- }
-
- private void safeFinish() {
- World currentWorld = Application.getCurrentWorld();
-
- if (currentWorld != null) {
- goToWorld(currentWorld);
- } else {
- setResult(RESULT_CANCELED);
- finish();
- }
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch (requestCode) {
- case REQUEST_OAUTH: {
- receiveOAuth(requestCode, resultCode, data);
- break;
- }
- default: {
- Logger.error("[BUG] unexpected activity result: reqCode=" + requestCode + ", resCode=" + resultCode);
- break;
- }
- }
- }
-
- private void receiveOAuth(int requestCode, int resultCode, Intent data) {
- if (resultCode == RESULT_OK) {
- Account account = Account.register(data.getStringExtra(OAuthSession.KEY_TOKEN),
- data.getStringExtra(OAuthSession.KEY_TOKEN_SECRET),
- data.getLongExtra(OAuthSession.KEY_USER_ID, -1L),
- data.getStringExtra(OAuthSession.KEY_SCREEN_NAME));
- adapter.add(account);
- } else {
- Logger.error(requestCode);
- Application.toast(R.string.notice_error_authenticate);
- }
- }
-
- private class EditAccountsAdapter extends BaseAdapter {
- private final List<Account> accounts;
-
- public EditAccountsAdapter() {
- accounts = new ArrayList<>(Account.all());
- }
-
- @Override
- public int getCount() {
- return accounts.size();
- }
-
- @Override
- public Account getItem(int position) {
- return accounts.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return accounts.get(position).getUserId();
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = getLayoutInflater().inflate(R.layout.list_item_account, null);
- }
- Account account = getItem(position);
- NetworkImageView iconView = (NetworkImageView) convertView.findViewById(R.id.account_icon);
- iconView.setImageUrl(account.getUser().getProfileImageUrlOriginal(), ImageCache.getImageLoader());
-
- TextView textView = (TextView) convertView.findViewById(R.id.account_text_view);
- String text = "@" + account.getUser().getScreenName();
- textView.setText(text);
-
- return convertView;
- }
-
- public int add(Account account) {
- if (!accounts.contains(account)) {
- accounts.add(account);
- notifyDataSetChanged();
- return accounts.size() - 1;
- } else {
- return accounts.indexOf(account);
- }
- }
-
- public Account removeAt(int position) {
- Account account = accounts.remove(position);
- if (account != null) {
- notifyDataSetChanged();
- }
- return account;
- }
- }
-}