Merge "Let Contacts refrain from stripping methods with the suffix ForTest."
diff --git a/res/layout/contact_detail_activity.xml b/res/layout/contact_detail_activity.xml
index 9b63fec..c13603f 100644
--- a/res/layout/contact_detail_activity.xml
+++ b/res/layout/contact_detail_activity.xml
@@ -14,14 +14,12 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
     <fragment android:name="com.android.contacts.views.detail.ContactDetailFragment"
             android:id="@+id/contact_detail_fragment"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_weight="1" />
-</LinearLayout>
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" />
+</FrameLayout>
diff --git a/res/layout/contact_editor_activity.xml b/res/layout/contact_editor_activity.xml
index 3768f6a..a2cdead 100644
--- a/res/layout/contact_editor_activity.xml
+++ b/res/layout/contact_editor_activity.xml
@@ -14,14 +14,12 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
     <fragment android:name="com.android.contacts.views.editor.ContactEditorFragment"
             android:id="@+id/contact_editor_fragment"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_weight="1" />
-</LinearLayout>
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" />
+</FrameLayout>
diff --git a/res/layout/contact_editor_fragment.xml b/res/layout/contact_editor_fragment.xml
index 6a360ac..602f7f9 100644
--- a/res/layout/contact_editor_fragment.xml
+++ b/res/layout/contact_editor_fragment.xml
@@ -14,16 +14,14 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:orientation="vertical"
 >
 
     <ScrollView
         android:layout_width="match_parent"
-        android:layout_height="1px"
-        android:layout_weight="1"
+        android:layout_height="match_parent"
         android:fillViewport="true"
     >
 
@@ -34,28 +32,4 @@
         />
 
     </ScrollView>
-
-    <LinearLayout
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:orientation="horizontal"
-        style="@android:style/ButtonBar"
-    >
-
-        <Button android:id="@+id/btn_done"
-            android:layout_width="0dip"
-            android:layout_height="wrap_content"
-            android:layout_weight="1"
-            android:text="@string/menu_done"
-        />
-
-        <Button android:id="@+id/btn_discard"
-            android:layout_width="0dip"
-            android:layout_height="wrap_content"
-            android:layout_weight="1"
-            android:text="@string/menu_doNotSave"
-        />
-
-    </LinearLayout>
-
-</LinearLayout>
+</FrameLayout>
diff --git a/res/layout/item_generic_editor.xml b/res/layout/item_generic_editor.xml
index 329ad28..e62612f 100644
--- a/res/layout/item_generic_editor.xml
+++ b/res/layout/item_generic_editor.xml
@@ -35,7 +35,7 @@
         android:layout_alignParentRight="true"
         style="@style/MinusButton" />
 
-    <LinearLayout
+    <FrameLayout
         android:id="@+id/edit_fields"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
@@ -43,7 +43,6 @@
         android:layout_alignWithParentIfMissing="true"
         android:layout_toRightOf="@id/edit_label"
         android:layout_toLeftOf="@id/edit_delete"
-        android:orientation="vertical"
         android:baselineAligned="false"
         android:gravity="center_vertical" />
 
diff --git a/res/layout/two_pane_activity.xml b/res/layout/two_pane_activity.xml
index 407360b..dbac12f 100644
--- a/res/layout/two_pane_activity.xml
+++ b/res/layout/two_pane_activity.xml
@@ -35,7 +35,7 @@
                 android:layout_weight="1" />
 
         <!--  Holder for detail- or editor-fragment. -->
-        <LinearLayout
+        <FrameLayout
             android:id="@+id/two_pane_right_view"
             android:orientation="horizontal"
             android:layout_width="0px"
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 49f1c0b..1846824 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -54,9 +54,6 @@
 
     <!-- For ContactsListActivity -->
     <item type="id" name="dialog_delete_contact_confirmation"/>
-    <item type="id" name="dialog_readonly_contact_hide_confirmation"/>
-    <item type="id" name="dialog_multiple_contact_delete_confirmation"/>
-    <item type="id" name="dialog_readonly_contact_delete_confirmation"/>
 
     <!-- For ExportVCardActivity -->
     <item type="id" name="dialog_export_confirmation"/>
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 2801618..4958be6 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -16,6 +16,7 @@
 
 package com.android.contacts;
 
