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.ContentUris; 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.net.Uri; 22 import android.provider.ContactsContract; 23 import android.provider.ContactsContract.CommonDataKinds.Callable; 24 import android.telephony.PhoneNumberUtils; 25 import android.text.TextUtils; 26 import android.util.Log; 27 28 import com.android.contacts.common.CallUtil; 29 import com.android.contacts.common.list.ContactListItemView; 30 import com.android.contacts.common.list.PhoneNumberListAdapter; 31 import com.android.contacts.common.list.PhoneNumberListAdapter.PhoneQuery; 32 import com.android.dialer.dialpad.SmartDialCursorLoader; 33 import com.android.dialer.dialpad.SmartDialNameMatcher; 34 import com.android.dialer.dialpad.SmartDialPrefix; 35 import com.android.dialer.dialpad.SmartDialMatchPosition; 36 37 import java.util.ArrayList; 38 39 /** 40 * List adapter to display the SmartDial search results. 41 */ 42 public class SmartDialNumberListAdapter extends DialerPhoneNumberListAdapter { 43 44 private static final String TAG = SmartDialNumberListAdapter.class.getSimpleName(); 45 private static final boolean DEBUG = false; 46 47 private SmartDialNameMatcher mNameMatcher; 48 49 public SmartDialNumberListAdapter(Context context) { 50 super(context); 51 mNameMatcher = new SmartDialNameMatcher("", SmartDialPrefix.getMap()); 52 53 if (DEBUG) { 54 Log.v(TAG, "Constructing List Adapter"); 55 } 56 } 57 58 /** 59 * Sets query for the SmartDialCursorLoader. 60 */ 61 public void configureLoader(SmartDialCursorLoader loader) { 62 if (DEBUG) { 63 Log.v(TAG, "Configure Loader with query" + getQueryString()); 64 } 65 66 if (getQueryString() == null) { 67 loader.configureQuery(""); 68 mNameMatcher.setQuery(""); 69 } else { 70 loader.configureQuery(getQueryString()); 71 mNameMatcher.setQuery(PhoneNumberUtils.normalizeNumber(getQueryString())); 72 } 73 } 74 75 /** 76 * Sets highlight options for a List item in the SmartDial search results. 77 * @param view ContactListItemView where the result will be displayed. 78 * @param cursor Object containing information of the associated List item. 79 */ 80 @Override 81 protected void setHighlight(ContactListItemView view, Cursor cursor) { 82 view.clearHighlightSequences(); 83 84 if (mNameMatcher.matches(cursor.getString(PhoneQuery.DISPLAY_NAME))) { 85 final ArrayList<SmartDialMatchPosition> nameMatches = mNameMatcher.getMatchPositions(); 86 for (SmartDialMatchPosition match:nameMatches) { 87 view.addNameHighlightSequence(match.start, match.end); 88 if (DEBUG) { 89 Log.v(TAG, cursor.getString(PhoneQuery.DISPLAY_NAME) + " " + 90 mNameMatcher.getQuery() + " " + String.valueOf(match.start)); 91 } 92 } 93 } 94 95 final SmartDialMatchPosition numberMatch = mNameMatcher.matchesNumber(cursor.getString( 96 PhoneQuery.PHONE_NUMBER)); 97 if (numberMatch != null) { 98 view.addNumberHighlightSequence(numberMatch.start, numberMatch.end); 99 } 100 } 101 102 /** 103 * Gets Uri for the list item at the given position. 104 * @param position Location of the data of interest. 105 * @return Data Uri of the entry. 106 */ 107 public Uri getDataUri(int position) { 108 Cursor cursor = ((Cursor)getItem(position)); 109 if (cursor != null) { 110 long id = cursor.getLong(PhoneQuery.PHONE_ID); 111 return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id); 112 } else { 113 Log.w(TAG, "Cursor was null in getDataUri() call. Returning null instead."); 114 return null; 115 } 116 } 117 118 @Override 119 public void setQueryString(String queryString) { 120 final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString()); 121 boolean changed = false; 122 changed |= setShortcutEnabled(SHORTCUT_ADD_NUMBER_TO_CONTACTS, showNumberShortcuts); 123 changed |= setShortcutEnabled(SHORTCUT_MAKE_VIDEO_CALL, 124 showNumberShortcuts && CallUtil.isVideoEnabled(getContext())); 125 if (changed) { 126 notifyDataSetChanged(); 127 } 128 super.setQueryString(queryString); 129 } 130 } 131