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