Fixes bug where delete option from Menu list is disabled in Contact view mode.
Bug was caused by the count increment logic being removed in the next submitted change
https://android-git.corp.google.com/w/?p=platform/packages/apps/Contacts.git;a=commit;h=df18120ed2c37a45eb47e39c4da0c8e3b6eb335c
Disabled menu was removed in some other subsequent change, so adding dialog for the case when writable contacts = 0.

Changes delete contact bebavior in EditContactActivity to be same
ViewContactActivity.

Updates dialog message for readonly contact deletion.

bug 2161222
Dr No : TS
Reviewed By: dplotnikov, timsullivan
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 72ab82f..5e287ad 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -43,7 +43,7 @@
 
     <!-- For ContactsListActivity -->
     <item type="id" name="dialog_delete_contact_confirmation" />
-    <item type="id" name="dialog_cannot_delete_readonly_contact" />
+    <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" />
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7dc1778..dbe8d09 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -174,10 +174,10 @@
     <string name="deleteConfirmation_title">Delete</string>
 
     <!-- Warning dialog contents after users selects to delete a ReadOnly contact. -->
-    <string name="readOnlyContactWarning">You cannot delete contacts from read-only accounts.</string>
+    <string name="readOnlyContactWarning">You cannot delete contacts from read-only accounts, but you can hide them in your contacts lists.</string>
 
     <!-- Warning dialog contents after users selects to delete a contact with ReadOnly and Writable sources. -->
-    <string name="readOnlyContactDeleteConfirmation">This contact contains information from multiple accounts. The information from read-only accounts will not be deleted.</string>
+    <string name="readOnlyContactDeleteConfirmation">This contact contains information from multiple accounts. Information from read-only accounts will be hidden in your contacts lists, not deleted.</string>
 
     <!-- Warning dialog contents after users selects to delete a contact with multiple Writable sources. -->
     <string name="multipleContactDeleteConfirmation">Deleting this contact will delete information from multiple accounts.</string>
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 3250c41..4125d7a 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -402,15 +402,7 @@
         }
 
         public void onClick(DialogInterface dialog, int which) {
-	    if (mReadOnlySourcesCnt > 0) {
-	        for (long rawContactIdToDelete: mWritableRawContactIds) {
-		    final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
-                            rawContactIdToDelete);
-                    getContentResolver().delete(rawContactUri, null, null);
-		}
-            } else {
-	        getContentResolver().delete(mUri, null, null);
-            }
+            getContentResolver().delete(mUri, null, null);
         }
     }
 
@@ -890,18 +882,21 @@
                         .setPositiveButton(android.R.string.ok,
                                 new DeleteClickListener(mSelectedContactUri)).create();
             }
-            case R.id.dialog_cannot_delete_readonly_contact: {
+            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)
-                        .setPositiveButton(android.R.string.ok, null).create();
+                        .setNegativeButton(android.R.string.cancel, null)
+                        .setPositiveButton(android.R.string.ok,
+                                new DeleteClickListener(mSelectedContactUri)).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(mSelectedContactUri)).create();
             }
@@ -910,6 +905,7 @@
                         .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(mSelectedContactUri)).create();
             }
@@ -1178,7 +1174,7 @@
 	    if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt > 0) {
 	        showDialog(R.id.dialog_readonly_contact_delete_confirmation);
 	    } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
-	        showDialog(R.id.dialog_cannot_delete_readonly_contact);
+	        showDialog(R.id.dialog_readonly_contact_hide_confirmation);
 	    } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
 	        showDialog(R.id.dialog_multiple_contact_delete_confirmation);
             } else {
diff --git a/src/com/android/contacts/ViewContactActivity.java b/src/com/android/contacts/ViewContactActivity.java
index ab8e931..d6ab83c 100644
--- a/src/com/android/contacts/ViewContactActivity.java
+++ b/src/com/android/contacts/ViewContactActivity.java
@@ -91,6 +91,7 @@
     private static final int DIALOG_CONFIRM_DELETE = 1;
     private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
     private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
+    private static final int DIALOG_CONFIRM_READONLY_HIDE = 4;
 
     private static final int REQUEST_JOIN_CONTACT = 1;
     private static final int REQUEST_EDIT_CONTACT = 2;
@@ -146,15 +147,7 @@
 
     public void onClick(DialogInterface dialog, int which) {
         closeCursor();
-	if (mReadOnlySourcesCnt > 0) {
-	    for (long rawContactIdToDelete: mWritableRawContactIds) {
-		final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
-                        rawContactIdToDelete);
-                getContentResolver().delete(rawContactUri, null, null);
-            }
-        } else {
-	    getContentResolver().delete(mLookupUri, null, null);
-	}
+        getContentResolver().delete(mLookupUri, null, null);
         finish();
     }
 
@@ -264,6 +257,15 @@
                         .setPositiveButton(android.R.string.ok, this)
                         .setCancelable(false)
                         .create();
+	    case DIALOG_CONFIRM_READONLY_HIDE: {
+                return new AlertDialog.Builder(this)
+                        .setTitle(R.string.deleteConfirmation_title)
+                        .setIcon(android.R.drawable.ic_dialog_alert)
+                        .setMessage(R.string.readOnlyContactWarning)
+                        .setPositiveButton(android.R.string.ok, this)
+			.create();
+            }
+
         }
         return null;
     }
@@ -384,10 +386,6 @@
         final boolean hasRawContact = (mRawContactIds.size() > 0);
         menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
 