+import com.android.contacts.interactions.ContactDeletionInteraction;
 import com.android.contacts.list.CallOrSmsInitiator;
 import com.android.contacts.list.ContactBrowseListContextMenuAdapter;
 import com.android.contacts.list.ContactEntryListFragment;
@@ -30,7 +31,6 @@
 import com.android.contacts.list.PhoneNumberPickerFragment;
 import com.android.contacts.list.PostalAddressPickerFragment;
 import com.android.contacts.list.StrequentContactListFragment;
-import com.android.contacts.model.ContactsSource;
 import com.android.contacts.model.Sources;
 import com.android.contacts.ui.ContactsPreferencesActivity;
 import com.android.contacts.util.AccountSelectionUtil;
@@ -44,8 +44,6 @@
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.FragmentTransaction;
-import android.app.SearchManager;
-import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -55,11 +53,9 @@
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.ContactsContract;
-import android.provider.Settings;
 import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.RawContacts;
+import android.provider.Settings;
 import android.telephony.TelephonyManager;
-import android.text.TextUtils;
 import android.util.Log;
 import android.view.ContextThemeWrapper;
 import android.view.KeyEvent;
@@ -73,13 +69,11 @@
 import android.widget.TextView;
 import android.widget.Toast;
 
-import java.util.ArrayList;
 import java.util.List;
 
 /**
  * Displays a list of contacts. Usually is embedded into the ContactsActivity.
  */
