Local account handling cleanup

Clean up null account handling and update local account detection to allow
for a non-null account to represent a local account.

Bug: 18959158
Change-Id: I0d5f7acb7d9a8d1ba7b6d3a4e0b6584e3646934a
diff --git a/src/com/android/contacts/activities/AttachPhotoActivity.java b/src/com/android/contacts/activities/AttachPhotoActivity.java
index 3196f87..ad64d8d 100644
--- a/src/com/android/contacts/activities/AttachPhotoActivity.java
+++ b/src/com/android/contacts/activities/AttachPhotoActivity.java
@@ -321,11 +321,7 @@
             // Otherwise, there should be a default account. Then either create a local contact
             // (if default account is null) or create a contact with the specified account.
             AccountWithDataSet defaultAccount = editorUtils.getDefaultAccount();
-            if (defaultAccount == null) {
-                createNewRawContact(null);
-            } else {
-                createNewRawContact(defaultAccount);
-            }
+            createNewRawContact(defaultAccount);
         }
     }
 
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 664b8c7..e98ba7d 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -682,11 +682,7 @@
             // Otherwise, there should be a default account. Then either create a local contact
             // (if default account is null) or create a contact with the specified account.
             AccountWithDataSet defaultAccount = mEditorUtils.getDefaultAccount();
-            if (defaultAccount == null) {
-                createContact(null);
-            } else {
-                createContact(defaultAccount);
-            }
+            createContact(defaultAccount);
         }
     }
 
@@ -715,9 +711,7 @@
      */
     private void createContact(AccountWithDataSet account) {
         final AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
-        final AccountType accountType =
-                accountTypes.getAccountType(account != null ? account.type : null,
-                        account != null ? account.dataSet : null);
+        final AccountType accountType = accountTypes.getAccountTypeForAccount(account);
 
         if (accountType.getCreateContactActivityClassName() != null) {
             if (mListener != null) {
@@ -740,10 +734,8 @@
             RawContactDelta oldState, AccountWithDataSet oldAccount,
             AccountWithDataSet newAccount) {
         AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
-        AccountType oldAccountType = accountTypes.getAccountType(
-                oldAccount.type, oldAccount.dataSet);
-        AccountType newAccountType = accountTypes.getAccountType(
-                newAccount.type, newAccount.dataSet);
+        AccountType oldAccountType = accountTypes.getAccountTypeForAccount(oldAccount);
+        AccountType newAccountType = accountTypes.getAccountTypeForAccount(newAccount);
 
         if (newAccountType.getCreateContactActivityClassName() != null) {
             Log.w(TAG, "external activity called in rebind situation");
@@ -772,11 +764,7 @@
         mStatus = Status.EDITING;
 
         final RawContact rawContact = new RawContact();
-        if (newAccount != null) {
-            rawContact.setAccount(newAccount);
-        } else {
-            rawContact.setAccountToLocal();
-        }
+        rawContact.setAccount(newAccount);
 
         final ValuesDelta valuesDelta = ValuesDelta.fromAfter(rawContact.getValues());
         final RawContactDelta insert = new RawContactDelta(valuesDelta);
diff --git a/src/com/android/contacts/editor/ContactEditorUtils.java b/src/com/android/contacts/editor/ContactEditorUtils.java
index b132217..105b885 100644
--- a/src/com/android/contacts/editor/ContactEditorUtils.java
+++ b/src/com/android/contacts/editor/ContactEditorUtils.java
@@ -119,7 +119,7 @@
         final SharedPreferences.Editor editor = mPrefs.edit()
                 .putBoolean(KEY_ANYTHING_SAVED, true);
 
-        if (defaultAccount == null) {
+        if (defaultAccount == null || defaultAccount.isLocalAccount()) {
             // If the default is "local only", there should be no writable accounts.
             // This should always be the case with our spec, but because we load the account list
             // asynchronously using a worker thread, it is possible that there are accounts at this
@@ -165,7 +165,7 @@
      */
     @VisibleForTesting
     boolean isValidAccount(AccountWithDataSet account) {
-        if (account == null) {
+        if (account == null || account.isLocalAccount()) {
             return true; // It's "local only" account, which is valid.
         }
         return getWritableAccounts().contains(account);
@@ -227,7 +227,8 @@
         // ("local" account) while there are multiple accounts, then show the notification dialog.
         // This shouldn't ever happen, but this should allow the user can get back into a normal
         // state after they respond to the notification.
-        if (defaultAccount == null && currentWritableAccounts.size() > 0) {
+        if ((defaultAccount == null || defaultAccount.isLocalAccount())
+                && currentWritableAccounts.size() > 0) {
             Log.e(TAG, "Preferences file in an inconsistent state, request that the default account"
                     + " and current writable accounts be saved again");
             return true;