ContactsIntentResolver.java revision ea14605e56c98fcaa93f2f1e4186ce9db5e2f159
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 com.android.contacts.CallContactActivity;
20import com.android.contacts.ContactsSearchManager;
21
22import android.app.Activity;
23import android.app.SearchManager;
24import android.content.Intent;
25import android.net.Uri;
26import android.os.Bundle;
27import android.provider.Contacts.ContactMethods;
28import android.provider.Contacts.People;
29import android.provider.Contacts.Phones;
30import android.provider.ContactsContract.CommonDataKinds.Email;
31import android.provider.ContactsContract.CommonDataKinds.Phone;
32import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
33import android.provider.ContactsContract.Contacts;
34import android.provider.ContactsContract.Intents;
35import android.provider.ContactsContract.Intents.UI;
36import android.text.TextUtils;
37import android.util.Log;
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 (UI.LIST_DEFAULT.equals(action) ) {
62            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
63        } else if (UI.LIST_ALL_CONTACTS_ACTION.equals(action)) {
64            request.setActionCode(ContactsRequest.ACTION_ALL_CONTACTS);
65        } else if (UI.LIST_CONTACTS_WITH_PHONES_ACTION.equals(action)) {
66            request.setActionCode(ContactsRequest.ACTION_CONTACTS_WITH_PHONES);
67        } else if (UI.LIST_STARRED_ACTION.equals(action)) {
68            request.setActionCode(ContactsRequest.ACTION_STARRED);
69        } else if (UI.LIST_FREQUENT_ACTION.equals(action)) {
70            request.setActionCode(ContactsRequest.ACTION_FREQUENT);
71        } else if (UI.LIST_STREQUENT_ACTION.equals(action)) {
72            request.setActionCode(ContactsRequest.ACTION_STREQUENT);
73        } else if (UI.LIST_GROUP_ACTION.equals(action)) {
74            request.setActionCode(ContactsRequest.ACTION_GROUP);
75
76            // TODO Selecting a group is not implemented, but it doesn't seem to be used anywhere.
77            // Can we remove this?
78            String groupName = intent.getStringExtra(UI.GROUP_NAME_EXTRA_KEY);
79            if (!TextUtils.isEmpty(groupName)) {
80                request.setGroupName(groupName);
81            } else {
82                Log.e(TAG, "Intent missing a required extra: " + UI.GROUP_NAME_EXTRA_KEY);
83                request.setValid(false);
84            }
85        } else if (Intent.ACTION_PICK.equals(action)) {
86            final String resolvedType = intent.resolveType(mContext);
87            if (Contacts.CONTENT_TYPE.equals(resolvedType)) {
88                request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
89            } else if (People.CONTENT_TYPE.equals(resolvedType)) {
90                request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
91                request.setLegacyCompatibilityMode(true);
92            } else if (Phone.CONTENT_TYPE.equals(resolvedType)) {
93                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
94            } else if (Phones.CONTENT_TYPE.equals(resolvedType)) {
95                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
96                request.setLegacyCompatibilityMode(true);
97            } else if (StructuredPostal.CONTENT_TYPE.equals(resolvedType)) {
98                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
99            } else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(resolvedType)) {
100                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
101                request.setLegacyCompatibilityMode(true);
102            } else if (Email.CONTENT_TYPE.equals(resolvedType)) {
103                request.setActionCode(ContactsRequest.ACTION_PICK_EMAIL);
104            }
105        } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
106            String component = intent.getComponent().getClassName();
107            if (component.equals("alias.DialShortcut")) {
108                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CALL);
109            } else if (component.equals("alias.MessageShortcut")) {
110                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_SMS);
111            } else {
112                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT);
113            }
114        } else if (Intent.ACTION_GET_CONTENT.equals(action)) {
115            String type = intent.getType();
116            if (Contacts.CONTENT_ITEM_TYPE.equals(type)) {
117                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
118            } else if (Phone.CONTENT_ITEM_TYPE.equals(type)) {
119                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
120            } else if (Phones.CONTENT_ITEM_TYPE.equals(type)) {
121                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
122                request.setLegacyCompatibilityMode(true);
123            } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(type)) {
124                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
125            } else if (ContactMethods.CONTENT_POSTAL_ITEM_TYPE.equals(type)) {
126                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
127                request.setLegacyCompatibilityMode(true);
128            }  else if (People.CONTENT_ITEM_TYPE.equals(type)) {
129                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
130                request.setLegacyCompatibilityMode(true);
131            }
132        } else if (Intent.ACTION_INSERT_OR_EDIT.equals(action)) {
133            request.setActionCode(ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT);
134        } else if (Intent.ACTION_SEARCH.equals(action)) {
135            // See if the suggestion was clicked with a search action key (call button)
136            if ("call".equals(intent.getStringExtra(SearchManager.ACTION_MSG))) {
137                String query = intent.getStringExtra(SearchManager.QUERY);
138                if (!TextUtils.isEmpty(query)) {
139                    Intent newIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
140                            Uri.fromParts("tel", query, null));
141                    request.setRedirectIntent(newIntent);
142                }
143            } else {
144                request.setQueryString(intent.getStringExtra(SearchManager.QUERY));
145                request.setSearchMode(true);
146            }
147        } else if (Intent.ACTION_VIEW.equals(action)) {
148            request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT);
149            request.setContactUri(intent.getData());
150            intent.setAction(Intent.ACTION_DEFAULT);
151            intent.setData(null);
152        } else if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
153            // When we get a FILTER_CONTACTS_ACTION, it represents search in the context
154            // of some other action. Let's retrieve the original action to provide proper
155            // context for the search queries.
156            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
157            Bundle extras = intent.getExtras();
158            if (extras != null) {
159                request.setQueryString(extras.getString(UI.FILTER_TEXT_EXTRA_KEY));
160
161                ContactsRequest originalRequest =
162                        (ContactsRequest)extras.get(ContactsSearchManager.ORIGINAL_REQUEST_KEY);
163                if (originalRequest != null) {
164                    request.copyFrom(originalRequest);
165                }
166            }
167
168            request.setSearchMode(true);
169
170        // Since this is the filter activity it receives all intents
171        // dispatched from the SearchManager for security reasons
172        // so we need to re-dispatch from here to the intended target.
173        } else if (Intents.SEARCH_SUGGESTION_CLICKED.equals(action)) {
174            Uri data = intent.getData();
175            // See if the suggestion was clicked with a search action key (call button)
176            if ("call".equals(intent.getStringExtra(SearchManager.ACTION_MSG))) {
177                Intent newIntent = new Intent(mContext, CallContactActivity.class);
178                newIntent.setData(data);
179                request.setRedirectIntent(newIntent);
180            } else {
181                request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT);
182                request.setContactUri(data);
183                intent.setAction(Intent.ACTION_DEFAULT);
184                intent.setData(null);
185            }
186        } else if (Intents.SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED.equals(action)) {
187            request.setRedirectIntent(new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData()));
188        } else if (Intents.SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED.equals(action)) {
189            // TODO actually support this in EditContactActivity.
190            String number = intent.getData().getSchemeSpecificPart();
191            Intent newIntent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
192            newIntent.putExtra(Intents.Insert.PHONE, number);
193            request.setRedirectIntent(newIntent);
194
195        }
196        // Allow the title to be set to a custom String using an extra on the intent
197        String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
198        if (title != null) {
199            request.setActivityTitle(title);
200        }
201        return request;
202    }
203}
204