Get join suggestions for the compact editor name field

Also fix photo height in portrait -- made a mistake in
ag/641233.

Bug 19124091

Change-Id: I988cbcf457002bc6f0996c87a8611fca82bdf78a
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index 5145416..cfed52a 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -26,6 +26,7 @@
 import com.android.contacts.common.model.account.AccountType;
 import com.android.contacts.common.util.MaterialColorMapUtils;
 import com.android.contacts.detail.PhotoSelectionHandler;
+import com.android.contacts.editor.Editor.EditorListener;
 import com.android.contacts.util.ContactPhotoUtils;
 
 import android.app.Activity;
@@ -204,16 +205,44 @@
             return;
         }
 
+        // Add input fields for the loaded Contact
         final CompactRawContactsEditorView editorView = getContent();
         editorView.setListener(this);
         editorView.setState(mState, mMaterialPalette, mViewIdGenerator);
-        editorView.setEnabled(isEnabled());
-        editorView.setVisibility(View.VISIBLE);
 
+        // Set up the photo widget
         mPhotoHandler = createPhotoHandler();
         mPhotoRawContactId = editorView.getPhotoRawContactId();
         editorView.setPhotoHandler(mPhotoHandler);
 
+        // Attach the aggregation suggestion engine to the structured name field
+        final StructuredNameEditorView nameEditorView = editorView.getStructuredNameEditorView();
+        if (nameEditorView != null) {
+            nameEditorView.setEditorListener(new EditorListener() {
+                @Override
+                public void onRequest(int request) {
+                    final Activity activity = getActivity();
+                    if (activity == null || activity.isFinishing()) {
+                        return;
+                    }
+                    if (request == EditorListener.FIELD_CHANGED && !mIsUserProfile) {
+                        acquireAggregationSuggestions(activity, nameEditorView.getRawContactId(),
+                                nameEditorView.getValues());
+                    }
+                }
+
+                @Override
+                public void onDeleteRequested(Editor editor) {
+                }
+            });
+        }
+
+        // The editor is ready now so make it visible
+        editorView.setEnabled(isEnabled());
+        editorView.setVisibility(View.VISIBLE);
+
+        // Refresh the ActionBar as the visibility of the join command
+        // Activity can be null if we have been detached from the Activity.
         invalidateOptionsMenu();
     }
 
@@ -268,6 +297,11 @@
     }
 
     @Override
+    protected View getAggregationAnchorView(long rawContactId) {
+        return getContent().getAggregationAnchorView();
+    }
+
+    @Override
     protected void setGroupMetaData() {
         // The compact editor does not support groups.
     }
diff --git a/src/com/android/contacts/editor/CompactPhotoEditorView.java b/src/com/android/contacts/editor/CompactPhotoEditorView.java
index 38e4f87..86a0ee8 100644
--- a/src/com/android/contacts/editor/CompactPhotoEditorView.java
+++ b/src/com/android/contacts/editor/CompactPhotoEditorView.java
@@ -149,7 +149,7 @@
                     photoWidth = (int) (photoHeight * mLandscapePhotoRatio);
                 } else {
                     // Make the photo a square
-                    photoHeight = getHeight();
+                    photoHeight = getWidth();
                     photoWidth = photoHeight;
                 }
                 final ViewGroup.LayoutParams layoutParams = getLayoutParams();
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 44cf698..48e655b 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -70,6 +70,7 @@
 
     private AccountTypeManager mAccountTypeManager;
     private LayoutInflater mLayoutInflater;
+
     private ViewIdGenerator mViewIdGenerator;
     private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
 
@@ -82,7 +83,6 @@
     private ViewGroup mOther;
     private View mMoreFields;
 
