am 81365081: Merge "Fix bugs on Contact editor." into ub-contactsdialer-a-dev

* commit '81365081af63f8a09cf10613ecd653b3b1ee7e5f':
  Fix bugs on Contact editor.
diff --git a/res/layout/compact_account_info.xml b/res/layout/compact_account_info.xml
index 7fad566..1ec1179 100644
--- a/res/layout/compact_account_info.xml
+++ b/res/layout/compact_account_info.xml
@@ -21,10 +21,18 @@
         android:id="@+id/account_container"
         android:layout_height="wrap_content"
         android:layout_width="match_parent"
+        android:orientation="horizontal"
         android:background="?android:attr/selectableItemBackground"
-        android:paddingStart="16dp"
+        android:paddingTop="8dip"
         >
 
+    <ImageView
+            android:id="@+id/account_type_icon"
+            android:layout_width="@dimen/detail_network_icon_size"
+            android:layout_height="@dimen/detail_network_icon_size"
+            android:layout_margin="16dip"
+            android:layout_gravity="center_vertical" />
+
     <LinearLayout
             android:layout_height="wrap_content"
             android:layout_width="0dp"
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index a000309..f081d54 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -258,14 +258,11 @@
     }
 
     @Override
-    public void onRawContactSelected(Uri uri, long rawContactId) {
+    public void onRawContactSelected(Uri uri, long rawContactId, boolean isReadOnly) {
         final Activity activity = getActivity();
         if (activity != null && !activity.isFinishing()) {
-            final Intent intent = new Intent(activity, ContactEditorActivity.class);
-            intent.setAction(ContactEditorBaseActivity.ACTION_EDIT);
-            intent.setData(uri);
-            intent.putExtra(ContactEditorFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE,
-                    rawContactId);
+            final Intent intent = EditorIntents.createEditContactIntentForRawContact(
+                    activity, uri, rawContactId, isReadOnly);
             activity.startActivity(intent);
         }
     }
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 091f1cb..ed5dc3b 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -125,7 +125,7 @@
         /**
          * Invoked when a rawcontact from merged contacts is selected in editor.
          */
-        public void onRawContactSelected(Uri uri, long rawContactId);
+        public void onRawContactSelected(Uri uri, long rawContactId, boolean isReadOnly);
     }
 
     /**
@@ -379,6 +379,7 @@
     private View mAccountHeaderContainer;
     private TextView mAccountHeaderType;
     private TextView mAccountHeaderName;
+    private ImageView mAccountHeaderIcon;
 
     // Account selector
     private View mAccountSelectorContainer;
@@ -429,6 +430,7 @@
         mAccountHeaderContainer = findViewById(R.id.account_container);
         mAccountHeaderType = (TextView) findViewById(R.id.account_type);
         mAccountHeaderName = (TextView) findViewById(R.id.account_name);
+        mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon);
 
         // Account selector
         mAccountSelectorContainer = findViewById(R.id.account_selector_container);
@@ -793,6 +795,10 @@
         }
         mAccountHeaderType.setText(accountInfo.second);
 
+        final AccountType primaryAccountType = mPrimaryRawContactDelta.getRawContactAccountType(
+                getContext());
+        mAccountHeaderIcon.setImageDrawable(primaryAccountType.getDisplayIcon(getContext()));
+
         mAccountHeaderContainer.setContentDescription(
                 EditorUiUtils.getAccountInfoContentDescription(
                         accountInfo.first, accountInfo.second));
@@ -875,19 +881,19 @@
                     public void onItemClick(AdapterView<?> parent, View view, int position,
                                             long id) {
                         UiClosables.closeQuietly(popup);
-                        final RawContactDelta rawContactDelta = adapter.getItem(position);
-                        final long rawContactId = adapter.getItemId(position);
-                        final Uri rawContactUri = ContentUris.withAppendedId(
-                                ContactsContract.RawContacts.CONTENT_URI, rawContactId);
-                        // Start new activity for the raw contact in selected account.
-                        final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
-                        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
-
-                        ArrayList<ContentValues> values = rawContactDelta.getContentValues();
-                        intent.putExtra(ContactsContract.Intents.Insert.DATA, values);
 
                         if (mListener != null) {
-                            mListener.onRawContactSelected(rawContactUri, rawContactId);
+                            final long rawContactId = adapter.getItemId(position);
+                            final Uri rawContactUri = ContentUris.withAppendedId(
+                                    ContactsContract.RawContacts.CONTENT_URI, rawContactId);
+                            final RawContactDelta rawContactDelta = adapter.getItem(position);
+                            final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
+                                    getContext());
+                            final AccountType accountType = rawContactDelta.getAccountType(
+                                    accountTypes);
+                            final boolean isReadOnly = !accountType.areContactsWritable();
+
+                            mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly);
                         }
                     }
                 });
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index 4ea6c15..54c3c07 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -128,6 +128,8 @@
     private static final String KEY_IS_EDIT = "isEdit";
     private static final String KEY_EXISTING_CONTACT_READY = "existingContactDataReady";
 
+    private static final String KEY_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY = "isReadOnly";
+
     // Phone option menus
     private static final String KEY_SEND_TO_VOICE_MAIL_STATE = "sendToVoicemailState";
     private static final String KEY_ARE_PHONE_OPTIONS_CHANGEABLE = "arePhoneOptionsChangable";
@@ -183,6 +185,13 @@
     public static final String INTENT_EXTRA_PHOTO_ID = "photo_id";
 
     /**
+     * Intent key to pass the boolean value of if the raw contact id that should be displayed
+     * in the full editor by itself is read-only.
+     */
+    public static final String INTENT_EXTRA_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY =
+            "raw_contact_display_alone_is_read_only";
+
+    /**
      * Intent extra to specify a {@link ContactEditor.SaveMode}.
      */
     public static final String SAVE_MODE_EXTRA_KEY = "saveMode";
