Adding a confirmation dialog for canceling out of contact editor

Bug: 3144593
Change-Id: Icd6c9f96d9d2fd4ad84ba38711d0af874258dba5
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3910958..15e2ddf 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1461,4 +1461,13 @@
 
     <!-- Toast shown when text is copied to the clipboard [CHAR LIMIT=64] -->
     <string name="toast_text_copied">Text copied</string>
+    
+    <!-- Title of the alert dialog when the user hits the Cancel button in the editor [CHAR LIMIT=64] -->
+    <string name="cancel_confirmation_dialog_title">Discard changes</string>
+
+    <!-- Contents of the alert dialog when the user hits the Cancel button in the editor [CHAR LIMIT=128] -->
+    <string name="cancel_confirmation_dialog_message">Do you want to discard your changes?</string>
+
+    <!-- Button in the alert dialog when the user hits the Cancel button in the editor [CHAR LIMIT=20] -->
+    <string name="discard">Discard</string>
 </resources>
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 8f5fb0e..96ba499 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -40,6 +40,7 @@
 import android.app.Dialog;
 import android.app.DialogFragment;
 import android.app.Fragment;
+import android.app.FragmentManager;
 import android.app.LoaderManager;
 import android.app.LoaderManager.LoaderCallbacks;
 import android.content.ActivityNotFoundException;
@@ -584,7 +585,7 @@
             case R.id.menu_done:
                 return save(SaveMode.CLOSE);
             case R.id.menu_discard:
-                return doRevertAction();
+                return revert();
             case R.id.menu_delete:
                 return doDeleteAction();
             case R.id.menu_split:
@@ -750,12 +751,48 @@
         return true;
     }
 
-    private boolean doRevertAction() {
+    public static class CancelEditDialogFragment extends DialogFragment {
+
+        public static void show(ContactEditorFragment fragment) {
+            CancelEditDialogFragment dialog = new CancelEditDialogFragment();
+            dialog.setTargetFragment(fragment, 0);
+            dialog.show(fragment.getFragmentManager(), "cancelEditor");
+        }
+
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            AlertDialog dialog = new AlertDialog.Builder(getActivity())
+                    .setIcon(android.R.drawable.ic_dialog_alert)
+                    .setTitle(R.string.cancel_confirmation_dialog_title)
+                    .setMessage(R.string.cancel_confirmation_dialog_message)
+                    .setPositiveButton(R.string.discard,
+                        new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int whichButton) {
+                                ((ContactEditorFragment)getTargetFragment()).doRevertAction();
+                            }
+                        }
+                    )
+                    .setNegativeButton(android.R.string.cancel, null)
+                    .create();
+            return dialog;
+        }
+    }
+
+    private boolean revert() {
+        final AccountTypes sources = AccountTypes.getInstance(mContext);
+        if (mState.buildDiff().isEmpty()) {
+            doRevertAction();
+        } else {
+            CancelEditDialogFragment.show(this);
+        }
+        return true;
+    }
+
+    private void doRevertAction() {
         // When this Fragment is closed we don't want it to auto-save
         mStatus = Status.CLOSING;
         if (mListener != null) mListener.onReverted();
-
-        return true;
     }
 
     public void onJoinCompleted(Uri uri) {