Move logic for loading local account on Nexus

Test: manually verify that device account is shown when there is no
Google account and is hidden when one is added.

Bug 33627801

Change-Id: If452ad1378fbdfe893379e8ff9367a4156561cdc
diff --git a/src/com/android/contacts/model/AccountTypeManager.java b/src/com/android/contacts/model/AccountTypeManager.java
index 434d10b..1d3a5fa 100644
--- a/src/com/android/contacts/model/AccountTypeManager.java
+++ b/src/com/android/contacts/model/AccountTypeManager.java
@@ -436,6 +436,7 @@
 
     /* This notification will arrive on the UI thread */
     public void onAccountsUpdated(Account[] accounts) {
+        reloadLocalAccounts();
         maybeNotifyAccountsUpdated(mAccountManagerAccounts,
                 getAccountsWithDataSets(accounts, mTypeProvider));
     }
@@ -560,22 +561,12 @@
                         "List should have exactly 2 elements");
 
                 final List<AccountInfo> result = new ArrayList<>();
-                boolean hasWritableGoogleAccount = false;
                 for (AccountWithDataSet account : input.get(0)) {
-                    hasWritableGoogleAccount = hasWritableGoogleAccount ||
-                            (GoogleAccountType.ACCOUNT_TYPE.equals(account.type) &&
-                                    account.dataSet == null);
-
                     result.add(
                             typeProvider.getTypeForAccount(account).wrapAccount(mContext, account));
                 }
 
                 for (AccountWithDataSet account : input.get(1)) {
-                    // Exclude the null account if a writable Google account exists because null
-                    // account contacts are automatically converted to Google contacts in this case
-                    if (hasWritableGoogleAccount && account.isNullAccount()) {
-                        continue;
-                    }
                     result.add(
                             typeProvider.getTypeForAccount(account).wrapAccount(mContext, account));
                 }
diff --git a/src/com/android/contacts/model/DeviceLocalAccountLocator.java b/src/com/android/contacts/model/DeviceLocalAccountLocator.java
index 0d34057..2b86644 100644
--- a/src/com/android/contacts/model/DeviceLocalAccountLocator.java
+++ b/src/com/android/contacts/model/DeviceLocalAccountLocator.java
@@ -21,6 +21,7 @@
 
 import com.android.contacts.Experiments;
 import com.android.contacts.model.account.AccountWithDataSet;
+import com.android.contacts.model.account.GoogleAccountType;
 import com.android.contactsbind.ObjectFactory;
 import com.android.contactsbind.experiments.Flags;
 
@@ -67,7 +68,38 @@
         if (Flags.getInstance().getBoolean(Experiments.OEM_CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) {
             return new Cp2DeviceLocalAccountLocator(context.getContentResolver(),
                     ObjectFactory.getDeviceLocalAccountTypeFactory(context), knownTypes);
+        } else {
+            return new NexusDeviceAccountLocator(accountManager);
         }
-        return NULL_ONLY;
+    }
+
+    /**
+     * On Nexus the "device" account uses "null" values for the account name and type columns
+     *
+     * <p>However, the focus sync adapter automatically migrates contacts from this null
+     * account to a Google account if one exists. Hence, the device account should be returned
+     * only when there is no Google Account added
+     * </p>
+     */
+    public static class NexusDeviceAccountLocator extends DeviceLocalAccountLocator {
+
+        private final AccountManager mAccountManager;
+
+        public NexusDeviceAccountLocator(AccountManager accountManager) {
+            mAccountManager = accountManager;
+        }
+
+        @Override
+        public List<AccountWithDataSet> getDeviceLocalAccounts() {
+            @SuppressWarnings("MissingPermission")
+            final Account[] accounts = mAccountManager
+                    .getAccountsByType(GoogleAccountType.ACCOUNT_TYPE);
+
+            if (accounts.length > 0) {
+                return Collections.emptyList();
+            } else {
+                return Collections.singletonList(AccountWithDataSet.getNullAccount());
+            }
+        }
     }
 }