Revert "Revert "Handle group insert, view, and edit intents (1/2)""

This reverts commit 6bf9394371218adfc81123c236b81cdb6b855639.

Bug 29902305

Change-Id: I0e6f23a3ac4be8d7a6785aece0cfe0db37287308
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 2058e70..900b49b 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -138,6 +138,12 @@
                 <data android:mimeType="vnd.android.cursor.dir/contact" />
             </intent-filter>
 
+            <intent-filter>
+                <action android:name="android.intent.action.INSERT" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:mimeType="vnd.android.cursor.dir/group" />
+            </intent-filter>
+
             <meta-data android:name="android.app.searchable"
                 android:resource="@xml/searchable"
             />
@@ -173,6 +179,7 @@
                 <data android:mimeType="vnd.android.cursor.dir/postal-address_v2" />
                 <data android:mimeType="vnd.android.cursor.dir/postal-address" />
                 <data android:mimeType="vnd.android.cursor.dir/email_v2" />
+                <data android:mimeType="vnd.android.cursor.dir/group"/>
             </intent-filter>
 
             <intent-filter>
@@ -267,7 +274,20 @@
 
         <!-- Displays the members of a group in a list -->
         <activity android:name=".activities.GroupMembersActivity"
-            android:theme="@style/PeopleActivityTheme"/>
+            android:theme="@style/PeopleActivityTheme">
+
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:mimeType="vnd.android.cursor.item/group" />
+            </intent-filter>
+
+            <intent-filter>
+                <action android:name="android.intent.action.EDIT" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:mimeType="vnd.android.cursor.item/group" />
+            </intent-filter>
+        </activity>
 
         <activity
             android:name=".quickcontact.QuickContactActivity"
diff --git a/src/com/android/contacts/ContactsDrawerActivity.java b/src/com/android/contacts/ContactsDrawerActivity.java
index b62d5e6..a12d0e1 100644
--- a/src/com/android/contacts/ContactsDrawerActivity.java
+++ b/src/com/android/contacts/ContactsDrawerActivity.java
@@ -426,7 +426,7 @@
         }
     }
 
