AllIntentsActivity.java revision 884e393af339ba1357dd1aea4c8e821396c214b9
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.ContactsSearchManager;
20import com.android.contacts.tests.R;
21
22import android.app.ListActivity;
23import android.app.SearchManager;
24import android.content.ComponentName;
25import android.content.ContentUris;
26import android.content.Intent;
27import android.database.Cursor;
28import android.net.Uri;
29import android.os.Bundle;
30import android.provider.ContactsContract;
31import android.provider.Contacts.ContactMethods;
32import android.provider.Contacts.People;
33import android.provider.Contacts.Phones;
34import android.provider.ContactsContract.Contacts;
35import android.provider.ContactsContract.Intents;
36import android.provider.ContactsContract.RawContacts;
37import android.provider.ContactsContract.CommonDataKinds.Phone;
38import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
39import android.provider.ContactsContract.Intents.Insert;
40import android.provider.ContactsContract.Intents.UI;
41import android.view.View;
42import android.widget.ArrayAdapter;
43import android.widget.ListView;
44import android.widget.Toast;
45
46/**
47 * An activity that provides access to various modes of the contacts application.
48 * Useful for manual and scripted tests.
49 */
50@SuppressWarnings("deprecation")
51public class AllIntentsActivity extends ListActivity {
52
53    private static final String ANDROID_CONTACTS_PACKAGE = "com.android.contacts";
54
55    private static final String CONTACTS_LIST_ACTIVITY_CLASS_NAME =
56            "com.android.contacts.ContactsListActivity";
57    private static final String SEARCH_RESULTS_ACTIVITY_CLASS_NAME =
58            "com.android.contacts.SearchResultsActivity";
59    private static final String MULTIPLE_PHONE_PICKER_ACTIVITY_CLASS_NAME =
60        "com.android.contacts.MultiplePhonePickerActivity";
61
62    private static final int LIST_DEFAULT = 0;
63    private static final int LIST_ALL_CONTACTS_ACTION = 1;
64    private static final int LIST_CONTACTS_WITH_PHONES_ACTION = 2;
65    private static final int LIST_STARRED_ACTION = 3;
66    private static final int LIST_STARRED_ACTION_WITH_FILTER = 4;
67    private static final int LIST_FREQUENT_ACTION = 5;
68    private static final int LIST_FREQUENT_ACTION_WITH_FILTER = 6;
69    private static final int LIST_STREQUENT_ACTION = 7;
70    private static final int LIST_STREQUENT_ACTION_WITH_FILTER = 8;
71    private static final int ACTION_PICK_CONTACT = 9;
72    private static final int ACTION_PICK_CONTACT_LEGACY = 10;
73    private static final int ACTION_PICK_PHONE = 11;
74    private static final int ACTION_PICK_PHONE_LEGACY = 12;
75    private static final int ACTION_PICK_POSTAL = 13;
76    private static final int ACTION_PICK_POSTAL_LEGACY = 14;
77    private static final int ACTION_CREATE_SHORTCUT_CONTACT = 15;
78    private static final int ACTION_CREATE_SHORTCUT_CONTACT_FILTER = 16;
79    private static final int ACTION_CREATE_SHORTCUT_DIAL = 17;
80    private static final int ACTION_CREATE_SHORTCUT_DIAL_FILTER = 18;
81    private static final int ACTION_CREATE_SHORTCUT_MESSAGE = 19;
82    private static final int ACTION_CREATE_SHORTCUT_MESSAGE_FILTER = 20;
83    private static final int ACTION_GET_CONTENT_CONTACT = 21;
84    private static final int ACTION_GET_CONTENT_CONTACT_FILTER = 22;
85    private static final int ACTION_GET_CONTENT_CONTACT_LEGACY = 23;
86    private static final int ACTION_GET_CONTENT_CONTACT_FILTER_LEGACY = 24;
87    private static final int ACTION_GET_CONTENT_PHONE = 25;
88    private static final int ACTION_GET_CONTENT_PHONE_FILTER = 26;
89    private static final int ACTION_GET_CONTENT_PHONE_LEGACY = 27;
90    private static final int ACTION_GET_CONTENT_POSTAL = 28;
91    private static final int ACTION_GET_CONTENT_POSTAL_FILTER = 29;
92    private static final int ACTION_GET_CONTENT_POSTAL_LEGACY = 30;
93    private static final int ACTION_INSERT_OR_EDIT = 31;
94    private static final int ACTION_SEARCH_CALL = 32;
95    private static final int ACTION_SEARCH_CONTACT = 33;
96    private static final int ACTION_SEARCH_EMAIL = 34;
97    private static final int ACTION_SEARCH_PHONE = 35;
98    private static final int SEARCH_SUGGESTION_CLICKED_CALL_BUTTON = 36;
99    private static final int SEARCH_SUGGESTION_CLICKED_CONTACT = 37;
100    private static final int SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED = 38;
101    private static final int SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED = 39;
102    private static final int JOIN_CONTACT = 40;
103    private static final int ACTION_GET_MULTIPLE_PHONES = 41;
104
105    private static final int EDIT_CONTACT = 42;
106    private static final int EDIT_CONTACT_LOOKUP = 43;
107    private static final int EDIT_CONTACT_LOOKUP_ID = 44;
108    private static final int EDIT_RAW_CONTACT = 45;
109    private static final int EDIT_LEGACY = 46;
110    private static final int EDIT_NEW_CONTACT = 47;
111    private static final int EDIT_NEW_RAW_CONTACT = 48;
112    private static final int EDIT_NEW_LEGACY = 49;
113
114    @Override
115    protected void onCreate(Bundle savedInstanceState) {
116        super.onCreate(savedInstanceState);
117        setListAdapter(new ArrayAdapter<String>(this, R.layout.intent_list_item,
118                getResources().getStringArray(R.array.allIntents)));
119    }
120
121    @Override
122    protected void onListItemClick(ListView l, View v, int position, long id) {
123        super.onListItemClick(l, v, position, id);
124
125        switch (position) {
126            case LIST_DEFAULT: {
127                startContactsListActivity(
128                        new Intent(Intent.ACTION_VIEW, Contacts.CONTENT_URI));
129                break;
130            }
131            case LIST_ALL_CONTACTS_ACTION: {
132                startContactsListActivity(
133                        new Intent(UI.LIST_ALL_CONTACTS_ACTION, Contacts.CONTENT_URI));
134                break;
135            }
136            case LIST_CONTACTS_WITH_PHONES_ACTION: {
137                startContactsListActivity(
138                        new Intent(UI.LIST_CONTACTS_WITH_PHONES_ACTION, Contacts.CONTENT_URI));
139                break;
140            }
141            case LIST_STARRED_ACTION: {
142                startContactsListActivity(
143                        new Intent(UI.LIST_STARRED_ACTION, Contacts.CONTENT_URI));
144                break;
145            }
146            case LIST_STARRED_ACTION_WITH_FILTER: {
147                startContactsListActivity(
148                        buildFilterIntent(UI.LIST_STARRED_ACTION, null, null));
149                break;
150            }
151            case LIST_FREQUENT_ACTION: {
152                startContactsListActivity(
153                        new Intent(UI.LIST_FREQUENT_ACTION, Contacts.CONTENT_URI));
154                break;
155            }
156            case LIST_FREQUENT_ACTION_WITH_FILTER: {
157                startContactsListActivity(
158                        buildFilterIntent(UI.LIST_FREQUENT_ACTION, null, null));
159                break;
160            }
161            case LIST_STREQUENT_ACTION: {
162                startContactsListActivity(
163                        new Intent(UI.LIST_STREQUENT_ACTION, Contacts.CONTENT_URI));
164                break;
165            }
166            case LIST_STREQUENT_ACTION_WITH_FILTER: {
167                startContactsListActivity(
168                        buildFilterIntent(UI.LIST_STREQUENT_ACTION, null, null));
169                break;
170            }
171            case ACTION_PICK_CONTACT: {
172                startContactsListActivityForResult(
173                        new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI));
174                break;
175            }
176            case ACTION_PICK_CONTACT_LEGACY: {
177                startContactsListActivityForResult(
178                        new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
179                break;
180            }
181            case ACTION_PICK_PHONE: {
182                startContactsListActivityForResult(
183                        new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI));
184                break;
185            }
186            case ACTION_PICK_PHONE_LEGACY: {
187                startContactsListActivityForResult(
188                        new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI));
189                break;
190            }
191            case ACTION_PICK_POSTAL: {
192                startContactsListActivityForResult(
193                        new Intent(Intent.ACTION_PICK, StructuredPostal.CONTENT_URI));
194                break;
195            }
196            case ACTION_PICK_POSTAL_LEGACY: {
197                Intent intent = new Intent(Intent.ACTION_PICK);
198                intent.setType(ContactMethods.CONTENT_POSTAL_TYPE);
199                startContactsListActivityForResult(intent);
200                break;
201            }
202            case ACTION_CREATE_SHORTCUT_CONTACT: {
203                Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
204                startContactsListActivityForResult(intent);
205                break;
206            }
207            case ACTION_CREATE_SHORTCUT_CONTACT_FILTER: {
208                startContactsListActivityForResult(
209                        buildFilterIntent(Intent.ACTION_CREATE_SHORTCUT,
210                                CONTACTS_LIST_ACTIVITY_CLASS_NAME, null));
211                break;
212            }
213            case ACTION_CREATE_SHORTCUT_DIAL: {
214                Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
215                intent.setComponent(
216                        new ComponentName(ANDROID_CONTACTS_PACKAGE, "alias.DialShortcut"));
217                startActivityForResult(intent, 0);
218                break;
219            }
220            case ACTION_CREATE_SHORTCUT_DIAL_FILTER: {
221                startContactsListActivityForResult(
222                        buildFilterIntent(Intent.ACTION_CREATE_SHORTCUT,
223                                "alias.DialShortcut", null));
224                break;
225            }
226            case ACTION_CREATE_SHORTCUT_MESSAGE: {
227                Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
228                intent.setComponent(
229                        new ComponentName(ANDROID_CONTACTS_PACKAGE, "alias.MessageShortcut"));
230                startActivityForResult(intent, 0);
231                break;
232            }
233            case ACTION_CREATE_SHORTCUT_MESSAGE_FILTER: {
234                startContactsListActivityForResult(
235                        buildFilterIntent(Intent.ACTION_CREATE_SHORTCUT,
236                                "alias.MessageShortcut", null));
237                break;
238            }
239            case ACTION_GET_CONTENT_CONTACT: {
240                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
241                intent.setType(Contacts.CONTENT_ITEM_TYPE);
242                startContactsListActivityForResult(intent);
243                break;
244            }
245            case ACTION_GET_CONTENT_CONTACT_LEGACY: {
246                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
247                intent.setType(People.CONTENT_ITEM_TYPE);
248                startContactsListActivityForResult(intent);
249                break;
250            }
251            case ACTION_GET_CONTENT_CONTACT_FILTER: {
252                startContactsListActivityForResult(
253                        buildFilterIntent(Intent.ACTION_GET_CONTENT,
254                                CONTACTS_LIST_ACTIVITY_CLASS_NAME,
255                                Contacts.CONTENT_ITEM_TYPE));
256                break;
257            }
258            case ACTION_GET_CONTENT_CONTACT_FILTER_LEGACY: {
259                startContactsListActivityForResult(
260                        buildFilterIntent(Intent.ACTION_GET_CONTENT,
261                                CONTACTS_LIST_ACTIVITY_CLASS_NAME,
262                                People.CONTENT_ITEM_TYPE));
263                break;
264            }
265            case ACTION_GET_CONTENT_PHONE: {
266                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
267                intent.setType(Phone.CONTENT_ITEM_TYPE);
268                startContactsListActivityForResult(intent);
269                break;
270            }
271            case ACTION_GET_CONTENT_PHONE_FILTER: {
272                startContactsListActivityForResult(
273                        buildFilterIntent(Intent.ACTION_GET_CONTENT,
274                                CONTACTS_LIST_ACTIVITY_CLASS_NAME,
275                                Phone.CONTENT_ITEM_TYPE));
276                break;
277            }
278            case ACTION_GET_CONTENT_PHONE_LEGACY: {
279                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
280                intent.setType(Phones.CONTENT_ITEM_TYPE);
281                startContactsListActivityForResult(intent);
282                break;
283            }
284            case ACTION_GET_CONTENT_POSTAL: {
285                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
286                intent.setType(StructuredPostal.CONTENT_ITEM_TYPE);
287                startContactsListActivityForResult(intent);
288                break;
289            }
290            case ACTION_GET_CONTENT_POSTAL_FILTER: {
291                startContactsListActivityForResult(
292                        buildFilterIntent(Intent.ACTION_GET_CONTENT,
293                                CONTACTS_LIST_ACTIVITY_CLASS_NAME,
294                                StructuredPostal.CONTENT_ITEM_TYPE));
295                break;
296            }
297            case ACTION_GET_CONTENT_POSTAL_LEGACY: {
298                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
299                intent.setType(ContactMethods.CONTENT_POSTAL_ITEM_TYPE);
300                startContactsListActivityForResult(intent);
301                break;
302            }
303            case ACTION_INSERT_OR_EDIT: {
304                Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
305                startContactsListActivity(intent);
306                break;
307            }
308            case ACTION_SEARCH_CALL: {
309                Intent intent = new Intent(Intent.ACTION_SEARCH);
310                intent.putExtra(SearchManager.ACTION_MSG, "call");
311                intent.putExtra(SearchManager.QUERY, "800-4664-411");
312                startSearchResultActivity(intent);
313                break;
314            }
315            case ACTION_SEARCH_CONTACT: {
316                Intent intent = new Intent(Intent.ACTION_SEARCH);
317                intent.putExtra(SearchManager.QUERY, "a");
318                startSearchResultActivity(intent);
319                break;
320            }
321            case ACTION_SEARCH_EMAIL: {
322                Intent intent = new Intent(Intent.ACTION_SEARCH);
323                intent.putExtra(Insert.EMAIL, "a");
324                startSearchResultActivity(intent);
325                break;
326            }
327            case ACTION_SEARCH_PHONE: {
328                Intent intent = new Intent(Intent.ACTION_SEARCH);
329                intent.putExtra(Insert.PHONE, "800");
330                startSearchResultActivity(intent);
331                break;
332            }
333            case SEARCH_SUGGESTION_CLICKED_CALL_BUTTON: {
334                long contactId = findArbitraryContactWithPhoneNumber();
335                if (contactId != -1) {
336                    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
337                    Intent intent = new Intent(Intents.SEARCH_SUGGESTION_CLICKED);
338                    intent.setData(contactUri);
339                    intent.putExtra(SearchManager.ACTION_MSG, "call");
340                    startContactsListActivity(intent);
341                }
342                break;
343            }
344            case SEARCH_SUGGESTION_CLICKED_CONTACT: {
345                long contactId = findArbitraryContactWithPhoneNumber();
346                if (contactId != -1) {
347                    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
348                    Intent intent = new Intent(Intents.SEARCH_SUGGESTION_CLICKED);
349                    intent.setData(contactUri);
350                    startContactsListActivity(intent);
351                }
352                break;
353            }
354            case SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED: {
355                Intent intent = new Intent(Intents.SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED);
356                intent.setData(Uri.parse("tel:800-4664411"));
357                startContactsListActivity(intent);
358                break;
359            }
360            case SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED: {
361                Intent intent = new Intent(Intents.SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED);
362                intent.setData(Uri.parse("tel:800-4664411"));
363                startContactsListActivity(intent);
364                break;
365            }
366            case JOIN_CONTACT: {
367                // TODO
368                break;
369            }
370            case ACTION_GET_MULTIPLE_PHONES: {
371                Intent intent = new Intent(Intents.ACTION_GET_MULTIPLE_PHONES);
372                intent.setType(Phone.CONTENT_TYPE);
373                intent.putExtra(Intents.EXTRA_PHONE_URIS, new Uri[] {
374                        Uri.parse("tel:555-1212"), Uri.parse("tel:555-2121")
375                });
376                startMultiplePhoneSelectionActivityForResult(intent);
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_RAW_CONTACT: {
427                startActivity(new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI));
428                break;
429            }
430            case EDIT_NEW_LEGACY: {
431                final Uri legacyContentUri = Uri.parse("content://contacts/people");
432                startActivity(new Intent(Intent.ACTION_INSERT, legacyContentUri));
433                break;
434            }
435            default: {
436                Toast.makeText(this, "Sorry, we forgot to write this...", Toast.LENGTH_LONG).show();
437            }
438        }
439    }
440
441    private Intent buildFilterIntent(String action, String component, String type) {
442        Intent intent = new Intent(UI.FILTER_CONTACTS_ACTION);
443        intent.putExtra(UI.FILTER_TEXT_EXTRA_KEY, "A");
444        intent.putExtra(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY, action);
445        if (component != null) {
446            intent.putExtra(ContactsSearchManager.ORIGINAL_COMPONENT_EXTRA_KEY, component);
447        }
448        if (type != null) {
449            intent.putExtra(ContactsSearchManager.ORIGINAL_TYPE_EXTRA_KEY, type);
450        }
451        return intent;
452    }
453
454    private void startContactsListActivity(Intent intent) {
455        intent.setComponent(
456                new ComponentName(ANDROID_CONTACTS_PACKAGE, CONTACTS_LIST_ACTIVITY_CLASS_NAME));
457        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
458        startActivity(intent);
459    }
460
461    private void startContactsListActivityForResult(Intent intent) {
462        intent.setComponent(
463                new ComponentName(ANDROID_CONTACTS_PACKAGE, CONTACTS_LIST_ACTIVITY_CLASS_NAME));
464        startActivityForResult(intent, 12);
465    }
466
467    private void startSearchResultActivity(Intent intent) {
468        intent.setComponent(
469                new ComponentName(ANDROID_CONTACTS_PACKAGE, SEARCH_RESULTS_ACTIVITY_CLASS_NAME));
470        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
471        startActivity(intent);
472    }
473
474    private void startMultiplePhoneSelectionActivityForResult(Intent intent) {
475        intent.setComponent(
476                new ComponentName(ANDROID_CONTACTS_PACKAGE,
477                        MULTIPLE_PHONE_PICKER_ACTIVITY_CLASS_NAME));
478        startActivityForResult(intent, 13);
479    }
480
481    @Override
482    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
483        Intent intent = new Intent(this, ResultActivity.class);
484        intent.putExtra("resultCode", resultCode);
485        intent.putExtra("data", data);
486        startActivity(intent);
487    }
488
489    private long findArbitraryContactWithPhoneNumber() {
490        final Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,
491                new String[] { Contacts._ID },
492                Contacts.HAS_PHONE_NUMBER + "!=0 AND " + Contacts.STARRED + "!=0" ,
493                null, "RANDOM() LIMIT 1");
494        try {
495            if (cursor.moveToFirst()) {
496                return cursor.getLong(0);
497            }
498        } finally {
499            cursor.close();
500        }
501
502        return -1;
503    }
504
505    private long findArbitraryRawContactOfContact(long contactId) {
506        final Cursor cursor = getContentResolver().query(RawContacts.CONTENT_URI,
507                new String[] { RawContacts._ID },
508                RawContacts.CONTACT_ID + "=?",
509                new String[] { String.valueOf(contactId) },
510                RawContacts._ID + " LIMIT 1");
511        try {
512            if (cursor.moveToFirst()) {
513                return cursor.getLong(0);
514            }
515        } finally {
516            cursor.close();
517        }
518
519        return -1;
520    }
521}
522