Use loader for accounts in editor fragment

Test: manually remove selected account in editor and verify that
it changes to the first account in list

Bug: 33627801

Change-Id: I6554f03c207fb761f7b8d971e0f07f7416efcd72
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index be583b0..c9153e1 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -76,8 +76,10 @@
 import com.android.contacts.model.RawContactDeltaList;
 import com.android.contacts.model.RawContactModifier;
 import com.android.contacts.model.ValuesDelta;
+import com.android.contacts.model.account.AccountInfo;
 import com.android.contacts.model.account.AccountType;
 import com.android.contacts.model.account.AccountWithDataSet;
+import com.android.contacts.model.account.AccountsLoader;
 import com.android.contacts.preference.ContactsPreferences;
 import com.android.contacts.quickcontact.InvisibleContactUtil;
 import com.android.contacts.quickcontact.QuickContactActivity;
@@ -95,6 +97,7 @@
 
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -114,6 +117,7 @@
 
     private static final int LOADER_CONTACT = 1;
     private static final int LOADER_GROUPS = 2;
+    private static final int LOADER_ACCOUNTS = 3;
 
     private static final String KEY_PHOTO_RAW_CONTACT_ID = "photo_raw_contact_id";
     private static final String KEY_UPDATED_PHOTOS = "updated_photos";
@@ -342,6 +346,7 @@
     // Whether to show the new contact blank form and if it's corresponding delta is ready.
     protected boolean mHasNewContact;
     protected AccountWithDataSet mAccountWithDataSet;
+    protected List<AccountInfo> mWritableAccounts = Collections.emptyList();
     protected boolean mNewContactDataReady;
     protected boolean mNewContactAccountChanged;
 
@@ -430,6 +435,48 @@
                 }
             };
 
+    protected LoaderManager.LoaderCallbacks<List<AccountInfo>> mAccountsLoaderListener =
+            new LoaderManager.LoaderCallbacks<List<AccountInfo>>() {
+                @Override
+                public Loader<List<AccountInfo>> onCreateLoader(int id, Bundle args) {
+                    return new AccountsLoader(getActivity(), AccountTypeManager.writableFilter());
+                }
+
+                @Override
+                public void onLoadFinished(
+                        Loader<List<AccountInfo>> loader, List<AccountInfo> data) {
+                    mWritableAccounts = data;
+
+                    final RawContactEditorView view = getContent();
+                    if (view == null) {
+                        return;
+                    }
+                    view.setAccounts(data);
+                    if (mAccountWithDataSet == null && view.getCurrentRawContactDelta() == null) {
+                        return;
+                    }
+
+                    final AccountWithDataSet account = mAccountWithDataSet != null
+                            ? mAccountWithDataSet
+                            : view.getCurrentRawContactDelta().getAccountWithDataSet();
+
+                    // The current account was removed
+                    if (!AccountInfo.contains(data, account) && !data.isEmpty()) {
+                        if (isReadyToBindEditors()) {
+                            onRebindEditorsForNewContact(getContent().getCurrentRawContactDelta(),
+                                    account, data.get(0).getAccount());
+                        } else {
+                            mAccountWithDataSet = data.get(0).getAccount();
+                        }
+                    }
+                }
+
+                @Override
+                public void onLoaderReset(Loader<List<AccountInfo>> loader) {
+                }
+            };
+
+
     private long mPhotoRawContactId;
     private Bundle mUpdatedPhotos = new Bundle();
 
@@ -520,6 +567,10 @@
 
         validateAction(mAction);
 
+        if (!Intent.ACTION_EDIT.equals(mAction)) {
+            getLoaderManager().initLoader(LOADER_ACCOUNTS, null, mAccountsLoaderListener);
+        }
+
         if (mState.isEmpty()) {
             // The delta list may not have finished loading before orientation change happens.
             // In this case, there will be a saved state but deltas will be missing.  Reload from