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