@@ -335,6 +344,7 @@
     //
     protected RawContactDeltaList mState;
     protected int mStatus;
+    protected boolean mRawContactDisplayAloneIsReadOnly = false;
 
     // Whether to show the new contact blank form and if it's corresponding delta is ready.
     protected boolean mHasNewContact;
@@ -474,6 +484,8 @@
             // Read state from savedState. No loading involved here
             mState = savedState.<RawContactDeltaList> getParcelable(KEY_EDIT_STATE);
             mStatus = savedState.getInt(KEY_STATUS);
+            mRawContactDisplayAloneIsReadOnly = savedState.getBoolean(
+                    KEY_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY);
 
             mHasNewContact = savedState.getBoolean(KEY_HAS_NEW_CONTACT);
             mNewContactDataReady = savedState.getBoolean(KEY_NEW_CONTACT_READY);
@@ -598,6 +610,8 @@
         outState.putBoolean(KEY_NEW_CONTACT_ACCOUNT_CHANGED, mNewContactAccountChanged);
         outState.putBoolean(KEY_IS_EDIT, mIsEdit);
         outState.putBoolean(KEY_EXISTING_CONTACT_READY, mExistingContactDataReady);
+        outState.putBoolean(KEY_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY,
+                mRawContactDisplayAloneIsReadOnly);
 
         outState.putBoolean(KEY_IS_USER_PROFILE, mIsUserProfile);
 
@@ -764,6 +778,9 @@
             helpMenu.setVisible(false);
         }
 
+        // Save menu is invisible when there's only one read only contact in the editor.
+        saveMenu.setVisible(!mRawContactDisplayAloneIsReadOnly);
+
         // Hide telephony-related settings (ringtone, send to voicemail)
         // if we don't have a telephone or are editing a new contact.
         sendToVoiceMailMenu.setChecked(mSendToVoicemailState);
@@ -1337,6 +1354,8 @@
                         mIntentExtras.getInt(INTENT_EXTRA_MATERIAL_PALETTE_SECONDARY_COLOR));
             }
             mPhotoId = mIntentExtras.getLong(INTENT_EXTRA_PHOTO_ID);
+            mRawContactDisplayAloneIsReadOnly = mIntentExtras.getBoolean(
+                    INTENT_EXTRA_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY);
         }
     }
 