-    private void onCreateGroupMenuItemClicked() {
+    protected void onCreateGroupMenuItemClicked() {
         // Select the account to create the group
         final Bundle extras = getIntent().getExtras();
         final Account account = extras == null ? null :
diff --git a/src/com/android/contacts/activities/PeopleActivity.java b/src/com/android/contacts/activities/PeopleActivity.java
index 8429d2a..756aeb4 100644
--- a/src/com/android/contacts/activities/PeopleActivity.java
+++ b/src/com/android/contacts/activities/PeopleActivity.java
@@ -281,12 +281,18 @@
             return false;
         }
 
-        if (mRequest.getActionCode() == ContactsRequest.ACTION_VIEW_CONTACT) {
-            final Intent intent = ImplicitIntentsUtil.composeQuickContactIntent(
-                    mRequest.getContactUri(), QuickContactActivity.MODE_FULLY_EXPANDED);
-            intent.putExtra(QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE, ScreenType.UNKNOWN);
-            ImplicitIntentsUtil.startActivityInApp(this, intent);
-            return false;
+        switch (mRequest.getActionCode()) {
+            case ContactsRequest.ACTION_VIEW_CONTACT: {
+                final Intent intent = ImplicitIntentsUtil.composeQuickContactIntent(
+                        mRequest.getContactUri(), QuickContactActivity.MODE_FULLY_EXPANDED);
+                intent.putExtra(QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE, ScreenType.UNKNOWN);
+                ImplicitIntentsUtil.startActivityInApp(this, intent);
+                return false;
+            }
+            case ContactsRequest.ACTION_INSERT_GROUP: {
+                onCreateGroupMenuItemClicked();
+                return true;
+            }
         }
         return true;
     }
diff --git a/src/com/android/contacts/group/GroupUtil.java b/src/com/android/contacts/group/GroupUtil.java
index beba2ea..fd8c03d 100644
--- a/src/com/android/contacts/group/GroupUtil.java
+++ b/src/com/android/contacts/group/GroupUtil.java
@@ -49,9 +49,6 @@
 @NeededForTesting
 public final class GroupUtil {
 
-    private static final String LEGACY_CONTACTS_AUTHORITY = "contacts";
-    private static final String LEGACY_CONTACTS_URI = "content://contacts/groups";
-
     // System IDs of FFC groups in Google accounts
     private static final Set<String> FFC_GROUPS =
             new HashSet(Arrays.asList("Friends", "Family", "Coworkers"));
@@ -134,7 +131,7 @@
     public static Intent createPickMemberIntent(
             GroupMetadata groupMetadata, ArrayList<String> memberContactIds) {
         final Intent intent = new Intent(Intent.ACTION_PICK);
-        intent.setType(Groups.CONTENT_ITEM_TYPE);
+        intent.setType(Groups.CONTENT_TYPE);
         intent.putExtra(UiIntentActions.GROUP_ACCOUNT_NAME, groupMetadata.accountName);
         intent.putExtra(UiIntentActions.GROUP_ACCOUNT_TYPE, groupMetadata.accountType);
         intent.putExtra(UiIntentActions.GROUP_ACCOUNT_DATA_SET, groupMetadata.dataSet);
@@ -143,21 +140,6 @@
     }
 
     /**
-     * Converts the given group Uri to the legacy format if the legacy authority was specified
-     * in the given Uri.
-     */
-    // TODO(wjang):
-    public static Uri maybeConvertToLegacyUri(Uri groupUri) {
-        final String requestAuthority = groupUri.getAuthority();
-        if (!LEGACY_CONTACTS_AUTHORITY.equals(requestAuthority)) {
-            return groupUri;
-        }
-        final long groupId = ContentUris.parseId(groupUri);
-        final Uri legacyContentUri = Uri.parse(LEGACY_CONTACTS_URI);
-        return ContentUris.withAppendedId(legacyContentUri, groupId);
-    }
-
-    /**
      * Returns true if it's an empty and read-only group of a Google account and the system ID of
      * the group is one of "Friends", "Family" and "Coworkers".
      */
diff --git a/src/com/android/contacts/list/ContactsIntentResolver.java b/src/com/android/contacts/list/ContactsIntentResolver.java
index 39eaeba..800ed11 100644
--- a/src/com/android/contacts/list/ContactsIntentResolver.java
+++ b/src/com/android/contacts/list/ContactsIntentResolver.java
@@ -16,7 +16,6 @@
 
 package com.android.contacts.list;
 
-import android.accounts.Account;
 import android.app.Activity;
 import android.app.SearchManager;
 import android.content.Intent;
@@ -131,6 +130,9 @@
             }
         } else if (Intent.ACTION_INSERT_OR_EDIT.equals(action)) {
             request.setActionCode(ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT);
+        } else if (Intent.ACTION_INSERT.equals(action) &&
+                Groups.CONTENT_TYPE.equals(intent.getType())) {
+            request.setActionCode(ContactsRequest.ACTION_INSERT_GROUP);
         } else if (Intent.ACTION_SEARCH.equals(action)) {
             String query = intent.getStringExtra(SearchManager.QUERY);
             // If the {@link SearchManager.QUERY} is empty, then check if a phone number
diff --git a/src/com/android/contacts/list/ContactsRequest.java b/src/com/android/contacts/list/ContactsRequest.java
index 615fac6..e9b3a86 100644
--- a/src/com/android/contacts/list/ContactsRequest.java
+++ b/src/com/android/contacts/list/ContactsRequest.java
@@ -42,6 +42,9 @@
     /** Show potential new members of a specific group */
     public static final int ACTION_PICK_GROUP_MEMBERS = 21;
 
+    /** Create a new group */
+    public static final int ACTION_INSERT_GROUP = 22;
+
     /** Show all starred contacts */
     public static final int ACTION_STARRED = 30;
 
diff --git a/tests/res/values/donottranslate_strings.xml b/tests/res/values/donottranslate_strings.xml
index bd30d6e..3c3e5cc 100644
--- a/tests/res/values/donottranslate_strings.xml
+++ b/tests/res/values/donottranslate_strings.xml
@@ -39,13 +39,14 @@
         <item>ACTION_INSERT_OR_EDIT</item>
         <item>ACTION_INSERT_OR_EDIT_PHONE_NUMBER</item>
         <item>ACTION_INSERT_OR_EDIT_EMAIL_ADDRESS</item>
+        <item>ACTION_INSERT_GROUP</item>
         <item>ACTION_SEARCH (call button)</item>
         <item>ACTION_SEARCH: contact</item>
         <item>ACTION_SEARCH: email</item>
         <item>ACTION_SEARCH: phone</item>
         <item>SEARCH_SUGGESTION_CLICKED: contact</item>
 
-        <!-- Edit Contact -->
+        <!-- Edit -->
         <item>EDIT (content uri with only id)</item>
         <item>EDIT (lookup uri without id)</item>
         <item>EDIT (lookup uri)</item>
@@ -57,13 +58,15 @@
         <item>EDIT (create new contact for account with data)</item>
         <item>EDIT (create new raw contact)</item>
         <item>EDIT (create new legacy)</item>
+        <item>EDIT (group)</item>
 
-        <!-- View Contact -->
+        <!-- View -->
         <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>
+        <item>VIEW (group)</item>
 
         <!-- Various ways to start Contacts -->
         <item>QuickContactTestsActivity</item>
diff --git a/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java b/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
index f0b285b..926f75e 100644
--- a/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
+++ b/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
@@ -35,6 +35,7 @@
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.Groups;
 import android.provider.ContactsContract.Intents;
 import android.provider.ContactsContract.Intents.Insert;
 import android.provider.ContactsContract.RawContacts;
@@ -43,6 +44,7 @@
 import android.widget.ListView;
 import android.widget.Toast;
 
+import com.android.contacts.GroupListLoader;
 import com.android.contacts.tests.R;
 import com.android.contacts.tests.quickcontact.QuickContactTestsActivity;
 
@@ -85,6 +87,7 @@
         ACTION_INSERT_OR_EDIT,
         ACTION_INSERT_OR_EDIT_PHONE_NUMBER,
         ACTION_INSERT_OR_EDIT_EMAIL_ADDRESS,
+        ACTION_INSERT_GROUP,
         ACTION_SEARCH_CALL,
         ACTION_SEARCH_CONTACT,
         ACTION_SEARCH_EMAIL,
@@ -101,11 +104,13 @@
         EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA,
         EDIT_NEW_RAW_CONTACT,
         EDIT_NEW_LEGACY,
+        EDIT_GROUP,
         VIEW_CONTACT,
         VIEW_CONTACT_LOOKUP,
         VIEW_CONTACT_LOOKUP_ID,
         VIEW_RAW_CONTACT,
         VIEW_LEGACY,
+        VIEW_GROUP,
         QUICK_CONTACT_TESTS_ACTIVITY;
 
         public static ContactsIntent get(int ordinal) {
@@ -238,6 +243,12 @@
                 startActivity(intent);
                 break;
             }
+            case ACTION_INSERT_GROUP: {
+                final Intent intent = new Intent(Intent.ACTION_INSERT);
+                intent.setType(Groups.CONTENT_TYPE);
+                startActivity(intent);
+                break;
+            }
             case ACTION_SEARCH_CALL: {
                 Intent intent = new Intent(Intent.ACTION_SEARCH);
                 intent.putExtra(SearchManager.ACTION_MSG, "call");
@@ -358,6 +369,13 @@
                 startActivity(new Intent(Intent.ACTION_INSERT, legacyContentUri));
                 break;
             }
+            case EDIT_GROUP: {
+                final Intent intent = findArbitraryGroupIntent(Intent.ACTION_EDIT);
+                if (intent != null) {
+                    startActivity(intent);
+                }
+                break;
+            }
             case VIEW_CONTACT: {
                 final long contactId = findArbitraryContactWithPhoneNumber();
                 if (contactId != -1) {
@@ -420,6 +438,13 @@
                 }
                 break;
             }
+            case VIEW_GROUP: {
+                final Intent intent = findArbitraryGroupIntent(Intent.ACTION_VIEW);
+                if (intent != null) {
+                    startActivity(intent);
+                }
+                break;
+            }
             case QUICK_CONTACT_TESTS_ACTIVITY: {
                 startActivity(new Intent(this, QuickContactTestsActivity.class));
                 break;
@@ -496,6 +521,34 @@
         return -1;
     }
 
+    private Intent findArbitraryGroupIntent(String action) {
+        final long groupId = findArbitraryGroup();
+        if (groupId == -1) return  null;
+        final Intent intent = new Intent(action) ;
+        intent.setData(ContentUris.withAppendedId(Groups.CONTENT_URI, groupId));
+        // TODO: ContactsProvider2#getType does handle the group mimetype
+        intent.setClassName("com.google.android.contacts",
+                "com.android.contacts.activities.GroupMembersActivity");
+        return intent;
+    }
+
+    private long findArbitraryGroup() {
+        final Cursor cursor = getContentResolver().query(Groups.CONTENT_URI,
+                new String[] { Groups._ID },
+                GroupListLoader.DEFAULT_SELECTION,
+                null,
+                "RANDOM() LIMIT 1");
+        try {
+            if (cursor.moveToFirst()) {
+                return cursor.getLong(0);
+            }
+        } finally {
+            cursor.close();
+        }
+        Toast.makeText(this, "Failed to find any group. Aborting.", Toast.LENGTH_SHORT).show();
+        return -1;
+    }
+
     @Override
     public void onAccountChosen(Account account, String dataSet, int tag) {
         switch (ContactsIntent.get(tag)) {