Merge "Support Null device account"
diff --git a/src/com/android/contacts/model/AccountTypeManager.java b/src/com/android/contacts/model/AccountTypeManager.java
index 34f9cb2..196e67f 100644
--- a/src/com/android/contacts/model/AccountTypeManager.java
+++ b/src/com/android/contacts/model/AccountTypeManager.java
@@ -405,28 +405,26 @@
 
         ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, this);
 
-        if (Flags.getInstance().getBoolean(Experiments.CP2_DEVICE_ACCOUNT_DETECTION_ENABLED)) {
-            // Observe changes to RAW_CONTACTS so that we will update the list of "Device" accounts
-            // if a new device contact is added.
-            mContext.getContentResolver().registerContentObserver(
-                    ContactsContract.RawContacts.CONTENT_URI, /* notifyDescendents */ true,
-                    new ContentObserver(mMainThreadHandler) {
-                        @Override
-                        public boolean deliverSelfNotifications() {
-                            return true;
-                        }
+        // Observe changes to RAW_CONTACTS so that we will update the list of "Device" accounts
+        // if a new device contact is added or removed.
+        mContext.getContentResolver().registerContentObserver(
+                ContactsContract.RawContacts.CONTENT_URI, /* notifyDescendents */ true,
+                new ContentObserver(mMainThreadHandler) {
+                    @Override
+                    public boolean deliverSelfNotifications() {
+                        return true;
+                    }
 
-                        @Override
-                        public void onChange(boolean selfChange) {
-                            reloadLocalAccounts();
-                        }
+                    @Override
+                    public void onChange(boolean selfChange) {
+                        reloadLocalAccounts();
+                    }
 
-                        @Override
-                        public void onChange(boolean selfChange, Uri uri) {
-                            reloadLocalAccounts();
-                        }
-                    });
-        }
+                    @Override
+                    public void onChange(boolean selfChange, Uri uri) {
+                        reloadLocalAccounts();
+                    }
+                });
         loadAccountTypes();
     }
 
diff --git a/src/com/android/contacts/model/DeviceLocalAccountLocator.java b/src/com/android/contacts/model/DeviceLocalAccountLocator.java
index 4281de9..2b987d3 100644
--- a/src/com/android/contacts/model/DeviceLocalAccountLocator.java
+++ b/src/com/android/contacts/model/DeviceLocalAccountLocator.java
@@ -18,6 +18,8 @@
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.content.Context;
+import android.database.Cursor;
+import android.provider.ContactsContract;
 
 import com.android.contacts.Experiments;
 import com.android.contacts.model.account.AccountWithDataSet;
@@ -69,23 +71,25 @@
             return new Cp2DeviceLocalAccountLocator(context.getContentResolver(),
                     ObjectFactory.getDeviceLocalAccountTypeFactory(context), knownTypes);
         } else {
-            return new NexusDeviceAccountLocator(accountManager);
+            return new NexusDeviceAccountLocator(context, accountManager);
         }
     }
 
     /**
      * 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>However, the focus sync adapter 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 or when there already exists contacts in the null account.
      * </p>
      */
     public static class NexusDeviceAccountLocator extends DeviceLocalAccountLocator {
-
+        private final Context mContext;
         private final AccountManager mAccountManager;
 
-        public NexusDeviceAccountLocator(AccountManager accountManager) {
+
+        public NexusDeviceAccountLocator(Context context, AccountManager accountManager) {
+            mContext = context;
             mAccountManager = accountManager;
         }
 
@@ -95,7 +99,7 @@
             final Account[] accounts = mAccountManager
                     .getAccountsByType(GoogleAccountType.ACCOUNT_TYPE);
 
-            if (accounts.length > 0) {
+            if (accounts.length > 0 && !AccountWithDataSet.getNullAccount().hasData(mContext)) {
                 return Collections.emptyList();
             } else {
                 return Collections.singletonList(AccountWithDataSet.getNullAccount());