-@SuppressWarnings("deprecation")
 public class ContactsListActivity extends Activity implements View.OnCreateContextMenuListener {
 
     private static final String TAG = "ContactsListActivity";
@@ -89,33 +83,15 @@
     private static final int SUBACTIVITY_DISPLAY_GROUP = 3;
     private static final int SUBACTIVITY_SEARCH = 4;
 
-    private static final String[] RAW_CONTACTS_PROJECTION = new String[] {
-        RawContacts._ID, //0
-        RawContacts.CONTACT_ID, //1
-        RawContacts.ACCOUNT_TYPE, //2
-    };
-
-    private Uri mSelectedContactUri;
-
-    private ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
-    private int  mWritableSourcesCnt;
-    private int  mReadOnlySourcesCnt;
-
     private final String[] sLookupProjection = new String[] {
             Contacts.LOOKUP_KEY
     };
-    private class DeleteClickListener implements DialogInterface.OnClickListener {
-        public void onClick(DialogInterface dialog, int which) {
-            if (mSelectedContactUri != null) {
-                getContentResolver().delete(mSelectedContactUri, null, null);
-            }
-        }
-    }
 
     private ContactsIntentResolver mIntentResolver;
     protected ContactEntryListFragment<?> mListFragment;
 
     protected CallOrSmsInitiator mCallOrSmsInitiator;
+    private ContactDeletionInteraction mContactDeletionInteraction;
 
     private int mActionCode;
 
@@ -124,17 +100,11 @@
     private ContactsRequest mRequest;
     private SearchEditText mSearchEditText;
 
+
     public ContactsListActivity() {
         mIntentResolver = new ContactsIntentResolver(this);
     }
 
-    /**
-     * Visible for testing: makes queries run on the UI thread.
-     */
-    /* package */ void runQueriesSynchronously() {
-        // TODO
-    }
-
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -376,7 +346,7 @@
         }
 
         public void onDeleteContactAction(Uri contactUri) {
-            doContactDelete(contactUri);
+            getContactDeletionInteraction().deleteContact(contactUri);
         }
 
         public void onFinishAction() {
@@ -514,6 +484,11 @@
 
     @Override
     protected Dialog onCreateDialog(int id, Bundle bundle) {
+        Dialog dialog = getContactDeletionInteraction().onCreateDialog(id, bundle);
+        if (dialog != null) {
+            return dialog;
+        }
+
         switch (id) {
             case R.string.import_from_sim:
             case R.string.import_from_sdcard: {
@@ -521,51 +496,23 @@
             }
             case R.id.dialog_sdcard_not_found: {
                 return new AlertDialog.Builder(this)
-                        .setTitle(R.string.no_sdcard_title)
-                        .setIcon(android.R.drawable.ic_dialog_alert)
-                        .setMessage(R.string.no_sdcard_message)
-                        .setPositiveButton(android.R.string.ok, null).create();
-            }
-            case R.id.dialog_delete_contact_confirmation: {
-                return new AlertDialog.Builder(this)
-                        .setTitle(R.string.deleteConfirmation_title)
-                        .setIcon(android.R.drawable.ic_dialog_alert)
-                        .setMessage(R.string.deleteConfirmation)
-                        .setNegativeButton(android.R.string.cancel, null)
-                        .setPositiveButton(android.R.string.ok,
-                                new DeleteClickListener()).create();
-            }
-            case R.id.dialog_readonly_contact_hide_confirmation: {
-                return new AlertDialog.Builder(this)
-                        .setTitle(R.string.deleteConfirmation_title)
-                        .setIcon(android.R.drawable.ic_dialog_alert)
-                        .setMessage(R.string.readOnlyContactWarning)
-                        .setNegativeButton(android.R.string.cancel, null)
-                        .setPositiveButton(android.R.string.ok,
-                                new DeleteClickListener()).create();
-            }
-            case R.id.dialog_readonly_contact_delete_confirmation: {
-                return new AlertDialog.Builder(this)
-                        .setTitle(R.string.deleteConfirmation_title)
-                        .setIcon(android.R.drawable.ic_dialog_alert)
-                        .setMessage(R.string.readOnlyContactDeleteConfirmation)
-                        .setNegativeButton(android.R.string.cancel, null)
-                        .setPositiveButton(android.R.string.ok,
-                                new DeleteClickListener()).create();
-            }
-            case R.id.dialog_multiple_contact_delete_confirmation: {
-                return new AlertDialog.Builder(this)
-                        .setTitle(R.string.deleteConfirmation_title)
-                        .setIcon(android.R.drawable.ic_dialog_alert)
-                        .setMessage(R.string.multipleContactDeleteConfirmation)
-                        .setNegativeButton(android.R.string.cancel, null)
-                        .setPositiveButton(android.R.string.ok,
-                                new DeleteClickListener()).create();
+                .setTitle(R.string.no_sdcard_title)
+                .setIcon(android.R.drawable.ic_dialog_alert)
+                .setMessage(R.string.no_sdcard_message)
+                .setPositiveButton(android.R.string.ok, null).create();
             }
         }
         return super.onCreateDialog(id, bundle);
     }
 
+    @Override
+    protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
+        if (getContactDeletionInteraction().onPrepareDialog(id, dialog, bundle)) {
+            return;
+        }
+
+        super.onPrepareDialog(id, dialog, bundle);
+    }
     /**
      * Create a {@link Dialog} that allows the user to pick from a bulk import
      * or bulk export task across all contacts.
@@ -790,53 +737,18 @@
         return false;
     }
 
-    /**
-     * Prompt the user before deleting the given {@link Contacts} entry.
-     */
-    protected void doContactDelete(Uri contactUri) {
-        mReadOnlySourcesCnt = 0;
-        mWritableSourcesCnt = 0;
-        mWritableRawContactIds.clear();
-
-        Sources sources = Sources.getInstance(ContactsListActivity.this);
-        Cursor c = getContentResolver().query(RawContacts.CONTENT_URI, RAW_CONTACTS_PROJECTION,
-                RawContacts.CONTACT_ID + "=" + ContentUris.parseId(contactUri), null,
-                null);
-        if (c != null) {
-            try {
-                while (c.moveToNext()) {
-                    final String accountType = c.getString(2);
-                    final long rawContactId = c.getLong(0);
-                    ContactsSource contactsSource = sources.getInflatedSource(accountType,
-                            ContactsSource.LEVEL_SUMMARY);
-                    if (contactsSource != null && contactsSource.readOnly) {
-                        mReadOnlySourcesCnt += 1;
-                    } else {
-                        mWritableSourcesCnt += 1;
-                        mWritableRawContactIds.add(rawContactId);
-                    }
-                }
-            } finally {
-                c.close();
-            }
-        }
-
-        mSelectedContactUri = contactUri;
-        if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt > 0) {
-            showDialog(R.id.dialog_readonly_contact_delete_confirmation);
-        } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
-            showDialog(R.id.dialog_readonly_contact_hide_confirmation);
-        } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
-            showDialog(R.id.dialog_multiple_contact_delete_confirmation);
-        } else {
-            showDialog(R.id.dialog_delete_contact_confirmation);
-        }
-    }
-
     private CallOrSmsInitiator getCallOrSmsInitiator() {
         if (mCallOrSmsInitiator == null) {
             mCallOrSmsInitiator = new CallOrSmsInitiator(this);
         }
         return mCallOrSmsInitiator;
     }
