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