From 89125443c97e955353f66761cd828f14ce20296c Mon Sep 17 00:00:00 2001 From: Tim Murray Date: Wed, 20 Jan 2021 08:40:17 -0800 Subject: [PATCH] NotificationManagerService: check cache before checking contacts In case a contact has been added to the cache before the future runs its work, check the cache before querying the contact. Test: atest com.android.server.notification.ValidateNotificationPeopleTest Bug: 161866124 Change-Id: I9c59a934952e4dc3cce1f99e905603cd357b9dbc --- .../ValidateNotificationPeople.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/services/core/java/com/android/server/notification/ValidateNotificationPeople.java b/services/core/java/com/android/server/notification/ValidateNotificationPeople.java index f59934fd68108..d7bc3bb8af289 100644 --- a/services/core/java/com/android/server/notification/ValidateNotificationPeople.java +++ b/services/core/java/com/android/server/notification/ValidateNotificationPeople.java @@ -523,27 +523,39 @@ public class ValidateNotificationPeople implements NotificationSignalExtractor { if (VERBOSE) Slog.i(TAG, "Executing: validation for: " + mKey); long timeStartMs = System.currentTimeMillis(); for (final String handle: mPendingLookups) { + final String cacheKey = getCacheKey(mContext.getUserId(), handle); LookupResult lookupResult = null; - final Uri uri = Uri.parse(handle); - if ("tel".equals(uri.getScheme())) { - if (DEBUG) Slog.d(TAG, "checking telephone URI: " + handle); - lookupResult = resolvePhoneContact(mContext, uri.getSchemeSpecificPart()); - } else if ("mailto".equals(uri.getScheme())) { - if (DEBUG) Slog.d(TAG, "checking mailto URI: " + handle); - lookupResult = resolveEmailContact(mContext, uri.getSchemeSpecificPart()); - } else if (handle.startsWith(Contacts.CONTENT_LOOKUP_URI.toString())) { - if (DEBUG) Slog.d(TAG, "checking lookup URI: " + handle); - lookupResult = searchContacts(mContext, uri); - } else { - lookupResult = new LookupResult(); // invalid person for the cache - if (!"name".equals(uri.getScheme())) { - Slog.w(TAG, "unsupported URI " + handle); + boolean cacheHit = false; + synchronized (mPeopleCache) { + lookupResult = mPeopleCache.get(cacheKey); + if (lookupResult != null && !lookupResult.isExpired()) { + // The name wasn't already added to the cache, no need to retry + cacheHit = true; + } + } + if (!cacheHit) { + final Uri uri = Uri.parse(handle); + if ("tel".equals(uri.getScheme())) { + if (DEBUG) Slog.d(TAG, "checking telephone URI: " + handle); + lookupResult = resolvePhoneContact(mContext, uri.getSchemeSpecificPart()); + } else if ("mailto".equals(uri.getScheme())) { + if (DEBUG) Slog.d(TAG, "checking mailto URI: " + handle); + lookupResult = resolveEmailContact(mContext, uri.getSchemeSpecificPart()); + } else if (handle.startsWith(Contacts.CONTENT_LOOKUP_URI.toString())) { + if (DEBUG) Slog.d(TAG, "checking lookup URI: " + handle); + lookupResult = searchContacts(mContext, uri); + } else { + lookupResult = new LookupResult(); // invalid person for the cache + if (!"name".equals(uri.getScheme())) { + Slog.w(TAG, "unsupported URI " + handle); + } } } if (lookupResult != null) { - synchronized (mPeopleCache) { - final String cacheKey = getCacheKey(mContext.getUserId(), handle); - mPeopleCache.put(cacheKey, lookupResult); + if (!cacheHit) { + synchronized (mPeopleCache) { + mPeopleCache.put(cacheKey, lookupResult); + } } if (DEBUG) { Slog.d(TAG, "lookup contactAffinity is " + lookupResult.getAffinity()); @@ -580,4 +592,3 @@ public class ValidateNotificationPeople implements NotificationSignalExtractor { } } } -