Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.incallui; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.drawable.BitmapDrawable; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.media.RingtoneManager; |
| 24 | import android.net.Uri; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 25 | import android.os.Build.VERSION; |
| 26 | import android.os.Build.VERSION_CODES; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 27 | import android.os.SystemClock; |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 28 | import android.os.Trace; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 29 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 30 | import android.provider.ContactsContract.Contacts; |
| 31 | import android.provider.ContactsContract.DisplayNameSources; |
| 32 | import android.support.annotation.AnyThread; |
| 33 | import android.support.annotation.MainThread; |
| 34 | import android.support.annotation.NonNull; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 35 | import android.support.annotation.Nullable; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 36 | import android.support.annotation.WorkerThread; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 37 | import android.support.v4.content.ContextCompat; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 38 | import android.support.v4.os.UserManagerCompat; |
| 39 | import android.telecom.TelecomManager; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 40 | import android.telephony.PhoneNumberUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
| 42 | import android.util.ArrayMap; |
| 43 | import android.util.ArraySet; |
| 44 | import com.android.contacts.common.ContactsUtils; |
| 45 | import com.android.dialer.common.Assert; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 46 | import com.android.dialer.common.concurrent.DialerExecutor; |
| 47 | import com.android.dialer.common.concurrent.DialerExecutor.Worker; |
zachh | 0cd36a6 | 2017-10-31 12:04:05 -0700 | [diff] [blame^] | 48 | import com.android.dialer.common.concurrent.DialerExecutorComponent; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 49 | import com.android.dialer.logging.ContactLookupResult; |
| 50 | import com.android.dialer.logging.ContactSource; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 51 | import com.android.dialer.oem.CequintCallerIdManager; |
| 52 | import com.android.dialer.oem.CequintCallerIdManager.CequintCallerIdContact; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 53 | import com.android.dialer.phonenumbercache.CachedNumberLookupService; |
| 54 | import com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo; |
| 55 | import com.android.dialer.phonenumbercache.ContactInfo; |
| 56 | import com.android.dialer.phonenumbercache.PhoneNumberCache; |
| 57 | import com.android.dialer.phonenumberutil.PhoneNumberHelper; |
| 58 | import com.android.dialer.util.MoreStrings; |
| 59 | import com.android.incallui.CallerInfoAsyncQuery.OnQueryCompleteListener; |
| 60 | import com.android.incallui.ContactsAsyncHelper.OnImageLoadCompleteListener; |
| 61 | import com.android.incallui.bindings.PhoneNumberService; |
| 62 | import com.android.incallui.call.DialerCall; |
| 63 | import com.android.incallui.incall.protocol.ContactPhotoType; |
| 64 | import java.util.Map; |
| 65 | import java.util.Objects; |
| 66 | import java.util.Set; |
| 67 | import java.util.concurrent.ConcurrentHashMap; |
| 68 | import org.json.JSONException; |
| 69 | import org.json.JSONObject; |
| 70 | |
| 71 | /** |
| 72 | * Class responsible for querying Contact Information for DialerCall objects. Can perform |
| 73 | * asynchronous requests to the Contact Provider for information as well as respond synchronously |
| 74 | * for any data that it currently has cached from previous queries. This class always gets called |
| 75 | * from the UI thread so it does not need thread protection. |
| 76 | */ |
| 77 | public class ContactInfoCache implements OnImageLoadCompleteListener { |
| 78 | |
| 79 | private static final String TAG = ContactInfoCache.class.getSimpleName(); |
| 80 | private static final int TOKEN_UPDATE_PHOTO_FOR_CALL_STATE = 0; |
| 81 | private static ContactInfoCache sCache = null; |
| 82 | private final Context mContext; |
| 83 | private final PhoneNumberService mPhoneNumberService; |
| 84 | // Cache info map needs to be thread-safe since it could be modified by both main thread and |
| 85 | // worker thread. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 86 | private final ConcurrentHashMap<String, ContactCacheEntry> mInfoMap = new ConcurrentHashMap<>(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 87 | private final Map<String, Set<ContactInfoCacheCallback>> mCallBacks = new ArrayMap<>(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 88 | private int mQueryId; |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 89 | private final DialerExecutor<CnapInformationWrapper> cachedNumberLookupExecutor; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 90 | |
| 91 | private static class CachedNumberLookupWorker implements Worker<CnapInformationWrapper, Void> { |
| 92 | @Nullable |
| 93 | @Override |
| 94 | public Void doInBackground(@Nullable CnapInformationWrapper input) { |
| 95 | if (input == null) { |
| 96 | return null; |
| 97 | } |
| 98 | ContactInfo contactInfo = new ContactInfo(); |
| 99 | CachedContactInfo cacheInfo = input.service.buildCachedContactInfo(contactInfo); |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 100 | cacheInfo.setSource(ContactSource.Type.SOURCE_TYPE_CNAP, "CNAP", 0); |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 101 | contactInfo.name = input.cnapName; |
| 102 | contactInfo.number = input.number; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 103 | try { |
| 104 | final JSONObject contactRows = |
| 105 | new JSONObject() |
| 106 | .put( |
| 107 | Phone.CONTENT_ITEM_TYPE, |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 108 | new JSONObject().put(Phone.NUMBER, contactInfo.number)); |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 109 | final String jsonString = |
| 110 | new JSONObject() |
| 111 | .put(Contacts.DISPLAY_NAME, contactInfo.name) |
| 112 | .put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.STRUCTURED_NAME) |
| 113 | .put(Contacts.CONTENT_ITEM_TYPE, contactRows) |
| 114 | .toString(); |
| 115 | cacheInfo.setLookupKey(jsonString); |
| 116 | } catch (JSONException e) { |
| 117 | Log.w(TAG, "Creation of lookup key failed when caching CNAP information"); |
| 118 | } |
| 119 | input.service.addContact(input.context.getApplicationContext(), cacheInfo); |
| 120 | return null; |
| 121 | } |
| 122 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 123 | |
| 124 | private ContactInfoCache(Context context) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 125 | Trace.beginSection("ContactInfoCache constructor"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 126 | mContext = context; |
| 127 | mPhoneNumberService = Bindings.get(context).newPhoneNumberService(context); |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 128 | cachedNumberLookupExecutor = |
zachh | 0cd36a6 | 2017-10-31 12:04:05 -0700 | [diff] [blame^] | 129 | DialerExecutorComponent.get(mContext) |
| 130 | .dialerExecutorFactory() |
| 131 | .createNonUiTaskBuilder(new CachedNumberLookupWorker()) |
| 132 | .build(); |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 133 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public static synchronized ContactInfoCache getInstance(Context mContext) { |
| 137 | if (sCache == null) { |
| 138 | sCache = new ContactInfoCache(mContext.getApplicationContext()); |
| 139 | } |
| 140 | return sCache; |
| 141 | } |
| 142 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 143 | static ContactCacheEntry buildCacheEntryFromCall( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 144 | Context context, DialerCall call, boolean isIncoming) { |
| 145 | final ContactCacheEntry entry = new ContactCacheEntry(); |
| 146 | |
| 147 | // TODO: get rid of caller info. |
| 148 | final CallerInfo info = CallerInfoUtils.buildCallerInfo(context, call); |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 149 | ContactInfoCache.populateCacheEntry(context, info, entry, call.getNumberPresentation()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 150 | return entry; |
| 151 | } |
| 152 | |
| 153 | /** Populate a cache entry from a call (which got converted into a caller info). */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 154 | private static void populateCacheEntry( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 155 | @NonNull Context context, |
| 156 | @NonNull CallerInfo info, |
| 157 | @NonNull ContactCacheEntry cce, |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 158 | int presentation) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 159 | Objects.requireNonNull(info); |
| 160 | String displayName = null; |
| 161 | String displayNumber = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 162 | String label = null; |
| 163 | boolean isSipCall = false; |
| 164 | |
| 165 | // It appears that there is a small change in behaviour with the |
| 166 | // PhoneUtils' startGetCallerInfo whereby if we query with an |
| 167 | // empty number, we will get a valid CallerInfo object, but with |
| 168 | // fields that are all null, and the isTemporary boolean input |
| 169 | // parameter as true. |
| 170 | |
| 171 | // In the past, we would see a NULL callerinfo object, but this |
| 172 | // ends up causing null pointer exceptions elsewhere down the |
| 173 | // line in other cases, so we need to make this fix instead. It |
| 174 | // appears that this was the ONLY call to PhoneUtils |
| 175 | // .getCallerInfo() that relied on a NULL CallerInfo to indicate |
| 176 | // an unknown contact. |
| 177 | |
| 178 | // Currently, info.phoneNumber may actually be a SIP address, and |
| 179 | // if so, it might sometimes include the "sip:" prefix. That |
| 180 | // prefix isn't really useful to the user, though, so strip it off |
| 181 | // if present. (For any other URI scheme, though, leave the |
| 182 | // prefix alone.) |
| 183 | // TODO: It would be cleaner for CallerInfo to explicitly support |
| 184 | // SIP addresses instead of overloading the "phoneNumber" field. |
| 185 | // Then we could remove this hack, and instead ask the CallerInfo |
| 186 | // for a "user visible" form of the SIP address. |
| 187 | String number = info.phoneNumber; |
| 188 | |
| 189 | if (!TextUtils.isEmpty(number)) { |
| 190 | isSipCall = PhoneNumberHelper.isUriNumber(number); |
| 191 | if (number.startsWith("sip:")) { |
| 192 | number = number.substring(4); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (TextUtils.isEmpty(info.name)) { |
| 197 | // No valid "name" in the CallerInfo, so fall back to |
| 198 | // something else. |
| 199 | // (Typically, we promote the phone number up to the "name" slot |
| 200 | // onscreen, and possibly display a descriptive string in the |
| 201 | // "number" slot.) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 202 | if (TextUtils.isEmpty(number) && TextUtils.isEmpty(info.cnapName)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 203 | // No name *or* number! Display a generic "unknown" string |
| 204 | // (or potentially some other default based on the presentation.) |
| 205 | displayName = getPresentationString(context, presentation, info.callSubject); |
| 206 | Log.d(TAG, " ==> no name *or* number! displayName = " + displayName); |
| 207 | } else if (presentation != TelecomManager.PRESENTATION_ALLOWED) { |
| 208 | // This case should never happen since the network should never send a phone # |
| 209 | // AND a restricted presentation. However we leave it here in case of weird |
| 210 | // network behavior |
| 211 | displayName = getPresentationString(context, presentation, info.callSubject); |
| 212 | Log.d(TAG, " ==> presentation not allowed! displayName = " + displayName); |
| 213 | } else if (!TextUtils.isEmpty(info.cnapName)) { |
| 214 | // No name, but we do have a valid CNAP name, so use that. |
| 215 | displayName = info.cnapName; |
| 216 | info.name = info.cnapName; |
wangqi | 1420a22 | 2017-09-21 09:37:40 -0700 | [diff] [blame] | 217 | displayNumber = PhoneNumberHelper.formatNumber(number, info.countryIso); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 218 | Log.d( |
| 219 | TAG, |
| 220 | " ==> cnapName available: displayName '" |
| 221 | + displayName |
| 222 | + "', displayNumber '" |
| 223 | + displayNumber |
| 224 | + "'"); |
| 225 | } else { |
| 226 | // No name; all we have is a number. This is the typical |
| 227 | // case when an incoming call doesn't match any contact, |
| 228 | // or if you manually dial an outgoing number using the |
| 229 | // dialpad. |
wangqi | 1420a22 | 2017-09-21 09:37:40 -0700 | [diff] [blame] | 230 | displayNumber = PhoneNumberHelper.formatNumber(number, info.countryIso); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 231 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 232 | Log.d( |
| 233 | TAG, |
| 234 | " ==> no name; falling back to number:" |
| 235 | + " displayNumber '" |
| 236 | + Log.pii(displayNumber) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 237 | + "'"); |
| 238 | } |
| 239 | } else { |
| 240 | // We do have a valid "name" in the CallerInfo. Display that |
| 241 | // in the "name" slot, and the phone number in the "number" slot. |
| 242 | if (presentation != TelecomManager.PRESENTATION_ALLOWED) { |
| 243 | // This case should never happen since the network should never send a name |
| 244 | // AND a restricted presentation. However we leave it here in case of weird |
| 245 | // network behavior |
| 246 | displayName = getPresentationString(context, presentation, info.callSubject); |
| 247 | Log.d( |
| 248 | TAG, |
| 249 | " ==> valid name, but presentation not allowed!" + " displayName = " + displayName); |
| 250 | } else { |
| 251 | // Causes cce.namePrimary to be set as info.name below. CallCardPresenter will |
| 252 | // later determine whether to use the name or nameAlternative when presenting |
| 253 | displayName = info.name; |
| 254 | cce.nameAlternative = info.nameAlternative; |
wangqi | 1420a22 | 2017-09-21 09:37:40 -0700 | [diff] [blame] | 255 | displayNumber = PhoneNumberHelper.formatNumber(number, info.countryIso); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 256 | label = info.phoneLabel; |
| 257 | Log.d( |
| 258 | TAG, |
| 259 | " ==> name is present in CallerInfo: displayName '" |
| 260 | + displayName |
| 261 | + "', displayNumber '" |
| 262 | + displayNumber |
| 263 | + "'"); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | cce.namePrimary = displayName; |
| 268 | cce.number = displayNumber; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 269 | cce.location = info.geoDescription; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 270 | cce.label = label; |
| 271 | cce.isSipCall = isSipCall; |
| 272 | cce.userType = info.userType; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 273 | cce.originalPhoneNumber = info.phoneNumber; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 274 | cce.shouldShowLocation = info.shouldShowGeoDescription; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 275 | cce.isEmergencyNumber = info.isEmergencyNumber(); |
| 276 | cce.isVoicemailNumber = info.isVoiceMailNumber(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 277 | |
| 278 | if (info.contactExists) { |
| 279 | cce.contactLookupResult = ContactLookupResult.Type.LOCAL_CONTACT; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** Gets name strings based on some special presentation modes and the associated custom label. */ |
| 284 | private static String getPresentationString( |
| 285 | Context context, int presentation, String customLabel) { |
| 286 | String name = context.getString(R.string.unknown); |
| 287 | if (!TextUtils.isEmpty(customLabel) |
| 288 | && ((presentation == TelecomManager.PRESENTATION_UNKNOWN) |
| 289 | || (presentation == TelecomManager.PRESENTATION_RESTRICTED))) { |
| 290 | name = customLabel; |
| 291 | return name; |
| 292 | } else { |
| 293 | if (presentation == TelecomManager.PRESENTATION_RESTRICTED) { |
| 294 | name = PhoneNumberHelper.getDisplayNameForRestrictedNumber(context).toString(); |
| 295 | } else if (presentation == TelecomManager.PRESENTATION_PAYPHONE) { |
| 296 | name = context.getString(R.string.payphone); |
| 297 | } |
| 298 | } |
| 299 | return name; |
| 300 | } |
| 301 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 302 | ContactCacheEntry getInfo(String callId) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 303 | return mInfoMap.get(callId); |
| 304 | } |
| 305 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 306 | private static final class CnapInformationWrapper { |
| 307 | final String number; |
| 308 | final String cnapName; |
| 309 | final Context context; |
| 310 | final CachedNumberLookupService service; |
| 311 | |
| 312 | CnapInformationWrapper( |
| 313 | String number, String cnapName, Context context, CachedNumberLookupService service) { |
| 314 | this.number = number; |
| 315 | this.cnapName = cnapName; |
| 316 | this.context = context; |
| 317 | this.service = service; |
| 318 | } |
| 319 | } |
| 320 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 321 | void maybeInsertCnapInformationIntoCache( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 322 | Context context, final DialerCall call, final CallerInfo info) { |
| 323 | final CachedNumberLookupService cachedNumberLookupService = |
| 324 | PhoneNumberCache.get(context).getCachedNumberLookupService(); |
| 325 | if (!UserManagerCompat.isUserUnlocked(context)) { |
| 326 | Log.i(TAG, "User locked, not inserting cnap info into cache"); |
| 327 | return; |
| 328 | } |
| 329 | if (cachedNumberLookupService == null |
| 330 | || TextUtils.isEmpty(info.cnapName) |
| 331 | || mInfoMap.get(call.getId()) != null) { |
| 332 | return; |
| 333 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 334 | Log.i(TAG, "Found contact with CNAP name - inserting into cache"); |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 335 | |
| 336 | cachedNumberLookupExecutor.executeParallel( |
| 337 | new CnapInformationWrapper( |
| 338 | call.getNumber(), info.cnapName, context, cachedNumberLookupService)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Requests contact data for the DialerCall object passed in. Returns the data through callback. |
| 343 | * If callback is null, no response is made, however the query is still performed and cached. |
| 344 | * |
| 345 | * @param callback The function to call back when the call is found. Can be null. |
| 346 | */ |
| 347 | @MainThread |
| 348 | public void findInfo( |
| 349 | @NonNull final DialerCall call, |
| 350 | final boolean isIncoming, |
| 351 | @NonNull ContactInfoCacheCallback callback) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 352 | Trace.beginSection("ContactInfoCache.findInfo"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 353 | Assert.isMainThread(); |
| 354 | Objects.requireNonNull(callback); |
| 355 | |
wangqi | c8cf79e | 2017-10-17 09:21:00 -0700 | [diff] [blame] | 356 | Trace.beginSection("prepare callback"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 357 | final String callId = call.getId(); |
| 358 | final ContactCacheEntry cacheEntry = mInfoMap.get(callId); |
| 359 | Set<ContactInfoCacheCallback> callBacks = mCallBacks.get(callId); |
| 360 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 361 | // We need to force a new query if phone number has changed. |
| 362 | boolean forceQuery = needForceQuery(call, cacheEntry); |
wangqi | c8cf79e | 2017-10-17 09:21:00 -0700 | [diff] [blame] | 363 | Trace.endSection(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 364 | Log.d(TAG, "findInfo: callId = " + callId + "; forceQuery = " + forceQuery); |
| 365 | |
| 366 | // If we have a previously obtained intermediate result return that now except needs |
| 367 | // force query. |
| 368 | if (cacheEntry != null && !forceQuery) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 369 | Log.d( |
| 370 | TAG, |
| 371 | "Contact lookup. In memory cache hit; lookup " |
| 372 | + (callBacks == null ? "complete" : "still running")); |
| 373 | callback.onContactInfoComplete(callId, cacheEntry); |
| 374 | // If no other callbacks are in flight, we're done. |
| 375 | if (callBacks == null) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 376 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 377 | return; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // If the entry already exists, add callback |
| 382 | if (callBacks != null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 383 | Log.d(TAG, "Another query is in progress, add callback only."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 384 | callBacks.add(callback); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 385 | if (!forceQuery) { |
| 386 | Log.d(TAG, "No need to query again, just return and wait for existing query to finish"); |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 387 | Trace.endSection(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | } else { |
| 391 | Log.d(TAG, "Contact lookup. In memory cache miss; searching provider."); |
| 392 | // New lookup |
| 393 | callBacks = new ArraySet<>(); |
| 394 | callBacks.add(callback); |
| 395 | mCallBacks.put(callId, callBacks); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 396 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 397 | |
wangqi | c8cf79e | 2017-10-17 09:21:00 -0700 | [diff] [blame] | 398 | Trace.beginSection("prepare query"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 399 | /** |
| 400 | * Performs a query for caller information. Save any immediate data we get from the query. An |
| 401 | * asynchronous query may also be made for any data that we do not already have. Some queries, |
| 402 | * such as those for voicemail and emergency call information, will not perform an additional |
| 403 | * asynchronous query. |
| 404 | */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 405 | final CallerInfoQueryToken queryToken = new CallerInfoQueryToken(mQueryId, callId); |
| 406 | mQueryId++; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 407 | final CallerInfo callerInfo = |
| 408 | CallerInfoUtils.getCallerInfoForCall( |
| 409 | mContext, |
| 410 | call, |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 411 | new DialerCallCookieWrapper(callId, call.getNumberPresentation(), call.getCnapName()), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 412 | new FindInfoCallback(isIncoming, queryToken)); |
wangqi | c8cf79e | 2017-10-17 09:21:00 -0700 | [diff] [blame] | 413 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 414 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 415 | if (cacheEntry != null) { |
| 416 | // We should not override the old cache item until the new query is |
| 417 | // back. We should only update the queryId. Otherwise, we may see |
| 418 | // flicker of the name and image (old cache -> new cache before query |
| 419 | // -> new cache after query) |
| 420 | cacheEntry.queryId = queryToken.mQueryId; |
| 421 | Log.d(TAG, "There is an existing cache. Do not override until new query is back"); |
| 422 | } else { |
| 423 | ContactCacheEntry initialCacheEntry = |
| 424 | updateCallerInfoInCacheOnAnyThread( |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 425 | callId, call.getNumberPresentation(), callerInfo, false, queryToken); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 426 | sendInfoNotifications(callId, initialCacheEntry); |
| 427 | } |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 428 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @AnyThread |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 432 | private ContactCacheEntry updateCallerInfoInCacheOnAnyThread( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 433 | String callId, |
| 434 | int numberPresentation, |
| 435 | CallerInfo callerInfo, |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 436 | boolean didLocalLookup, |
| 437 | CallerInfoQueryToken queryToken) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 438 | Trace.beginSection("ContactInfoCache.updateCallerInfoInCacheOnAnyThread"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 439 | Log.d( |
| 440 | TAG, |
| 441 | "updateCallerInfoInCacheOnAnyThread: callId = " |
| 442 | + callId |
| 443 | + "; queryId = " |
| 444 | + queryToken.mQueryId |
| 445 | + "; didLocalLookup = " |
| 446 | + didLocalLookup); |
| 447 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 448 | ContactCacheEntry existingCacheEntry = mInfoMap.get(callId); |
| 449 | Log.d(TAG, "Existing cacheEntry in hashMap " + existingCacheEntry); |
| 450 | |
| 451 | // Mark it as emergency/voicemail if the cache exists and was emergency/voicemail before the |
| 452 | // number changed. |
| 453 | if (existingCacheEntry != null) { |
| 454 | if (existingCacheEntry.isEmergencyNumber) { |
| 455 | callerInfo.markAsEmergency(mContext); |
| 456 | } else if (existingCacheEntry.isVoicemailNumber) { |
| 457 | callerInfo.markAsVoiceMail(mContext); |
| 458 | } |
| 459 | } |
| 460 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 461 | int presentationMode = numberPresentation; |
| 462 | if (callerInfo.contactExists |
| 463 | || callerInfo.isEmergencyNumber() |
| 464 | || callerInfo.isVoiceMailNumber()) { |
| 465 | presentationMode = TelecomManager.PRESENTATION_ALLOWED; |
| 466 | } |
| 467 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 468 | // We always replace the entry. The only exception is the same photo case. |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 469 | ContactCacheEntry cacheEntry = buildEntry(mContext, callerInfo, presentationMode); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 470 | cacheEntry.queryId = queryToken.mQueryId; |
| 471 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 472 | if (didLocalLookup) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 473 | if (cacheEntry.displayPhotoUri != null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 474 | // When the difference between 2 numbers is only the prefix (e.g. + or IDD), |
| 475 | // we will still trigger force query so that the number can be updated on |
| 476 | // the calling screen. We need not query the image again if the previous |
| 477 | // query already has the image to avoid flickering. |
| 478 | if (existingCacheEntry != null |
| 479 | && existingCacheEntry.displayPhotoUri != null |
| 480 | && existingCacheEntry.displayPhotoUri.equals(cacheEntry.displayPhotoUri) |
| 481 | && existingCacheEntry.photo != null) { |
| 482 | Log.d(TAG, "Same picture. Do not need start image load."); |
| 483 | cacheEntry.photo = existingCacheEntry.photo; |
| 484 | cacheEntry.photoType = existingCacheEntry.photoType; |
| 485 | return cacheEntry; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 486 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 487 | |
| 488 | Log.d(TAG, "Contact lookup. Local contact found, starting image load"); |
| 489 | // Load the image with a callback to update the image state. |
| 490 | // When the load is finished, onImageLoadComplete() will be called. |
| 491 | cacheEntry.hasPendingQuery = true; |
| 492 | ContactsAsyncHelper.startObtainPhotoAsync( |
| 493 | TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, |
| 494 | mContext, |
| 495 | cacheEntry.displayPhotoUri, |
| 496 | ContactInfoCache.this, |
| 497 | queryToken); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 498 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 499 | Log.d(TAG, "put entry into map: " + cacheEntry); |
| 500 | mInfoMap.put(callId, cacheEntry); |
| 501 | } else { |
| 502 | // Don't overwrite if there is existing cache. |
| 503 | Log.d(TAG, "put entry into map if not exists: " + cacheEntry); |
| 504 | mInfoMap.putIfAbsent(callId, cacheEntry); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 505 | } |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 506 | Trace.endSection(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 507 | return cacheEntry; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 510 | private void maybeUpdateFromCequintCallerId( |
| 511 | CallerInfo callerInfo, String cnapName, boolean isIncoming) { |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 512 | if (!CequintCallerIdManager.isCequintCallerIdEnabled(mContext)) { |
| 513 | return; |
| 514 | } |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 515 | if (callerInfo.phoneNumber == null) { |
| 516 | return; |
| 517 | } |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 518 | CequintCallerIdContact cequintCallerIdContact = |
| 519 | CequintCallerIdManager.getCequintCallerIdContactForInCall( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 520 | mContext, callerInfo.phoneNumber, cnapName, isIncoming); |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 521 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 522 | if (cequintCallerIdContact == null) { |
| 523 | return; |
| 524 | } |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 525 | boolean hasUpdate = false; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 526 | |
| 527 | if (TextUtils.isEmpty(callerInfo.name) && !TextUtils.isEmpty(cequintCallerIdContact.name)) { |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 528 | callerInfo.name = cequintCallerIdContact.name; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 529 | hasUpdate = true; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 530 | } |
| 531 | if (!TextUtils.isEmpty(cequintCallerIdContact.geoDescription)) { |
| 532 | callerInfo.geoDescription = cequintCallerIdContact.geoDescription; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 533 | callerInfo.shouldShowGeoDescription = true; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 534 | hasUpdate = true; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 535 | } |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 536 | // Don't overwrite photo in local contacts. |
| 537 | if (!callerInfo.contactExists |
| 538 | && callerInfo.contactDisplayPhotoUri == null |
| 539 | && cequintCallerIdContact.imageUrl != null) { |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 540 | callerInfo.contactDisplayPhotoUri = Uri.parse(cequintCallerIdContact.imageUrl); |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 541 | hasUpdate = true; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 542 | } |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 543 | // Set contact to exist to avoid phone number service lookup. |
| 544 | callerInfo.contactExists = hasUpdate; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 547 | /** |
| 548 | * Implemented for ContactsAsyncHelper.OnImageLoadCompleteListener interface. Update contact photo |
| 549 | * when image is loaded in worker thread. |
| 550 | */ |
| 551 | @WorkerThread |
| 552 | @Override |
| 553 | public void onImageLoaded(int token, Drawable photo, Bitmap photoIcon, Object cookie) { |
| 554 | Assert.isWorkerThread(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 555 | CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie; |
| 556 | final String callId = myCookie.mCallId; |
| 557 | final int queryId = myCookie.mQueryId; |
| 558 | if (!isWaitingForThisQuery(callId, queryId)) { |
| 559 | return; |
| 560 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 561 | loadImage(photo, photoIcon, cookie); |
| 562 | } |
| 563 | |
| 564 | private void loadImage(Drawable photo, Bitmap photoIcon, Object cookie) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 565 | Log.d(TAG, "Image load complete with context: ", mContext); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 566 | // TODO: may be nice to update the image view again once the newer one |
| 567 | // is available on contacts database. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 568 | CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie; |
| 569 | final String callId = myCookie.mCallId; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 570 | ContactCacheEntry entry = mInfoMap.get(callId); |
| 571 | |
| 572 | if (entry == null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 573 | Log.e(TAG, "Image Load received for empty search entry."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 574 | clearCallbacks(callId); |
| 575 | return; |
| 576 | } |
| 577 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 578 | Log.d(TAG, "setting photo for entry: ", entry); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 579 | |
| 580 | // Conference call icons are being handled in CallCardPresenter. |
| 581 | if (photo != null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 582 | Log.v(TAG, "direct drawable: ", photo); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 583 | entry.photo = photo; |
| 584 | entry.photoType = ContactPhotoType.CONTACT; |
| 585 | } else if (photoIcon != null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 586 | Log.v(TAG, "photo icon: ", photoIcon); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 587 | entry.photo = new BitmapDrawable(mContext.getResources(), photoIcon); |
| 588 | entry.photoType = ContactPhotoType.CONTACT; |
| 589 | } else { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 590 | Log.v(TAG, "unknown photo"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 591 | entry.photo = null; |
| 592 | entry.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Implemented for ContactsAsyncHelper.OnImageLoadCompleteListener interface. make sure that the |
| 598 | * call state is reflected after the image is loaded. |
| 599 | */ |
| 600 | @MainThread |
| 601 | @Override |
| 602 | public void onImageLoadComplete(int token, Drawable photo, Bitmap photoIcon, Object cookie) { |
| 603 | Assert.isMainThread(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 604 | CallerInfoQueryToken myCookie = (CallerInfoQueryToken) cookie; |
| 605 | final String callId = myCookie.mCallId; |
| 606 | final int queryId = myCookie.mQueryId; |
| 607 | if (!isWaitingForThisQuery(callId, queryId)) { |
| 608 | return; |
| 609 | } |
| 610 | sendImageNotifications(callId, mInfoMap.get(callId)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 611 | |
| 612 | clearCallbacks(callId); |
| 613 | } |
| 614 | |
| 615 | /** Blows away the stored cache values. */ |
| 616 | public void clearCache() { |
| 617 | mInfoMap.clear(); |
| 618 | mCallBacks.clear(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 619 | mQueryId = 0; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 622 | private ContactCacheEntry buildEntry(Context context, CallerInfo info, int presentation) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 623 | final ContactCacheEntry cce = new ContactCacheEntry(); |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 624 | populateCacheEntry(context, info, cce, presentation); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 625 | |
| 626 | // This will only be true for emergency numbers |
| 627 | if (info.photoResource != 0) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 628 | cce.photo = ContextCompat.getDrawable(context, info.photoResource); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 629 | } else if (info.isCachedPhotoCurrent) { |
| 630 | if (info.cachedPhoto != null) { |
| 631 | cce.photo = info.cachedPhoto; |
| 632 | cce.photoType = ContactPhotoType.CONTACT; |
| 633 | } else { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 634 | cce.photoType = ContactPhotoType.DEFAULT_PLACEHOLDER; |
| 635 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 636 | } else { |
| 637 | cce.displayPhotoUri = info.contactDisplayPhotoUri; |
| 638 | cce.photo = null; |
| 639 | } |
| 640 | |
| 641 | // Support any contact id in N because QuickContacts in N starts supporting enterprise |
| 642 | // contact id |
| 643 | if (info.lookupKeyOrNull != null |
| 644 | && (VERSION.SDK_INT >= VERSION_CODES.N || info.contactIdOrZero != 0)) { |
| 645 | cce.lookupUri = Contacts.getLookupUri(info.contactIdOrZero, info.lookupKeyOrNull); |
| 646 | } else { |
| 647 | Log.v(TAG, "lookup key is null or contact ID is 0 on M. Don't create a lookup uri."); |
| 648 | cce.lookupUri = null; |
| 649 | } |
| 650 | |
| 651 | cce.lookupKey = info.lookupKeyOrNull; |
| 652 | cce.contactRingtoneUri = info.contactRingtoneUri; |
| 653 | if (cce.contactRingtoneUri == null || Uri.EMPTY.equals(cce.contactRingtoneUri)) { |
| 654 | cce.contactRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); |
| 655 | } |
| 656 | |
| 657 | return cce; |
| 658 | } |
| 659 | |
| 660 | /** Sends the updated information to call the callbacks for the entry. */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 661 | @MainThread |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 662 | private void sendInfoNotifications(String callId, ContactCacheEntry entry) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 663 | Trace.beginSection("ContactInfoCache.sendInfoNotifications"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 664 | Assert.isMainThread(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 665 | final Set<ContactInfoCacheCallback> callBacks = mCallBacks.get(callId); |
| 666 | if (callBacks != null) { |
| 667 | for (ContactInfoCacheCallback callBack : callBacks) { |
| 668 | callBack.onContactInfoComplete(callId, entry); |
| 669 | } |
| 670 | } |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 671 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 674 | @MainThread |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 675 | private void sendImageNotifications(String callId, ContactCacheEntry entry) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 676 | Trace.beginSection("ContactInfoCache.sendImageNotifications"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 677 | Assert.isMainThread(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 678 | final Set<ContactInfoCacheCallback> callBacks = mCallBacks.get(callId); |
| 679 | if (callBacks != null && entry.photo != null) { |
| 680 | for (ContactInfoCacheCallback callBack : callBacks) { |
| 681 | callBack.onImageLoadComplete(callId, entry); |
| 682 | } |
| 683 | } |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 684 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | private void clearCallbacks(String callId) { |
| 688 | mCallBacks.remove(callId); |
| 689 | } |
| 690 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 691 | /** Callback interface for the contact query. */ |
| 692 | public interface ContactInfoCacheCallback { |
| 693 | |
| 694 | void onContactInfoComplete(String callId, ContactCacheEntry entry); |
| 695 | |
| 696 | void onImageLoadComplete(String callId, ContactCacheEntry entry); |
| 697 | } |
| 698 | |
| 699 | /** This is cached contact info, which should be the ONLY info used by UI. */ |
| 700 | public static class ContactCacheEntry { |
| 701 | |
| 702 | public String namePrimary; |
| 703 | public String nameAlternative; |
| 704 | public String number; |
| 705 | public String location; |
| 706 | public String label; |
| 707 | public Drawable photo; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 708 | @ContactPhotoType int photoType; |
| 709 | boolean isSipCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 710 | // Note in cache entry whether this is a pending async loading action to know whether to |
| 711 | // wait for its callback or not. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 712 | boolean hasPendingQuery; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 713 | /** Either a display photo or a thumbnail URI. */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 714 | Uri displayPhotoUri; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 715 | |
| 716 | public Uri lookupUri; // Sent to NotificationMananger |
| 717 | public String lookupKey; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 718 | public ContactLookupResult.Type contactLookupResult = ContactLookupResult.Type.NOT_FOUND; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 719 | public long userType = ContactsUtils.USER_TYPE_CURRENT; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 720 | Uri contactRingtoneUri; |
| 721 | /** Query id to identify the query session. */ |
| 722 | int queryId; |
| 723 | /** The phone number without any changes to display to the user (ex: cnap...) */ |
| 724 | String originalPhoneNumber; |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 725 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 726 | boolean shouldShowLocation; |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 727 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 728 | boolean isBusiness; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 729 | boolean isEmergencyNumber; |
| 730 | boolean isVoicemailNumber; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 731 | |
wangqi | ae6c8ec | 2017-09-28 17:39:40 -0700 | [diff] [blame] | 732 | public boolean isLocalContact() { |
| 733 | return contactLookupResult == ContactLookupResult.Type.LOCAL_CONTACT; |
| 734 | } |
| 735 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 736 | @Override |
| 737 | public String toString() { |
| 738 | return "ContactCacheEntry{" |
| 739 | + "name='" |
| 740 | + MoreStrings.toSafeString(namePrimary) |
| 741 | + '\'' |
| 742 | + ", nameAlternative='" |
| 743 | + MoreStrings.toSafeString(nameAlternative) |
| 744 | + '\'' |
| 745 | + ", number='" |
| 746 | + MoreStrings.toSafeString(number) |
| 747 | + '\'' |
| 748 | + ", location='" |
| 749 | + MoreStrings.toSafeString(location) |
| 750 | + '\'' |
| 751 | + ", label='" |
| 752 | + label |
| 753 | + '\'' |
| 754 | + ", photo=" |
| 755 | + photo |
| 756 | + ", isSipCall=" |
| 757 | + isSipCall |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 758 | + ", displayPhotoUri=" |
| 759 | + displayPhotoUri |
| 760 | + ", contactLookupResult=" |
| 761 | + contactLookupResult |
| 762 | + ", userType=" |
| 763 | + userType |
| 764 | + ", contactRingtoneUri=" |
| 765 | + contactRingtoneUri |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 766 | + ", queryId=" |
| 767 | + queryId |
| 768 | + ", originalPhoneNumber=" |
| 769 | + originalPhoneNumber |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 770 | + ", shouldShowLocation=" |
| 771 | + shouldShowLocation |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 772 | + ", isEmergencyNumber=" |
| 773 | + isEmergencyNumber |
| 774 | + ", isVoicemailNumber=" |
| 775 | + isVoicemailNumber |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 776 | + '}'; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | private static final class DialerCallCookieWrapper { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 781 | final String callId; |
| 782 | final int numberPresentation; |
| 783 | final String cnapName; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 784 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 785 | DialerCallCookieWrapper(String callId, int numberPresentation, String cnapName) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 786 | this.callId = callId; |
| 787 | this.numberPresentation = numberPresentation; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 788 | this.cnapName = cnapName; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | private class FindInfoCallback implements OnQueryCompleteListener { |
| 793 | |
| 794 | private final boolean mIsIncoming; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 795 | private final CallerInfoQueryToken mQueryToken; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 796 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 797 | FindInfoCallback(boolean isIncoming, CallerInfoQueryToken queryToken) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 798 | mIsIncoming = isIncoming; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 799 | mQueryToken = queryToken; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | @Override |
| 803 | public void onDataLoaded(int token, Object cookie, CallerInfo ci) { |
| 804 | Assert.isWorkerThread(); |
| 805 | DialerCallCookieWrapper cw = (DialerCallCookieWrapper) cookie; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 806 | if (!isWaitingForThisQuery(cw.callId, mQueryToken.mQueryId)) { |
| 807 | return; |
| 808 | } |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 809 | long start = SystemClock.uptimeMillis(); |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 810 | maybeUpdateFromCequintCallerId(ci, cw.cnapName, mIsIncoming); |
Eric Erfanian | 9779f96 | 2017-03-27 12:31:48 -0700 | [diff] [blame] | 811 | long time = SystemClock.uptimeMillis() - start; |
| 812 | Log.d(TAG, "Cequint Caller Id look up takes " + time + " ms."); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 813 | updateCallerInfoInCacheOnAnyThread(cw.callId, cw.numberPresentation, ci, true, mQueryToken); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | @Override |
| 817 | public void onQueryComplete(int token, Object cookie, CallerInfo callerInfo) { |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 818 | Trace.beginSection("ContactInfoCache.FindInfoCallback.onQueryComplete"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 819 | Assert.isMainThread(); |
| 820 | DialerCallCookieWrapper cw = (DialerCallCookieWrapper) cookie; |
| 821 | String callId = cw.callId; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 822 | if (!isWaitingForThisQuery(cw.callId, mQueryToken.mQueryId)) { |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 823 | Trace.endSection(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 824 | return; |
| 825 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 826 | ContactCacheEntry cacheEntry = mInfoMap.get(callId); |
| 827 | // This may happen only when InCallPresenter attempt to cleanup. |
| 828 | if (cacheEntry == null) { |
| 829 | Log.w(TAG, "Contact lookup done, but cache entry is not found."); |
| 830 | clearCallbacks(callId); |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 831 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 832 | return; |
| 833 | } |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 834 | // Before issuing a request for more data from other services, we only check that the |
| 835 | // contact wasn't found in the local DB. We don't check the if the cache entry already |
| 836 | // has a name because we allow overriding cnap data with data from other services. |
| 837 | if (!callerInfo.contactExists && mPhoneNumberService != null) { |
| 838 | Log.d(TAG, "Contact lookup. Local contacts miss, checking remote"); |
| 839 | final PhoneNumberServiceListener listener = |
| 840 | new PhoneNumberServiceListener(callId, mQueryToken.mQueryId); |
| 841 | cacheEntry.hasPendingQuery = true; |
| 842 | mPhoneNumberService.getPhoneNumberInfo(cacheEntry.number, listener, listener, mIsIncoming); |
| 843 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 844 | sendInfoNotifications(callId, cacheEntry); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 845 | if (!cacheEntry.hasPendingQuery) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 846 | if (callerInfo.contactExists) { |
| 847 | Log.d(TAG, "Contact lookup done. Local contact found, no image."); |
| 848 | } else { |
| 849 | Log.d( |
| 850 | TAG, |
| 851 | "Contact lookup done. Local contact not found and" |
| 852 | + " no remote lookup service available."); |
| 853 | } |
| 854 | clearCallbacks(callId); |
| 855 | } |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 856 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | class PhoneNumberServiceListener |
| 861 | implements PhoneNumberService.NumberLookupListener, PhoneNumberService.ImageLookupListener { |
| 862 | |
| 863 | private final String mCallId; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 864 | private final int mQueryIdOfRemoteLookup; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 865 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 866 | PhoneNumberServiceListener(String callId, int queryId) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 867 | mCallId = callId; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 868 | mQueryIdOfRemoteLookup = queryId; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | @Override |
| 872 | public void onPhoneNumberInfoComplete(final PhoneNumberService.PhoneNumberInfo info) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 873 | Log.d(TAG, "PhoneNumberServiceListener.onPhoneNumberInfoComplete"); |
| 874 | if (!isWaitingForThisQuery(mCallId, mQueryIdOfRemoteLookup)) { |
| 875 | return; |
| 876 | } |
| 877 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 878 | // If we got a miss, this is the end of the lookup pipeline, |
| 879 | // so clear the callbacks and return. |
| 880 | if (info == null) { |
| 881 | Log.d(TAG, "Contact lookup done. Remote contact not found."); |
| 882 | clearCallbacks(mCallId); |
| 883 | return; |
| 884 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 885 | ContactCacheEntry entry = new ContactCacheEntry(); |
| 886 | entry.namePrimary = info.getDisplayName(); |
| 887 | entry.number = info.getNumber(); |
| 888 | entry.contactLookupResult = info.getLookupSource(); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 889 | entry.isBusiness = info.isBusiness(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 890 | final int type = info.getPhoneType(); |
| 891 | final String label = info.getPhoneLabel(); |
| 892 | if (type == Phone.TYPE_CUSTOM) { |
| 893 | entry.label = label; |
| 894 | } else { |
| 895 | final CharSequence typeStr = Phone.getTypeLabel(mContext.getResources(), type, label); |
| 896 | entry.label = typeStr == null ? null : typeStr.toString(); |
| 897 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 898 | final ContactCacheEntry oldEntry = mInfoMap.get(mCallId); |
| 899 | if (oldEntry != null) { |
| 900 | // Location is only obtained from local lookup so persist |
| 901 | // the value for remote lookups. Once we have a name this |
| 902 | // field is no longer used; it is persisted here in case |
| 903 | // the UI is ever changed to use it. |
| 904 | entry.location = oldEntry.location; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 905 | entry.shouldShowLocation = oldEntry.shouldShowLocation; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 906 | // Contact specific ringtone is obtained from local lookup. |
| 907 | entry.contactRingtoneUri = oldEntry.contactRingtoneUri; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 908 | entry.originalPhoneNumber = oldEntry.originalPhoneNumber; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 909 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 910 | |
| 911 | // If no image and it's a business, switch to using the default business avatar. |
| 912 | if (info.getImageUrl() == null && info.isBusiness()) { |
| 913 | Log.d(TAG, "Business has no image. Using default."); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 914 | entry.photoType = ContactPhotoType.BUSINESS; |
| 915 | } |
| 916 | |
| 917 | Log.d(TAG, "put entry into map: " + entry); |
| 918 | mInfoMap.put(mCallId, entry); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 919 | sendInfoNotifications(mCallId, entry); |
| 920 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 921 | entry.hasPendingQuery = info.getImageUrl() != null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 922 | |
| 923 | // If there is no image then we should not expect another callback. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 924 | if (!entry.hasPendingQuery) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 925 | // We're done, so clear callbacks |
| 926 | clearCallbacks(mCallId); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | @Override |
| 931 | public void onImageFetchComplete(Bitmap bitmap) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 932 | Log.d(TAG, "PhoneNumberServiceListener.onImageFetchComplete"); |
| 933 | if (!isWaitingForThisQuery(mCallId, mQueryIdOfRemoteLookup)) { |
| 934 | return; |
| 935 | } |
| 936 | CallerInfoQueryToken queryToken = new CallerInfoQueryToken(mQueryIdOfRemoteLookup, mCallId); |
| 937 | loadImage(null, bitmap, queryToken); |
| 938 | onImageLoadComplete(TOKEN_UPDATE_PHOTO_FOR_CALL_STATE, null, bitmap, queryToken); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | private boolean needForceQuery(DialerCall call, ContactCacheEntry cacheEntry) { |
| 943 | if (call == null || call.isConferenceCall()) { |
| 944 | return false; |
| 945 | } |
| 946 | |
| 947 | String newPhoneNumber = PhoneNumberUtils.stripSeparators(call.getNumber()); |
| 948 | if (cacheEntry == null) { |
| 949 | // No info in the map yet so it is the 1st query |
| 950 | Log.d(TAG, "needForceQuery: first query"); |
| 951 | return true; |
| 952 | } |
| 953 | String oldPhoneNumber = PhoneNumberUtils.stripSeparators(cacheEntry.originalPhoneNumber); |
| 954 | |
| 955 | if (!TextUtils.equals(oldPhoneNumber, newPhoneNumber)) { |
| 956 | Log.d(TAG, "phone number has changed: " + oldPhoneNumber + " -> " + newPhoneNumber); |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | private static final class CallerInfoQueryToken { |
| 964 | final int mQueryId; |
| 965 | final String mCallId; |
| 966 | |
| 967 | CallerInfoQueryToken(int queryId, String callId) { |
| 968 | mQueryId = queryId; |
| 969 | mCallId = callId; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /** Check if the queryId in the cached map is the same as the one from query result. */ |
| 974 | private boolean isWaitingForThisQuery(String callId, int queryId) { |
| 975 | final ContactCacheEntry existingCacheEntry = mInfoMap.get(callId); |
| 976 | if (existingCacheEntry == null) { |
| 977 | // This might happen if lookup on background thread comes back before the initial entry is |
| 978 | // created. |
| 979 | Log.d(TAG, "Cached entry is null."); |
| 980 | return true; |
| 981 | } else { |
| 982 | int waitingQueryId = existingCacheEntry.queryId; |
| 983 | Log.d(TAG, "waitingQueryId = " + waitingQueryId + "; queryId = " + queryId); |
| 984 | return waitingQueryId == queryId; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | } |