Remove account selector when editing a raw contact

Keep the header, but remove the onClick drop down.
Remove the boolean for if the raw contact is read only,
and use the current state to determine if that's true.

Test:
Applied this commit on top of the spring board one and tested:
 1) edit read only raw contact
 2) edit writable raw contact
 3) edit contact made of single read only raw contact still opens
    the insert a new raw contact version
 4) insert new contact

Bug: 31826229
Bug: 31088704
Change-Id: Ia8e5b0a5965b77b0c160336ce81ddac801b201ff
diff --git a/res/drawable-hdpi/ic_link_grey600_drawable_24dp.png b/res/drawable-hdpi/ic_link_grey600_drawable_24dp.png
deleted file mode 100644
index 31fcce2..0000000
--- a/res/drawable-hdpi/ic_link_grey600_drawable_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-mdpi/ic_link_grey600_drawable_24dp.png b/res/drawable-mdpi/ic_link_grey600_drawable_24dp.png
deleted file mode 100644
index 65997eb..0000000
--- a/res/drawable-mdpi/ic_link_grey600_drawable_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xhdpi/ic_link_grey600_drawable_24dp.png b/res/drawable-xhdpi/ic_link_grey600_drawable_24dp.png
deleted file mode 100644
index 9f37410..0000000
--- a/res/drawable-xhdpi/ic_link_grey600_drawable_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xxhdpi/ic_link_grey600_drawable_24dp.png b/res/drawable-xxhdpi/ic_link_grey600_drawable_24dp.png
deleted file mode 100644
index 140daef..0000000
--- a/res/drawable-xxhdpi/ic_link_grey600_drawable_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xxxhdpi/ic_link_grey600_drawable_24dp.png b/res/drawable-xxxhdpi/ic_link_grey600_drawable_24dp.png
deleted file mode 100644
index 6fe9b8c..0000000
--- a/res/drawable-xxxhdpi/ic_link_grey600_drawable_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/layout-land/contact_editor_fragment.xml b/res/layout-land/contact_editor_fragment.xml
index 6b02462..7f98765 100644
--- a/res/layout-land/contact_editor_fragment.xml
+++ b/res/layout-land/contact_editor_fragment.xml
@@ -44,8 +44,6 @@
                 android:layout_height="wrap_content"
                 android:orientation="vertical">
 
-            <include layout="@layout/editor_all_rawcontacts_accounts_selector" />
-
             <include layout="@layout/editor_account_header" />
 
             <include layout="@layout/contact_editor_fields" />
diff --git a/res/layout/contact_editor_fragment.xml b/res/layout/contact_editor_fragment.xml
index 85c4cf1..690be8c 100644
--- a/res/layout/contact_editor_fragment.xml
+++ b/res/layout/contact_editor_fragment.xml
@@ -39,7 +39,6 @@
             android:focusable="true"
             android:focusableInTouchMode="true"/>
 
-        <include layout="@layout/editor_all_rawcontacts_accounts_selector" />
         <include layout="@layout/editor_account_header" />
 
         <include layout="@layout/contact_editor_fields" />
