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