ContactsIntentResolver.java revision c3d202ccbaf93ddd8291672027e59f549c32eee3
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.ContactsSearchManager;
20
21import android.app.Activity;
22import android.app.SearchManager;
23import android.content.Intent;
24import android.net.Uri;
25import android.os.Bundle;
26import android.provider.Contacts.ContactMethods;
27import android.provider.Contacts.People;
28import android.provider.Contacts.Phones;
29import android.provider.ContactsContract;
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.Insert;
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 (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            }
96        } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
97            String component = intent.getComponent().getClassName();
98            if (component.equals("alias.DialShortcut")) {
99                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CALL);
100            } else if (component.equals("alias.MessageShortcut")) {
101                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_SMS);
102            } else {
103                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT);
104            }
105        } else if (Intent.ACTION_GET_CONTENT.equals(action)) {
106            String type = intent.getType();
107            if (Contacts.CONTENT_ITEM_TYPE.equals(type)) {
108                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
109            } else if (Phone.CONTENT_ITEM_TYPE.equals(type)) {
110                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
111            } else if (Phones.CONTENT_ITEM_TYPE.equals(type)) {
112                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
113                request.setLegacyCompatibilityMode(true);
114            } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(type)) {
115                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
116            } else if (ContactMethods.CONTENT_POSTAL_ITEM_TYPE.equals(type)) {
117                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
118                request.setLegacyCompatibilityMode(true);
119            }  else if (People.CONTENT_ITEM_TYPE.equals(type)) {
120                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
121                request.setLegacyCompatibilityMode(true);
122            }
123        } else if (Intent.ACTION_INSERT_OR_EDIT.equals(action)) {
124            request.setActionCode(ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT);
125        } else if (Intent.ACTION_SEARCH.equals(action)) {
126            String query = intent.getStringExtra(SearchManager.QUERY);
127            // If the {@link SearchManager.QUERY} is empty, then check if a phone number
128            // or email is specified, in that priority.
129            if (TextUtils.isEmpty(query)) {
130                query = intent.getStringExtra(Insert.PHONE);
131            }
132            if (TextUtils.isEmpty(query)) {
133                query = intent.getStringExtra(Insert.EMAIL);
134            }
135            request.setQueryString(query);
136            request.setSearchMode(true);
137        } else if (Intent.ACTION_VIEW.equals(action)) {
138            final String resolvedType = intent.resolveType(mContext);
139            if (ContactsContract.Contacts.CONTENT_TYPE.equals(resolvedType)
140                    || android.provider.Contacts.People.CONTENT_TYPE.equals(resolvedType)) {
141                request.setActionCode(ContactsRequest.ACTION_ALL_CONTACTS);
142            } else {
143                request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT);
144                request.setContactUri(intent.getData());
145                intent.setAction(Intent.ACTION_DEFAULT);
146                intent.setData(null);
147            }
148        } else if (UiIntentActions.FILTER_CONTACTS_ACTION.equals(action)) {
149            // When we get a FILTER_CONTACTS_ACTION, it represents search in the context
150            // of some other action. Let's retrieve the original action to provide proper
151            // context for the search queries.
152            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
153            Bundle extras = intent.getExtras();
154            if (extras != null) {
155                request.setQueryString(extras.getString(UiIntentActions.FILTER_TEXT_EXTRA_KEY));
156
157                ContactsRequest originalRequest =
158                        (ContactsRequest)extras.get(ContactsSearchManager.ORIGINAL_REQUEST_KEY);
159                if (originalRequest != null) {
160                    request.copyFrom(originalRequest);
161                }
162            }
163
164            request.setSearchMode(true);
165
166        // Since this is the filter activity it receives all intents
167        // dispatched from the SearchManager for security reasons
168        // so we need to re-dispatch from here to the intended target.
169        } else if (Intents.SEARCH_SUGGESTION_CLICKED.equals(action)) {
170            Uri data = intent.getData();
171            request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT);
172            request.setContactUri(data);
173            intent.setAction(Intent.ACTION_DEFAULT);
174            intent.setData(null);
175        } else if (UiIntentActions.PICK_JOIN_CONTACT_ACTION.equals(action)) {
176            request.setActionCode(ContactsRequest.ACTION_PICK_JOIN);
177        }
178        // Allow the title to be set to a custom String using an extra on the intent
179        String title = intent.getStringExtra(UiIntentActions.TITLE_EXTRA_KEY);
180        if (title != null) {
181            request.setActivityTitle(title);
182        }
183        return request;
184    }
185}
186