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