ContactsIntentResolver.java revision 7967545e62b473503473b2c9e127cef405f67201
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; 38import com.android.contacts.group.GroupUtil; 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 (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 (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 } else if (Groups.CONTENT_TYPE.equals(resolvedType)) { 97 request.setActionCode(ContactsRequest.ACTION_PICK_GROUP_MEMBERS); 98 request.setAccountWithDataSet(new AccountWithDataSet( 99 intent.getStringExtra(UiIntentActions.GROUP_ACCOUNT_NAME), 100 intent.getStringExtra(UiIntentActions.GROUP_ACCOUNT_TYPE), 101 intent.getStringExtra(UiIntentActions.GROUP_ACCOUNT_DATA_SET))); 102 request.setRawContactIds(intent.getStringArrayListExtra( 103 UiIntentActions.GROUP_CONTACT_IDS)); 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_INSERT.equals(action) && 135 Groups.CONTENT_TYPE.equals(intent.getType())) { 136 request.setActionCode(ContactsRequest.ACTION_INSERT_GROUP); 137 } else if (Intent.ACTION_SEARCH.equals(action)) { 138 String query = intent.getStringExtra(SearchManager.QUERY); 139 // If the {@link SearchManager.QUERY} is empty, then check if a phone number 140 // or email is specified, in that priority. 141 if (TextUtils.isEmpty(query)) { 142 query = intent.getStringExtra(Insert.PHONE); 143 } 144 if (TextUtils.isEmpty(query)) { 145 query = intent.getStringExtra(Insert.EMAIL); 146 } 147 request.setQueryString(query); 148 request.setSearchMode(true); 149 } else if (Intent.ACTION_VIEW.equals(action)) { 150 final String resolvedType = intent.resolveType(mContext); 151 if (ContactsContract.Contacts.CONTENT_TYPE.equals(resolvedType) 152 || android.provider.Contacts.People.CONTENT_TYPE.equals(resolvedType)) { 153 request.setActionCode(ContactsRequest.ACTION_ALL_CONTACTS); 154 } else if (!GroupUtil.isGroupUri(intent.getData())){ 155 request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT); 156 request.setContactUri(intent.getData()); 157 intent.setAction(Intent.ACTION_DEFAULT); 158 intent.setData(null); 159 } else { 160 request.setActionCode(ContactsRequest.ACTION_VIEW_GROUP); 161 request.setContactUri(intent.getData()); 162 } 163 } else if (Intent.ACTION_EDIT.equals(action)) { 164 if (GroupUtil.isGroupUri(intent.getData())){ 165 request.setActionCode(ContactsRequest.ACTION_EDIT_GROUP); 166 request.setContactUri(intent.getData()); 167 } 168 // Since this is the filter activity it receives all intents 169 // dispatched from the SearchManager for security reasons 170 // so we need to re-dispatch from here to the intended target. 171 } else if (Intents.SEARCH_SUGGESTION_CLICKED.equals(action)) { 172 Uri data = intent.getData(); 173 request.setActionCode(ContactsRequest.ACTION_VIEW_CONTACT); 174 request.setContactUri(data); 175 intent.setAction(Intent.ACTION_DEFAULT); 176 intent.setData(null); 177 } else if (UiIntentActions.PICK_JOIN_CONTACT_ACTION.equals(action)) { 178 request.setActionCode(ContactsRequest.ACTION_PICK_JOIN); 179 } 180 // Allow the title to be set to a custom String using an extra on the intent 181 String title = intent.getStringExtra(UiIntentActions.TITLE_EXTRA_KEY); 182 if (title != null) { 183 request.setActivityTitle(title); 184 } 185 return request; 186 } 187} 188