@@ -1375,8 +1394,7 @@
             }
         }
         switch (saveMode) {
-            case SaveMode.CLOSE:
-            case SaveMode.COMPACT: {
+            case SaveMode.CLOSE: {
                 final Intent resultIntent;
                 if (saveSucceeded && contactLookupUri != null) {
                     final Uri lookupUri = maybeConvertToLegacyLookupUri(
@@ -1391,6 +1409,12 @@
                 if (mListener != null) mListener.onSaveFinished(resultIntent);
                 break;
             }
+            case SaveMode.COMPACT: {
+                // It is already saved, so prevent it from being saved again
+                mStatus = Status.CLOSING;
+                if (mListener != null) mListener.onSaveFinished(/* resultIntent= */ null);
+                break;
+            }
             case SaveMode.RELOAD:
             case SaveMode.JOIN:
                 if (saveSucceeded && contactLookupUri != null) {
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 28ede51..fc438f9 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -36,6 +36,7 @@
 import com.android.contacts.ContactSaveService;
 import com.android.contacts.R;
 import com.android.contacts.activities.ContactEditorActivity;
+import com.android.contacts.activities.ContactEditorBaseActivity.ContactEditor;
 import com.android.contacts.common.model.AccountTypeManager;
 import com.android.contacts.common.model.RawContactDelta;
 import com.android.contacts.common.model.RawContactDeltaList;
@@ -168,6 +169,8 @@
     public boolean onOptionsItemSelected(MenuItem item) {
         if (item.getItemId() == android.R.id.home) {
             return revert();
+        } else if (item.getItemId() == R.id.menu_save && mRawContactIdToDisplayAlone != -1) {
+            return super.save(SaveMode.COMPACT);
         }
         return super.onOptionsItemSelected(item);
     }
@@ -354,7 +357,7 @@
     private void bindPhotoHandler(BaseRawContactEditorView editor, AccountType type,
             RawContactDeltaList state) {
         final int mode;
-        final boolean showIsPrimaryOption;
+        boolean showIsPrimaryOption;
         if (type.areContactsWritable()) {
             if (editor.hasSetPhoto()) {
                 mode = PhotoActionPopup.Modes.WRITE_ABLE_PHOTO;
@@ -372,6 +375,9 @@
             editor.getPhotoEditor().setShowPrimary(false);
             return;
         }
+        if (mRawContactIdToDisplayAlone != -1) {
+            showIsPrimaryOption = false;
+        }
         final PhotoHandler photoHandler = new PhotoHandler(mContext, editor, mode, state);
         editor.getPhotoEditor().setEditorListener(
                 (PhotoHandler.PhotoEditorListener) photoHandler.getListener());
diff --git a/src/com/android/contacts/editor/EditorIntents.java b/src/com/android/contacts/editor/EditorIntents.java
index 26279df..d87a726 100644
--- a/src/com/android/contacts/editor/EditorIntents.java
+++ b/src/com/android/contacts/editor/EditorIntents.java
@@ -21,7 +21,9 @@
 import com.android.contacts.common.model.RawContactDeltaList;
 import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
 
+import android.app.Activity;
 import android.content.ContentValues;
+import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
@@ -120,6 +122,23 @@
         return intent;
     }
 
+    /**
+     * Returns an Intent to start the full editor for the given raw contact. The full editor will
+     * only display this one raw contact.
+     */
+    public static Intent createEditContactIntentForRawContact(Context context,
+            Uri rawContactUri, long rawContactId, boolean isReadOnly) {
+        final Intent intent = new Intent(context, ContactEditorActivity.class);
+        intent.setAction(ContactEditorBaseActivity.ACTION_EDIT);
+        intent.setData(rawContactUri);
+        intent.putExtra(ContactEditorFragment.INTENT_EXTRA_RAW_CONTACT_ID_TO_DISPLAY_ALONE,
+                rawContactId);
+        intent.putExtra(
+                ContactEditorBaseFragment.INTENT_EXTRA_RAW_CONTACT_DISPLAY_ALONE_IS_READ_ONLY,
+                isReadOnly);
+        return intent;
+    }
+
     private static void addContactIntentFlags(Intent intent) {
         intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                 | Intent.FLAG_ACTIVITY_FORWARD_RESULT);