1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.contacts.tests.allintents;
18 
19 import android.accounts.Account;
20 import android.app.ListActivity;
21 import android.app.SearchManager;
22 import android.content.ComponentName;
23 import android.content.ContentUris;
24 import android.content.ContentValues;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.provider.Contacts.ContactMethods;
30 import android.provider.Contacts.People;
31 import android.provider.Contacts.Phones;
32 import android.provider.ContactsContract.CommonDataKinds.Email;
33 import android.provider.ContactsContract.CommonDataKinds.Organization;
34 import android.provider.ContactsContract.CommonDataKinds.Phone;
35 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
36 import android.provider.ContactsContract.Contacts;
37 import android.provider.ContactsContract.Data;
38 import android.provider.ContactsContract.Groups;
39 import android.provider.ContactsContract.Intents;
40 import android.provider.ContactsContract.Intents.Insert;
41 import android.provider.ContactsContract.RawContacts;
42 import android.view.View;
43 import android.widget.ArrayAdapter;
44 import android.widget.ListView;
45 import android.widget.Toast;
46 
47 import com.android.contacts.group.GroupUtil;
48 import com.android.contacts.list.UiIntentActions;
49 import com.android.contacts.tests.R;
50 import com.android.contacts.tests.quickcontact.QuickContactTestsActivity;
51 
52 import java.util.ArrayList;
53 
54 /**
55  * An activity that provides access to various modes of the contacts application.
56  * Useful for manual and scripted tests.
57  * <p>
58  * Note: this class cannot depend (directly on indirectly) on anything outside the test package.
59  */
60 @SuppressWarnings("deprecation")
61 public class AllIntentsActivity extends ListActivity
62         implements SelectAccountDialogFragment.Listener {
63 
64     /** The name of the package of the contacts application. */
65     private String mContactsPackageName;
66 
67     private static final String CONTACT_LIST_ACTIVITY_CLASS_NAME =
68             "com.android.contacts.activities.PeopleActivity";
69 
70     public enum ContactsIntent {
71         ACTION_PICK_CONTACT,
72         ACTION_PICK_CONTACT_LEGACY,
73         ACTION_PICK_PHONE,
74         ACTION_PICK_PHONE_LEGACY,
75         ACTION_PICK_POSTAL,
76         ACTION_PICK_POSTAL_LEGACY,
77         ACTION_PICK_EMAIL,
78         ACTION_CREATE_SHORTCUT_CONTACT,
79         ACTION_CREATE_SHORTCUT_DIAL,
80         ACTION_CREATE_SHORTCUT_MESSAGE,
81         ACTION_GET_CONTENT_CONTACT,
82         ACTION_GET_CONTENT_CONTACT_LEGACY,
83         ACTION_GET_CONTENT_PHONE,
84         ACTION_GET_CONTENT_PHONE_LEGACY,
85         ACTION_GET_CONTENT_POSTAL,
86         ACTION_GET_CONTENT_POSTAL_LEGACY,
87         ACTION_INSERT_OR_EDIT,
88         ACTION_INSERT_OR_EDIT_PHONE_NUMBER,
89         ACTION_INSERT_OR_EDIT_EMAIL_ADDRESS,
90         ACTION_INSERT_GROUP,
91         ACTION_SEARCH_CALL,
92         ACTION_SEARCH_CONTACT,
93         ACTION_SEARCH_EMAIL,
94         ACTION_SEARCH_PHONE,
95         ACTION_SEARCH_GENERAL,
96         SEARCH_SUGGESTION_CLICKED_CONTACT,
97         EDIT_CONTACT,
98         EDIT_CONTACT_LOOKUP,
99         EDIT_CONTACT_LOOKUP_ID,
100         EDIT_RAW_CONTACT,
101         EDIT_LEGACY,
102         EDIT_NEW_CONTACT,
103         EDIT_NEW_CONTACT_WITH_DATA,
104         EDIT_NEW_CONTACT_FOR_ACCOUNT,
105         EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA,
106         EDIT_NEW_RAW_CONTACT,
107         EDIT_NEW_LEGACY,
108         EDIT_GROUP,
109         VIEW_CONTACT_WITHOUT_ID,
110         VIEW_PERSON_WITHOUT_ID,
111         VIEW_CONTACT,
112         VIEW_CONTACT_LOOKUP,
113         VIEW_CONTACT_LOOKUP_ID,
114         VIEW_RAW_CONTACT,
115         VIEW_LEGACY,
116         VIEW_GROUP,
117         QUICK_CONTACT_TESTS_ACTIVITY,
118         LIST_DEFAULT,
119         LIST_CONTACTS,
120         LIST_ALL_CONTACTS,
121         LIST_CONTACTS_WITH_PHONES,
122         LIST_STARRED,
123         LIST_FREQUENT,
124         LIST_STREQUENT;
125 
get(int ordinal)126         public static ContactsIntent get(int ordinal) {
127             return values()[ordinal];
128         }
129     }
130 
131     @Override
onCreate(Bundle savedInstanceState)132     protected void onCreate(Bundle savedInstanceState) {
133         super.onCreate(savedInstanceState);
134 
135         setListAdapter(new ArrayAdapter<String>(this, R.layout.intent_list_item,
136                 getResources().getStringArray(R.array.allIntents)));
137         mContactsPackageName = getResources().getString(
138                 R.string.target_package_name);
139     }
140 
141     @Override
onListItemClick(ListView l, View v, int position, long id)142     protected void onListItemClick(ListView l, View v, int position, long id) {
143         super.onListItemClick(l, v, position, id);
144 
145         switch (ContactsIntent.get(position)) {
146             case ACTION_PICK_CONTACT: {
147                 startContactSelectionActivityForResult(
148                         new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI));
149                 break;
150             }
151             case ACTION_PICK_CONTACT_LEGACY: {
152                 startContactSelectionActivityForResult(
153                         new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
154                 break;
155             }
156             case ACTION_PICK_PHONE: {
157                 startContactSelectionActivityForResult(
158                         new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI));
159                 break;
160             }
161             case ACTION_PICK_PHONE_LEGACY: {
162                 startContactSelectionActivityForResult(
163                         new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI));
164                 break;
165             }
166             case ACTION_PICK_POSTAL: {
167                 startContactSelectionActivityForResult(
168                         new Intent(Intent.ACTION_PICK, StructuredPostal.CONTENT_URI));
169                 break;
170             }
171             case ACTION_PICK_POSTAL_LEGACY: {
172                 Intent intent = new Intent(Intent.ACTION_PICK);
173                 intent.setType(ContactMethods.CONTENT_POSTAL_TYPE);
174                 startContactSelectionActivityForResult(intent);
175                 break;
176             }
177             case ACTION_PICK_EMAIL: {
178                 startContactSelectionActivityForResult(
179                         new Intent(Intent.ACTION_PICK, Email.CONTENT_URI));
180                 break;
181             }
182             case ACTION_CREATE_SHORTCUT_CONTACT: {
183                 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
184                 startContactSelectionActivityForResult(intent);
185                 break;
186             }
187             case ACTION_CREATE_SHORTCUT_DIAL: {
188                 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
189                 bindIntentToClass(intent, "alias.DialShortcut");
190                 startActivityForResult(intent, 0);
191                 break;
192             }
193             case ACTION_CREATE_SHORTCUT_MESSAGE: {
194                 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
195                 bindIntentToClass(intent, "alias.MessageShortcut");
196                 startActivityForResult(intent, 0);
197                 break;
198             }
199             case ACTION_GET_CONTENT_CONTACT: {
200                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
201                 intent.setType(Contacts.CONTENT_ITEM_TYPE);
202                 startContactSelectionActivityForResult(intent);
203                 break;
204             }
205             case ACTION_GET_CONTENT_CONTACT_LEGACY: {
206                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
207                 intent.setType(People.CONTENT_ITEM_TYPE);
208                 startContactSelectionActivityForResult(intent);
209                 break;
210             }
211             case ACTION_GET_CONTENT_PHONE: {
212                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
213                 intent.setType(Phone.CONTENT_ITEM_TYPE);
214                 startContactSelectionActivityForResult(intent);
215                 break;
216             }
217             case ACTION_GET_CONTENT_PHONE_LEGACY: {
218                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
219                 intent.setType(Phones.CONTENT_ITEM_TYPE);
220                 startContactSelectionActivityForResult(intent);
221                 break;
222             }
223             case ACTION_GET_CONTENT_POSTAL: {
224                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
225                 intent.setType(StructuredPostal.CONTENT_ITEM_TYPE);
226                 startContactSelectionActivityForResult(intent);
227                 break;
228             }
229             case ACTION_GET_CONTENT_POSTAL_LEGACY: {
230                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
231                 intent.setType(ContactMethods.CONTENT_POSTAL_ITEM_TYPE);
232                 startContactSelectionActivityForResult(intent);
233                 break;
234             }
235             case ACTION_INSERT_OR_EDIT: {
236                 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
237                 intent.setType(Contacts.CONTENT_ITEM_TYPE);
238                 putDataExtra(intent);
239                 startActivity(intent);
240                 break;
241             }
242             case ACTION_INSERT_OR_EDIT_PHONE_NUMBER: {
243                 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
244                 intent.setType(Contacts.CONTENT_ITEM_TYPE);
245                 intent.putExtra(Insert.PHONE, "5123456789");
246                 startActivity(intent);
247                 break;
248             }
249             case ACTION_INSERT_OR_EDIT_EMAIL_ADDRESS: {
250                 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
251                 intent.setType(Contacts.CONTENT_ITEM_TYPE);
252                 intent.putExtra(Insert.EMAIL, "android@android.com");
253                 startActivity(intent);
254                 break;
255             }
256             case ACTION_INSERT_GROUP: {
257                 final Intent intent = new Intent(Intent.ACTION_INSERT);
258                 intent.setType(Groups.CONTENT_TYPE);
259                 startActivity(intent);
260                 break;
261             }
262             case ACTION_SEARCH_CALL: {
263                 Intent intent = new Intent(Intent.ACTION_SEARCH);
264                 intent.putExtra(SearchManager.ACTION_MSG, "call");
265                 intent.putExtra(SearchManager.QUERY, "800-4664-411");
266                 startSearchResultActivity(intent);
267                 break;
268             }
269             case ACTION_SEARCH_CONTACT: {
270                 Intent intent = new Intent(Intent.ACTION_SEARCH);
271                 intent.putExtra(SearchManager.QUERY, "a");
272                 intent.setType(Contacts.CONTENT_TYPE);
273                 startSearchResultActivity(intent);
274                 break;
275             }
276             case ACTION_SEARCH_EMAIL: {
277                 Intent intent = new Intent(Intent.ACTION_SEARCH);
278                 intent.putExtra(Insert.EMAIL, "a");
279                 startSearchResultActivity(intent);
280                 break;
281             }
282             case ACTION_SEARCH_PHONE: {
283                 Intent intent = new Intent(Intent.ACTION_SEARCH);
284                 intent.putExtra(Insert.PHONE, "800");
285                 startSearchResultActivity(intent);
286                 break;
287             }
288             case ACTION_SEARCH_GENERAL: {
289                 Intent intent = new Intent(Intent.ACTION_SEARCH);
290                 intent.putExtra(SearchManager.QUERY, "a");
291                 startSearchResultActivity(intent);
292                 break;
293             }
294             case SEARCH_SUGGESTION_CLICKED_CONTACT: {
295                 long contactId = findArbitraryContactWithPhoneNumber();
296                 if (contactId != -1) {
297                     Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
298                     Intent intent = new Intent(Intents.SEARCH_SUGGESTION_CLICKED);
299                     intent.setData(contactUri);
300                     startContactListActivity(intent);
301                 }
302                 break;
303             }
304             case EDIT_CONTACT: {
305                 final long contactId = findArbitraryContactWithPhoneNumber();
306                 if (contactId != -1) {
307                     final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
308                     final Intent intent = new Intent(Intent.ACTION_EDIT, uri);
309                     startActivity(intent);
310                 }
311                 break;
312             }
313             case EDIT_CONTACT_LOOKUP: {
314                 final long contactId = findArbitraryContactWithPhoneNumber();
315                 if (contactId != -1) {
316                     final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
317                     final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), uri);
318                     final String lookupKey = lookupUri.getPathSegments().get(2);
319                     final Uri lookupWithoutIdUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,
320                             lookupKey);
321                     final Intent intent = new Intent(Intent.ACTION_EDIT, lookupWithoutIdUri);
322                     startActivity(intent);
323                 }
324                 break;
325             }
326             case EDIT_CONTACT_LOOKUP_ID: {
327                 final long contactId = findArbitraryContactWithPhoneNumber();
328                 if (contactId != -1) {
329                     final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
330                     final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), uri);
331                     final Intent intent = new Intent(Intent.ACTION_EDIT, lookupUri);
332                     startActivity(intent);
333                 }
334                 break;
335             }
336             case EDIT_RAW_CONTACT: {
337                 final long contactId = findArbitraryContactWithPhoneNumber();
338                 if (contactId != -1) {
339                     final long rawContactId = findArbitraryRawContactOfContact(contactId);
340                     if (rawContactId != -1) {
341                         final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
342                                 rawContactId);
343                         final Intent intent = new Intent(Intent.ACTION_EDIT, uri);
344                         startActivity(intent);
345                     }
346                 }
347                 break;
348             }
349             case EDIT_LEGACY: {
350                 final long contactId = findArbitraryContactWithPhoneNumber();
351                 if (contactId != -1) {
352                     final long rawContactId = findArbitraryRawContactOfContact(contactId);
353                     if (rawContactId != -1) {
354                         final Uri legacyContentUri = Uri.parse("content://contacts/people");
355                         final Uri uri = ContentUris.withAppendedId(legacyContentUri, rawContactId);
356                         final Intent intent = new Intent(Intent.ACTION_EDIT, uri);
357                         startActivity(intent);
358                     }
359                 }
360                 break;
361             }
362             case EDIT_NEW_CONTACT: {
363                 startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
364                 break;
365             }
366             case EDIT_NEW_CONTACT_WITH_DATA: {
367                 Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
368                 putDataExtra(intent);
369                 startActivity(intent);
370                 break;
371             }
372             case EDIT_NEW_CONTACT_FOR_ACCOUNT:
373             case EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA: {
374                 final SelectAccountDialogFragment dialog = new SelectAccountDialogFragment();
375                 dialog.setArguments(SelectAccountDialogFragment.createBundle(position));
376                 dialog.show(getFragmentManager(), SelectAccountDialogFragment.TAG);
377                 break;
378             }
379             case EDIT_NEW_RAW_CONTACT: {
380                 startActivity(new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI));
381                 break;
382             }
383             case EDIT_NEW_LEGACY: {
384                 final Uri legacyContentUri = Uri.parse("content://contacts/people");
385                 startActivity(new Intent(Intent.ACTION_INSERT, legacyContentUri));
386                 break;
387             }
388             case EDIT_GROUP: {
389                 final Intent intent = findArbitraryGroupIntent(Intent.ACTION_EDIT);
390                 if (intent != null) {
391                     startActivity(intent);
392                 }
393                 break;
394             }
395             case VIEW_CONTACT: {
396                 final long contactId = findArbitraryContactWithPhoneNumber();
397                 if (contactId != -1) {
398                     final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
399                     final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
400                     startActivity(intent);
401                 }
402                 break;
403             }
404             case VIEW_CONTACT_WITHOUT_ID: {
405                 startActivity(new Intent(Intent.ACTION_VIEW, Contacts.CONTENT_URI));
406                 break;
407             }
408             case VIEW_PERSON_WITHOUT_ID: {
409                 Intent intent = new Intent(Intent.ACTION_VIEW);
410                 intent.setType("vnd.android.cursor.dir/person");
411                 startActivity(intent);
412                 break;
413             }
414             case VIEW_CONTACT_LOOKUP: {
415                 final long contactId = findArbitraryContactWithPhoneNumber();
416                 if (contactId != -1) {
417                     final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
418                     final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), uri);
419                     final String lookupKey = lookupUri.getPathSegments().get(2);
420                     final Uri lookupWithoutIdUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,
421                             lookupKey);
422                     final Intent intent = new Intent(Intent.ACTION_VIEW, lookupWithoutIdUri);
423                     startActivity(intent);
424                 }
425                 break;
426             }
427             case VIEW_CONTACT_LOOKUP_ID: {
428                 final long contactId = findArbitraryContactWithPhoneNumber();
429                 if (contactId != -1) {
430                     final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
431                     final Uri lookupUri = Contacts.getLookupUri(getContentResolver(), uri);
432                     final Intent intent = new Intent(Intent.ACTION_VIEW, lookupUri);
433                     startActivity(intent);
434                 }
435                 break;
436             }
437             case VIEW_RAW_CONTACT: {
438                 final long contactId = findArbitraryContactWithPhoneNumber();
439                 if (contactId != -1) {
440                     final long rawContactId = findArbitraryRawContactOfContact(contactId);
441                     if (rawContactId != -1) {
442                         final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
443                                 rawContactId);
444                         final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
445                         startActivity(intent);
446                     }
447                 }
448                 break;
449             }
450             case VIEW_LEGACY: {
451                 final long contactId = findArbitraryContactWithPhoneNumber();
452                 if (contactId != -1) {
453                     final long rawContactId = findArbitraryRawContactOfContact(contactId);
454                     if (rawContactId != -1) {
455                         final Uri legacyContentUri = Uri.parse("content://contacts/people");
456                         final Uri uri = ContentUris.withAppendedId(legacyContentUri, rawContactId);
457                         final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
458                         startActivity(intent);
459                     }
460                 }
461                 break;
462             }
463             case VIEW_GROUP: {
464                 final Intent intent = findArbitraryGroupIntent(Intent.ACTION_VIEW);
465                 if (intent != null) {
466                     startActivity(intent);
467                 }
468                 break;
469             }
470             case QUICK_CONTACT_TESTS_ACTIVITY: {
471                 startActivity(new Intent(this, QuickContactTestsActivity.class));
472                 break;
473             }
474             case LIST_DEFAULT: {
475                 startActivity(new Intent(UiIntentActions.LIST_DEFAULT));
476                 break;
477             }
478             case LIST_CONTACTS: {
479                 startActivity(new Intent(UiIntentActions.LIST_CONTACTS));
480                 break;
481             }
482             case LIST_ALL_CONTACTS: {
483                 startActivity(new Intent(UiIntentActions.LIST_ALL_CONTACTS_ACTION));
484                 break;
485             }
486             case LIST_CONTACTS_WITH_PHONES: {
487                 startActivity(new Intent(UiIntentActions.LIST_CONTACTS_WITH_PHONES_ACTION));
488                 break;
489             }
490             case LIST_STARRED: {
491                 startActivity(new Intent(UiIntentActions.LIST_STARRED_ACTION));
492                 break;
493             }
494             case LIST_FREQUENT: {
495                 startActivity(new Intent(UiIntentActions.LIST_FREQUENT_ACTION));
496                 break;
497             }
498             case LIST_STREQUENT: {
499                 startActivity(new Intent(UiIntentActions.LIST_STREQUENT_ACTION));
500                 break;
501             }
502 
503             default: {
504                 Toast.makeText(this, "Sorry, we forgot to write this...", Toast.LENGTH_LONG).show();
505             }
506         }
507     }
508 
509     /** Creates an intent that is bound to a specific activity by name. */
bindIntentToClass(Intent intent, String activityClassName)510     private Intent bindIntentToClass(Intent intent, String activityClassName) {
511         intent.setComponent(new ComponentName(mContactsPackageName,
512                     activityClassName));
513         return intent;
514     }
515 
startContactListActivity(Intent intent)516     private void startContactListActivity(Intent intent) {
517         bindIntentToClass(intent, CONTACT_LIST_ACTIVITY_CLASS_NAME);
518         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
519         startActivity(intent);
520     }
521 
startContactSelectionActivityForResult(Intent intent)522     private void startContactSelectionActivityForResult(Intent intent) {
523         startActivityForResult(intent, 12);
524     }
525 
startSearchResultActivity(Intent intent)526     private void startSearchResultActivity(Intent intent) {
527         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
528         startActivity(intent);
529     }
530 
531     @Override
onActivityResult(int requestCode, int resultCode, Intent data)532     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
533         Intent intent = new Intent(this, ResultActivity.class);
534         intent.putExtra("resultCode", resultCode);
535         intent.putExtra("data", data);
536         startActivity(intent);
537     }
538 
findArbitraryContactWithPhoneNumber()539     private long findArbitraryContactWithPhoneNumber() {
540         final Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,
541                 new String[] { Contacts._ID },
542                 Contacts.HAS_PHONE_NUMBER + "!=0",
543                 null, "RANDOM() LIMIT 1");
544         try {
545             if (cursor.moveToFirst()) {
546                 return cursor.getLong(0);
547             }
548         } finally {
549             cursor.close();
550         }
551         Toast.makeText(this, "Failed to find a contact with a phone number. Aborting.",
552                 Toast.LENGTH_SHORT).show();
553         return -1;
554     }
555 
findArbitraryRawContactOfContact(long contactId)556     private long findArbitraryRawContactOfContact(long contactId) {
557         final Cursor cursor = getContentResolver().query(RawContacts.CONTENT_URI,
558                 new String[] { RawContacts._ID },
559                 RawContacts.CONTACT_ID + "=?",
560                 new String[] { String.valueOf(contactId) },
561                 RawContacts._ID + " LIMIT 1");
562         try {
563             if (cursor.moveToFirst()) {
564                 return cursor.getLong(0);
565             }
566         } finally {
567             cursor.close();
568         }
569         Toast.makeText(this, "Failed to find a raw contact of contact with ID " + contactId +
570                 ". Aborting", Toast.LENGTH_SHORT).show();
571         return -1;
572     }
573 
findArbitraryGroupIntent(String action)574     private Intent findArbitraryGroupIntent(String action) {
575         final long groupId = findArbitraryGroup();
576         if (groupId == -1) return  null;
577         final Intent intent = new Intent(action) ;
578         intent.setData(ContentUris.withAppendedId(Groups.CONTENT_URI, groupId));
579         // TODO: ContactsProvider2#getType does handle the group mimetype
580         intent.setClassName("com.google.android.contacts",
581                 "com.android.contacts.activities.PeopleActivity");
582         return intent;
583     }
584 
findArbitraryGroup()585     private long findArbitraryGroup() {
586         final Cursor cursor = getContentResolver().query(Groups.CONTENT_URI,
587                 new String[] { Groups._ID },
588                 GroupUtil.DEFAULT_SELECTION,
589                 null,
590                 "RANDOM() LIMIT 1");
591         try {
592             if (cursor.moveToFirst()) {
593                 return cursor.getLong(0);
594             }
595         } finally {
596             cursor.close();
597         }
598         Toast.makeText(this, "Failed to find any group. Aborting.", Toast.LENGTH_SHORT).show();
599         return -1;
600     }
601 
602     @Override
onAccountChosen(Account account, String dataSet, int tag)603     public void onAccountChosen(Account account, String dataSet, int tag) {
604         switch (ContactsIntent.get(tag)) {
605             case EDIT_NEW_CONTACT_FOR_ACCOUNT: {
606                 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
607                 intent.putExtra(Insert.EXTRA_ACCOUNT, account);
608                 intent.putExtra(Insert.EXTRA_DATA_SET, dataSet);
609                 startActivity(intent);
610                 break;
611             }
612             case EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA: {
613                 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
614 
615                 intent.putExtra(Insert.EXTRA_ACCOUNT, account);
616                 intent.putExtra(Insert.EXTRA_DATA_SET, dataSet);
617                 putDataExtra(intent);
618 
619                 startActivity(intent);
620                 break;
621             }
622             default:
623                 break;
624         }
625     }
626 
putDataExtra(final Intent intent)627     public void putDataExtra(final Intent intent) {
628         ContentValues row1 = new ContentValues();
629         row1.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
630         row1.put(Organization.COMPANY, "Android");
631 
632         ContentValues row2 = new ContentValues();
633         row2.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
634         row2.put(Email.TYPE, Email.TYPE_CUSTOM);
635         row2.put(Email.LABEL, "Green Bot");
636         row2.put(Email.ADDRESS, "android@android.com");
637 
638         final ArrayList<ContentValues> rows = new ArrayList<>();
639         rows.add(row1);
640         rows.add(row2);
641 
642         intent.putParcelableArrayListExtra(Insert.DATA, rows);
643     }
644 }
645