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