diff --git a/res/layout/editor_all_rawcontacts_accounts_selector.xml b/res/layout/editor_all_rawcontacts_accounts_selector.xml
deleted file mode 100644
index 5606422..0000000
--- a/res/layout/editor_all_rawcontacts_accounts_selector.xml
+++ /dev/null
@@ -1,59 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<!-- Header at the top of a raw contact editor. This allows users to change the account that
-    the raw contact is saved in. -->
-<LinearLayout
-        xmlns:android="http://schemas.android.com/apk/res/android"
-        android:id="@+id/all_rawcontacts_accounts_container"
-        android:layout_height="wrap_content"
-        android:layout_width="match_parent"
-        android:minHeight="@dimen/editor_min_line_item_height"
-        android:orientation="horizontal"
-        android:visibility="gone" >
-
-    <ImageView
-            android:src="@drawable/ic_link_grey600_drawable_24dp"
-            android:tint="@color/editor_icon_color"
-            style="@style/EditSelectorIconStyle"/>
-
-    <TextView
-            android:id="@+id/rawcontacts_accounts_summary"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:layout_marginStart="@dimen/editor_account_left_margin"
-            android:textSize="16sp"
-            android:singleLine="true"
-            android:layout_weight="1"
-            android:textColor="@color/primary_text_color"
-            android:textAlignment="viewStart"
-            android:layout_gravity="center_vertical"
-            android:gravity="center_vertical"
-            android:ellipsize="end" />
-
-    <ImageView
-            android:src="@drawable/ic_menu_expand_minimized_24dp"
-            android:tint="@color/quantum_black_secondary_text"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="center_vertical|end"
-            android:layout_alignParentEnd="true"
-            android:paddingStart="@dimen/editor_round_button_padding_left"
-            android:paddingEnd="@dimen/editor_round_button_padding_right"
-            android:paddingTop="@dimen/editor_round_button_padding_top"
-            android:paddingBottom="@dimen/editor_round_button_padding_bottom"/>
-
-</LinearLayout>
\ No newline at end of file
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 90aad4c..48e4bee 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -143,8 +143,6 @@
     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";
@@ -350,7 +348,6 @@
     protected RawContactDeltaList mState;
     protected int mStatus;
     protected long mRawContactIdToDisplayAlone = -1;
-    protected boolean mRawContactDisplayAloneIsReadOnly = false;
 
     // Whether to show the new contact blank form and if it's corresponding delta is ready.
     protected boolean mHasNewContact;
@@ -497,8 +494,6 @@
             // 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);
@@ -629,8 +624,6 @@
         outState.putBoolean(KEY_NEW_CONTACT_READY, mNewContactDataReady);
         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);
 
@@ -780,7 +773,7 @@
         }
 
         // Save menu is invisible when there's only one read only contact in the editor.