+
+    private ContactDeletionInteraction getContactDeletionInteraction() {
+        if (mContactDeletionInteraction == null) {
+            mContactDeletionInteraction = new ContactDeletionInteraction();
+            mContactDeletionInteraction.attachToActivity(this);
+        }
+        return mContactDeletionInteraction;
+    }
 }
diff --git a/src/com/android/contacts/DialtactsActivity.java b/src/com/android/contacts/DialtactsActivity.java
index 758a3b5..882a4fb 100644
--- a/src/com/android/contacts/DialtactsActivity.java
+++ b/src/com/android/contacts/DialtactsActivity.java
@@ -102,7 +102,7 @@
 
         setCurrentTab(intent);
 
-        if (intent.getAction().equals(UI.FILTER_CONTACTS_ACTION)
+        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
                 && icicle == null) {
             setupFilterText(intent);
         }
@@ -275,7 +275,7 @@
         fixIntent(newIntent);
         setCurrentTab(newIntent);
         final String action = newIntent.getAction();
-        if (action.equals(UI.FILTER_CONTACTS_ACTION)) {
+        if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
             setupFilterText(newIntent);
         } else if (isDialIntent(newIntent)) {
             setupDialUri(newIntent);
diff --git a/src/com/android/contacts/activities/ContactEditorActivity.java b/src/com/android/contacts/activities/ContactEditorActivity.java
index 2bac145..4369160 100644
--- a/src/com/android/contacts/activities/ContactEditorActivity.java
+++ b/src/com/android/contacts/activities/ContactEditorActivity.java
@@ -25,7 +25,6 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
-import android.view.MenuItem;
 import android.widget.Toast;
 
 public class ContactEditorActivity extends Activity {
@@ -59,14 +58,6 @@
     }
 
     @Override
-    public boolean onContextItemSelected(MenuItem item) {
-        // TODO: This is too hardwired.
-        if (mFragment.onContextItemSelected(item)) return true;
-
-        return super.onContextItemSelected(item);
-    }
-
-    @Override
     public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
             boolean globalSearch) {
         if (globalSearch) {
diff --git a/src/com/android/contacts/interactions/ContactDeletionInteraction.java b/src/com/android/contacts/interactions/ContactDeletionInteraction.java
new file mode 100644
index 0000000..2f5a290
--- /dev/null
+++ b/src/com/android/contacts/interactions/ContactDeletionInteraction.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+package com.android.contacts.interactions;
+
+import com.android.contacts.R;
+import com.android.contacts.model.ContactsSource;
+import com.android.contacts.model.Sources;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.ContentUris;
+import android.content.Context;
+import android.content.CursorLoader;
+import android.content.DialogInterface;
+import android.content.Loader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.RawContacts;
+import android.util.Log;
+
+/**
+ * An interaction invoked to delete a contact.
+ */
+public class ContactDeletionInteraction {
+
+    private static final String TAG = "ContactDeletionInteraction";
+
+    public static final String EXTRA_KEY_CONTACT_URI = "contactUri";
+    public static final String EXTRA_KEY_MESSAGE_ID = "messageId";
+
+    private static final String[] RAW_CONTACTS_PROJECTION = new String[] {
+        RawContacts._ID, //0
+        RawContacts.ACCOUNT_TYPE, //1
+    };
+
+    private final class RawContactLoader extends CursorLoader {
+        private final Uri mContactUri;
+
+        private RawContactLoader(Context context, Uri contactUri) {
+            super(context,
+                    RawContacts.CONTENT_URI,
+                    RAW_CONTACTS_PROJECTION,
+                    RawContacts.CONTACT_ID + "=?",
+                    new String[] { String.valueOf(ContentUris.parseId(contactUri)) },
+                    null);
+            this.mContactUri = contactUri;
+        }
+
+        @Override
+        public void deliverResult(Cursor data) {
+            if (data == null || data.getCount() == 0) {
+                Log.e(TAG, "No such contact: " + mContactUri);
+                return;
+            }
+
+            showConfirmationDialog(data, mContactUri);
+        }
+    }
+
+    private final class ConfirmationDialogClickListener implements DialogInterface.OnClickListener {
+        private final Uri mContactUri;
+
+        public ConfirmationDialogClickListener(Uri contactUri) {
+            this.mContactUri = contactUri;
+        }
+
+        public void onClick(DialogInterface dialog, int which) {
+            doDeleteContact(mContactUri);
+        }
+    }
+
+    private Context mContext;
+    private CursorLoader mLoader;
+
+    public void attachToActivity(Activity activity) {
+        setContext(activity);
+    }
+
+    /* Visible for testing */
+    void setContext(Context context) {
+        mContext = context;
+    }
+
+    public void deleteContact(Uri contactUri) {
+        if (mLoader != null) {
+            mLoader.destroy();
+        }
+        mLoader = new RawContactLoader(mContext, contactUri);
+        startLoading(mLoader);
+    }
+
+    protected void showConfirmationDialog(Cursor cursor, Uri contactUri) {
+        int  writableSourcesCnt = 0;
+        int  readOnlySourcesCnt = 0;
+
+        Sources sources = getSources();
+        try {
+            while (cursor.moveToNext()) {
+                final long rawContactId = cursor.getLong(0);
+                final String accountType = cursor.getString(1);
+                ContactsSource contactsSource = sources.getInflatedSource(accountType,
+                        ContactsSource.LEVEL_SUMMARY);
+                boolean readonly = contactsSource != null && contactsSource.readOnly;
+                if (readonly) {
+                    readOnlySourcesCnt ++;
+                } else {
+                    writableSourcesCnt ++;
+                }
+            }
+        } finally {
+            cursor.close();
+        }
+
+        int messageId;
+        if (readOnlySourcesCnt > 0 && writableSourcesCnt > 0) {
+            messageId = R.string.readOnlyContactDeleteConfirmation;
+        } else if (readOnlySourcesCnt > 0 && writableSourcesCnt == 0) {
+            messageId = R.string.readOnlyContactWarning;
+        } else if (readOnlySourcesCnt == 0 && writableSourcesCnt > 1) {
+            messageId = R.string.multipleContactDeleteConfirmation;
+        } else {
+            messageId = R.string.deleteConfirmation;
+        }
+
+        Bundle bundle = new Bundle();
+        bundle.putParcelable(EXTRA_KEY_CONTACT_URI, contactUri);
+        bundle.putInt(EXTRA_KEY_MESSAGE_ID, messageId);
+
+        showDialog(bundle);
+    }
+
+    /**
+     * Creates a delete confirmation dialog and returns it.  Returns null if the
+     * id is not for a deletion confirmation.
+     */
+    public Dialog onCreateDialog(int id, Bundle bundle) {
+        if (id != R.id.dialog_delete_contact_confirmation) {
+            return null;
+        }
+
+        return new AlertDialog.Builder(mContext)
+                .setTitle(R.string.deleteConfirmation_title)
+                .setIcon(android.R.drawable.ic_dialog_alert)
+                .setMessage(R.string.deleteConfirmation)
+                .setNegativeButton(android.R.string.cancel, null)
+                .setPositiveButton(android.R.string.ok, null)
+                .create();
+    }
+
+    public boolean onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
+        if (id != R.id.dialog_delete_contact_confirmation) {
+            return false;
+        }
+
+        Uri contactUri = bundle.getParcelable(EXTRA_KEY_CONTACT_URI);
+        int messageId = bundle.getInt(EXTRA_KEY_MESSAGE_ID, R.string.deleteConfirmation);
+
+        ((AlertDialog)dialog).setMessage(mContext.getText(messageId));
+        ((AlertDialog)dialog).setButton(DialogInterface.BUTTON_POSITIVE,
+                mContext.getText(android.R.string.ok),
+                new ConfirmationDialogClickListener(contactUri));
+
+        return true;
+    }
+
+    protected void doDeleteContact(Uri contactUri) {
+        mContext.getContentResolver().delete(contactUri, null, null);
+    }
+
+    /* Visible for testing */
+    void startLoading(Loader<Cursor> loader) {
+        loader.startLoading();
+    }
+
+    /* Visible for testing */
+    Sources getSources() {
+        return Sources.getInstance(mContext);
+    }
+
+    /* Visible for testing */
+    void showDialog(Bundle bundle) {
+        ((Activity)mContext).showDialog(R.id.dialog_delete_contact_confirmation, bundle);
+    }
+}
diff --git a/src/com/android/contacts/ui/widget/GenericEditorView.java b/src/com/android/contacts/ui/widget/GenericEditorView.java
index 5c2f9b7..1a981a8 100644
--- a/src/com/android/contacts/ui/widget/GenericEditorView.java
+++ b/src/com/android/contacts/ui/widget/GenericEditorView.java
@@ -51,7 +51,9 @@
 import android.view.inputmethod.EditorInfo;
 import android.widget.ArrayAdapter;
 import android.widget.EditText;
+import android.widget.FrameLayout;
 import android.widget.ImageButton;
+import android.widget.LinearLayout;
 import android.widget.ListAdapter;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
@@ -78,7 +80,7 @@
             | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS;
 
     protected TextView mLabel;
-    protected ViewGroup mFields;
+    protected FrameLayout mFields;
     protected View mDelete;
     protected ImageButton mMoreOrLess;
 
@@ -86,6 +88,7 @@
     protected ValuesDelta mEntry;
     protected EntityDelta mState;
     protected boolean mReadOnly;
+    private EditText[] mFieldEditTexts = null;
 
     protected boolean mHideOptional = true;
 
@@ -113,7 +116,7 @@
         mLabel = (TextView)findViewById(R.id.edit_label);
         mLabel.setOnClickListener(this);
 
-        mFields = (ViewGroup)findViewById(R.id.edit_fields);
+        mFields = (FrameLayout)findViewById(R.id.edit_fields);
 
         mDelete = findViewById(R.id.edit_delete);
         mDelete.setOnClickListener(this);
@@ -135,10 +138,12 @@
     @Override
     public void setEnabled(boolean enabled) {
         mLabel.setEnabled(enabled);
-        final int count = mFields.getChildCount();
-        for (int pos = 0; pos < count; pos++) {
-            final View v = mFields.getChildAt(pos);
-            v.setEnabled(enabled);
+        if (mFields.getChildCount() == 0) return;
+
+        if (mFieldEditTexts != null) {
+            for (int index = 0; index < mFieldEditTexts.length; index++) {
+                mFieldEditTexts[index].setEnabled(enabled);
+            }
         }
         mMoreOrLess.setEnabled(enabled);
     }
@@ -177,11 +182,12 @@
     }
 
     public boolean isAnyFieldFilledOut() {
-        int childCount = mFields.getChildCount();
-        for (int i = 0; i < childCount; i++) {
-            EditText editorView = (EditText) mFields.getChildAt(i);
-            if (!TextUtils.isEmpty(editorView.getText())) {
-                return true;
+        if (mFieldEditTexts != null) {
+            for (int index = 0; index < mFieldEditTexts.length; index++) {
+                final EditText editText = mFieldEditTexts[index];
+                if (!TextUtils.isEmpty(editText.getText())) {
+                    return true;
+                }
             }
         }
         return false;
@@ -226,11 +232,27 @@
         // Build out set of fields
         mFields.removeAllViews();
         boolean hidePossible = false;
-        int n = 0;
-        for (EditField field : kind.fieldList) {
+
+        // If there is one field, put it directly into the FrameLayout mFields. If there are
+        // several or 0, put them into a LinearLayout
+        final ViewGroup container;
+        if (kind.fieldList.size() == 1) {
+            container = mFields;
+        } else {
+            final LinearLayout linearLayout = new LinearLayout(mContext);
+            linearLayout.setOrientation(LinearLayout.VERTICAL);
+            linearLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
+                    LayoutParams.WRAP_CONTENT));
+            mFields.addView(linearLayout);
+            container = linearLayout;
+        }
+        mFieldEditTexts = new EditText[kind.fieldList.size()];
+        for (int index = 0; index < kind.fieldList.size(); index++) {
+            final EditField field = kind.fieldList.get(index);
             // Inflate field from definition
-            EditText fieldView = (EditText)mInflater.inflate(RES_FIELD, mFields, false);
-            fieldView.setId(vig.getId(state, kind, entry, n++));
+            EditText fieldView = (EditText)mInflater.inflate(RES_FIELD, container, false);
+            mFieldEditTexts[index] = fieldView;
+            fieldView.setId(vig.getId(state, kind, entry, index));
             if (field.titleRes > 0) {
                 fieldView.setHint(field.titleRes);
             }
@@ -267,7 +289,7 @@
             fieldView.setEnabled(enabled);
             hidePossible = hidePossible || couldHide;
 
-            mFields.addView(fieldView);
+            container.addView(fieldView);
         }
 
         // When hiding fields, place expandable
diff --git a/src/com/android/contacts/views/detail/ContactDetailFragment.java b/src/com/android/contacts/views/detail/ContactDetailFragment.java
index a35285f..cb6eba6 100644
--- a/src/com/android/contacts/views/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/views/detail/ContactDetailFragment.java
@@ -796,10 +796,12 @@
         }
     }
 
+    @Override
     public void onCreateOptionsMenu(Menu menu, final MenuInflater inflater) {
         inflater.inflate(R.menu.view, menu);
     }
 
+    @Override
     public void onPrepareOptionsMenu(Menu menu) {
         // Only allow edit when we have at least one raw_contact id
         final boolean hasRawContact = (mRawContactIds.size() > 0);
diff --git a/src/com/android/contacts/views/editor/ContactEditorFragment.java b/src/com/android/contacts/views/editor/ContactEditorFragment.java
index 5b281fc..d6baa9c 100644
--- a/src/com/android/contacts/views/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/views/editor/ContactEditorFragment.java
@@ -77,7 +77,6 @@
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
-import android.view.View.OnClickListener;
 import android.widget.ArrayAdapter;
 import android.widget.LinearLayout;
 import android.widget.ListAdapter;
@@ -164,16 +163,7 @@
 
         mContent = (LinearLayout) view.findViewById(R.id.editors);
 
-        view.findViewById(R.id.btn_done).setOnClickListener(new OnClickListener() {
-            public void onClick(View v) {
-                doSaveAction(SAVE_MODE_DEFAULT);
-            }
-        });
-        view.findViewById(R.id.btn_discard).setOnClickListener(new OnClickListener() {
-            public void onClick(View v) {
-                doRevertAction();
-            }
-        });
+        setHasOptionsMenu(true);
 
         return view;
     }
diff --git a/tests/src/com/android/contacts/interactions/ContactDeletionInteractionTest.java b/tests/src/com/android/contacts/interactions/ContactDeletionInteractionTest.java
new file mode 100644
index 0000000..b2ca81d
--- /dev/null
+++ b/tests/src/com/android/contacts/interactions/ContactDeletionInteractionTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+package com.android.contacts.interactions;
+
+import com.android.contacts.R;
+import com.android.contacts.model.Sources;
+import com.android.contacts.tests.mocks.ContactsMockContext;
+import com.android.contacts.tests.mocks.MockContentProvider;
+import com.android.contacts.tests.mocks.MockContentProvider.Query;
+import com.android.contacts.tests.mocks.MockSources;
+
+import android.content.AsyncTaskLoader;
+import android.content.ContentUris;
+import android.content.Loader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.Contacts;
+import android.provider.ContactsContract.RawContacts;
+import android.test.InstrumentationTestCase;
+import android.test.suitebuilder.annotation.Smoke;
+
+/**
+ * Tests for {@link ContactDeletionInteraction}.
+ *
+ * Running all tests:
+ *
+ *   runtest contacts
+ * or
+ *   adb shell am instrument \
+ *     -w com.android.contacts.tests/android.test.InstrumentationTestRunner
+ */
+@Smoke
+public class ContactDeletionInteractionTest extends InstrumentationTestCase {
+
+    private final static class TestContactDeletionInteraction extends ContactDeletionInteraction {
+        public Uri contactUri;
+        public int messageId;
+
+        @Override
+        void startLoading(Loader<Cursor> loader) {
+            // Execute the loader synchronously
+            AsyncTaskLoader<Cursor> atLoader = (AsyncTaskLoader<Cursor>)loader;
+            Cursor data = atLoader.loadInBackground();
+            atLoader.deliverResult(data);
+        }
+
+        @Override
+        void showDialog(Bundle bundle) {
+            contactUri = bundle.getParcelable(EXTRA_KEY_CONTACT_URI);
+            messageId = bundle.getInt(EXTRA_KEY_MESSAGE_ID);
+        }
+
+        @Override
+        Sources getSources() {
+            return new MockSources();
+        }
+    }
+
+    private ContactsMockContext mContext;
+    private MockContentProvider mContactsProvider;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
+        mContactsProvider = mContext.getContactsProvider();
+    }
+
+    public void testSingleWritableRawContact() {
+        expectQuery().returnRow(1, MockSources.WRITABLE_ACCOUNT_TYPE);
+        assertWithMessageId(R.string.deleteConfirmation);
+    }
+
+    public void testReadOnlyRawContacts() {
+        expectQuery().returnRow(1, MockSources.READONLY_ACCOUNT_TYPE);
+        assertWithMessageId(R.string.readOnlyContactWarning);
+    }
+
+    public void testMixOfWritableAndReadOnlyRawContacts() {
+        expectQuery()
+                .returnRow(1, MockSources.WRITABLE_ACCOUNT_TYPE)
+                .returnRow(2, MockSources.READONLY_ACCOUNT_TYPE);
+        assertWithMessageId(R.string.readOnlyContactDeleteConfirmation);
+    }
+
+    public void testMultipleWritableRawContacts() {
+        expectQuery()
+                .returnRow(1, MockSources.WRITABLE_ACCOUNT_TYPE)
+                .returnRow(2, MockSources.WRITABLE_ACCOUNT_TYPE);
+        assertWithMessageId(R.string.multipleContactDeleteConfirmation);
+    }
+
+    private Query expectQuery() {
+        return mContactsProvider.expectQuery(RawContacts.CONTENT_URI)
+                .withProjection(RawContacts._ID, RawContacts.ACCOUNT_TYPE)
+                .withSelection("contact_id=?", "13");
+    }
+
+    private void assertWithMessageId(int messageId) {
+        TestContactDeletionInteraction interaction = new TestContactDeletionInteraction();
+        interaction.setContext(mContext);
+        interaction.deleteContact(ContentUris.withAppendedId(Contacts.CONTENT_URI, 13));
+        assertEquals("content://com.android.contacts/contacts/13",
+                interaction.contactUri.toString());
+        assertEquals(messageId, interaction.messageId);
+        mContactsProvider.verify();
+    }
+}
diff --git a/tests/src/com/android/contacts/tests/mocks/MockSources.java b/tests/src/com/android/contacts/tests/mocks/MockSources.java
new file mode 100644
index 0000000..201928f
--- /dev/null
+++ b/tests/src/com/android/contacts/tests/mocks/MockSources.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+package com.android.contacts.tests.mocks;
+
+import com.android.contacts.model.ContactsSource;
+import com.android.contacts.model.FallbackSource;
+import com.android.contacts.model.Sources;
+
+/**
+ * A mock {@link Sources} class.
+ */
+public class MockSources extends Sources {
+
+    public static final String WRITABLE_ACCOUNT_TYPE = "writable";
+    public static final String READONLY_ACCOUNT_TYPE = "readonly";
+
+    @Override
+    public ContactsSource getInflatedSource(String accountType, int inflateLevel) {
+        if (accountType.equals(WRITABLE_ACCOUNT_TYPE)) {
+            ContactsSource source = new FallbackSource();
+            source.readOnly = false;
+            return source;
+        }
+
+        if (accountType.equals(READONLY_ACCOUNT_TYPE)) {
+            ContactsSource source = new FallbackSource();
+            source.readOnly = true;
+            return source;
+        }
+
+        return null;
+    }
+}