blob: 27ca89ae66034dd5a9e1787d3181c5e9fda3829e [file] [log] [blame]
Wenyi Wang1fc3ef42016-01-14 18:21:40 -08001/*
2 * Copyright (C) 2016 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
17package com.android.contacts.callblocking;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.database.Cursor;
22import android.telephony.PhoneNumberUtils;
Wenyi Wang1fc3ef42016-01-14 18:21:40 -080023import android.text.TextUtils;
24import android.view.View;
25import android.view.ViewGroup;
26
Wenyi Wang1fc3ef42016-01-14 18:21:40 -080027import com.android.contacts.common.CallUtil;
28import com.android.contacts.common.GeoUtil;
29import com.android.contacts.common.list.ContactListItemView;
30import com.android.contacts.common.list.PhoneNumberListAdapter;
31import com.android.contacts.common.util.PhoneNumberHelper;
32
33import com.android.contacts.R;
34
35public class SearchAdapter extends PhoneNumberListAdapter {
36
37 private String mFormattedQueryString;
38 private String mCountryIso;
39
40 public final static int SHORTCUT_INVALID = -1;
Wenyi Wang1fc3ef42016-01-14 18:21:40 -080041 public final static int SHORTCUT_CREATE_NEW_CONTACT = 1;
42 public final static int SHORTCUT_ADD_TO_EXISTING_CONTACT = 2;
Wenyi Wang1fc3ef42016-01-14 18:21:40 -080043 public final static int SHORTCUT_BLOCK_NUMBER = 5;
44
45 public final static int SHORTCUT_COUNT = 6;
46
47 private final boolean[] mShortcutEnabled = new boolean[SHORTCUT_COUNT];
48
Wenyi Wang1fc3ef42016-01-14 18:21:40 -080049 private boolean mVideoCallingEnabled = false;
50
51 protected boolean mIsQuerySipAddress;
52
53 private Resources mResources;
54 private FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler;
55
56 public SearchAdapter(Context context) {
57 super(context);
58 // below is from ContactsPhoneNumberListAdapter
59 mCountryIso = GeoUtil.getCurrentCountryIso(context);
60 mVideoCallingEnabled = CallUtil.isVideoEnabled(context);
61 // below is from RegularSearchListAdapter
62 setShortcutEnabled(SHORTCUT_CREATE_NEW_CONTACT, false);
63 setShortcutEnabled(SHORTCUT_ADD_TO_EXISTING_CONTACT, false);
64 // below is from BlockedListSearchAdapter
65 mResources = context.getResources();
66 disableAllShortcuts();
67 setShortcutEnabled(SHORTCUT_BLOCK_NUMBER, true);
68 mFilteredNumberAsyncQueryHandler =
69 new FilteredNumberAsyncQueryHandler(context.getContentResolver());
70 }
71
72 @Override
73 public int getCount() {
74 return super.getCount() + getShortcutCount();
75 }
76
77 /**
78 * @return The number of enabled shortcuts. Ranges from 0 to a maximum of SHORTCUT_COUNT
79 */
80 public int getShortcutCount() {
81 int count = 0;
82 for (int i = 0; i < mShortcutEnabled.length; i++) {
83 if (mShortcutEnabled[i]) count++;
84 }
85 return count;
86 }
87
88 public void disableAllShortcuts() {
89 for (int i = 0; i < mShortcutEnabled.length; i++) {
90 mShortcutEnabled[i] = false;
91 }
92 }
93
94 @Override
95 public int getItemViewType(int position) {
96 final int shortcut = getShortcutTypeFromPosition(position);
97 if (shortcut >= 0) {
98 // shortcutPos should always range from 1 to SHORTCUT_COUNT
99 return super.getViewTypeCount() + shortcut;
100 } else {
101 return super.getItemViewType(position);
102 }
103 }
104
105 @Override
106 public int getViewTypeCount() {
107 // Number of item view types in the super implementation + 2 for the 2 new shortcuts
108 return super.getViewTypeCount() + SHORTCUT_COUNT;
109 }
110
111 @Override
112 public View getView(int position, View convertView, ViewGroup parent) {
113 if (getShortcutTypeFromPosition(position) >= 0) {
114 if (convertView != null) {
115 assignShortcutToView((ContactListItemView) convertView);
116 return convertView;
117 } else {
118 final ContactListItemView v = new ContactListItemView(getContext(), null,
119 mVideoCallingEnabled);
120 assignShortcutToView(v);
121 return v;
122 }
123 } else {
124 return super.getView(position, convertView, parent);
125 }
126 }
127
128 @Override
129 protected ContactListItemView newView(
130 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
131 final ContactListItemView view = super.newView(context, partition, cursor, position,
132 parent);
133
134 view.setSupportVideoCallIcon(mVideoCallingEnabled);
135 return view;
136 }
137
138 /**
139 * @param position The position of the item
140 * @return The enabled shortcut type matching the given position if the item is a
141 * shortcut, -1 otherwise
142 */
143 public int getShortcutTypeFromPosition(int position) {
144 int shortcutCount = position - super.getCount();
145 if (shortcutCount >= 0) {
146 // Iterate through the array of shortcuts, looking only for shortcuts where
147 // mShortcutEnabled[i] is true
148 for (int i = 0; shortcutCount >= 0 && i < mShortcutEnabled.length; i++) {
149 if (mShortcutEnabled[i]) {
150 shortcutCount--;
151 if (shortcutCount < 0) return i;
152 }
153 }
154 throw new IllegalArgumentException("Invalid position - greater than cursor count "
155 + " but not a shortcut.");
156 }
157 return SHORTCUT_INVALID;
158 }
159
160 @Override
161 public boolean isEmpty() {
162 return getShortcutCount() == 0 && super.isEmpty();
163 }
164
165 @Override
166 public boolean isEnabled(int position) {
167 final int shortcutType = getShortcutTypeFromPosition(position);
168 if (shortcutType >= 0) {
169 return true;
170 } else {
171 return super.isEnabled(position);
172 }
173 }
174
175 private void assignShortcutToView(ContactListItemView v) {
176 v.setDrawableResource(R.drawable.ic_not_interested_googblue_24dp);
177 v.setDisplayName(
178 getContext().getResources().getString(R.string.search_shortcut_block_number));
179 v.setPhotoPosition(super.getPhotoPosition());
180 v.setAdjustSelectionBoundsEnabled(false);
181 }
182
183 /**
184 * @return True if the shortcut state (disabled vs enabled) was changed by this operation
185 */
186 public boolean setShortcutEnabled(int shortcutType, boolean visible) {
187 final boolean changed = mShortcutEnabled[shortcutType] != visible;
188 mShortcutEnabled[shortcutType] = visible;
189 return changed;
190 }
191
192 public String getFormattedQueryString() {
193 if (mIsQuerySipAddress) {
194 // Return unnormalized SIP address
195 return getQueryString();
196 }
197 return mFormattedQueryString;
198 }
199
200 @Override
201 public void setQueryString(String queryString) {
202 // Don't show actions if the query string contains a letter.
203 final boolean showNumberShortcuts = !TextUtils.isEmpty(getFormattedQueryString())
204 && hasDigitsInQueryString();
205 mIsQuerySipAddress = PhoneNumberHelper.isUriNumber(queryString);
206
207 if (isChanged(showNumberShortcuts)) {
208 notifyDataSetChanged();
209 }
210 mFormattedQueryString = PhoneNumberUtils.formatNumber(
211 PhoneNumberUtils.normalizeNumber(queryString), mCountryIso);
212 super.setQueryString(queryString);
213 }
214
215 protected boolean isChanged(boolean showNumberShortcuts) {
216 return setShortcutEnabled(SHORTCUT_BLOCK_NUMBER, showNumberShortcuts || mIsQuerySipAddress);
217 }
218
219 /**
220 * Whether there is at least one digit in the query string.
221 */
222 private boolean hasDigitsInQueryString() {
223 String queryString = getQueryString();
224 int length = queryString.length();
225 for (int i = 0; i < length; i++) {
226 if (Character.isDigit(queryString.charAt(i))) {
227 return true;
228 }
229 }
230 return false;
231 }
232
Wenyi Wangbf452192016-01-22 10:11:43 -0800233 public void setViewBlocked(ContactListItemView view, Long id) {
Wenyi Wang1fc3ef42016-01-14 18:21:40 -0800234 view.setTag(R.id.block_id, id);
235 final int textColor = mResources.getColor(R.color.blocked_number_block_color);
236 view.getDataView().setTextColor(textColor);
237 view.getLabelView().setTextColor(textColor);
238 //TODO: Add icon
239 }
240
241 public void setViewUnblocked(ContactListItemView view) {
242 view.setTag(R.id.block_id, null);
243 final int textColor = mResources.getColor(R.color.blocked_number_secondary_text_color);
244 view.getDataView().setTextColor(textColor);
245 view.getLabelView().setTextColor(textColor);
246 //TODO: Remove icon
247 }
248
249 @Override
250 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
251 super.bindView(itemView, partition, cursor, position);
252
253 final ContactListItemView view = (ContactListItemView) itemView;
254 // Reset view state to unblocked.
255 setViewUnblocked(view);
256
257 final String number = getPhoneNumber(position);
258 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
259 final FilteredNumberAsyncQueryHandler.OnCheckBlockedListener onCheckListener =
260 new FilteredNumberAsyncQueryHandler.OnCheckBlockedListener() {
261 @Override
Wenyi Wangbf452192016-01-22 10:11:43 -0800262 public void onCheckComplete(Long id) {
Wenyi Wang1fc3ef42016-01-14 18:21:40 -0800263 if (id != null) {
264 setViewBlocked(view, id);
265 }
266 }
267 };
268 mFilteredNumberAsyncQueryHandler.isBlockedNumber(onCheckListener, number, countryIso);
269 }
270}