Make it possible for View and Edit to be in a Dialog

Change-Id: I55e0f3d9369e638651034131812a590984ff2282
diff --git a/res/layout-xlarge/contact_editor_activity.xml b/res/layout-xlarge/contact_editor_activity.xml
new file mode 100644
index 0000000..906152c
--- /dev/null
+++ b/res/layout-xlarge/contact_editor_activity.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="600dip"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <fragment android:name="com.android.contacts.views.editor.ContactEditorFragment"
+            android:id="@+id/contact_editor_fragment"
+            android:layout_width="match_parent"
+            android:layout_height="0px"
+            android:layout_weight="1" />
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="50dip"
+        android:orientation="horizontal"
+        android:gravity="center_horizontal">
+
+        <Button
+            android:id="@+id/done"
+            android:layout_width="150dip"
+            android:layout_height="match_parent"
+            android:text="@string/menu_done" />
+        <Button
+            android:id="@+id/revert"
+            android:layout_width="150dip"
+            android:layout_height="match_parent"
+            android:text="@string/menu_doNotSave" />
+    </LinearLayout>
+</LinearLayout>
diff --git a/res/layout/item_contact_editor.xml b/res/layout/item_contact_editor.xml
index 7566b37..b75c82a 100644
--- a/res/layout/item_contact_editor.xml
+++ b/res/layout/item_contact_editor.xml
@@ -14,7 +14,6 @@
      limitations under the License.
 -->
 
-<!-- placed inside act_edit -->
 <com.android.contacts.ui.widget.ContactEditorView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
diff --git a/res/values-xlarge/styles.xml b/res/values-xlarge/styles.xml
index 3f773f9..cc8682f 100644
--- a/res/values-xlarge/styles.xml
+++ b/res/values-xlarge/styles.xml
@@ -21,5 +21,7 @@
     </style>
     <style name="ContactsPreferencesTheme" parent="@android:Theme.Dialog">
     </style>
-
+    <style name="TallTitleBarTheme" parent="@android:Theme.Dialog">
+        <item name="android:windowContentOverlay">@null</item>
+    </style>
 </resources>
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 309119f..25ea72b 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -69,4 +69,7 @@
     <!-- For PhoneNumberMessageSendInteraction -->
     <item type="id" name="dialog_phone_number_message_disambiguation"/>
 
+    <!-- Dialog Manager Ids -->
+    <item type="id" name="dialog_manager_id_1"/>
+    <item type="id" name="dialog_manager_id_2"/>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index f4f962c..b7df91f 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -107,10 +107,10 @@
     
     <style name="ContactBrowserTheme" parent="@android:Theme">
     </style>
-    
+
     <style name="ContactPickerTheme" parent="@android:Theme">
     </style>
-    
+
     <style name="ContactsPreferencesTheme" parent="@android:Theme">
     </style>
 </resources>
diff --git a/src/com/android/contacts/activities/ContactBrowserActivity.java b/src/com/android/contacts/activities/ContactBrowserActivity.java
index d05f2b1..c94907e 100644
--- a/src/com/android/contacts/activities/ContactBrowserActivity.java
+++ b/src/com/android/contacts/activities/ContactBrowserActivity.java
@@ -72,11 +72,7 @@
     private static final int SUBACTIVITY_DISPLAY_GROUP = 3;
     private static final int SUBACTIVITY_SEARCH = 4;
 
-    private static final int DIALOG_MANAGER_ID_1 = 1;
-    private static final int DIALOG_MANAGER_ID_2 = 2;
-
-    private DialogManager mDialogManager = new DialogManager(this, DIALOG_MANAGER_ID_1,
-            DIALOG_MANAGER_ID_2);
+    private DialogManager mDialogManager = new DialogManager(this);
 
     private ContactsIntentResolver mIntentResolver;
     private ContactsRequest mRequest;
