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