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