@@ -642,9 +638,7 @@
 
     @Override
     protected Dialog onCreateDialog(int id, Bundle bundle) {
-        if (id == DIALOG_MANAGER_ID_1 || id == DIALOG_MANAGER_ID_2) {
-            return mDialogManager.onCreateDialog(id, bundle);
-        }
+        if (DialogManager.isManagedId(id)) return mDialogManager.onCreateDialog(id, bundle);
 
         Dialog dialog = getContactDeletionInteraction().onCreateDialog(id, bundle);
         if (dialog != null) {
diff --git a/src/com/android/contacts/activities/ContactEditorActivity.java b/src/com/android/contacts/activities/ContactEditorActivity.java
index 4369160..e3780b4 100644
--- a/src/com/android/contacts/activities/ContactEditorActivity.java
+++ b/src/com/android/contacts/activities/ContactEditorActivity.java
@@ -18,6 +18,7 @@
 
 import com.android.contacts.ContactsSearchManager;
 import com.android.contacts.R;
+import com.android.contacts.util.DialogManager;
 import com.android.contacts.views.editor.ContactEditorFragment;
 
 import android.app.Activity;
@@ -25,12 +26,20 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
 import android.widget.Toast;
 
-public class ContactEditorActivity extends Activity {
+public class ContactEditorActivity extends Activity implements
+        DialogManager.DialogShowingViewActivity {
     private static final String TAG = "ContactEditorActivity";
 
     private ContactEditorFragment mFragment;
+    private Button mDoneButton;
+    private Button mRevertButton;
+
+    private DialogManager mDialogManager = new DialogManager(this);
 
     @Override
     public void onCreate(Bundle savedState) {
@@ -43,11 +52,29 @@
         mFragment.load(getIntent().getAction(), getIntent().getData(),
                 getIntent().resolveType(getContentResolver()), getIntent().getExtras());
 
+        // Depending on the use-case, this activity has Done and Revert buttons or not.
+        mDoneButton = (Button) findViewById(R.id.done);
+        mRevertButton = (Button) findViewById(R.id.revert);
+        if (mDoneButton != null) mDoneButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                mFragment.save();
+            }
+        });
+        if (mRevertButton != null) mRevertButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                finish();
+            }
+        });
+
         Log.i(TAG, getIntent().getData().toString());
     }
 
     @Override
     protected Dialog onCreateDialog(int id, Bundle args) {
+        if (DialogManager.isManagedId(id)) return mDialogManager.onCreateDialog(id, args);
+
         // ask the Fragment whether it knows about the dialog
         final Dialog fragmentResult = mFragment.onCreateDialog(id, args);
         if (fragmentResult != null) return fragmentResult;
@@ -77,31 +104,27 @@
 
         @Override
         public void closeAfterRevert() {
-            Toast.makeText(ContactEditorActivity.this, "closeAfterRevert",
-                    Toast.LENGTH_LONG).show();
+            finish();
         }
 
         @Override
         public void closeAfterSaving(int resultCode, Intent resultIntent) {
-            Toast.makeText(ContactEditorActivity.this, "closeAfterSaving",
-                    Toast.LENGTH_LONG).show();
+            finish();
         }
 
         @Override
         public void closeAfterSplit() {
-            Toast.makeText(ContactEditorActivity.this, "closeAfterSplit", Toast.LENGTH_LONG).show();
+            finish();
         }
 
         @Override
         public void closeBecauseAccountSelectorAborted() {
-            Toast.makeText(ContactEditorActivity.this, "closeBecauseAccountSelectorAborted",
-                    Toast.LENGTH_LONG).show();
+            finish();
         }
 
         @Override
         public void closeBecauseContactNotFound() {
-            Toast.makeText(ContactEditorActivity.this, "closeBecauseContactNotFound",
-                    Toast.LENGTH_LONG).show();
+            finish();
         }
 
         @Override
@@ -109,4 +132,9 @@
             Toast.makeText(ContactEditorActivity.this, "setTitleTo", Toast.LENGTH_LONG).show();
         }
     };
+
+    @Override
+    public DialogManager getDialogManager() {
+        return mDialogManager;
+    }
 }
diff --git a/src/com/android/contacts/util/DialogManager.java b/src/com/android/contacts/util/DialogManager.java
index 4c6baf3..42a35a7 100644
--- a/src/com/android/contacts/util/DialogManager.java
+++ b/src/com/android/contacts/util/DialogManager.java
@@ -16,6 +16,8 @@
 
 package com.android.contacts.util;
 
+import com.android.contacts.R;
+
 import android.app.Activity;
 import android.app.Dialog;
 import android.content.DialogInterface;
