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.ContactsUtils; 25 import com.android.contacts.common.compat.DirectoryCompat; 26 import com.android.contacts.common.list.DirectoryPartition; 27 import com.android.contacts.common.util.PhoneNumberHelper; 28 import com.android.dialer.calllog.ContactInfo; 29 import com.android.dialer.service.CachedNumberLookupService; 30 import com.android.dialer.service.CachedNumberLookupService.CachedContactInfo; 31 32 /** 33 * List adapter to display regular search results. 34 */ 35 public class RegularSearchListAdapter extends DialerPhoneNumberListAdapter { 36 protected boolean mIsQuerySipAddress; 37 38 public RegularSearchListAdapter(Context context) { 39 super(context); 40 setShortcutEnabled(SHORTCUT_CREATE_NEW_CONTACT, false); 41 setShortcutEnabled(SHORTCUT_ADD_TO_EXISTING_CONTACT, false); 42 } 43 44 public CachedContactInfo getContactInfo( 45 CachedNumberLookupService lookupService, int position) { 46 ContactInfo info = new ContactInfo(); 47 CachedContactInfo cacheInfo = lookupService.buildCachedContactInfo(info); 48 final Cursor item = (Cursor) getItem(position); 49 if (item != null) { 50 final DirectoryPartition partition = 51 (DirectoryPartition) getPartition(getPartitionForPosition(position)); 52 final long directoryId = partition.getDirectoryId(); 53 final boolean isExtendedDirectory = isExtendedDirectory(directoryId); 54 55 info.name = item.getString(PhoneQuery.DISPLAY_NAME); 56 info.type = item.getInt(PhoneQuery.PHONE_TYPE); 57 info.label = item.getString(PhoneQuery.PHONE_LABEL); 58 info.number = item.getString(PhoneQuery.PHONE_NUMBER); 59 final String photoUriStr = item.getString(PhoneQuery.PHOTO_URI); 60 info.photoUri = photoUriStr == null ? null : Uri.parse(photoUriStr); 61 /* 62 * An extended directory is custom directory in the app, but not a directory provided by 63 * framework. So it can't be USER_TYPE_WORK. 64 * 65 * When a search result is selected, RegularSearchFragment calls getContactInfo and 66 * cache the resulting @{link ContactInfo} into local db. Set usertype to USER_TYPE_WORK 67 * only if it's NOT extended directory id and is enterprise directory. 68 */ 69 info.userType = !isExtendedDirectory 70 && DirectoryCompat.isEnterpriseDirectoryId(directoryId) 71 ? ContactsUtils.USER_TYPE_WORK : ContactsUtils.USER_TYPE_CURRENT; 72 73 cacheInfo.setLookupKey(item.getString(PhoneQuery.LOOKUP_KEY)); 74 75 final String sourceName = partition.getLabel(); 76 if (isExtendedDirectory) { 77 cacheInfo.setExtendedSource(sourceName, directoryId); 78 } else { 79 cacheInfo.setDirectorySource(sourceName, directoryId); 80 } 81 } 82 return cacheInfo; 83 } 84 85 @Override 86 public String getFormattedQueryString() { 87 if (mIsQuerySipAddress) { 88 // Return unnormalized SIP address 89 return getQueryString(); 90 } 91 return super.getFormattedQueryString(); 92 } 93 94 @Override 95 public void setQueryString(String queryString) { 96 // Don't show actions if the query string contains a letter. 97 final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString()) 98 && hasDigitsInQueryString(); 99 mIsQuerySipAddress = PhoneNumberHelper.isUriNumber(queryString); 100 101 if (isChanged(showNumberShortcuts)) { 102 notifyDataSetChanged(); 103 } 104 super.setQueryString(queryString); 105 } 106 107 protected boolean isChanged(boolean showNumberShortcuts) { 108 boolean changed = false; 109 changed |= setShortcutEnabled(SHORTCUT_DIRECT_CALL, 110 showNumberShortcuts || mIsQuerySipAddress); 111 changed |= setShortcutEnabled(SHORTCUT_SEND_SMS_MESSAGE, showNumberShortcuts); 112 changed |= setShortcutEnabled(SHORTCUT_MAKE_VIDEO_CALL, 113 showNumberShortcuts && CallUtil.isVideoEnabled(getContext())); 114 return changed; 115 } 116 117 /** 118 * Whether there is at least one digit in the query string. 119 */ 120 private boolean hasDigitsInQueryString() { 121 String queryString = getQueryString(); 122 int length = queryString.length(); 123 for (int i = 0; i < length; i++) { 124 if (Character.isDigit(queryString.charAt(i))) { 125 return true; 126 } 127 } 128 return false; 129 } 130 } 131