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