1 /* 2 * Copyright (C) 2013 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 package com.android.dialer.list; 17 18 import android.content.Context; 19 import android.database.Cursor; 20 import android.net.Uri; 21 import android.text.TextUtils; 22 23 import com.android.contacts.common.CallUtil; 24 import com.android.contacts.common.list.DirectoryPartition; 25 import com.android.contacts.common.util.PhoneNumberHelper; 26 import com.android.dialer.calllog.ContactInfo; 27 import com.android.dialer.service.CachedNumberLookupService; 28 import com.android.dialer.service.CachedNumberLookupService.CachedContactInfo; 29 30 /** 31 * List adapter to display regular search results. 32 */ 33 public class RegularSearchListAdapter extends DialerPhoneNumberListAdapter { 34 private boolean mIsQuerySipAddress; 35 36 public RegularSearchListAdapter(Context context) { 37 super(context); 38 setShortcutEnabled(SHORTCUT_CREATE_NEW_CONTACT, false); 39 setShortcutEnabled(SHORTCUT_ADD_TO_EXISTING_CONTACT, false); 40 } 41 42 public CachedContactInfo getContactInfo( 43 CachedNumberLookupService lookupService, int position) { 44 ContactInfo info = new ContactInfo(); 45 CachedContactInfo cacheInfo = lookupService.buildCachedContactInfo(info); 46 final Cursor item = (Cursor) getItem(position); 47 if (item != null) { 48 info.name = item.getString(PhoneQuery.DISPLAY_NAME); 49 info.type = item.getInt(PhoneQuery.PHONE_TYPE); 50 info.label = item.getString(PhoneQuery.PHONE_LABEL); 51 info.number = item.getString(PhoneQuery.PHONE_NUMBER); 52 final String photoUriStr = item.getString(PhoneQuery.PHOTO_URI); 53 info.photoUri = photoUriStr == null ? null : Uri.parse(photoUriStr); 54 55 cacheInfo.setLookupKey(item.getString(PhoneQuery.LOOKUP_KEY)); 56 57 final int partitionIndex = getPartitionForPosition(position); 58 final DirectoryPartition partition = 59 (DirectoryPartition) getPartition(partitionIndex); 60 final long directoryId = partition.getDirectoryId(); 61 final String sourceName = partition.getLabel(); 62 if (isExtendedDirectory(directoryId)) { 63 cacheInfo.setExtendedSource(sourceName, directoryId); 64 } else { 65 cacheInfo.setDirectorySource(sourceName, directoryId); 66 } 67 } 68 return cacheInfo; 69 } 70 71 @Override 72 public String getFormattedQueryString() { 73 if (mIsQuerySipAddress) { 74 // Return unnormalized SIP address 75 return getQueryString(); 76 } 77 return super.getFormattedQueryString(); 78 } 79 80 @Override 81 public void setQueryString(String queryString) { 82 // Don't show actions if the query string contains a letter. 83 final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString()) 84 && hasDigitsInQueryString(); 85 // Email addresses that could be SIP addresses are an exception. 86 mIsQuerySipAddress = PhoneNumberHelper.isUriNumber(queryString); 87 boolean changed = false; 88 changed |= setShortcutEnabled(SHORTCUT_DIRECT_CALL, 89 showNumberShortcuts || mIsQuerySipAddress); 90 changed |= setShortcutEnabled(SHORTCUT_SEND_SMS_MESSAGE, showNumberShortcuts); 91 changed |= setShortcutEnabled(SHORTCUT_MAKE_VIDEO_CALL, 92 showNumberShortcuts && CallUtil.isVideoEnabled(getContext())); 93 if (changed) { 94 notifyDataSetChanged(); 95 } 96 super.setQueryString(queryString); 97 } 98 99 /** 100 * Whether there is at least one digit in the query string. 101 */ 102 private boolean hasDigitsInQueryString() { 103 String queryString = getQueryString(); 104 int length = queryString.length(); 105 for (int i = 0; i < length; i++) { 106 if (Character.isDigit(queryString.charAt(i))) { 107 return true; 108 } 109 } 110 return false; 111 } 112 } 113