From 1fa5025767336509cf127d62e6de59b7ec28cc36 Mon Sep 17 00:00:00 2001 From: bjoern Date: Fri, 15 Oct 2021 11:32:36 +0200 Subject: [PATCH] simpler account switching (#2080) remove the "progress" dialog that shows up for a fraction of a second. as all accounts are already in memory since the switch to dc_accounts_t, there is nothing that can take so much time to satisfy a "wait" dialog. the switching is now at least visually nicer - however, due to less task switching and less code, it should also be a bit faster. --- .../AccountSelectionListFragment.java | 5 +- .../securesms/connect/AccountManager.java | 56 +++++-------------- 2 files changed, 17 insertions(+), 44 deletions(-) diff --git a/src/org/thoughtcrime/securesms/accounts/AccountSelectionListFragment.java b/src/org/thoughtcrime/securesms/accounts/AccountSelectionListFragment.java index f670d0d64..1f8dd2c1f 100644 --- a/src/org/thoughtcrime/securesms/accounts/AccountSelectionListFragment.java +++ b/src/org/thoughtcrime/securesms/accounts/AccountSelectionListFragment.java @@ -16,7 +16,6 @@ import com.b44t.messenger.DcAccounts; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.connect.AccountManager; -import org.thoughtcrime.securesms.connect.AccountManager.SwitchAccountAsyncTask; import org.thoughtcrime.securesms.connect.DcHelper; import org.thoughtcrime.securesms.mms.GlideApp; import org.thoughtcrime.securesms.util.ViewUtil; @@ -74,9 +73,9 @@ public class AccountSelectionListFragment extends DialogFragment AccountSelectionListFragment.this.dismiss(); int accountId = contact.getAccountId(); if (accountId == DC_CONTACT_ID_ADD_ACCOUNT) { - new SwitchAccountAsyncTask(activity, R.string.one_moment, 0, null).execute(); + AccountManager.getInstance().switchAccountAndStartActivity(activity, 0, null); } else if (accountId != DcHelper.getAccounts(activity).getSelectedAccount().getAccountId()) { - new SwitchAccountAsyncTask(activity, R.string.switching_account, accountId, null).execute(); + AccountManager.getInstance().switchAccountAndStartActivity(activity, accountId, null); } } diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index 50021133a..4109b4060 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -102,51 +102,25 @@ public class AccountManager { accounts.removeAccount(selectedAccount.getAccountId()); } - new SwitchAccountAsyncTask(activity, R.string.switching_account, accounts.getSelectedAccount().getAccountId(), null).execute(); + switchAccountAndStartActivity(activity, accounts.getSelectedAccount().getAccountId(), null); } - - // helper class for switching accounts gracefully - - public static class SwitchAccountAsyncTask extends ProgressDialogAsyncTask { - private final WeakReference activityWeakReference; - private final int destAccountId; // 0 creates a new account - private final @Nullable String qrAccount; - - public SwitchAccountAsyncTask(Activity activity, int title, int destAccountId, @Nullable String qrAccount) { - super(activity, null, activity.getString(title)); - this.activityWeakReference = new WeakReference<>(activity); - this.destAccountId = destAccountId; - this.qrAccount = qrAccount; - } - @Override - protected Void doInBackground(Void... voids) { - Activity activity = activityWeakReference.get(); - if (activity!=null) { - if (destAccountId==0) { - AccountManager.getInstance().beginAccountCreation(activity); - } else { - AccountManager.getInstance().switchAccount(activity, destAccountId); - } - } - return null; + public void switchAccountAndStartActivity(Activity activity, int destAccountId, @Nullable String qrAccount) { + if (destAccountId==0) { + beginAccountCreation(activity); + } else { + switchAccount(activity, destAccountId); } - @Override - protected void onPostExecute(Void result) { - Activity activity = activityWeakReference.get(); - if (activity!=null) { - activity.finishAffinity(); - if (destAccountId==0) { - Intent intent = new Intent(activity, WelcomeActivity.class); - if (qrAccount!=null) { - intent.putExtra(WelcomeActivity.QR_ACCOUNT_EXTRA, qrAccount); - } - activity.startActivity(intent); - } else { - activity.startActivity(new Intent(activity.getApplicationContext(), ConversationListActivity.class)); - } + activity.finishAffinity(); + if (destAccountId==0) { + Intent intent = new Intent(activity, WelcomeActivity.class); + if (qrAccount!=null) { + intent.putExtra(WelcomeActivity.QR_ACCOUNT_EXTRA, qrAccount); } + activity.startActivity(intent); + } else { + activity.startActivity(new Intent(activity.getApplicationContext(), ConversationListActivity.class)); } } @@ -158,6 +132,6 @@ public class AccountManager { } public void addAccountFromQr(Activity activity, String qr) { - new SwitchAccountAsyncTask(activity, R.string.one_moment, 0, qr).execute(); + switchAccountAndStartActivity(activity, 0, qr); } }