blob: 4c8ede90623909de80f3c080eb18a8ad377d6f4b [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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
17package com.android.incallui;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.drawable.BitmapDrawable;
22import android.graphics.drawable.Drawable;
23import android.media.RingtoneManager;
24import android.net.Uri;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.os.Build.VERSION;
26import android.os.Build.VERSION_CODES;
Eric Erfanian9779f962017-03-27 12:31:48 -070027import android.os.SystemClock;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import android.provider.ContactsContract.CommonDataKinds.Phone;
29import android.provider.ContactsContract.Contacts;
30import android.provider.ContactsContract.DisplayNameSources;
31import android.support.annotation.AnyThread;
32import android.support.annotation.MainThread;
33import android.support.annotation.NonNull;
Eric Erfaniand8046e52017-04-06 09:41:50 -070034import android.support.annotation.Nullable;
Eric Erfanianccca3152017-02-22 16:32:36 -080035import android.support.annotation.WorkerThread;
36import android.support.v4.os.UserManagerCompat;
37import android.telecom.TelecomManager;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070038import android.telephony.PhoneNumberUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import android.text.TextUtils;
40import android.util.ArrayMap;
41import android.util.ArraySet;
42import com.android.contacts.common.ContactsUtils;
43import com.android.dialer.common.Assert;
Eric Erfaniand8046e52017-04-06 09:41:50 -070044import com.android.dialer.common.concurrent.DialerExecutor;
45import com.android.dialer.common.concurrent.DialerExecutor.Worker;
46import com.android.dialer.common.concurrent.DialerExecutors;
Eric Erfanian8369df02017-05-03 10:27:13 -070047import com.android.dialer.logging.ContactLookupResult;
48import com.android.dialer.logging.ContactSource;
Eric Erfanian9779f962017-03-27 12:31:48 -070049import com.android.dialer.oem.CequintCallerIdManager;
50import com.android.dialer.oem.CequintCallerIdManager.CequintCallerIdContact;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import com.android.dialer.phonenumbercache.CachedNumberLookupService;
52import com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo;
53import com.android.dialer.phonenumbercache.ContactInfo;
54import com.android.dialer.phonenumbercache.PhoneNumberCache;
55import com.android.dialer.phonenumberutil.PhoneNumberHelper;
56import com.android.dialer.util.MoreStrings;
57import com.android.incallui.CallerInfoAsyncQuery.OnQueryCompleteListener;
58import com.android.incallui.ContactsAsyncHelper.OnImageLoadCompleteListener;
59import com.android.incallui.bindings.PhoneNumberService;
60import com.android.incallui.call.DialerCall;
61import com.android.incallui.incall.protocol.ContactPhotoType;
62import java.util.Map;
63import java.util.Objects;
64import java.util.Set;
65import java.util.concurrent.ConcurrentHashMap;
66import org.json.JSONException;
67import org.json.JSONObject;
68
69/**
70 * Class responsible for querying Contact Information for DialerCall objects. Can perform
71 * asynchronous requests to the Contact Provider for information as well as respond synchronously
72 * for any data that it currently has cached from previous queries. This class always gets called
73 * from the UI thread so it does not need thread protection.
74 */
75public class ContactInfoCache implements OnImageLoadCompleteListener {
76
77 private static final String TAG = ContactInfoCache.class.getSimpleName();
78 private static final int TOKEN_UPDATE_PHOTO_FOR_CALL_STATE = 0;
79 private static ContactInfoCache sCache = null;
80 private final Context mContext;
81 private final PhoneNumberService mPhoneNumberService;
82 // Cache info map needs to be thread-safe since it could be modified by both main thread and
83 // worker thread.
Eric Erfaniand5e47f62017-03-15 14:41:07 -070084 private final ConcurrentHashMap<String, ContactCacheEntry> mInfoMap = new ConcurrentHashMap<>();
Eric Erfanianccca3152017-02-22 16:32:36 -080085 private final Map<String, Set<ContactInfoCacheCallback>> mCallBacks = new ArrayMap<>();
86 private Drawable mDefaultContactPhotoDrawable;
87 private Drawable mConferencePhotoDrawable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070088 private int mQueryId;
Eric Erfaniand8046e52017-04-06 09:41:50 -070089 private final DialerExecutor<CnapInformationWrapper> cachedNumberLookupExecutor =
90 DialerExecutors.createNonUiTaskBuilder(new CachedNumberLookupWorker()).build();
91
92 private static class CachedNumberLookupWorker implements Worker<CnapInformationWrapper, Void> {
93 @Nullable
94 @Override
95 public Void doInBackground(@Nullable CnapInformationWrapper input) {
96 if (input == null) {
97 return null;
98 }
99 ContactInfo contactInfo = new ContactInfo();
100 CachedContactInfo cacheInfo = input.service.buildCachedContactInfo(contactInfo);
Eric Erfanian8369df02017-05-03 10:27:13 -0700101 cacheInfo.setSource(ContactSource.Type.SOURCE_TYPE_CNAP, "CNAP", 0);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700102 contactInfo.name = input.cnapName;
103 contactInfo.number = input.number;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700104 try {
105 final JSONObject contactRows =
106 new JSONObject()
107 .put(
108 Phone.CONTENT_ITEM_TYPE,
Eric Erfanian10b34a52017-05-04 08:23:17 -0700109 new JSONObject().put(Phone.NUMBER, contactInfo.number));
Eric Erfaniand8046e52017-04-06 09:41:50 -0700110 final String jsonString =
111 new JSONObject()
112 .put(Contacts.DISPLAY_NAME, contactInfo.name)
113 .put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.STRUCTURED_NAME)
114 .put(Contacts.CONTENT_ITEM_TYPE, contactRows)
115 .toString();
116 cacheInfo.setLookupKey(jsonString);
117 } catch (JSONException e) {
118 Log.w(TAG, "Creation of lookup key failed when caching CNAP information");
119 }
120 input.service.addContact(input.context.getApplicationContext(), cacheInfo);
121 return null;
122 }
123 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800124
125 private ContactInfoCache(Context context) {
126 mContext = context;
127 mPhoneNumberService = Bindings.get(context).newPhoneNumberService(context);
128 }
129
130 public static synchronized ContactInfoCache getInstance(Context mContext) {
131 if (sCache == null) {
132 sCache = new ContactInfoCache(mContext.getApplicationContext());
133 }
134 return sCache;
135 }
136
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700137 static ContactCacheEntry buildCacheEntryFromCall(
Eric Erfanianccca3152017-02-22 16:32:36 -0800138 Context context, DialerCall call, boolean isIncoming) {
139 final ContactCacheEntry entry = new ContactCacheEntry();
140
141 // TODO: get rid of caller info.
142 final CallerInfo info = CallerInfoUtils.buildCallerInfo(context, call);
Eric Erfanian8369df02017-05-03 10:27:13 -0700143 ContactInfoCache.populateCacheEntry(context, info, entry, call.getNumberPresentation());
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 return entry;
145 }
146
147 /** Populate a cache entry from a call (which got converted into a caller info). */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700148 private static void populateCacheEntry(
Eric Erfanianccca3152017-02-22 16:32:36 -0800149 @NonNull Context context,
150 @NonNull CallerInfo info,
151 @NonNull ContactCacheEntry cce,
Eric Erfanian8369df02017-05-03 10:27:13 -0700152 int presentation) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 Objects.requireNonNull(info);
154 String displayName = null;
155 String displayNumber = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800156 String label = null;
157 boolean isSipCall = false;
158
159 // It appears that there is a small change in behaviour with the
160 // PhoneUtils' startGetCallerInfo whereby if we query with an
161 // empty number, we will get a valid CallerInfo object, but with
162 // fields that are all null, and the isTemporary boolean input
163 // parameter as true.
164
165 // In the past, we would see a NULL callerinfo object, but this
166 // ends up causing null pointer exceptions elsewhere down the
167 // line in other cases, so we need to make this fix instead. It
168 // appears that this was the ONLY call to PhoneUtils
169 // .getCallerInfo() that relied on a NULL CallerInfo to indicate
170 // an unknown contact.
171
172 // Currently, info.phoneNumber may actually be a SIP address, and
173 // if so, it might sometimes include the "sip:" prefix. That
174 // prefix isn't really useful to the user, though, so strip it off
175 // if present. (For any other URI scheme, though, leave the
176 // prefix alone.)
177 // TODO: It would be cleaner for CallerInfo to explicitly support
178 // SIP addresses instead of overloading the "phoneNumber" field.
179 // Then we could remove this hack, and instead ask the CallerInfo
180 // for a "user visible" form of the SIP address.
181 String number = info.phoneNumber;
182
183 if (!TextUtils.isEmpty(number)) {
184 isSipCall = PhoneNumberHelper.isUriNumber(number);
185 if (number.startsWith("sip:")) {
186 number = number.substring(4);
187 }
188 }
189
190 if (TextUtils.isEmpty(info.name)) {
191 // No valid "name" in the CallerInfo, so fall back to
192 // something else.
193 // (Typically, we promote the phone number up to the "name" slot
194 // onscreen, and possibly display a descriptive string in the
195 // "number" slot.)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700196 if (TextUtils.isEmpty(number) && TextUtils.isEmpty(info.cnapName)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800197 // No name *or* number! Display a generic "unknown" string
198 // (or potentially some other default based on the presentation.)
199 displayName = getPresentationString(context, presentation, info.callSubject);
200 Log.d(TAG, " ==> no name *or* number! displayName = " + displayName);
201 } else if (presentation != TelecomManager.PRESENTATION_ALLOWED) {
202 // This case should never happen since the network should never send a phone #
203 // AND a restricted presentation. However we leave it here in case of weird
204 // network behavior
205 displayName = getPresentationString(context, presentation, info.callSubject);
206 Log.d(TAG, " ==> presentation not allowed! displayName = " + displayName);
207 } else if (!TextUtils.isEmpty(info.cnapName)) {
208 // No name, but we do have a valid CNAP name, so use that.
209 displayName = info.cnapName;
210 info.name = info.cnapName;
211 displayNumber = PhoneNumberHelper.formatNumber(number, context);
212 Log.d(
213 TAG,
214 " ==> cnapName available: displayName '"
215 + displayName
216 + "', displayNumber '"
217 + displayNumber
218 + "'");
219 } else {
220 // No name; all we have is a number. This is the typical
221 // case when an incoming call doesn't match any contact,
222 // or if you manually dial an outgoing number using the
223 // dialpad.
224 displayNumber = PhoneNumberHelper.formatNumber(number, context);
225
Eric Erfanianccca3152017-02-22 16:32:36 -0800226 Log.d(
227 TAG,
228 " ==> no name; falling back to number:"
229 + " displayNumber '"
230 + Log.pii(displayNumber)
Eric Erfanianccca3152017-02-22 16:32:36 -0800231 + "'");
232 }
233 } else {
234 // We do have a valid "name" in the CallerInfo. Display that
235 // in the "name" slot, and the phone number in the "number" slot.
236 if (presentation != TelecomManager.PRESENTATION_ALLOWED) {
237 // This case should never happen since the network should never send a name
238 // AND a restricted presentation. However we leave it here in case of weird
239 // network behavior
240 displayName = getPresentationString(context, presentation, info.callSubject);
241 Log.d(
242 TAG,
243 " ==> valid name, but presentation not allowed!" + " displayName = " + displayName);
244 } else {
245 // Causes cce.namePrimary to be set as info.name below. CallCardPresenter will
246 // later determine whether to use the name or nameAlternative when presenting
247 displayName = info.name;
248 cce.nameAlternative = info.nameAlternative;
249 displayNumber = PhoneNumberHelper.formatNumber(number, context);
250 label = info.phoneLabel;
251 Log.d(
252 TAG,
253 " ==> name is present in CallerInfo: displayName '"
254 + displayName
255 + "', displayNumber '"
256 + displayNumber
257 + "'");
258 }
259 }
260
261 cce.namePrimary = displayName;
262 cce.number = displayNumber;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700263 cce.location = info.geoDescription;
Eric Erfanianccca3152017-02-22 16:32:36 -0800264 cce.label = label;
265 cce.isSipCall = isSipCall;
266 cce.userType = info.userType;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700267 cce.originalPhoneNumber = info.phoneNumber;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700268 cce.shouldShowLocation = info.shouldShowGeoDescription;
Eric Erfanianccca3152017-02-22 16:32:36 -0800269
270 if (info.contactExists) {
271 cce.contactLookupResult = ContactLookupResult.Type.LOCAL_CONTACT;
272 }
273 }
274
275 /** Gets name strings based on some special presentation modes and the associated custom label. */
276 private static String getPresentationString(
277 Context context, int presentation, String customLabel) {
278 String name = context.getString(R.string.unknown);
279 if (!TextUtils.isEmpty(customLabel)
280 && ((presentation == TelecomManager.PRESENTATION_UNKNOWN)
281 || (presentation == TelecomManager.PRESENTATION_RESTRICTED))) {
282 name = customLabel;
283 return name;
284 } else {
285 if (presentation == TelecomManager.PRESENTATION_RESTRICTED) {
286 name = PhoneNumberHelper.getDisplayNameForRestrictedNumber(context).toString();
287 } else if (presentation == TelecomManager.PRESENTATION_PAYPHONE) {
288 name = context.getString(R.string.payphone);
289 }
290 }
291 return name;
292 }
293
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700294 ContactCacheEntry getInfo(String callId) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800295 return mInfoMap.get(callId);
296 }
297
Eric Erfaniand8046e52017-04-06 09:41:50 -0700298 private static final class CnapInformationWrapper {
299 final String number;
300 final String cnapName;
301 final Context context;
302 final CachedNumberLookupService service;
303
304 CnapInformationWrapper(
305 String number, String cnapName, Context context, CachedNumberLookupService service) {
306 this.number = number;
307 this.cnapName = cnapName;
308 this.context = context;
309 this.service = service;
310 }
311 }
312
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700313 void maybeInsertCnapInformationIntoCache(
Eric Erfanianccca3152017-02-22 16:32:36 -0800314 Context context, final DialerCall call, final CallerInfo info) {
315 final CachedNumberLookupService cachedNumberLookupService =
316 PhoneNumberCache.get(context).getCachedNumberLookupService();
317 if (!UserManagerCompat.isUserUnlocked(context)) {
318 Log.i(TAG, "User locked, not inserting cnap info into cache");
319 return;
320 }
321 if (cachedNumberLookupService == null
322 || TextUtils.isEmpty(info.cnapName)
323 || mInfoMap.get(call.getId()) != null) {
324 return;
325 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 Log.i(TAG, "Found contact with CNAP name - inserting into cache");
Eric Erfaniand8046e52017-04-06 09:41:50 -0700327
328 cachedNumberLookupExecutor.executeParallel(
329 new CnapInformationWrapper(
330 call.getNumber(), info.cnapName, context, cachedNumberLookupService));
Eric Erfanianccca3152017-02-22 16:32:36 -0800331 }
332
333 /**
334 * Requests contact data for the DialerCall object passed in. Returns the data through callback.
335 * If callback is null, no response is made, however the query is still performed and cached.
336 *
337 * @param callback The function to call back when the call is found. Can be null.
338 */
339 @MainThread
340 public void findInfo(
341 @NonNull final DialerCall call,
342 final boolean isIncoming,
343 @NonNull ContactInfoCacheCallback callback) {
344 Assert.isMainThread();
345 Objects.requireNonNull(callback);
346
347 final String callId = call.getId();
348 final ContactCacheEntry cacheEntry = mInfoMap.get(callId);
349 Set<ContactInfoCacheCallback> callBacks = mCallBacks.get(callId);
350
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700351 // We need to force a new query if phone number has changed.
352 boolean forceQuery = needForceQuery(call, cacheEntry);
353 Log.d(TAG, "findInfo: callId = " + callId + "; forceQuery = " + forceQuery);
354
355 // If we have a previously obtained intermediate result return that now except needs
356 // force query.
357 if (cacheEntry != null && !forceQuery) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800358 Log.d(
359 TAG,
360 "Contact lookup. In memory cache hit; lookup "
361 + (callBacks == null ? "complete" : "still running"));
362 callback.onContactInfoComplete(callId, cacheEntry);
363 // If no other callbacks are in flight, we're done.
364 if (callBacks == null) {
365 return;
366 }
367 }
368
369 // If the entry already exists, add callback
370 if (callBacks != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700371 Log.d(TAG, "Another query is in progress, add callback only.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800372 callBacks.add(callback);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700373 if (!forceQuery) {
374 Log.d(TAG, "No need to query again, just return and wait for existing query to finish");
375 return;
376 }
377 } else {
378 Log.d(TAG, "Contact lookup. In memory cache miss; searching provider.");
379 // New lookup
380 callBacks = new ArraySet<>();
381 callBacks.add(callback);
382 mCallBacks.put(callId, callBacks);
Eric Erfanianccca3152017-02-22 16:32:36 -0800383 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800384
385 /**
386 * Performs a query for caller information. Save any immediate data we get from the query. An
387 * asynchronous query may also be made for any data that we do not already have. Some queries,
388 * such as those for voicemail and emergency call information, will not perform an additional
389 * asynchronous query.
390 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700391 final CallerInfoQueryToken queryToken = new CallerInfoQueryToken(mQueryId, callId);
392 mQueryId++;
Eric Erfanianccca3152017-02-22 16:32:36 -0800393 final CallerInfo callerInfo =
394 CallerInfoUtils.getCallerInfoForCall(
395 mContext,
396 call,
Eric Erfaniand8046e52017-04-06 09:41:50 -0700397 new DialerCallCookieWrapper(callId, call.getNumberPresentation(), call.getCnapName()),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700398 new FindInfoCallback(isIncoming, queryToken));
Eric Erfanianccca3152017-02-22 16:32:36 -0800399
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700400 if (cacheEntry != null) {
401 // We should not override the old cache item until the new query is
402 // back. We should only update the queryId. Otherwise, we may see
403 // flicker of the name and image (old cache -> new cache before query
404 // -> new cache after query)
405 cacheEntry.queryId = queryToken.mQueryId;
406 Log.d(TAG, "There is an existing cache. Do not override until new query is back");
407 } else {
408 ContactCacheEntry initialCacheEntry =
409 updateCallerInfoInCacheOnAnyThread(
410 callId, call.getNumberPresentation(), callerInfo, isIncoming, false, queryToken);
411 sendInfoNotifications(callId, initialCacheEntry);
412 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 }
414
415 @AnyThread
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700416 private ContactCacheEntry updateCallerInfoInCacheOnAnyThread(
Eric Erfanianccca3152017-02-22 16:32:36 -0800417 String callId,
418 int numberPresentation,
419 CallerInfo callerInfo,
420 boolean isIncoming,
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700421 boolean didLocalLookup,
422 CallerInfoQueryToken queryToken) {
423 Log.d(
424 TAG,
425 "updateCallerInfoInCacheOnAnyThread: callId = "
426 + callId
427 + "; queryId = "
428 + queryToken.mQueryId
429 + "; didLocalLookup = "
430 + didLocalLookup);
431
Eric Erfanianccca3152017-02-22 16:32:36 -0800432 int presentationMode = numberPresentation;
433 if (callerInfo.contactExists
434 || callerInfo.isEmergencyNumber()
435 || callerInfo.isVoiceMailNumber()) {
436 presentationMode = TelecomManager.PRESENTATION_ALLOWED;
437 }
438
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700439 // We always replace the entry. The only exception is the same photo case.
Eric Erfanian8369df02017-05-03 10:27:13 -0700440 ContactCacheEntry cacheEntry = buildEntry(mContext, callerInfo, presentationMode);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700441 cacheEntry.queryId = queryToken.mQueryId;
442
443 ContactCacheEntry existingCacheEntry = mInfoMap.get(callId);
444 Log.d(TAG, "Existing cacheEntry in hashMap " + existingCacheEntry);
445
446 if (didLocalLookup) {
447 // Before issuing a request for more data from other services, we only check that the
448 // contact wasn't found in the local DB. We don't check the if the cache entry already
449 // has a name because we allow overriding cnap data with data from other services.
450 if (!callerInfo.contactExists && mPhoneNumberService != null) {
451 Log.d(TAG, "Contact lookup. Local contacts miss, checking remote");
452 final PhoneNumberServiceListener listener =
453 new PhoneNumberServiceListener(callId, queryToken.mQueryId);
454 cacheEntry.hasPendingQuery = true;
455 mPhoneNumberService.getPhoneNumberInfo(cacheEntry.number, listener, listener, isIncoming);
456 } else if (cacheEntry.displayPhotoUri != null) {
457 // When the difference between 2 numbers is only the prefix (e.g. + or IDD),
458 // we will still trigger force query so that the number can be updated on
459 // the calling screen. We need not query the image again if the previous
460 // query already has the image to avoid flickering.
461 if (existingCacheEntry != null
462 && existingCacheEntry.displayPhotoUri != null
463 && existingCacheEntry.displayPhotoUri.equals(cacheEntry.displayPhotoUri)
464 && existingCacheEntry.photo != null) {
465 Log.d(TAG, "Same picture. Do not need start image load.");
466 cacheEntry.photo = existingCacheEntry.photo;
467 cacheEntry.photoType = existingCacheEntry.photoType;
468 return cacheEntry;
Eric Erfanianccca3152017-02-22 16:32:36 -0800469 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700470
471 Log.d(TAG, "Contact lookup. Local contact found, starting image load");
472 // Load the image with a callback to update the image state.
473 // When the load is finished, onImageLoadComplete() will be called.
474 cacheEntry.hasPendingQuery = true;
475 ContactsAsyncHelper.startObtainPhotoAsync(
476 TOKEN_UPDATE_PHOTO_FOR_CALL_STATE,
477 mContext,
478 cacheEntry.displayPhotoUri,
479 ContactInfoCache.this,
480 queryToken);
Eric Erfanianccca3152017-02-22 16:32:36 -0800481 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700482 Log.d(TAG, "put entry into map: " + cacheEntry);
483 mInfoMap.put(callId, cacheEntry);
484 } else {
485 // Don't overwrite if there is existing cache.
486 Log.d(TAG, "put entry into map if not exists: " + cacheEntry);
487 mInfoMap.putIfAbsent(callId, cacheEntry);
Eric Erfanianccca3152017-02-22 16:32:36 -0800488 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700489 return cacheEntry;
Eric Erfanianccca3152017-02-22 16:32:36 -0800490 }
491
Eric Erfaniand8046e52017-04-06 09:41:50 -0700492 private void maybeUpdateFromCequintCallerId(
493 CallerInfo callerInfo, String cnapName, boolean isIncoming) {
Eric Erfanian9779f962017-03-27 12:31:48 -0700494 if (!CequintCallerIdManager.isCequintCallerIdEnabled(mContext)) {
495 return;
496 }
Eric Erfaniand8046e52017-04-06 09:41:50 -0700497 if (callerInfo.phoneNumber == null) {
498 return;
499 }
Eric Erfanian9779f962017-03-27 12:31:48 -0700500 CequintCallerIdContact cequintCallerIdContact =
501 CequintCallerIdManager.getCequintCallerIdContactForInCall(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700502 mContext, callerInfo.phoneNumber, cnapName, isIncoming);
Eric Erfanian9779f962017-03-27 12:31:48 -0700503
Eric Erfaniand8046e52017-04-06 09:41:50 -0700504 if (cequintCallerIdContact == null) {
505 return;
506 }
Eric Erfanian8369df02017-05-03 10:27:13 -0700507 boolean hasUpdate = false;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700508
509 if (TextUtils.isEmpty(callerInfo.name) && !TextUtils.isEmpty(cequintCallerIdContact.name)) {
Eric Erfanian9779f962017-03-27 12:31:48 -0700510 callerInfo.name = cequintCallerIdContact.name;
Eric Erfanian8369df02017-05-03 10:27:13 -0700511 hasUpdate = true;
Eric Erfanian9779f962017-03-27 12:31:48 -0700512 }
513 if (!TextUtils.isEmpty(cequintCallerIdContact.geoDescription)) {
514 callerInfo.geoDescription = cequintCallerIdContact.geoDescription;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700515 callerInfo.shouldShowGeoDescription = true;
Eric Erfanian8369df02017-05-03 10:27:13 -0700516 hasUpdate = true;
Eric Erfanian9779f962017-03-27 12:31:48 -0700517 }
Eric Erfanian8369df02017-05-03 10:27:13 -0700518 // Don't overwrite photo in local contacts.
519 if (!callerInfo.contactExists
520 && callerInfo.contactDisplayPhotoUri == null
521 && cequintCallerIdContact.imageUrl != null) {
Eric Erfanian9779f962017-03-27 12:31:48 -0700522 callerInfo.contactDisplayPhotoUri = Uri.parse(cequintCallerIdContact.imageUrl);
Eric Erfanian8369df02017-05-03 10:27:13 -0700523 hasUpdate = true;
Eric Erfanian9779f962017-03-27 12:31:48 -0700524 }
Eric Erfanian8369df02017-05-03 10:27:13 -0700525 // Set contact to exist to avoid phone number service lookup.
526 callerInfo.contactExists = hasUpdate;
Eric Erfanian9779f962017-03-27 12:31:48 -0700527 }
528
Eric Erfanianccca3152017-02-22 16:32:36 -0800529 /**
530 * Implemented for ContactsAsyncHelper.OnImageLoadCompleteListener interface. Update contact photo
531 * when image is loaded in worker thread.
532 */
533 @WorkerThread
534 @Override
535 public void onImageLoaded(int token, Drawable photo, Bitmap photoIcon, Object cookie) {
536 Assert.isWorkerThread();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700537 CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie;
538 final String callId = myCookie.mCallId;
539 final int queryId = myCookie.mQueryId;
540 if (!isWaitingForThisQuery(callId, queryId)) {
541 return;
542 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800543 loadImage(photo, photoIcon, cookie);
544 }
545
546 private void loadImage(Drawable photo, Bitmap photoIcon, Object cookie) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700547 Log.d(TAG, "Image load complete with context: ", mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800548 // TODO: may be nice to update the image view again once the newer one
549 // is available on contacts database.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700550 CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie;
551 final String callId = myCookie.mCallId;
Eric Erfanianccca3152017-02-22 16:32:36 -0800552 ContactCacheEntry entry = mInfoMap.get(callId);
553
554 if (entry == null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700555 Log.e(TAG, "Image Load received for empty search entry.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800556 clearCallbacks(callId);
557 return;
558 }
559
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700560 Log.d(TAG, "setting photo for entry: ", entry);
Eric Erfanianccca3152017-02-22 16:32:36 -0800561
562 // Conference call icons are being handled in CallCardPresenter.
563 if (photo != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700564 Log.v(TAG, "direct drawable: ", photo);
Eric Erfanianccca3152017-02-22 16:32:36 -0800565 entry.photo = photo;
566 entry.photoType = ContactPhotoType.CONTACT;
567 } else if (photoIcon != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700568 Log.v(TAG, "photo icon: ", photoIcon);
Eric Erfanianccca3152017-02-22 16:32:36 -0800569 entry.photo = new BitmapDrawable(mContext.getResources(), photoIcon);
570 entry.photoType = ContactPhotoType.CONTACT;
571 } else {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700572 Log.v(TAG, "unknown photo");
Eric Erfanianccca3152017-02-22 16:32:36 -0800573 entry.photo = null;
574 entry.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER;
575 }
576 }
577
578 /**
579 * Implemented for ContactsAsyncHelper.OnImageLoadCompleteListener interface. make sure that the
580 * call state is reflected after the image is loaded.
581 */
582 @MainThread
583 @Override
584 public void onImageLoadComplete(int token, Drawable photo, Bitmap photoIcon, Object cookie) {
585 Assert.isMainThread();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700586 CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie;
587 final String callId = myCookie.mCallId;
588 final int queryId = myCookie.mQueryId;
589 if (!isWaitingForThisQuery(callId, queryId)) {
590 return;
591 }
592 sendImageNotifications(callId, mInfoMap.get(callId));
Eric Erfanianccca3152017-02-22 16:32:36 -0800593
594 clearCallbacks(callId);
595 }
596
597 /** Blows away the stored cache values. */
598 public void clearCache() {
599 mInfoMap.clear();
600 mCallBacks.clear();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700601 mQueryId = 0;
Eric Erfanianccca3152017-02-22 16:32:36 -0800602 }
603
Eric Erfanian8369df02017-05-03 10:27:13 -0700604 private ContactCacheEntry buildEntry(Context context, CallerInfo info, int presentation) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 final ContactCacheEntry cce = new ContactCacheEntry();
Eric Erfanian8369df02017-05-03 10:27:13 -0700606 populateCacheEntry(context, info, cce, presentation);
Eric Erfanianccca3152017-02-22 16:32:36 -0800607
608 // This will only be true for emergency numbers
609 if (info.photoResource != 0) {
610 cce.photo = context.getResources().getDrawable(info.photoResource);
611 } else if (info.isCachedPhotoCurrent) {
612 if (info.cachedPhoto != null) {
613 cce.photo = info.cachedPhoto;
614 cce.photoType = ContactPhotoType.CONTACT;
615 } else {
616 cce.photo = getDefaultContactPhotoDrawable();
617 cce.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER;
618 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800619 } else {
620 cce.displayPhotoUri = info.contactDisplayPhotoUri;
621 cce.photo = null;
622 }
623
624 // Support any contact id in N because QuickContacts in N starts supporting enterprise
625 // contact id
626 if (info.lookupKeyOrNull != null
627 && (VERSION.SDK_INT >= VERSION_CODES.N || info.contactIdOrZero != 0)) {
628 cce.lookupUri = Contacts.getLookupUri(info.contactIdOrZero, info.lookupKeyOrNull);
629 } else {
630 Log.v(TAG, "lookup key is null or contact ID is 0 on M. Don't create a lookup uri.");
631 cce.lookupUri = null;
632 }
633
634 cce.lookupKey = info.lookupKeyOrNull;
635 cce.contactRingtoneUri = info.contactRingtoneUri;
636 if (cce.contactRingtoneUri == null || Uri.EMPTY.equals(cce.contactRingtoneUri)) {
637 cce.contactRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
638 }
639
640 return cce;
641 }
642
643 /** Sends the updated information to call the callbacks for the entry. */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700644 @MainThread
Eric Erfanianccca3152017-02-22 16:32:36 -0800645 private void sendInfoNotifications(String callId, ContactCacheEntry entry) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700646 Assert.isMainThread();
Eric Erfanianccca3152017-02-22 16:32:36 -0800647 final Set<ContactInfoCacheCallback> callBacks = mCallBacks.get(callId);
648 if (callBacks != null) {
649 for (ContactInfoCacheCallback callBack : callBacks) {
650 callBack.onContactInfoComplete(callId, entry);
651 }
652 }
653 }
654
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700655 @MainThread
Eric Erfanianccca3152017-02-22 16:32:36 -0800656 private void sendImageNotifications(String callId, ContactCacheEntry entry) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700657 Assert.isMainThread();
Eric Erfanianccca3152017-02-22 16:32:36 -0800658 final Set<ContactInfoCacheCallback> callBacks = mCallBacks.get(callId);
659 if (callBacks != null && entry.photo != null) {
660 for (ContactInfoCacheCallback callBack : callBacks) {
661 callBack.onImageLoadComplete(callId, entry);
662 }
663 }
664 }
665
666 private void clearCallbacks(String callId) {
667 mCallBacks.remove(callId);
668 }
669
670 public Drawable getDefaultContactPhotoDrawable() {
671 if (mDefaultContactPhotoDrawable == null) {
672 mDefaultContactPhotoDrawable =
673 mContext.getResources().getDrawable(R.drawable.img_no_image_automirrored);
674 }
675 return mDefaultContactPhotoDrawable;
676 }
677
678 public Drawable getConferenceDrawable() {
679 if (mConferencePhotoDrawable == null) {
680 mConferencePhotoDrawable =
681 mContext.getResources().getDrawable(R.drawable.img_conference_automirrored);
682 }
683 return mConferencePhotoDrawable;
684 }
685
686 /** Callback interface for the contact query. */
687 public interface ContactInfoCacheCallback {
688
689 void onContactInfoComplete(String callId, ContactCacheEntry entry);
690
691 void onImageLoadComplete(String callId, ContactCacheEntry entry);
692 }
693
694 /** This is cached contact info, which should be the ONLY info used by UI. */
695 public static class ContactCacheEntry {
696
697 public String namePrimary;
698 public String nameAlternative;
699 public String number;
700 public String location;
701 public String label;
702 public Drawable photo;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700703 @ContactPhotoType int photoType;
704 boolean isSipCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 // Note in cache entry whether this is a pending async loading action to know whether to
706 // wait for its callback or not.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700707 boolean hasPendingQuery;
Eric Erfanianccca3152017-02-22 16:32:36 -0800708 /** This will be used for the "view" notification. */
709 public Uri contactUri;
710 /** Either a display photo or a thumbnail URI. */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700711 Uri displayPhotoUri;
Eric Erfanianccca3152017-02-22 16:32:36 -0800712
713 public Uri lookupUri; // Sent to NotificationMananger
714 public String lookupKey;
Eric Erfanian8369df02017-05-03 10:27:13 -0700715 public ContactLookupResult.Type contactLookupResult = ContactLookupResult.Type.NOT_FOUND;
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 public long userType = ContactsUtils.USER_TYPE_CURRENT;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700717 Uri contactRingtoneUri;
718 /** Query id to identify the query session. */
719 int queryId;
720 /** The phone number without any changes to display to the user (ex: cnap...) */
721 String originalPhoneNumber;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700722 boolean shouldShowLocation;
Eric Erfanian9779f962017-03-27 12:31:48 -0700723
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700724 boolean isBusiness;
Eric Erfanianccca3152017-02-22 16:32:36 -0800725
726 @Override
727 public String toString() {
728 return "ContactCacheEntry{"
729 + "name='"
730 + MoreStrings.toSafeString(namePrimary)
731 + '\''
732 + ", nameAlternative='"
733 + MoreStrings.toSafeString(nameAlternative)
734 + '\''
735 + ", number='"
736 + MoreStrings.toSafeString(number)
737 + '\''
738 + ", location='"
739 + MoreStrings.toSafeString(location)
740 + '\''
741 + ", label='"
742 + label
743 + '\''
744 + ", photo="
745 + photo
746 + ", isSipCall="
747 + isSipCall
748 + ", contactUri="
749 + contactUri
750 + ", displayPhotoUri="
751 + displayPhotoUri
752 + ", contactLookupResult="
753 + contactLookupResult
754 + ", userType="
755 + userType
756 + ", contactRingtoneUri="
757 + contactRingtoneUri
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700758 + ", queryId="
759 + queryId
760 + ", originalPhoneNumber="
761 + originalPhoneNumber
Eric Erfaniand8046e52017-04-06 09:41:50 -0700762 + ", shouldShowLocation="
763 + shouldShowLocation
Eric Erfanianccca3152017-02-22 16:32:36 -0800764 + '}';
765 }
766 }
767
768 private static final class DialerCallCookieWrapper {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700769 final String callId;
770 final int numberPresentation;
771 final String cnapName;
Eric Erfanianccca3152017-02-22 16:32:36 -0800772
Eric Erfaniand8046e52017-04-06 09:41:50 -0700773 DialerCallCookieWrapper(String callId, int numberPresentation, String cnapName) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 this.callId = callId;
775 this.numberPresentation = numberPresentation;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700776 this.cnapName = cnapName;
Eric Erfanianccca3152017-02-22 16:32:36 -0800777 }
778 }
779
780 private class FindInfoCallback implements OnQueryCompleteListener {
781
782 private final boolean mIsIncoming;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700783 private final CallerInfoQueryToken mQueryToken;
Eric Erfanianccca3152017-02-22 16:32:36 -0800784
Eric Erfaniand8046e52017-04-06 09:41:50 -0700785 FindInfoCallback(boolean isIncoming, CallerInfoQueryToken queryToken) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800786 mIsIncoming = isIncoming;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700787 mQueryToken = queryToken;
Eric Erfanianccca3152017-02-22 16:32:36 -0800788 }
789
790 @Override
791 public void onDataLoaded(int token, Object cookie, CallerInfo ci) {
792 Assert.isWorkerThread();
793 DialerCallCookieWrapper cw = (DialerCallCookieWrapper) cookie;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700794 if (!isWaitingForThisQuery(cw.callId, mQueryToken.mQueryId)) {
795 return;
796 }
Eric Erfanian9779f962017-03-27 12:31:48 -0700797 long start = SystemClock.uptimeMillis();
Eric Erfaniand8046e52017-04-06 09:41:50 -0700798 maybeUpdateFromCequintCallerId(ci, cw.cnapName, mIsIncoming);
Eric Erfanian9779f962017-03-27 12:31:48 -0700799 long time = SystemClock.uptimeMillis() - start;
800 Log.d(TAG, "Cequint Caller Id look up takes " + time + " ms.");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700801 updateCallerInfoInCacheOnAnyThread(
802 cw.callId, cw.numberPresentation, ci, mIsIncoming, true, mQueryToken);
Eric Erfanianccca3152017-02-22 16:32:36 -0800803 }
804
805 @Override
806 public void onQueryComplete(int token, Object cookie, CallerInfo callerInfo) {
807 Assert.isMainThread();
808 DialerCallCookieWrapper cw = (DialerCallCookieWrapper) cookie;
809 String callId = cw.callId;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700810 if (!isWaitingForThisQuery(cw.callId, mQueryToken.mQueryId)) {
811 return;
812 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800813 ContactCacheEntry cacheEntry = mInfoMap.get(callId);
814 // This may happen only when InCallPresenter attempt to cleanup.
815 if (cacheEntry == null) {
816 Log.w(TAG, "Contact lookup done, but cache entry is not found.");
817 clearCallbacks(callId);
818 return;
819 }
820 sendInfoNotifications(callId, cacheEntry);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700821 if (!cacheEntry.hasPendingQuery) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 if (callerInfo.contactExists) {
823 Log.d(TAG, "Contact lookup done. Local contact found, no image.");
824 } else {
825 Log.d(
826 TAG,
827 "Contact lookup done. Local contact not found and"
828 + " no remote lookup service available.");
829 }
830 clearCallbacks(callId);
831 }
832 }
833 }
834
835 class PhoneNumberServiceListener
836 implements PhoneNumberService.NumberLookupListener, PhoneNumberService.ImageLookupListener {
837
838 private final String mCallId;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700839 private final int mQueryIdOfRemoteLookup;
Eric Erfanianccca3152017-02-22 16:32:36 -0800840
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700841 PhoneNumberServiceListener(String callId, int queryId) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800842 mCallId = callId;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700843 mQueryIdOfRemoteLookup = queryId;
Eric Erfanianccca3152017-02-22 16:32:36 -0800844 }
845
846 @Override
847 public void onPhoneNumberInfoComplete(final PhoneNumberService.PhoneNumberInfo info) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700848 Log.d(TAG, "PhoneNumberServiceListener.onPhoneNumberInfoComplete");
849 if (!isWaitingForThisQuery(mCallId, mQueryIdOfRemoteLookup)) {
850 return;
851 }
852
Eric Erfanianccca3152017-02-22 16:32:36 -0800853 // If we got a miss, this is the end of the lookup pipeline,
854 // so clear the callbacks and return.
855 if (info == null) {
856 Log.d(TAG, "Contact lookup done. Remote contact not found.");
857 clearCallbacks(mCallId);
858 return;
859 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 ContactCacheEntry entry = new ContactCacheEntry();
861 entry.namePrimary = info.getDisplayName();
862 entry.number = info.getNumber();
863 entry.contactLookupResult = info.getLookupSource();
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700864 entry.isBusiness = info.isBusiness();
Eric Erfanianccca3152017-02-22 16:32:36 -0800865 final int type = info.getPhoneType();
866 final String label = info.getPhoneLabel();
867 if (type == Phone.TYPE_CUSTOM) {
868 entry.label = label;
869 } else {
870 final CharSequence typeStr = Phone.getTypeLabel(mContext.getResources(), type, label);
871 entry.label = typeStr == null ? null : typeStr.toString();
872 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700873 final ContactCacheEntry oldEntry = mInfoMap.get(mCallId);
874 if (oldEntry != null) {
875 // Location is only obtained from local lookup so persist
876 // the value for remote lookups. Once we have a name this
877 // field is no longer used; it is persisted here in case
878 // the UI is ever changed to use it.
879 entry.location = oldEntry.location;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700880 entry.shouldShowLocation = oldEntry.shouldShowLocation;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700881 // Contact specific ringtone is obtained from local lookup.
882 entry.contactRingtoneUri = oldEntry.contactRingtoneUri;
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700884
885 // If no image and it's a business, switch to using the default business avatar.
886 if (info.getImageUrl() == null && info.isBusiness()) {
887 Log.d(TAG, "Business has no image. Using default.");
888 entry.photo = mContext.getResources().getDrawable(R.drawable.img_business);
889 entry.photoType = ContactPhotoType.BUSINESS;
890 }
891
892 Log.d(TAG, "put entry into map: " + entry);
893 mInfoMap.put(mCallId, entry);
Eric Erfanianccca3152017-02-22 16:32:36 -0800894 sendInfoNotifications(mCallId, entry);
895
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700896 entry.hasPendingQuery = info.getImageUrl() != null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800897
898 // If there is no image then we should not expect another callback.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700899 if (!entry.hasPendingQuery) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800900 // We're done, so clear callbacks
901 clearCallbacks(mCallId);
902 }
903 }
904
905 @Override
906 public void onImageFetchComplete(Bitmap bitmap) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700907 Log.d(TAG, "PhoneNumberServiceListener.onImageFetchComplete");
908 if (!isWaitingForThisQuery(mCallId, mQueryIdOfRemoteLookup)) {
909 return;
910 }
911 CallerInfoQueryToken queryToken = new CallerInfoQueryToken(mQueryIdOfRemoteLookup, mCallId);
912 loadImage(null, bitmap, queryToken);
913 onImageLoadComplete(TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, null, bitmap, queryToken);
914 }
915 }
916
917 private boolean needForceQuery(DialerCall call, ContactCacheEntry cacheEntry) {
918 if (call == null || call.isConferenceCall()) {
919 return false;
920 }
921
922 String newPhoneNumber = PhoneNumberUtils.stripSeparators(call.getNumber());
923 if (cacheEntry == null) {
924 // No info in the map yet so it is the 1st query
925 Log.d(TAG, "needForceQuery: first query");
926 return true;
927 }
928 String oldPhoneNumber = PhoneNumberUtils.stripSeparators(cacheEntry.originalPhoneNumber);
929
930 if (!TextUtils.equals(oldPhoneNumber, newPhoneNumber)) {
931 Log.d(TAG, "phone number has changed: " + oldPhoneNumber + " -> " + newPhoneNumber);
932 return true;
933 }
934
935 return false;
936 }
937
938 private static final class CallerInfoQueryToken {
939 final int mQueryId;
940 final String mCallId;
941
942 CallerInfoQueryToken(int queryId, String callId) {
943 mQueryId = queryId;
944 mCallId = callId;
945 }
946 }
947
948 /** Check if the queryId in the cached map is the same as the one from query result. */
949 private boolean isWaitingForThisQuery(String callId, int queryId) {
950 final ContactCacheEntry existingCacheEntry = mInfoMap.get(callId);
951 if (existingCacheEntry == null) {
952 // This might happen if lookup on background thread comes back before the initial entry is
953 // created.
954 Log.d(TAG, "Cached entry is null.");
955 return true;
956 } else {
957 int waitingQueryId = existingCacheEntry.queryId;
958 Log.d(TAG, "waitingQueryId = " + waitingQueryId + "; queryId = " + queryId);
959 return waitingQueryId == queryId;
Eric Erfanianccca3152017-02-22 16:32:36 -0800960 }
961 }
962}