-    // TODO: remove this after we handle account selection for photos
     private long mPhotoRawContactId;
 
     public CompactRawContactsEditorView(Context context) {
@@ -171,11 +171,25 @@
     /**
      * Get the raw contact ID for the CompactHeaderView photo.
      */
-    // TODO: remove me once we support multiple accounts
     public long getPhotoRawContactId() {
         return mPhotoRawContactId;
     }
 
+    public StructuredNameEditorView getStructuredNameEditorView() {
+        // We only ever show one StructuredName
+        return mNames.getChildCount() == 0
+                ? null : (StructuredNameEditorView) mNames.getChildAt(0);
+    }
+
+    public View getAggregationAnchorView() {
+        // Since there is only one structured name we can just return it as the anchor for
+        // the aggregation suggestions popup
+        if (mNames.getChildCount() == 0) {
+            return null;
+        }
+        return mNames.getChildAt(0).findViewById(R.id.anchor_view);
+    }
+
     public void setState(RawContactDeltaList rawContactDeltas,
             MaterialColorMapUtils.MaterialPalette materialPalette,
             ViewIdGenerator viewIdGenerator) {
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index d2f8aa7..3ca492b 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -1353,8 +1353,7 @@
      * Triggers an asynchronous search for aggregation suggestions.
      */
     protected void acquireAggregationSuggestions(Context context,
-            RawContactEditorView rawContactEditor) {
-        long rawContactId = rawContactEditor.getRawContactId();
+            long rawContactId, ValuesDelta valuesDelta) {
         if (mAggregationSuggestionsRawContactId != rawContactId
                 && mAggregationSuggestionView != null) {
             mAggregationSuggestionView.setVisibility(View.GONE);
@@ -1372,8 +1371,7 @@
 
         mAggregationSuggestionEngine.setContactId(getContactId());
 
-        LabeledEditorView nameEditor = rawContactEditor.getNameEditor();
-        mAggregationSuggestionEngine.onNameChange(nameEditor.getValues());
+        mAggregationSuggestionEngine.onNameChange(valuesDelta);
     }
 
     /**
@@ -1403,12 +1401,10 @@
             return;
         }
 
-        final RawContactEditorView rawContactView = (RawContactEditorView)
-                getRawContactEditorView(mAggregationSuggestionsRawContactId);
-        if (rawContactView == null) {
+        final View anchorView = getAggregationAnchorView(mAggregationSuggestionsRawContactId);
+        if (anchorView == null) {
             return; // Raw contact deleted?
         }
-        final View anchorView = rawContactView.findViewById(R.id.anchor_view);
         mAggregationSuggestionPopup = new ListPopupWindow(mContext, null);
         mAggregationSuggestionPopup.setAnchorView(anchorView);
         mAggregationSuggestionPopup.setWidth(anchorView.getWidth());
@@ -1432,20 +1428,10 @@
     }
 
     /**
-     * Finds raw contact editor view for the given rawContactId.
+     * Returns the raw contact editor view for the given rawContactId that should be used as the
+     * anchor for aggregation suggestions.
      */
-    private BaseRawContactEditorView getRawContactEditorView(long rawContactId) {
-        for (int i = 0; i < mContent.getChildCount(); i++) {
-            final View childView = mContent.getChildAt(i);
-            if (childView instanceof BaseRawContactEditorView) {
-                final BaseRawContactEditorView editor = (BaseRawContactEditorView) childView;
-                if (editor.getRawContactId() == rawContactId) {
-                    return editor;
-                }
-            }
-        }
-        return null;
-    }
+    abstract protected View getAggregationAnchorView(long rawContactId);
 
     /**
      * Whether the given raw contact ID matches the one used to last load aggregation
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index c87be34..eb2de3a 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -48,9 +48,6 @@
 import com.android.contacts.util.ContactPhotoUtils;
 import com.android.contacts.util.UiClosables;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
-
 import java.io.FileNotFoundException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -252,7 +249,9 @@
                             return;
                         }
                         if (request == EditorListener.FIELD_CHANGED && !isEditingUserProfile()) {
-                            acquireAggregationSuggestions(activity, rawContactEditor);
+                            acquireAggregationSuggestions(activity,
+                                    rawContactEditor.getNameEditor().getRawContactId(),
+                                    rawContactEditor.getNameEditor().getValues());
                         } else if (request == EditorListener.EDITOR_FOCUS_CHANGED) {
                             adjustNameFieldsHintDarkness(rawContactEditor);
                         }
@@ -283,7 +282,9 @@
                 nickNameEditor.setEditorListener(listener);
 
                 if (isAggregationSuggestionRawContactId(rawContactId)) {
-                    acquireAggregationSuggestions(activity, rawContactEditor);
+                    acquireAggregationSuggestions(activity,
+                            rawContactEditor.getNameEditor().getRawContactId(),
+                            rawContactEditor.getNameEditor().getValues());
                 }
 
                 adjustNameFieldsHintDarkness(rawContactEditor);
@@ -497,6 +498,12 @@
     /**
      * Finds raw contact editor view for the given rawContactId.
      */
+    @Override
+    protected View getAggregationAnchorView(long rawContactId) {
+        BaseRawContactEditorView editorView = getRawContactEditorView(rawContactId);
+        return editorView == null ? null : editorView.findViewById(R.id.anchor_view);
+    }
+
     public BaseRawContactEditorView getRawContactEditorView(long rawContactId) {
         for (int i = 0; i < mContent.getChildCount(); i++) {
             final View childView = mContent.getChildAt(i);
diff --git a/src/com/android/contacts/editor/JoinSuggestedContactDialogFragment.java b/src/com/android/contacts/editor/JoinSuggestedContactDialogFragment.java
index dca6e18..4d35332 100644
--- a/src/com/android/contacts/editor/JoinSuggestedContactDialogFragment.java
+++ b/src/com/android/contacts/editor/JoinSuggestedContactDialogFragment.java
@@ -47,8 +47,8 @@
                         new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int whichButton) {
-                                ContactEditorFragment targetFragment =
-                                        (ContactEditorFragment) getTargetFragment();
+                                ContactEditorBaseFragment targetFragment =
+                                        (ContactEditorBaseFragment) getTargetFragment();
                                 long rawContactIds[] =
                                         getArguments().getLongArray(ARG_RAW_CONTACT_IDS);
                                 targetFragment.doJoinSuggestedContact(rawContactIds);
diff --git a/src/com/android/contacts/editor/LabeledEditorView.java b/src/com/android/contacts/editor/LabeledEditorView.java
index 397c335..5996fa6 100644
--- a/src/com/android/contacts/editor/LabeledEditorView.java
+++ b/src/com/android/contacts/editor/LabeledEditorView.java
@@ -120,6 +120,10 @@
         init(context);
     }
 
+    public Long getRawContactId() {
+        return mState == null ? null : mState.getRawContactId();
+    }
+
     private void init(Context context) {
         mMinLineItemHeight = context.getResources().getDimensionPixelSize(
                 R.dimen.editor_min_line_item_height);
diff --git a/src/com/android/contacts/editor/SuggestionEditConfirmationDialogFragment.java b/src/com/android/contacts/editor/SuggestionEditConfirmationDialogFragment.java
index 0af2d2d..c13d5ea 100644
--- a/src/com/android/contacts/editor/SuggestionEditConfirmationDialogFragment.java
+++ b/src/com/android/contacts/editor/SuggestionEditConfirmationDialogFragment.java
@@ -49,8 +49,8 @@
                         new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int whichButton) {
-                                final ContactEditorFragment targetFragment =
-                                        (ContactEditorFragment) getTargetFragment();
+                                final ContactEditorBaseFragment targetFragment =
+                                        (ContactEditorBaseFragment) getTargetFragment();
                                 final Uri contactUri =
                                         getArguments().getParcelable(ARG_CONTACT_URI);
                                 targetFragment.doEditSuggestedContact(contactUri);