From 99f4c7d0c9c9c29ced22da0a8af4d1a04b0ef186 Mon Sep 17 00:00:00 2001 From: Chris Wren Date: Thu, 11 Sep 2014 13:38:18 -0400 Subject: [PATCH] Invalidate the people cache on Contacts provider changes. The Contacts database only seem to notify that a change has happened, not which row was changed. We simply invalidate the whole cache on changes. Added an info log for even 100th invalidation, so we can see how often it happens. Bug: 17466975 Change-Id: I9988ea8db776c88afd835a4b3c50b9a982a309d0 --- .../ValidateNotificationPeople.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/services/core/java/com/android/server/notification/ValidateNotificationPeople.java b/services/core/java/com/android/server/notification/ValidateNotificationPeople.java index d49dc9a60a79a..1c1a034492b34 100644 --- a/services/core/java/com/android/server/notification/ValidateNotificationPeople.java +++ b/services/core/java/com/android/server/notification/ValidateNotificationPeople.java @@ -19,9 +19,11 @@ package com.android.server.notification; import android.app.Notification; import android.content.Context; import android.content.pm.PackageManager; +import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.os.UserHandle; import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts; @@ -75,6 +77,9 @@ public class ValidateNotificationPeople implements NotificationSignalExtractor { // maps raw person handle to resolved person object private LruCache mPeopleCache; private Map mUserToContextMap; + private Handler mHandler; + private ContentObserver mObserver; + private int mEvictionCount; public void initialize(Context context) { if (DEBUG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + "."); @@ -83,6 +88,22 @@ public class ValidateNotificationPeople implements NotificationSignalExtractor { mPeopleCache = new LruCache(PEOPLE_CACHE_SIZE); mEnabled = ENABLE_PEOPLE_VALIDATOR && 1 == Settings.Global.getInt( mBaseContext.getContentResolver(), SETTING_ENABLE_PEOPLE_VALIDATOR, 1); + if (mEnabled) { + mHandler = new Handler(); + mObserver = new ContentObserver(mHandler) { + @Override + public void onChange(boolean selfChange, Uri uri, int userId) { + super.onChange(selfChange, uri, userId); + if (DEBUG || mEvictionCount % 100 == 0) { + if (INFO) Slog.i(TAG, "mEvictionCount: " + mEvictionCount); + } + mPeopleCache.evictAll(); + mEvictionCount++; + } + }; + mBaseContext.getContentResolver().registerContentObserver(Contacts.CONTENT_URI, true, + mObserver, UserHandle.USER_ALL); + } } public RankingReconsideration process(NotificationRecord record) {