summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/app/list/SmartDialNumberListAdapter.java
blob: 1d2cda3ea7bbc92d429079a20d08b28cf7fe0441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.dialer.app.list;

import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import com.android.contacts.common.list.ContactListItemView;
import com.android.dialer.common.LogUtil;
import com.android.dialer.smartdial.SmartDialCursorLoader;
import com.android.dialer.smartdial.util.SmartDialMatchPosition;
import com.android.dialer.smartdial.util.SmartDialNameMatcher;
import com.android.dialer.util.CallUtil;
import java.util.ArrayList;

/** List adapter to display the SmartDial search results. */
public class SmartDialNumberListAdapter extends DialerPhoneNumberListAdapter {

  private static final String TAG = SmartDialNumberListAdapter.class.getSimpleName();
  private static final boolean DEBUG = false;

  private final Context mContext;
  @NonNull private final SmartDialNameMatcher mNameMatcher;

  public SmartDialNumberListAdapter(Context context) {
    super(context);
    mContext = context;
    mNameMatcher = new SmartDialNameMatcher("");
    setShortcutEnabled(SmartDialNumberListAdapter.SHORTCUT_DIRECT_CALL, false);

    if (DEBUG) {
      LogUtil.v(TAG, "Constructing List Adapter");
    }
  }

  /** Sets query for the SmartDialCursorLoader. */
  public void configureLoader(SmartDialCursorLoader loader) {
    if (DEBUG) {
      LogUtil.v(TAG, "Configure Loader with query" + getQueryString());
    }

    if (getQueryString() == null) {
      loader.configureQuery("");
      mNameMatcher.setQuery("");
    } else {
      loader.configureQuery(getQueryString());
      mNameMatcher.setQuery(PhoneNumberUtils.normalizeNumber(getQueryString()));
    }
  }

  /**
   * Sets highlight options for a List item in the SmartDial search results.
   *
   * @param view ContactListItemView where the result will be displayed.
   * @param cursor Object containing information of the associated List item.
   */
  @Override
  protected void setHighlight(ContactListItemView view, Cursor cursor) {
    view.clearHighlightSequences();

    if (mNameMatcher.matches(mContext, cursor.getString(PhoneQuery.DISPLAY_NAME))) {
      final ArrayList<SmartDialMatchPosition> nameMatches = mNameMatcher.getMatchPositions();
      for (SmartDialMatchPosition match : nameMatches) {
        view.addNameHighlightSequence(match.start, match.end);
        if (DEBUG) {
          LogUtil.v(
              TAG,
              cursor.getString(PhoneQuery.DISPLAY_NAME)
                  + " "
                  + mNameMatcher.getQuery()
                  + " "
                  + String.valueOf(match.start));
        }
      }
    }

    final SmartDialMatchPosition numberMatch =
        mNameMatcher.matchesNumber(mContext, cursor.getString(PhoneQuery.PHONE_NUMBER));
    if (numberMatch != null) {
      view.addNumberHighlightSequence(numberMatch.start, numberMatch.end);
    }
  }

  @Override
  public void setQueryString(String queryString) {
    final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString());
    boolean changed = false;
    changed |= setShortcutEnabled(SHORTCUT_CREATE_NEW_CONTACT, showNumberShortcuts);
    changed |= setShortcutEnabled(SHORTCUT_ADD_TO_EXISTING_CONTACT, showNumberShortcuts);
    changed |= setShortcutEnabled(SHORTCUT_SEND_SMS_MESSAGE, showNumberShortcuts);
    changed |=
        setShortcutEnabled(
            SHORTCUT_MAKE_VIDEO_CALL, showNumberShortcuts && CallUtil.isVideoEnabled(getContext()));
    if (changed) {
      notifyDataSetChanged();
    }
    super.setQueryString(queryString);
  }

  public void setShowEmptyListForNullQuery(boolean show) {
    mNameMatcher.setShouldMatchEmptyQuery(!show);
  }
}