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