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