Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 107cd7a2 authored by Marcus Hagerott's avatar Marcus Hagerott
Browse files

Replace getAccounts method with blockForWritableAccounts

This makes it more explicit that the method may block and should be
avoided.

Test: ran GoogleContactsTests

Bug 33627801

Change-Id: Idaffc3f0f6c986e21701f661d46b39ee8cbcc0a1
parent ee3d3a50
Loading
Loading
Loading
Loading
+22 −20
Original line number Diff line number Diff line
@@ -138,11 +138,6 @@ public abstract class AccountTypeManager {

    private static final AccountTypeManager EMPTY = new AccountTypeManager() {

        @Override
        public List<AccountWithDataSet> getAccounts(boolean contactWritableOnly) {
            return Collections.emptyList();
        }

        @Override
        public ListenableFuture<List<AccountInfo>> getAccountsAsync() {
            return Futures.immediateFuture(Collections.<AccountInfo>emptyList());
@@ -178,9 +173,29 @@ public abstract class AccountTypeManager {
    /**
     * Returns the list of all accounts (if contactWritableOnly is false) or just the list of
     * contact writable accounts (if contactWritableOnly is true).
     *
     * <p>TODO(mhagerott) delete this method. It's left in place to prevent build breakages when
     * this change is automerged. Usages of this method in downstream branches should be
     * replaced with an asynchronous account loading pattern</p>
     */
    // TODO: Consider splitting this into getContactWritableAccounts() and getAllAccounts()
    public abstract List<AccountWithDataSet> getAccounts(boolean contactWritableOnly);
    public List<AccountWithDataSet> getAccounts(boolean contactWritableOnly) {
        return contactWritableOnly
                ? blockForWritableAccounts()
                : AccountInfo.extractAccounts(Futures.getUnchecked(getAccountsAsync()));
    }

    /**
     * Returns all contact writable accounts
     *
     * <p>In general this method should be avoided. It exists to support some legacy usages of
     * accounts in infrequently used features where refactoring to asynchronous loading is
     * not justified. The chance that this will actually block is pretty low if the app has been
     * launched previously</p>
     */
    public List<AccountWithDataSet> blockForWritableAccounts() {
        return AccountInfo.extractAccounts(
                Futures.getUnchecked(filterAccountsAsync(AccountFilter.CONTACTS_WRITABLE)));
    }

    /**
     * Loads accounts in background and returns future that will complete with list of all accounts
@@ -520,19 +535,6 @@ class AccountTypeManagerImpl extends AccountTypeManager
                mMainThreadExecutor);
    }

    /**
     * Return list of all known or contact writable {@link AccountWithDataSet}'s.
     * {@param contactWritableOnly} whether to restrict to contact writable accounts only
     */
    @Override
    public List<AccountWithDataSet> getAccounts(boolean contactWritableOnly) {
        final Predicate<AccountInfo> filter = contactWritableOnly ?
                writableFilter() : Predicates.<AccountInfo>alwaysTrue();
        // TODO: Shouldn't have a synchronous version for getting all accounts
        return Lists.transform(Futures.getUnchecked(filterAccountsAsync(filter)),
                AccountInfo.ACCOUNT_EXTRACTOR);
    }

    @Override
    public ListenableFuture<List<AccountInfo>> getAccountsAsync() {
        return getAllAccountsAsyncInternal();
+2 −10
Original line number Diff line number Diff line
@@ -84,15 +84,6 @@ public class AccountSelectionUtil {
        }
    }

    public static Dialog getSelectAccountDialog(Activity activity, int resId) {
        return getSelectAccountDialog(activity, resId, null, null);
    }

    public static Dialog getSelectAccountDialog(Activity activity, int resId,
            DialogInterface.OnClickListener onClickListener) {
        return getSelectAccountDialog(activity, resId, onClickListener, null);
    }

    /**
     * When OnClickListener or OnCancelListener is null, uses a default listener.
     * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
@@ -101,7 +92,8 @@ public class AccountSelectionUtil {
            DialogInterface.OnClickListener onClickListener,
            DialogInterface.OnCancelListener onCancelListener) {
        final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
        final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true);
        final List<AccountWithDataSet> writableAccountList =
                accountTypes.blockForWritableAccounts();

        Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());

+1 −1
Original line number Diff line number Diff line
@@ -597,7 +597,7 @@ public class ImportVCardActivity extends Activity implements ImportVCardDialogFr
            mAccount = new AccountWithDataSet(accountName, accountType, dataSet);
        } else {
            final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this);
            final List<AccountWithDataSet> accountList = accountTypes.getAccounts(true);
            final List<AccountWithDataSet> accountList = accountTypes.blockForWritableAccounts();
            if (accountList.size() == 0) {
                mAccount = null;
            } else if (accountList.size() == 1) {
+1 −1
Original line number Diff line number Diff line
@@ -194,7 +194,7 @@ public class NfcImportVCardActivity extends Activity implements ServiceConnectio
        mRecord = msg.getRecords()[0];

        final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this);
        final List<AccountWithDataSet> accountList = accountTypes.getAccounts(true);
        final List<AccountWithDataSet> accountList = accountTypes.blockForWritableAccounts();
        if (accountList.size() == 0) {
            mAccount = null;
        } else if (accountList.size() == 1) {
+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ public class SelectAccountActivity extends Activity {
        // - no account -> use phone-local storage without asking the user
        final int resId = R.string.import_from_vcf_file;
        final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this);
        final List<AccountWithDataSet> accountList = accountTypes.getAccounts(true);
        final List<AccountWithDataSet> accountList = accountTypes.blockForWritableAccounts();
        if (accountList.size() == 0) {
            Log.w(LOG_TAG, "Account does not exist");
            finish();
Loading