ContactsIntentResolver.java revision 01c2f7f2e324dbf7ce70229ecb29f9c33542be0e
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.list;
18
19import android.app.Activity;
20import android.app.SearchManager;
21import android.content.Intent;
22import android.net.Uri;
23import android.provider.Contacts.ContactMethods;
24import android.provider.Contacts.People;
25import android.provider.Contacts.Phones;
26import android.provider.ContactsContract;
27import android.provider.ContactsContract.CommonDataKinds.Email;
28import android.provider.ContactsContract.CommonDataKinds.Phone;
29import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
30import android.provider.ContactsContract.Contacts;
31import android.provider.ContactsContract.Groups;
32import android.provider.ContactsContract.Intents;
33import android.provider.ContactsContract.Intents.Insert;
34import android.text.TextUtils;
35import android.util.Log;
36
37import com.android.contacts.common.model.account.AccountWithDataSet;
38
39/**
40 * Parses a Contacts intent, extracting all relevant parts and packaging them
41 * as a {@link ContactsRequest} object.
42 */
43@SuppressWarnings("deprecation")
44public class ContactsIntentResolver {
45
46    private static final String TAG = "ContactsIntentResolver";
47
48    private final Activity mContext;
49
50    public ContactsIntentResolver(Activity context) {
51        this.mContext = context;
52    }
53
54    public ContactsRequest resolveIntent(Intent intent) {
55        ContactsRequest request = new ContactsRequest();
56
57        String action = intent.getAction();
58
59        Log.i(TAG, "Called with action: " + action);
60
61        if (UiIntentActions.LIST_DEFAULT.equals(action) ) {
62            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
63        } else if (UiIntentActions.LIST_ALL_CONTACTS_ACTION.equals(action)) {
64            request.setActionCode(ContactsRequest.ACTION_ALL_CONTACTS);
65        } else if (UiIntentActions.LIST_CONTACTS_WITH_PHONES_ACTION.equals(action)) {
66            request.setActionCode(ContactsRequest.ACTION_CONTACTS_WITH_PHONES);
67        } else if (UiIntentActions.LIST_STARRED_ACTION.equals(action)) {
68            request.setActionCode(ContactsRequest.ACTION_STARRED);
69        } else if (UiIntentActions.LIST_FREQUENT_ACTION.equals(action)) {
70            request.setActionCode(ContactsRequest.ACTION_FREQUENT);
71        } else if (UiIntentActions.LIST_STREQUENT_ACTION.equals(action)) {
72            request.setActionCode(ContactsRequest.ACTION_STREQUENT);
73        } else if (UiIntentActions.LIST_GROUP_ACTION.equals(action)) {
74            request.setActionCode(ContactsRequest.ACTION_GROUP);
75            // We no longer support UiIntentActions.GROUP_NAME_EXTRA_KEY
76        } else if (Intent.ACTION_PICK.equals(action)) {
77            final String resolvedType = intent.resolveType(mContext);
78            if (Contacts.CONTENT_TYPE.equals(resolvedType)) {
79                request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
80            } else if (People.CONTENT_TYPE.equals(resolvedType)) {
81                request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
82                request.setLegacyCompatibilityMode(true);
83            } else if (Phone.CONTENT_TYPE.equals(resolvedType)) {
84                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
85            } else if (Phones.CONTENT_TYPE.equals(resolvedType)) {
86                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
87                request.setLegacyCompatibilityMode(true);
88            } else if (StructuredPostal.CONTENT_TYPE.equals(resolvedType)) {
89                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
90            } else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(resolvedType)) {
91                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
92                request.setLegacyCompatibilityMode(true);
93            } else if (Email.CONTENT_TYPE.equals(resolvedType)) {
94                request.setActionCode(ContactsRequest.ACTION_PICK_EMAIL);
95            } else if (Groups.CONTENT_ITEM_TYPE.equals(resolvedType)) {
96                request.setActionCode(ContactsRequest.ACTION_PICK_GROUP_MEMBERS);
97                request.setAccountWithDataSet(new AccountWithDataSet(
98                        intent.getStringExtra(UiIntentActions.GROUP_ACCOUNT_NAME),
99                        intent.getStringExtra(UiIntentActions.GROUP_ACCOUNT_TYPE),
100                        intent.getStringExtra(UiIntentActions.GROUP_ACCOUNT_DATA_SET)));
101                request.setRawContactIds(intent.getStringArrayListExtra(
102                        UiIntentActions.GROUP_CONTACT_IDS));
103            }
104        } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
105            String component = intent.getComponent().getClassName();
106            if (component.equals("alias.DialShortcut")) {
107                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CALL);
108            } else if (component.equals("alias.MessageShortcut")) {
109                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_SMS);
110            } else {
111                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT);
112            }
113        } else if (Intent.ACTION_GET_CONTENT.equals(action)) {
114            String type = intent.getType();
115            if (Contacts.CONTENT_ITEM_TYPE.equals(type)) {
116                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
117            } else if (Phone.CONTENT_ITEM_TYPE.equals(type)) {
118                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
119            } else if (Phones.CONTENT_ITEM_TYPE.equals(type)) {
120                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
121                request.setLegacyCompatibilityMode(true);
122            } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(type)) {
123                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
124            } else if (ContactMethods.CONTENT_POSTAL_ITEM_TYPE.equals(type)) {
125                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
126                request.setLegacyCompatibilityMode(true);
127            }  else if (People.CONTENT_ITEM_TYPE.equals(type)) {
128                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
129                request.setLegacyCompatibilityMode(true);
130            }
131        } else if (Intent.ACTION_INSERT_OR_EDIT.equals(action)) {
132            request.setActionCode(ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT);
133        } else if (Intent.ACTION_INSERT.equals(action) &&
134                Groups.CONTENT_TYPE.equals(intent.getType())) {
135            request.setActionCode(ContactsRequest.ACTION_INSERT_GROUP);
136        } else if (Intent.ACTION_SEARCH.equals(action)) {
137            String query = intent.getStringExtra(SearchManager.QUERY);
138            // If the {@link SearchManager.QUERY} is empty, then check if a phone number
139            // or email is specified, in that priority.
140            if (TextUtils.isEmpty(query)) {
141                query = intent.getStringExtra(Insert.PHONE);
142            }
143            if (TextUtils.isEmpty(query)) {
144                query = intent.getStringExtra(Insert.EMAIL);
145            }
146            request.setQueryString(query);
147            request.setSearchMode(true);
148        } else if (Intent.ACTION_VIEW.equals(action)) {
149            final String resolvedType = intent.resolveType(mContext);
150            if (ContactsContract.Contacts.CONTENT_TYPE.equals(resolvedType)
151                    || android.provider.Contacts.People.CONTENT_TYPE.equals(resolvedType)) {
152                request.setActionCode(ContactsRequest.ACTION_ALL_CONTACTS);
153            } else {
154                request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT);
155                request.setContactUri(intent.getData());
156                intent.setAction(Intent.ACTION_DEFAULT);
157                intent.setData(null);
158            }
159        // Since this is the filter activity it receives all intents
160        // dispatched from the SearchManager for security reasons
161        // so we need to re-dispatch from here to the intended target.
162        } else if (Intents.SEARCH_SUGGESTION_CLICKED.equals(action)) {
163            Uri data = intent.getData();
164            request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT);
165            request.setContactUri(data);
166            intent.setAction(Intent.ACTION_DEFAULT);
167            intent.setData(null);
168        } else if (UiIntentActions.PICK_JOIN_CONTACT_ACTION.equals(action)) {
169            request.setActionCode(ContactsRequest.ACTION_PICK_JOIN);
170        }
171        // Allow the title to be set to a custom String using an extra on the intent
172        String title = intent.getStringExtra(UiIntentActions.TITLE_EXTRA_KEY);
173        if (title != null) {
174            request.setActivityTitle(title);
175        }
176        return request;
177    }
178}
179