Delay loading groups until compact editors are bound (E14)

Bug 24533689
Bug 23589603

Change-Id: I517b61d116f2d91237c493dc8a18f09aed9ad48a
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index e98a8de..031805b 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -365,6 +365,11 @@
         }
     }
 
+    @Override
+    public void onEditorsBound() {
+        getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupsLoaderListener);
+    }
+
     private CompactRawContactsEditorView getContent() {
         return (CompactRawContactsEditorView) mContent;
     }
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 9d81276..e71c814 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -109,6 +109,11 @@
          * Invoked when no editors could be bound for the contact.
          */
         public void onBindEditorsFailed();
+
+        /**
+         * Invoked after editors have been bound for the contact.
+         */
+        public void onEditorsBound();
     }
 
     /** Used to sort entire kind sections. */
@@ -360,8 +365,7 @@
     @Override
     public void onClick(View view) {
         if (view.getId() == R.id.more_fields) {
-            showMoreFields();
-            updateMoreFieldsButton();
+            showAllFields();
         }
     }
 
@@ -391,8 +395,8 @@
         final SavedState savedState = (SavedState) state;
         super.onRestoreInstanceState(savedState.getSuperState());
         mIsExpanded = savedState.mIsExpanded;
-        if (mIsExpanded && !mKindSectionDataMap.isEmpty()) {
-            showMoreFields();
+        if (mIsExpanded) {
+            showAllFields();
         }
     }
 
@@ -445,20 +449,17 @@
                 GroupMembership.CONTENT_ITEM_TYPE);
         for (CompactKindSectionView kindSectionView : kindSectionViews) {
             kindSectionView.setGroupMetaData(groupMetaData);
+            if (mIsExpanded) {
+                kindSectionView.setHideWhenEmpty(false);
+                kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
+            }
         }
-
-        // Groups metadata may be set after we restore expansion state so just do it again
-        if (mIsExpanded) {
-            showMoreFields();
-        }
-        updateMoreFieldsButton();
     }
 
     public void setState(RawContactDeltaList rawContactDeltas,
             MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
             long photoId, boolean hasNewContact, boolean isUserProfile,
             AccountWithDataSet primaryAccount) {
-        // Clear previous state and reset views
         mKindSectionDataMap.clear();
         mKindSectionViews.removeAllViews();
         mMoreFields.setVisibility(View.VISIBLE);
@@ -488,13 +489,15 @@
         }
 
         // Setup the view
-        setId(mViewIdGenerator.getId(rawContactDeltas.get(0), /* dataKind =*/ null,
-                /* valuesDelta =*/ null, ViewIdGenerator.NO_VIEW_INDEX));
         addAccountInfo();
         addPhotoView();
         addKindSectionViews();
 
-        updateMoreFieldsButton();
+        if (mIsExpanded) {
+            showAllFields();
+        }
+
+        if (mListener != null) mListener.onEditorsBound();
     }
 
     private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas,
@@ -836,7 +839,7 @@
         return kindSectionView;
     }
 
-    private void showMoreFields() {
+    private void showAllFields() {
         // Stop hiding empty editors and allow the user to enter values for all kinds now
         for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
             final CompactKindSectionView kindSectionView =
@@ -845,19 +848,7 @@
             kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
         }
         mIsExpanded = true;
-    }
 
-    private void updateMoreFieldsButton() {
-        // If any kind section views are hidden then show the link
-        for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
-            final CompactKindSectionView kindSectionView =
-                    (CompactKindSectionView) mKindSectionViews.getChildAt(i);
-            if (kindSectionView.getVisibility() == View.GONE) {
-                // Show the more fields button
-                mMoreFields.setVisibility(View.VISIBLE);
-                return;
-            }
-        }
         // Hide the more fields button
         mMoreFields.setVisibility(View.GONE);
     }
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index 5b341a0..2c3b6c9 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -95,7 +95,7 @@
 
     static final String TAG = "ContactEditor";
 
-    protected static final int LOADER_DATA = 1;
+    protected static final int LOADER_CONTACT = 1;
     protected static final int LOADER_GROUPS = 2;
 
     private static final List<String> VALID_INTENT_ACTIONS = new ArrayList<String>() {{
@@ -366,7 +366,7 @@
     /**
      * The contact data loader listener.
      */
-    protected final LoaderManager.LoaderCallbacks<Contact> mDataLoaderListener =
+    protected final LoaderManager.LoaderCallbacks<Contact> mContactLoaderListener =
             new LoaderManager.LoaderCallbacks<Contact>() {
 
                 protected long mLoaderStartTime;
@@ -405,9 +405,9 @@
             };
 
     /**
-     * The group meta data loader listener.
+     * The groups meta data loader listener.
      */
-    protected final LoaderManager.LoaderCallbacks<Cursor> mGroupLoaderListener =
+    protected final LoaderManager.LoaderCallbacks<Cursor> mGroupsLoaderListener =
             new LoaderManager.LoaderCallbacks<Cursor>() {
 
                 @Override
@@ -507,11 +507,11 @@
             // database.
             if (Intent.ACTION_EDIT.equals(mAction) ||
                     ContactEditorBaseActivity.ACTION_EDIT.equals(mAction)) {
-                // Either...
+                // Either
                 // 1) orientation change but load never finished.
-                // or
-                // 2) not an orientation change.  data needs to be loaded for first time.
-                getLoaderManager().initLoader(LOADER_DATA, null, mDataLoaderListener);
+                // 2) not an orientation change so data needs to be loaded for first time.
+                getLoaderManager().initLoader(LOADER_CONTACT, null, mContactLoaderListener);
+                getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupsLoaderListener);
             }
         } else {
             // Orientation change, we already have mState, it was loaded by onCreate
@@ -560,12 +560,6 @@
     }
 
     @Override
-    public void onStart() {
-        getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupLoaderListener);
-        super.onStart();
-    }
-
-    @Override
     public void onSaveInstanceState(Bundle outState) {
         outState.putString(KEY_ACTION, mAction);
         outState.putParcelable(KEY_URI, mLookupUri);
@@ -898,7 +892,7 @@
         // If we are about to close the editor - there is no need to refresh the data
         if (saveMode == SaveMode.CLOSE || saveMode == SaveMode.COMPACT
                 || saveMode == SaveMode.SPLIT) {
-            getLoaderManager().destroyLoader(LOADER_DATA);
+            getLoaderManager().destroyLoader(LOADER_CONTACT);
         }
 
         mStatus = Status.SAVING;
@@ -1360,7 +1354,7 @@
                     mState = new RawContactDeltaList();
                     load(Intent.ACTION_EDIT, contactLookupUri, null);
                     mStatus = Status.LOADING;
-                    getLoaderManager().restartLoader(LOADER_DATA, null, mDataLoaderListener);
+                    getLoaderManager().restartLoader(LOADER_CONTACT, null, mContactLoaderListener);
                 }
                 break;
 
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index ccbf615..5d84f53 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -115,6 +115,12 @@
     }
 
     @Override
+    public void onStart() {
+        getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupsLoaderListener);
+        super.onStart();
+    }
+
+    @Override
     public void onExternalEditorRequest(AccountWithDataSet account, Uri uri) {
         if (mListener != null) {
             mListener.onCustomEditContactActivityRequested(account, uri, null, false);