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