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.java218
1 files changed, 218 insertions, 0 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
new file mode 100644
index 00000000..508c3b83
--- /dev/null
+++ b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.java
@@ -0,0 +1,218 @@
+/*
+ * 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.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.view.*;
+import android.widget.*;
+import net.lacolaco.smileessence.Application;
+import net.lacolaco.smileessence.R;
+import net.lacolaco.smileessence.entity.Account;
+import net.lacolaco.smileessence.logging.Logger;
+import net.lacolaco.smileessence.notification.Notificator;
+import net.lacolaco.smileessence.preference.InternalPreferenceHelper;
+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 AppCompatActivity implements AdapterView.OnItemClickListener, AbsListView.OnItemLongClickListener {
+ public static final int REQUEST_OAUTH = 10;
+ private EditAccountsAdapter adapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ setTheme(Application.getThemeResId());
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.layout_edit_list);
+
+ Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
+ setSupportActionBar(toolbar);
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+
+ ListView listView = (ListView) findViewById(R.id.listview_edit_list);
+ adapter = new EditAccountsAdapter();
+ listView.setAdapter(adapter);
+ listView.setOnItemClickListener(this);
+ listView.setOnItemLongClickListener(this);
+
+ Logger.debug("onCreate");
+ }
+
+ @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_ALWAYS);
+ return true;
+ }
+
+ @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.getModelId());
+ if (account == Application.getCurrentAccount()) {
+ setCurrentAccount(adapter.getItem(0));
+ }
+ }, false);
+ return true;
+ } else {
+ Notificator.getInstance().publish("You can't remove last account");
+ return false;
+ }
+ }
+
+ @Override
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ // switch current application to this account
+ Account account = adapter.getItem(position);
+ setCurrentAccount(account);
+ safeFinish();
+ }
+
+ @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() {
+ if (Application.getCurrentAccount() != null) {
+ setResult(RESULT_OK);
+ finish();
+ } else {
+ Notificator.getInstance().publish("[TODO] No account selected"); // TODO
+ }
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ switch (requestCode) {
+ case REQUEST_OAUTH: {
+ receiveOAuth(requestCode, resultCode, data);
+ break;
+ }
+ default: {
+ Logger.error(String.format("[BUG] unexpected activity result: reqCode=%d, resCode=%d", requestCode, 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);
+ if (Application.getCurrentAccount() == null) {
+ setCurrentAccount(account);
+ }
+ } else {
+ Logger.error(requestCode);
+ Notificator.getInstance().publish(R.string.notice_error_authenticate);
+ }
+ }
+
+ private void setCurrentAccount(Account account) {
+ Application.setCurrentAccount(account);
+ InternalPreferenceHelper.getInstance().set(R.string.key_last_used_account_id, account.getModelId());
+ }
+
+ 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).getModelId();
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ if (convertView == null) {
+ LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ convertView = inflater.inflate(R.layout.menu_item_simple_text, null);
+ }
+ Account account = getItem(position);
+ TextView textView = (TextView) convertView.findViewById(R.id.textView_menuItem_simple);
+ String text = account.getUser().getScreenName();
+ if (account == Application.getCurrentAccount()) {
+ text = "(*) " + text;
+ }
+ textView.setText(text); // TODO: show profile image
+
+ return convertView;
+ }
+
+ public int add(Account account) {
+ accounts.add(account);
+ notifyDataSetChanged();
+ return accounts.size() - 1;
+ }
+
+ public Account removeAt(int position) {
+ Account account = accounts.remove(position);
+ notifyDataSetChanged();
+ return account;
+ }
+ }
+}