Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dda48f12 authored by Christoph Studer's avatar Christoph Studer
Browse files

NoListener: Optimize RankingMap

Cache ranks and interception bits instead of re-calculating
them every time they're accessed.

Change-Id: I2c6bbeaa01648d03ff8b3690b7f21cef4e2b9d97
parent 6fab2b83
Loading
Loading
Loading
Loading
+31 −11
Original line number Diff line number Diff line
@@ -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<String,Integer> mRanks;
        private ArraySet<Object> 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