From 960b7343d3bf2511be4a6da838b9cf4a0f0a3c43 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 3 Jan 2015 13:36:48 +0100 Subject: disable / enable all accounts --- .../conversations/ui/ManageAccountActivity.java | 100 +++++++++++++++++---- src/main/res/menu/manageaccounts.xml | 28 +++--- src/main/res/values/strings.xml | 2 + 3 files changed, 103 insertions(+), 27 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java index 906a16cc..b3ab5ee6 100644 --- a/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java @@ -26,21 +26,25 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda protected Account selectedAccount = null; - protected List accountList = new ArrayList(); + protected final List accountList = new ArrayList<>(); protected ListView accountListView; protected AccountAdapter mAccountAdapter; - @Override - public void onAccountUpdate() { + + @Override + public void onAccountUpdate() { + synchronized (this.accountList) { accountList.clear(); accountList.addAll(xmppConnectionService.getAccounts()); - runOnUiThread(new Runnable() { - - @Override - public void run() { - mAccountAdapter.notifyDataSetChanged(); - } - }); } + runOnUiThread(new Runnable() { + + @Override + public void run() { + invalidateOptionsMenu(); + mAccountAdapter.notifyDataSetChanged(); + } + }); + } @Override protected void onCreate(Bundle savedInstanceState) { @@ -91,6 +95,14 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.manageaccounts, menu); + MenuItem enableAll = menu.findItem(R.id.action_enable_all); + if (!accountsLeftToEnable()) { + enableAll.setVisible(false); + } + MenuItem disableAll = menu.findItem(R.id.action_disable_all); + if (!accountsLeftToDisable()) { + disableAll.setVisible(false); + } return true; } @@ -120,12 +132,18 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { - case R.id.action_add_account: - startActivity(new Intent(getApplicationContext(), - EditAccountActivity.class)); - break; - default: - break; + case R.id.action_add_account: + startActivity(new Intent(getApplicationContext(), + EditAccountActivity.class)); + break; + case R.id.action_disable_all: + disableAllAccounts(); + break; + case R.id.action_enable_all: + enableAllAccounts(); + break; + default: + break; } return super.onOptionsItemSelected(item); } @@ -158,6 +176,56 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda startActivity(intent); } + private void disableAllAccounts() { + List list = new ArrayList<>(); + synchronized (this.accountList) { + for (Account account : this.accountList) { + if (!account.isOptionSet(Account.OPTION_DISABLED)) { + list.add(account); + } + } + } + for(Account account : list) { + disableAccount(account); + } + } + + private boolean accountsLeftToDisable() { + synchronized (this.accountList) { + for (Account account : this.accountList) { + if (!account.isOptionSet(Account.OPTION_DISABLED)) { + return true; + } + } + return false; + } + } + + private boolean accountsLeftToEnable() { + synchronized (this.accountList) { + for (Account account : this.accountList) { + if (account.isOptionSet(Account.OPTION_DISABLED)) { + return true; + } + } + return false; + } + } + + private void enableAllAccounts() { + List list = new ArrayList<>(); + synchronized (this.accountList) { + for (Account account : this.accountList) { + if (account.isOptionSet(Account.OPTION_DISABLED)) { + list.add(account); + } + } + } + for(Account account : list) { + enableAccount(account); + } + } + private void disableAccount(Account account) { account.setOption(Account.OPTION_DISABLED, true); xmppConnectionService.updateAccount(account); diff --git a/src/main/res/menu/manageaccounts.xml b/src/main/res/menu/manageaccounts.xml index b5cd9b50..5b04484f 100644 --- a/src/main/res/menu/manageaccounts.xml +++ b/src/main/res/menu/manageaccounts.xml @@ -1,15 +1,21 @@ - + - - + + + + \ No newline at end of file diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index ebb95278..73259228 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -403,4 +403,6 @@ Current password New password Password should not be empty + Enable all accounts + Disable all accounts -- cgit v1.2.3