@@ -41,23 +43,20 @@
  */
 public class DialogManager {
     private final Activity mActivity;
-    private final int mDialogId1;
-    private final int mDialogId2;
     private boolean mUseDialogId2 = false;
     public final static String VIEW_ID_KEY = "view_id";
 
+    public static final boolean isManagedId(int id) {
+        return (id == R.id.dialog_manager_id_1) || (id == R.id.dialog_manager_id_2);
+    }
+
     /**
      * Creates a new instance of this class for the given Activity.
      * @param activity The activity this object is used for
-     * @param dialogId1 The first Id that is reserved for use by child-views
-     * @param dialogId2 The second Id that is reserved for use by child-views
      */
-    public DialogManager(final Activity activity, final int dialogId1, final int dialogId2) {
+    public DialogManager(final Activity activity) {
         if (activity == null) throw new IllegalArgumentException("activity must not be null");
-        if (dialogId1 == dialogId2) throw new IllegalArgumentException("Ids must be different");
         mActivity = activity;
-        mDialogId1 = dialogId1;
-        mDialogId2 = dialogId2;
     }
 
     /**
@@ -75,7 +74,7 @@
             throw new IllegalArgumentException("View does not have a proper ViewId");
         }
         bundle.putInt(VIEW_ID_KEY, viewId);
-        int dialogId = mUseDialogId2 ? mDialogId2 : mDialogId1;
+        int dialogId = mUseDialogId2 ? R.id.dialog_manager_id_2 : R.id.dialog_manager_id_1;
         mActivity.showDialog(dialogId, bundle);
     }
 
@@ -84,9 +83,9 @@
      * This function returns null if the id is not one of the two reserved Ids.
      */
     public Dialog onCreateDialog(final int id, final Bundle bundle) {
-        if (id == mDialogId1) {
+        if (id == R.id.dialog_manager_id_1) {
             mUseDialogId2 = true;
-        } else if (id == mDialogId2) {
+        } else if (id == R.id.dialog_manager_id_2) {
             mUseDialogId2 = false;
         } else {
             return null;
diff --git a/tests/res/values/donottranslate_strings.xml b/tests/res/values/donottranslate_strings.xml
index 93095e3..21a8e7a 100644
--- a/tests/res/values/donottranslate_strings.xml
+++ b/tests/res/values/donottranslate_strings.xml
@@ -72,6 +72,13 @@
         <item>EDIT (create new contact)</item>
         <item>EDIT (create new raw contact)</item>
         <item>EDIT (create new legacy)</item>
+
+        <!-- View Contact -->
+        <item>VIEW (content uri with only id)</item>
+        <item>VIEW (lookup uri without id)</item>
+        <item>VIEW (lookup uri)</item>
+        <item>VIEW (called for raw contact)</item>
+        <item>VIEW (legacy style uri)</item>
     </string-array>
 
     <string name="pinnedHeaderList">Pinned Headers</string>
diff --git a/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java b/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
index 7af7dfc..94f2aad 100644
--- a/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
+++ b/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
@@ -111,6 +111,12 @@
     private static final int EDIT_NEW_RAW_CONTACT = 48;
     private static final int EDIT_NEW_LEGACY = 49;
 
+    private static final int VIEW_CONTACT = 50;
+    private static final int VIEW_CONTACT_LOOKUP = 51;
+    private static final int VIEW_CONTACT_LOOKUP_ID = 52;
+    private static final int VIEW_RAW_CONTACT = 53;
+    private static final int VIEW_LEGACY = 54;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -423,6 +429,49 @@
                 startActivity(new Intent(Intent.ACTION_INSERT, legacyContentUri));
                 break;
             }
+            case VIEW_CONTACT: {
+                final long contactId = findArbitraryContactWithPhoneNumber();
+                final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
+                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
+                startActivity(intent);
+                break;
+            }
+            case VIEW_CONTACT_LOOKUP: {
+                final long contactId = findArbitraryContactWithPhoneNumber();
+                final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
+                final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), uri);
+                final String lookupKey = lookupUri.getPathSegments().get(2);
+                final Uri lookupWithoutIdUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,
+                        lookupKey);
+                final Intent intent = new Intent(Intent.ACTION_VIEW, lookupWithoutIdUri);
+                startActivity(intent);
+                break;
+            }
+            case VIEW_CONTACT_LOOKUP_ID: {
+                final long contactId = findArbitraryContactWithPhoneNumber();
+                final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
+                final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), uri);
+                final Intent intent = new Intent(Intent.ACTION_VIEW, lookupUri);
+                startActivity(intent);
+                break;
+            }
+            case VIEW_RAW_CONTACT: {
+                final long contactId = findArbitraryContactWithPhoneNumber();
+                final long rawContactId = findArbitraryRawContactOfContact(contactId);
+                final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
+                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
+                startActivity(intent);
+                break;
+            }
+            case VIEW_LEGACY: {
+                final Uri legacyContentUri = Uri.parse("content://contacts/people");
+                final long contactId = findArbitraryContactWithPhoneNumber();
+                final long rawContactId = findArbitraryRawContactOfContact(contactId);
+                final Uri uri = ContentUris.withAppendedId(legacyContentUri, rawContactId);
+                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
+                startActivity(intent);
+                break;
+            }
             default: {
                 Toast.makeText(this, "Sorry, we forgot to write this...", Toast.LENGTH_LONG).show();
             }