-        // Disable delete for readonly contact
-	if (mWritableSourcesCnt == 0) {
-            menu.findItem(R.id.menu_delete).setEnabled(false);
-        }
         return true;
     }
 
@@ -446,7 +444,9 @@
                 // Get confirmation
 		if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
                     showDialog(DIALOG_CONFIRM_READONLY_DELETE);
-		} else if (mWritableSourcesCnt > 1) {
+		} else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
+                    showDialog(DIALOG_CONFIRM_READONLY_HIDE);
+		} else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
 		    showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
 		} else {
 		    showDialog(DIALOG_CONFIRM_DELETE);
@@ -649,7 +649,9 @@
             case KeyEvent.KEYCODE_DEL: {
 		if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
                     showDialog(DIALOG_CONFIRM_READONLY_DELETE);
-		} else if (mWritableSourcesCnt > 1) {
+		} else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
+                    showDialog(DIALOG_CONFIRM_READONLY_HIDE);
+		} else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
 		    showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
 		} else {
 		    showDialog(DIALOG_CONFIRM_DELETE);
@@ -725,6 +727,15 @@
                 if (!mRawContactIds.contains(rawContactId)) {
                     mRawContactIds.add(rawContactId);
                 }
+                ContactsSource contactsSource = sources.getInflatedSource(accountType,
+                        ContactsSource.LEVEL_SUMMARY);
+                if (contactsSource != null && contactsSource.readOnly) {
+                    mReadOnlySourcesCnt += 1;
+                } else {
+                    mWritableSourcesCnt += 1;
+		    mWritableRawContactIds.add(rawContactId);
+		}
+
 
                 for (NamedContentValues subValue : entity.getSubValues()) {
                     ViewEntry entry = new ViewEntry();
diff --git a/src/com/android/contacts/ui/EditContactActivity.java b/src/com/android/contacts/ui/EditContactActivity.java
index 3377bd6..fef6a9f 100644
--- a/src/com/android/contacts/ui/EditContactActivity.java
+++ b/src/com/android/contacts/ui/EditContactActivity.java
@@ -105,6 +105,10 @@
 
     private long mRawContactIdRequestingPhoto = -1;
 
+    private static final int DIALOG_CONFIRM_DELETE = 1;
+    private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
+    private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
+    private static final int DIALOG_CONFIRM_READONLY_HIDE = 4;
 
     String mQuerySelection;
 
@@ -240,6 +244,48 @@
         }
     }
 
+    @Override
+    protected Dialog onCreateDialog(int id) {
+        switch (id) {
+            case DIALOG_CONFIRM_DELETE:
+                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())
+                        .setCancelable(false)
+                        .create();
+            case DIALOG_CONFIRM_READONLY_DELETE:
+                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())
+                        .setCancelable(false)
+                        .create();
+            case DIALOG_CONFIRM_MULTIPLE_DELETE:
+                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())
+                        .setCancelable(false)
+                        .create();
+            case DIALOG_CONFIRM_READONLY_HIDE:
+                return new AlertDialog.Builder(this)
+                        .setTitle(R.string.deleteConfirmation_title)
+                        .setIcon(android.R.drawable.ic_dialog_alert)
+                        .setMessage(R.string.readOnlyContactWarning)
+                        .setPositiveButton(android.R.string.ok, new DeleteClickListener())
+                        .setCancelable(false)
+                        .create();
+        }
+        return null;
+    }
+
     /**
      * Start managing this {@link Dialog} along with the {@link Activity}.
      */
@@ -566,6 +612,20 @@
         return true;
     }
 
+    private class DeleteClickListener implements DialogInterface.OnClickListener {
+
+        public void onClick(DialogInterface dialog, int which) {
+            Sources sources = Sources.getInstance(EditContactActivity.this);
+            // Mark all raw contacts for deletion
+            for (EntityDelta delta : mState) {
+                delta.markDeleted();
+            }
+            // Save the deletes
+            doSaveAction(SAVE_MODE_DEFAULT);
+            finish();
+        }
+    }
+
     private void onSaveCompleted(boolean success, int saveMode, Uri contactLookupUri) {
         switch (saveMode) {
             case SAVE_MODE_DEFAULT:
@@ -702,9 +762,30 @@
      */
     private boolean doDeleteAction() {
         if (!hasValidState()) return false;
+        int readOnlySourcesCnt = 0;
+	int writableSourcesCnt = 0;
+        Sources sources = Sources.getInstance(EditContactActivity.this);
+        for (EntityDelta delta : mState) {
+	    final String accountType = delta.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
+            final ContactsSource contactsSource = sources.getInflatedSource(accountType,
+                    ContactsSource.LEVEL_CONSTRAINTS);
+            if (contactsSource != null && contactsSource.readOnly) {
+                readOnlySourcesCnt += 1;
+            } else {
+                writableSourcesCnt += 1;
+            }
+	}
 
-        showAndManageDialog(createDeleteDialog());
-        return true;
+        if (readOnlySourcesCnt > 0 && writableSourcesCnt > 0) {
+	    showDialog(DIALOG_CONFIRM_READONLY_DELETE);
+	} else if (readOnlySourcesCnt > 0 && writableSourcesCnt == 0) {
+	    showDialog(DIALOG_CONFIRM_READONLY_HIDE);
+	} else if (readOnlySourcesCnt == 0 && writableSourcesCnt > 1) {
+	    showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
+        } else {
+	    showDialog(DIALOG_CONFIRM_DELETE);
+	}
+	return true;
     }
 
     /**