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