From aa10b9ff053d853b1a6e3473eb66d5e9df8d5f56 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 6 Nov 2017 13:57:25 +0100 Subject: [PATCH] code refactoring to provide shorter account enabled check --- .../siacs/conversations/entities/Account.java | 4 ++++ .../services/NotificationService.java | 2 +- .../services/XmppConnectionService.java | 15 +++++++-------- .../conversations/ui/EditAccountActivity.java | 2 +- .../ui/ManageAccountActivity.java | 18 +++++++++--------- .../conversations/ui/SettingsActivity.java | 6 +++--- .../ui/StartConversationActivity.java | 2 +- .../conversations/utils/ExceptionHelper.java | 2 +- 8 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 6967fe5fa..e716ebda6 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -108,6 +108,10 @@ public class Account extends AbstractEntity { return key == null || Boolean.parseBoolean(key); } + public boolean isEnabled() { + return !isOptionSet(Account.OPTION_DISABLED); + } + public enum State { DISABLED(false,false), OFFLINE(false), diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index eaa07d07d..634384f78 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -682,7 +682,7 @@ public class NotificationService { if (account.isOnlineAndConnected()) { connected++; enabled++; - } else if (!account.isOptionSet(Account.OPTION_DISABLED)) { + } else if (account.isEnabled()) { enabled++; } } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 9792cd3a3..cfcc4b900 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -358,7 +358,7 @@ public class XmppConnectionService extends Service { scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode()); } else if (account.getStatus() == Account.State.OFFLINE || account.getStatus() == Account.State.DISABLED) { resetSendingToWaiting(account); - if (!account.isOptionSet(Account.OPTION_DISABLED) && isInLowPingTimeoutMode(account)) { + if (account.isEnabled() && isInLowPingTimeoutMode(account)) { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": went into offline state during low ping mode. reconnecting now"); reconnectAccount(account, true, false); } else { @@ -2325,7 +2325,7 @@ public class XmppConnectionService extends Service { private boolean hasEnabledAccounts() { for (Account account : this.accounts) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { return true; } } @@ -3088,7 +3088,7 @@ public class XmppConnectionService extends Service { account.setXmppConnection(connection); } boolean hasInternet = hasInternetConnection(); - if (!account.isOptionSet(Account.OPTION_DISABLED) && hasInternet) { + if (account.isEnabled() && hasInternet) { if (!force) { disconnect(account, false); } @@ -3539,7 +3539,7 @@ public class XmppConnectionService extends Service { public void refreshAllPresences() { boolean includeIdleTimestamp = checkListeners() && broadcastLastActivity(); for (Account account : getAccounts()) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { sendPresence(account, includeIdleTimestamp); } } @@ -3585,7 +3585,7 @@ public class XmppConnectionService extends Service { public List findContacts(Jid jid, String accountJid) { ArrayList contacts = new ArrayList<>(); for (Account account : getAccounts()) { - if ((!account.isOptionSet(Account.OPTION_DISABLED) || accountJid != null) + if ((account.isEnabled() || accountJid != null) && (accountJid == null || accountJid.equals(account.getJid().toBareJid().toString()))) { Contact contact = account.getRoster().getContactFromRoster(jid); if (contact != null) { @@ -3598,8 +3598,7 @@ public class XmppConnectionService extends Service { public Conversation findFirstMuc(Jid jid) { for (Conversation conversation : getConversations()) { - if (conversation.getJid().toBareJid().equals(jid.toBareJid()) - && conversation.getMode() == Conversation.MODE_MULTI) { + if (conversation.getAccount().isEnabled() && conversation.getJid().toBareJid().equals(jid.toBareJid()) && conversation.getMode() == Conversation.MODE_MULTI) { return conversation; } } @@ -3830,7 +3829,7 @@ public class XmppConnectionService extends Service { account.setPresenceStatus(status); account.setPresenceStatusMessage(statusMessage); databaseBackend.updateAccount(account); - if (!account.isOptionSet(Account.OPTION_DISABLED) && send) { + if (account.isEnabled() && send) { sendPresence(account); } } diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index 96b466e72..16f01b398 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -253,7 +253,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } mHostname.setError(null); mPort.setError(null); - if (!mAccount.isOptionSet(Account.OPTION_DISABLED) + if (mAccount.isEnabled() && !registerNewAccount && !mInitMode) { finish(); diff --git a/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java index 72a0abba1..378351958 100644 --- a/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java @@ -127,15 +127,15 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda R.menu.manageaccounts_context, menu); AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo; this.selectedAccount = accountList.get(acmi.position); - if (this.selectedAccount.isOptionSet(Account.OPTION_DISABLED)) { + if (this.selectedAccount.isEnabled()) { + menu.findItem(R.id.mgmt_account_enable).setVisible(false); + menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(Config.supportOpenPgp()); + menu.findItem(R.id.mgmt_account_change_presence).setVisible(manuallyChangePresence()); + } else { menu.findItem(R.id.mgmt_account_disable).setVisible(false); menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(false); menu.findItem(R.id.mgmt_account_publish_avatar).setVisible(false); menu.findItem(R.id.mgmt_account_change_presence).setVisible(false); - } else { - menu.findItem(R.id.mgmt_account_enable).setVisible(false); - menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(Config.supportOpenPgp()); - menu.findItem(R.id.mgmt_account_change_presence).setVisible(manuallyChangePresence()); } menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString()); } @@ -280,7 +280,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda List list = new ArrayList<>(); synchronized (this.accountList) { for (Account account : this.accountList) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { list.add(account); } } @@ -293,7 +293,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda private boolean accountsLeftToDisable() { synchronized (this.accountList) { for (Account account : this.accountList) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { return true; } } @@ -304,7 +304,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda private boolean accountsLeftToEnable() { synchronized (this.accountList) { for (Account account : this.accountList) { - if (account.isOptionSet(Account.OPTION_DISABLED)) { + if (!account.isEnabled()) { return true; } } @@ -316,7 +316,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda List list = new ArrayList<>(); synchronized (this.accountList) { for (Account account : this.accountList) { - if (account.isOptionSet(Account.OPTION_DISABLED)) { + if (!account.isEnabled()) { list.add(account); } } diff --git a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java index 5aabc01dc..1cdea2522 100644 --- a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java @@ -278,7 +278,7 @@ public class SettingsActivity extends XmppActivity implements builder.setTitle(R.string.pref_delete_omemo_identities); final List accounts = new ArrayList<>(); for(Account account : xmppConnectionService.getAccounts()) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { accounts.add(account.getJid().toBareJid().toString()); } } @@ -345,7 +345,7 @@ public class SettingsActivity extends XmppActivity implements if (xmppConnectionServiceBound) { for (Account account : xmppConnectionService.getAccounts()) { if (account.setResource(resource)) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { XmppConnection connection = account.getXmppConnection(); if (connection != null) { connection.resetStreamId(); @@ -410,7 +410,7 @@ public class SettingsActivity extends XmppActivity implements private void reconnectAccounts() { for (Account account : xmppConnectionService.getAccounts()) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { + if (account.isEnabled()) { xmppConnectionService.reconnectAccountInBackground(account); } } diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index 442c8ef02..8f2c88193 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -867,7 +867,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU private boolean handleJid(Invite invite) { Account account = xmppConnectionService.findAccountByJid(invite.getJid()); - if (account != null && !account.isOptionSet(Account.OPTION_DISABLED)) { + if (account != null && account.isEnabled()) { if (invite.hasFingerprints() && xmppConnectionService.verifyFingerprints(account,invite.getFingerprints())) { Toast.makeText(this,R.string.verified_fingerprints,Toast.LENGTH_SHORT).show(); } diff --git a/src/main/java/eu/siacs/conversations/utils/ExceptionHelper.java b/src/main/java/eu/siacs/conversations/utils/ExceptionHelper.java index b3184dff1..3943a5c4c 100644 --- a/src/main/java/eu/siacs/conversations/utils/ExceptionHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/ExceptionHelper.java @@ -50,7 +50,7 @@ public class ExceptionHelper { List accounts = service.getAccounts(); Account account = null; for (int i = 0; i < accounts.size(); ++i) { - if (!accounts.get(i).isOptionSet(Account.OPTION_DISABLED)) { + if (accounts.get(i).isEnabled()) { account = accounts.get(i); break; }