-        saveMenu.setVisible(!mRawContactDisplayAloneIsReadOnly);
+        saveMenu.setVisible(!isEditingReadOnlyRawContact());
         if (saveMenu.isVisible()) {
             // Since we're using a custom action layout we have to manually hook up the handler.
             saveMenu.getActionView().setOnClickListener(new View.OnClickListener() {
@@ -1009,6 +1002,16 @@
     }
 
     /**
+     * @return true if the single raw contact we're looking at is read-only.
+     */
+    private boolean isEditingReadOnlyRawContact() {
+        return hasValidState() && mRawContactIdToDisplayAlone > 0
+                && !mState.getByRawContactId(mRawContactIdToDisplayAlone)
+                        .getAccountType(AccountTypeManager.getInstance(mContext))
+                                .areContactsWritable();
+    }
+
+    /**
      * Return true if there are any edits to the current contact which need to
      * be saved.
      */
@@ -1860,13 +1863,6 @@
         getEditorActivity().changePhoto(getPhotoMode());
     }
 
-    @Override
-    public void onRawContactSelected(long rawContactId, boolean isReadOnly) {
-        mRawContactDisplayAloneIsReadOnly = isReadOnly;
-        mRawContactIdToDisplayAlone = rawContactId;
-        bindEditors();
-    }
-
     private int getPhotoMode() {
         return getContent().isWritablePhotoSet() ? PhotoActionPopup.Modes.WRITE_ABLE_PHOTO
                 : PhotoActionPopup.Modes.NO_PHOTO;
diff --git a/src/com/android/contacts/editor/RawContactEditorView.java b/src/com/android/contacts/editor/RawContactEditorView.java
index bbd23d4..d3c7535 100644
--- a/src/com/android/contacts/editor/RawContactEditorView.java
+++ b/src/com/android/contacts/editor/RawContactEditorView.java
@@ -44,7 +44,6 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
-import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ListPopupWindow;
@@ -62,8 +61,6 @@
 import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
 import com.android.contacts.common.model.account.AccountType;
 import com.android.contacts.common.model.account.AccountWithDataSet;
-import com.android.contacts.common.model.account.DeviceLocalAccountType;
-import com.android.contacts.common.model.account.SimAccountType;
 import com.android.contacts.common.model.dataitem.CustomDataItem;
 import com.android.contacts.common.model.dataitem.DataKind;
 import com.android.contacts.common.util.AccountsListAdapter;
@@ -73,7 +70,6 @@
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
@@ -120,76 +116,7 @@
          * Invoked after editors have been bound for the contact.
          */
         public void onEditorsBound();
-
-        /**
-         * Invoked when a rawcontact from linked contacts is selected in editor.
-         */
-        void onRawContactSelected(long rawContactId, boolean isReadOnly);
     }
-
-    /**
-     * Used to list the account info for the given raw contacts list.
-     */
-    private static final class RawContactAccountListAdapter extends BaseAdapter {
-        private final LayoutInflater mInflater;
-        private final Context mContext;
-        private final RawContactDeltaList mRawContactDeltas;
-
-        public RawContactAccountListAdapter(Context context, RawContactDeltaList rawContactDeltas) {
-            mContext = context;
-            mRawContactDeltas = new RawContactDeltaList();
-            for (RawContactDelta rawContactDelta : rawContactDeltas) {
-                if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
-                    mRawContactDeltas.add(rawContactDelta);
-                }
-            }
-            mInflater = LayoutInflater.from(context);
-        }
-
-        @Override
-        public View getView(int position, View convertView, ViewGroup parent) {
-            final View resultView = convertView != null ? convertView
-                    : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
-
-            final RawContactDelta rawContactDelta = mRawContactDeltas.get(position);
-
-            final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
-            final AccountType accountType = rawContactDelta.getRawContactAccountType(mContext);
-            text1.setText(accountType.getDisplayLabel(mContext));
-
-            final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
-            final String accountName = rawContactDelta.getAccountName();
-            if (TextUtils.isEmpty(accountName) || accountType instanceof DeviceLocalAccountType
-                    || accountType instanceof SimAccountType) {
-                text2.setVisibility(View.GONE);
-            } else {
-                // Truncate email addresses in the middle so we don't lose the domain
-                text2.setText(accountName);
-                text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
-            }
-
-            final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
-            icon.setImageDrawable(accountType.getDisplayIcon(mContext));
-
-            return resultView;
-        }
-
-        @Override
-        public int getCount() {
-            return mRawContactDeltas.size();
-        }
-
-        @Override
-        public RawContactDelta getItem(int position) {
-            return mRawContactDeltas.get(position);
-        }
-
-        @Override
-        public long getItemId(int position) {
-            return getItem(position).getRawContactId();
-        }
-    }
-
     /**
      * Sorts kinds roughly the same as quick contacts; we diverge in the following ways:
      * <ol>
@@ -578,7 +505,6 @@
     }
 
     private void pickRawContactDelta() {
-        // Build the kind section data list map
         vlog("parse: " + mRawContactDeltas.size() + " rawContactDelta(s)");
         for (int j = 0; j < mRawContactDeltas.size(); j++) {
             final RawContactDelta rawContactDelta = mRawContactDeltas.get(j);
@@ -618,15 +544,17 @@
 
         for (int i = 0; i < dataKindSize; i++) {
             final DataKind dataKind = dataKinds.get(i);
-            if (dataKind == null) {
-                vlog("parse: " + i + " " + dataKind.mimeType + " dropped null data kind");
+            // Skip null and un-editable fields.
+            if (dataKind == null || !dataKind.editable) {
+                vlog("parse: " + i +
+                        (dataKind == null ? " dropped null data kind"
+                        : " dropped uneditable mimetype: " + dataKind.mimeType));
                 continue;
             }
             final String mimeType = dataKind.mimeType;
 
             // Skip psuedo mime types
-            if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
-                    || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
+            if (DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
                 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
                 continue;
             }
@@ -777,51 +705,16 @@
                 ? EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(), account)
                 : account.getNameLabel().toString();
 
-        // Either the account header or selector should be shown, not both.
+        addAccountHeader(accountLabel);
+
+        // If we're saving a new contact and there are multiple accounts, add the account selector.
         final List<AccountWithDataSet> accounts =
                 AccountTypeManager.getInstance(getContext()).getAccounts(true);
-
-        if (mHasNewContact && !mIsUserProfile) {
-            if (accounts.size() > 1) {
-                addAccountSelector(mCurrentRawContactDelta, accountLabel);
-            } else {
-                addAccountHeader(accountLabel);
-            }
-        } else {
-            // The raw contact selector should only display linked raw contacts that can be edited
-            // in the full editor (i.e. they are not newly created raw contacts)
-            final RawContactAccountListAdapter adapter =  new RawContactAccountListAdapter(
-                    getContext(), getRawContactDeltaListForSelector(mRawContactDeltas));
-            if (adapter.getCount() > 0 && !mIsEditingReadOnlyRawContactWithNewContact) {
-                addRawContactAccountSelector(accountLabel, adapter);
-            } else {
-                addAccountHeader(accountLabel);
-            }
+        if (mHasNewContact && !mIsUserProfile && accounts.size() > 1) {
+            addAccountSelector(mCurrentRawContactDelta);
         }
     }
 
-    private RawContactDeltaList getRawContactDeltaListForSelector(
-            RawContactDeltaList rawContactDeltas) {
-        // Sort raw contacts so google accounts come first
-        Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
-
-        final RawContactDeltaList result = new RawContactDeltaList();
-        for (int i = 0; i < rawContactDeltas.size(); i++) {
-            final RawContactDelta rawContactDelta = rawContactDeltas.get(i);
-            if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
-                // Only add raw contacts that can be opened in the editor
-                result.add(rawContactDelta);
-            }
-        }
-        // Don't return a list of size 1 that would just open the current raw contact being edited.
-        if (result.size() == 1 && result.get(0).getRawContactAccountType(
-                getContext()).areContactsWritable()) {
-            result.clear();
-            return result;
-        }
-        return result;
-    }
-
     private void addAccountHeader(String accountLabel) {
         mAccountHeaderContainer.setVisibility(View.VISIBLE);
 
@@ -846,8 +739,10 @@
                         selectorTitle));
     }
 
-    private void addAccountSelector(final RawContactDelta rawContactDelta, CharSequence nameLabel) {
-        final View.OnClickListener onClickListener = new View.OnClickListener() {
+    private void addAccountSelector(final RawContactDelta rawContactDelta) {
+        // Add handlers for choosing another account to save to.
+        mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
+        mAccountHeaderContainer.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
@@ -877,53 +772,7 @@
                 });
                 popup.show();
             }
-        };
-        setUpAccountSelector(nameLabel.toString(), onClickListener);
-    }
-
-    private void addRawContactAccountSelector(String nameLabel,
-            final RawContactAccountListAdapter adapter) {
-        final View.OnClickListener onClickListener = new OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
-                popup.setWidth(mAccountHeaderContainer.getWidth());
-                popup.setAnchorView(mAccountHeaderContainer);
-                popup.setAdapter(adapter);
-                popup.setModal(true);
-                popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
-                popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
-                    @Override
-                    public void onItemClick(AdapterView<?> parent, View view, int position,
-                                            long id) {
-                        UiClosables.closeQuietly(popup);
-                        final long rawContactId = adapter.getItemId(position);
-                        // Only switch if it's actually a different raw contact.
-                        if (rawContactId != mCurrentRawContactDelta.getRawContactId()
-                                && mListener != null) {
-                            final RawContactDelta rawContactDelta = adapter.getItem(position);
-                            final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
-                                    getContext());
-                            final AccountType accountType = rawContactDelta.getAccountType(
-                                    accountTypes);
-                            final boolean isReadOnly = !accountType.areContactsWritable();
-                            // Reset state.
-                            mIsExpanded = false;
-                            mListener.onRawContactSelected(rawContactId, isReadOnly);
-                        }
-                    }
-                });
-                popup.show();
-            }
-        };
-        setUpAccountSelector(nameLabel, onClickListener);
-    }
-
-    private void setUpAccountSelector(String nameLabel, OnClickListener listener) {
-        addAccountHeader(nameLabel);
-        // Add handlers for choosing another account to save to.
-        mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
-        mAccountHeaderContainer.setOnClickListener(listener);
+        });
     }
 
     private void addPhotoView() {