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