From dda48f12212a24371de0e6ba2e5e637925cd9b5e Mon Sep 17 00:00:00 2001 From: Christoph Studer Date: Tue, 29 Jul 2014 23:13:16 +0200 Subject: [PATCH] NoListener: Optimize RankingMap Cache ranks and interception bits instead of re-calculating them every time they're accessed. Change-Id: I2c6bbeaa01648d03ff8b3690b7f21cef4e2b9d97 --- .../NotificationListenerService.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/core/java/android/service/notification/NotificationListenerService.java b/core/java/android/service/notification/NotificationListenerService.java index 1fc64e4d3e812..7c8624c5ffbec 100644 --- a/core/java/android/service/notification/NotificationListenerService.java +++ b/core/java/android/service/notification/NotificationListenerService.java @@ -29,8 +29,11 @@ import android.os.Parcel; import android.os.Parcelable; import android.os.RemoteException; import android.os.ServiceManager; +import android.util.ArrayMap; +import android.util.ArraySet; import android.util.Log; +import java.util.Collections; import java.util.List; /** @@ -540,6 +543,8 @@ public abstract class NotificationListenerService extends Service { */ public static class RankingMap implements Parcelable { private final NotificationRankingUpdate mRankingUpdate; + private ArrayMap mRanks; + private ArraySet mIntercepted; private RankingMap(NotificationRankingUpdate rankingUpdate) { mRankingUpdate = rankingUpdate; @@ -569,14 +574,13 @@ public abstract class NotificationListenerService extends Service { } private int getRank(String key) { - // TODO: Optimize. - String[] orderedKeys = mRankingUpdate.getOrderedKeys(); - for (int i = 0; i < orderedKeys.length; i++) { - if (orderedKeys[i].equals(key)) { - return i; + synchronized (this) { + if (mRanks == null) { + buildRanksLocked(); } } - return -1; + Integer rank = mRanks.get(key); + return rank != null ? rank : -1; } private boolean isAmbient(String key) { @@ -585,13 +589,29 @@ public abstract class NotificationListenerService extends Service { } private boolean isIntercepted(String key) { - // TODO: Optimize. - for (String interceptedKey : mRankingUpdate.getInterceptedKeys()) { - if (interceptedKey.equals(key)) { - return true; + synchronized (this) { + if (mIntercepted == null) { + buildInterceptedSetLocked(); } } - return false; + return mIntercepted.contains(key); + } + + // Locked by 'this' + private void buildRanksLocked() { + String[] orderedKeys = mRankingUpdate.getOrderedKeys(); + mRanks = new ArrayMap<>(orderedKeys.length); + for (int i = 0; i < orderedKeys.length; i++) { + String key = orderedKeys[i]; + mRanks.put(key, i); + } + } + + // Locked by 'this' + private void buildInterceptedSetLocked() { + String[] dndInterceptedKeys = mRankingUpdate.getInterceptedKeys(); + mIntercepted = new ArraySet<>(dndInterceptedKeys.length); + Collections.addAll(mIntercepted, dndInterceptedKeys); } // ----------- Parcelable