Fix bugs on Contact editor.

1) Remove primary photo checkbox in full editor. Primary photo should
be set in photo picker.
2) Remove save menu if there's only one read only contact in the full editor.
3) Add account type icon in account info when there's only one account
for a contact.
4) Return back to Compact editor after saving results in full editor.

BUG 24547289

Change-Id: I02f8ee01f7cc7d1b0b06ad338570dc4b17c55e31
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 a0e0c9e..d073f68 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -260,14 +260,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 f808d4d..a6f2d1c 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -129,7 +129,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);
     }
 
     /**
@@ -382,6 +382,7 @@
     private View mAccountHeaderContainer;
     private TextView mAccountHeaderType;
     private TextView mAccountHeaderName;
+    private ImageView mAccountHeaderIcon;
 
     // Account selector
     private View mAccountSelectorContainer;
@@ -430,6 +431,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);
@@ -824,6 +826,10 @@
         }
         mAccountHeaderType.setText(accountInfo.second);
 
+        final AccountType primaryAccountType = mPrimaryRawContactDelta.getRawContactAccountType(
+                getContext());
+        mAccountHeaderIcon.setImageDrawable(primaryAccountType.getDisplayIcon(getContext()));
+
         mAccountHeaderContainer.setContentDescription(
                 EditorUiUtils.getAccountInfoContentDescription(
                         accountInfo.first, accountInfo.second));
@@ -906,19 +912,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 c5e6afd..02ac40b 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;
@@ -463,6 +473,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);
@@ -587,6 +599,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);
 
@@ -753,6 +767,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);
@@ -1291,6 +1308,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);
         }
     }
 
@@ -1329,8 +1348,7 @@
             }
         }
         switch (saveMode) {
-            case SaveMode.CLOSE:
-            case SaveMode.COMPACT: {
+            case SaveMode.CLOSE: {
                 final Intent resultIntent;
                 if (saveSucceeded && contactLookupUri != null) {
                     final Uri lookupUri = maybeConvertToLegacyLookupUri(
@@ -1345,6 +1363,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);