Introducing a fragment for contact picker

Change-Id: I8dbf956c97b945d31ccb1b3e81a2c36e81bd10e2
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index b0d77de..378342d 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -16,15 +16,17 @@
 
 package com.android.contacts;
 
+import com.android.contacts.list.ContactBrowseListContextMenuAdapter;
+import com.android.contacts.list.ContactBrowseListFragment;
 import com.android.contacts.list.ContactEntryListAdapter;
 import com.android.contacts.list.ContactEntryListFragment;
 import com.android.contacts.list.ContactItemListAdapter;
+import com.android.contacts.list.ContactPickerFragment;
 import com.android.contacts.list.ContactsIntentResolver;
 import com.android.contacts.list.DefaultContactListFragment;
-import com.android.contacts.list.LightContactBrowserContextMenuAdapter;
-import com.android.contacts.list.LightContactBrowser;
 import com.android.contacts.list.MultiplePhonePickerFragment;
 import com.android.contacts.list.OnContactBrowserActionListener;
+import com.android.contacts.list.OnContactPickerActionListener;
 import com.android.contacts.model.ContactsSource;
 import com.android.contacts.model.Sources;
 import com.android.contacts.ui.ContactsPreferences;
@@ -536,8 +538,10 @@
         switch (mMode) {
             case MODE_DEFAULT:
             case MODE_INSERT_OR_EDIT_CONTACT:
-            case MODE_QUERY_PICK_TO_EDIT: {
-                LightContactBrowser fragment = new LightContactBrowser();
+            case MODE_QUERY_PICK_TO_EDIT:
+            case MODE_STREQUENT:
+            case MODE_FREQUENT: {
+                ContactBrowseListFragment fragment = new ContactBrowseListFragment();
                 if (!mSearchMode) {
                     fragment.setSectionHeaderDisplayEnabled(true);
                 }
@@ -551,7 +555,7 @@
                     fragment.setCreateContactEnabled(true);
                 }
 
-                fragment.setOnContactBrowserActionListener(new OnContactBrowserActionListener() {
+                fragment.setOnContactListActionListener(new OnContactBrowserActionListener() {
                     public void onSearchAllContactsAction(String string) {
                         doSearch();
                     }
@@ -602,17 +606,54 @@
                         doContactDelete(contactUri);
                     }
                 });
-                mContextMenuAdapter = new LightContactBrowserContextMenuAdapter(fragment);
+                mContextMenuAdapter = new ContactBrowseListContextMenuAdapter(fragment);
                 mListFragment = fragment;
                 break;
             }
-            case MODE_LEGACY_PICK_POSTAL:
-            case MODE_PICK_POSTAL:
+            case MODE_PICK_CONTACT:
+            case MODE_PICK_OR_CREATE_CONTACT: {
+                ContactPickerFragment fragment = new ContactPickerFragment();
+                if (!mSearchMode) {
+                    fragment.setSectionHeaderDisplayEnabled(true);
+                }
+
+                if (mMode == MODE_INSERT_OR_EDIT_CONTACT) {
+                    fragment.setCreateContactEnabled(true);
+                }
+
+                fragment.setOnContactPickerActionListener(new OnContactPickerActionListener() {
+                    public void onSearchAllContactsAction(String string) {
+                        doSearch();
+                    }
+
+                    public void onCreateNewContactAction() {
+                        Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
+                        startActivityAndForwardResult(intent);
+                    }
+
+                    public void onPickContactAction(Uri contactUri) {
+                        setResult(RESULT_OK, intent.setData(contactUri));
+                        finish();
+                    }
+                });
+
+                mListFragment = fragment;
+                break;
+            }
             case MODE_LEGACY_PICK_PHONE:
-            case MODE_PICK_PHONE:
-            case MODE_STREQUENT:
-            case MODE_FREQUENT: {
+            case MODE_PICK_PHONE: {
                 mListFragment = new DefaultContactListFragment();
+                if (mMode == MODE_LEGACY_PICK_PHONE) {
+                    mListFragment.setLegacyCompatibility(true);
+                }
+                break;
+            }
+            case MODE_LEGACY_PICK_POSTAL:
+            case MODE_PICK_POSTAL: {
+                mListFragment = new DefaultContactListFragment();
+                if (mMode == MODE_LEGACY_PICK_POSTAL) {
+                    mListFragment.setLegacyCompatibility(true);
+                }
                 break;
             }
             case MODE_PICK_MULTIPLE_PHONES: {
diff --git a/src/com/android/contacts/list/LightContactBrowserContextMenuAdapter.java b/src/com/android/contacts/list/ContactBrowseListContextMenuAdapter.java
similarity index 79%
rename from src/com/android/contacts/list/LightContactBrowserContextMenuAdapter.java
rename to src/com/android/contacts/list/ContactBrowseListContextMenuAdapter.java
index 7e7a336..6ff3110 100644
--- a/src/com/android/contacts/list/LightContactBrowserContextMenuAdapter.java
+++ b/src/com/android/contacts/list/ContactBrowseListContextMenuAdapter.java
@@ -27,9 +27,9 @@
 import android.widget.AdapterView;
 
 /**
- * A contextual menu adapter for the light version of the contact browser.
+ * A contextual menu adapter for the basic contact list.
  */
-public class LightContactBrowserContextMenuAdapter implements ContextMenuAdapter {
+public class ContactBrowseListContextMenuAdapter implements ContextMenuAdapter {
 
     private static final int MENU_ITEM_VIEW_CONTACT = 1;
     private static final int MENU_ITEM_CALL = 2;
@@ -40,10 +40,10 @@
 
     private static final String TAG = "LightContactBrowserContextMenuAdapter";
 
-    private final LightContactBrowser mBrowser;
+    private final ContactBrowseListFragment mContactListFragment;
 
-    public LightContactBrowserContextMenuAdapter(LightContactBrowser browser) {
-        this.mBrowser = browser;
+    public ContactBrowseListContextMenuAdapter(ContactBrowseListFragment fragment) {
+        this.mContactListFragment = fragment;
     }
 
     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
@@ -55,7 +55,7 @@
             return;
         }
 
-        ContactEntryListAdapter adapter = mBrowser.getAdapter();
+        ContactEntryListAdapter adapter = mContactListFragment.getAdapter();
         adapter.moveToPosition(info.position);
 
         // Setup the menu header
@@ -92,42 +92,42 @@
             return false;
         }
 
-        ContactEntryListAdapter adapter = mBrowser.getAdapter();
+        ContactEntryListAdapter adapter = mContactListFragment.getAdapter();
         adapter.moveToPosition(info.position);
         final Uri contactUri = adapter.getContactUri();
 
         switch (item.getItemId()) {
             case MENU_ITEM_VIEW_CONTACT: {
-                mBrowser.viewContact(contactUri);
+                mContactListFragment.viewContact(contactUri);
                 return true;
             }
 
             case MENU_ITEM_TOGGLE_STAR: {
                 if (adapter.isContactStarred()) {
-                    mBrowser.removeFromFavorites(contactUri);
+                    mContactListFragment.removeFromFavorites(contactUri);
                 } else {
-                    mBrowser.addToFavorites(contactUri);
+                    mContactListFragment.addToFavorites(contactUri);
                 }
                 return true;
             }
 
             case MENU_ITEM_CALL: {
-                mBrowser.callContact(contactUri);
+                mContactListFragment.callContact(contactUri);
                 return true;
             }
 
             case MENU_ITEM_SEND_SMS: {
-                mBrowser.smsContact(contactUri);
+                mContactListFragment.smsContact(contactUri);
                 return true;
             }
 
             case MENU_ITEM_EDIT: {
-                mBrowser.editContact(contactUri);
+                mContactListFragment.editContact(contactUri);
                 return true;
             }
 
             case MENU_ITEM_DELETE: {
-                mBrowser.deleteContact(contactUri);
+                mContactListFragment.deleteContact(contactUri);
                 return true;
             }
         }
diff --git a/src/com/android/contacts/list/LightContactBrowser.java b/src/com/android/contacts/list/ContactBrowseListFragment.java
similarity index 92%
rename from src/com/android/contacts/list/LightContactBrowser.java
rename to src/com/android/contacts/list/ContactBrowseListFragment.java
index 2c17d32..5203df5 100644
--- a/src/com/android/contacts/list/LightContactBrowser.java
+++ b/src/com/android/contacts/list/ContactBrowseListFragment.java
@@ -25,15 +25,16 @@
 import android.view.ViewGroup;
 
 /**
- * Fragment for the light-weight contact list.
+ * Fragment containing a contact list used for browsing (as compared to
+ * picking a contact with one of the PICK intents).
  */
-public class LightContactBrowser extends ContactEntryListFragment {
+public class ContactBrowseListFragment extends ContactEntryListFragment {
 
     private OnContactBrowserActionListener mListener;
     private boolean mEditMode;
     private boolean mCreateContactEnabled;
 
-    public void setOnContactBrowserActionListener(OnContactBrowserActionListener listener) {
+    public void setOnContactListActionListener(OnContactBrowserActionListener listener) {
         mListener = listener;
     }
 
@@ -46,7 +47,6 @@
         if (isSearchAllContactsItemPosition(position)) {
             mListener.onSearchAllContactsAction((String)null);
         } else if (isEditMode()) {
-            Intent intent;
             if (position == 0 && !isSearchMode() && isCreateContactEnabled()) {
                 mListener.onCreateNewContactAction();
             } else {
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index fd38599..6e28322 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -29,7 +29,6 @@
 import android.view.ViewGroup;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.AdapterView;
-import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
 
diff --git a/src/com/android/contacts/list/LightContactBrowser.java b/src/com/android/contacts/list/ContactPickerFragment.java
similarity index 64%
copy from src/com/android/contacts/list/LightContactBrowser.java
copy to src/com/android/contacts/list/ContactPickerFragment.java
index 2c17d32..b1768a2 100644
--- a/src/com/android/contacts/list/LightContactBrowser.java
+++ b/src/com/android/contacts/list/ContactPickerFragment.java
@@ -25,15 +25,15 @@
 import android.view.ViewGroup;
 
 /**
- * Fragment for the light-weight contact list.
+ * Fragment for the contact list used for browsing contacts (as compared to
+ * picking a contact with one of the PICK or SHORTCUT intents).
  */
-public class LightContactBrowser extends ContactEntryListFragment {
+public class ContactPickerFragment extends ContactEntryListFragment {
 
-    private OnContactBrowserActionListener mListener;
-    private boolean mEditMode;
+    private OnContactPickerActionListener mListener;
     private boolean mCreateContactEnabled;
 
-    public void setOnContactBrowserActionListener(OnContactBrowserActionListener listener) {
+    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
         mListener = listener;
     }
 
@@ -45,19 +45,15 @@
     protected void onItemClick(int position, long id) {
         if (isSearchAllContactsItemPosition(position)) {
             mListener.onSearchAllContactsAction((String)null);
-        } else if (isEditMode()) {
+        } else {
             Intent intent;
             if (position == 0 && !isSearchMode() && isCreateContactEnabled()) {
                 mListener.onCreateNewContactAction();
             } else {
                 ContactEntryListAdapter adapter = getAdapter();
                 adapter.moveToPosition(position);
-                mListener.onEditContactAction(adapter.getContactUri());
+                mListener.onPickContactAction(adapter.getContactUri());
             }
-        } else {
-            ContactEntryListAdapter adapter = getAdapter();
-            adapter.moveToPosition(position);
-            mListener.onViewContactAction(adapter.getContactUri());
         }
     }
 
@@ -81,14 +77,6 @@
         }
     }
 
-    public void setEditMode(boolean flag) {
-        mEditMode = flag;
-    }
-
-    public boolean isEditMode() {
-        return mEditMode;
-    }
-
     public void setCreateContactEnabled(boolean flag) {
         this.mCreateContactEnabled = flag;
     }
@@ -96,32 +84,4 @@
     public boolean isCreateContactEnabled() {
         return mCreateContactEnabled;
     }
-
-    public void viewContact(Uri contactUri) {
-        mListener.onViewContactAction(contactUri);
-    }
-
-    public void editContact(Uri contactUri) {
-        mListener.onEditContactAction(contactUri);
-    }
-
-    public void deleteContact(Uri contactUri) {
-        mListener.onDeleteContactAction(contactUri);
-    }
-
-    public void addToFavorites(Uri contactUri) {
-        mListener.onAddToFavoritesAction(contactUri);
-    }
-
-    public void removeFromFavorites(Uri contactUri) {
-        mListener.onRemoveFromFavoritesAction(contactUri);
-    }
-
-    public void callContact(Uri contactUri) {
-        mListener.onCallContactAction(contactUri);
-    }
-
-    public void smsContact(Uri contactUri) {
-        mListener.onSmsContactAction(contactUri);
-    }
 }
diff --git a/src/com/android/contacts/list/OnContactBrowserActionListener.java b/src/com/android/contacts/list/OnContactBrowserActionListener.java
index c6fe102..b038108 100644
--- a/src/com/android/contacts/list/OnContactBrowserActionListener.java
+++ b/src/com/android/contacts/list/OnContactBrowserActionListener.java
@@ -18,7 +18,7 @@
 import android.net.Uri;
 
 /**
- * Action callbacks that can be sent by a contact browser.
+ * Action callbacks that can be sent by a contact list.
  */
 public interface OnContactBrowserActionListener  {
 
diff --git a/src/com/android/contacts/list/OnContactPickerActionListener.java b/src/com/android/contacts/list/OnContactPickerActionListener.java
new file mode 100644
index 0000000..38adf92
--- /dev/null
+++ b/src/com/android/contacts/list/OnContactPickerActionListener.java
@@ -0,0 +1,39 @@
+/*
+ * 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.list;
+
+import android.net.Uri;
+
+/**
+ * Action callbacks that can be sent by a contact picker.
+ */
+public interface OnContactPickerActionListener  {
+
+    /**
+     * Searches all contacts for the specified string an show results for browsing.
+     */
+    void onSearchAllContactsAction(String string);
+
+    /**
+     * Creates a new contact and then returns it to the caller.
+     */
+    void onCreateNewContactAction();
+
+    /**
+     * Returns the selected contact to the requester.
+     */
+    void onPickContactAction(Uri contactUri);
+}