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
diff --git a/src/com/android/contacts/model/AccountTypeManager.java b/src/com/android/contacts/model/AccountTypeManager.java
index b3f05c5..cb0fe24 100644
--- a/src/com/android/contacts/model/AccountTypeManager.java
+++ b/src/com/android/contacts/model/AccountTypeManager.java
@@ -139,11 +139,6 @@
     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 @@
     /**
      * 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 @@
                 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();
diff --git a/src/com/android/contacts/util/AccountSelectionUtil.java b/src/com/android/contacts/util/AccountSelectionUtil.java
index a29af9a..bfe8a08 100644
--- a/src/com/android/contacts/util/AccountSelectionUtil.java
+++ b/src/com/android/contacts/util/AccountSelectionUtil.java
@@ -84,15 +84,6 @@
         }
     }
 
-    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 @@
             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());
 
diff --git a/src/com/android/contacts/vcard/ImportVCardActivity.java b/src/com/android/contacts/vcard/ImportVCardActivity.java
index 6d486e3..2c69cdf 100644
--- a/src/com/android/contacts/vcard/ImportVCardActivity.java
+++ b/src/com/android/contacts/vcard/ImportVCardActivity.java
@@ -597,7 +597,7 @@
             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) {
diff --git a/src/com/android/contacts/vcard/NfcImportVCardActivity.java b/src/com/android/contacts/vcard/NfcImportVCardActivity.java
index 4793d47..4eb9b57 100644
--- a/src/com/android/contacts/vcard/NfcImportVCardActivity.java
+++ b/src/com/android/contacts/vcard/NfcImportVCardActivity.java
@@ -194,7 +194,7 @@
         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) {
diff --git a/src/com/android/contacts/vcard/SelectAccountActivity.java b/src/com/android/contacts/vcard/SelectAccountActivity.java
index 8809fac..ac5b3eb 100644
--- a/src/com/android/contacts/vcard/SelectAccountActivity.java
+++ b/src/com/android/contacts/vcard/SelectAccountActivity.java
@@ -58,7 +58,7 @@
         // - 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();
diff --git a/tests/src/com/android/contacts/test/mocks/MockAccountTypeManager.java b/tests/src/com/android/contacts/test/mocks/MockAccountTypeManager.java
index 956f775..b5ccb1e 100644
--- a/tests/src/com/android/contacts/test/mocks/MockAccountTypeManager.java
+++ b/tests/src/com/android/contacts/test/mocks/MockAccountTypeManager.java
@@ -66,7 +66,7 @@
     }
 
     @Override
-    public List<AccountWithDataSet> getAccounts(boolean writableOnly) {
+    public List<AccountWithDataSet> blockForWritableAccounts() {
         return Arrays.asList(mAccounts);
     }