From 0c22b1da5e27400a30fe0fff4e293cf867120ac0 Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Wed, 16 Sep 2020 10:07:55 -0700 Subject: [PATCH] Generalize SparseArrayMap. SparseArrayMap required Strings as keys, which restricted its uses. Now it can take any object as a key, which better reflects the underlying ArrayMap generalization. Bug: 138469672 Test: atest com.android.server.job.controllers.QuotaControllerTest Test: atest QuotaTrackerTest Test: atest SparseArrayMapTest Change-Id: If6da7dfdf3cdd9be0fe9006484c78c574ef2028b --- .../job/controllers/QuotaController.java | 13 +++-- api/test-current.txt | 20 ++++---- core/java/android/util/SparseArrayMap.java | 49 ++++++++++--------- .../server/utils/quota/QuotaTracker.java | 2 +- .../android/server/utils/quota/UptcMap.java | 2 +- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/QuotaController.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/QuotaController.java index 4e37a1e956395..c06e19cbf6870 100644 --- a/apex/jobscheduler/service/java/com/android/server/job/controllers/QuotaController.java +++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/QuotaController.java @@ -296,13 +296,14 @@ public final class QuotaController extends StateController { } /** List of all tracked jobs keyed by source package-userId combo. */ - private final SparseArrayMap> mTrackedJobs = new SparseArrayMap<>(); + private final SparseArrayMap> mTrackedJobs = new SparseArrayMap<>(); /** Timer for each package-userId combo. */ - private final SparseArrayMap mPkgTimers = new SparseArrayMap<>(); + private final SparseArrayMap mPkgTimers = new SparseArrayMap<>(); /** List of all timing sessions for a package-userId combo, in chronological order. */ - private final SparseArrayMap> mTimingSessions = new SparseArrayMap<>(); + private final SparseArrayMap> mTimingSessions = + new SparseArrayMap<>(); /** * Listener to track and manage when each package comes back within quota. @@ -311,7 +312,8 @@ public final class QuotaController extends StateController { private final InQuotaAlarmListener mInQuotaAlarmListener = new InQuotaAlarmListener(); /** Cached calculation results for each app, with the standby buckets as the array indices. */ - private final SparseArrayMap mExecutionStatsCache = new SparseArrayMap<>(); + private final SparseArrayMap mExecutionStatsCache = + new SparseArrayMap<>(); /** List of UIDs currently in the foreground. */ private final SparseBooleanArray mForegroundUids = new SparseBooleanArray(); @@ -1225,7 +1227,8 @@ public final class QuotaController extends StateController { } private class UidConstraintUpdater implements Consumer { - private final SparseArrayMap mToScheduleStartAlarms = new SparseArrayMap<>(); + private final SparseArrayMap mToScheduleStartAlarms = + new SparseArrayMap<>(); public boolean wasJobChanged; @Override diff --git a/api/test-current.txt b/api/test-current.txt index 30972b65a1fdd..a30be783f4e5a 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -5076,23 +5076,23 @@ package android.util { field public static final String SETTINGS_WIFITRACKER2 = "settings_wifitracker2"; } - public class SparseArrayMap { + public class SparseArrayMap { ctor public SparseArrayMap(); - method public void add(int, @NonNull String, @Nullable T); + method public void add(int, @NonNull K, @Nullable V); method public void clear(); - method public boolean contains(int, @NonNull String); + method public boolean contains(int, @NonNull K); method public void delete(int); - method @Nullable public T delete(int, @NonNull String); - method public void forEach(@NonNull java.util.function.Consumer); - method @Nullable public T get(int, @NonNull String); - method @Nullable public T getOrDefault(int, @NonNull String, T); + method @Nullable public V delete(int, @NonNull K); + method public void forEach(@NonNull java.util.function.Consumer); + method @Nullable public V get(int, @NonNull K); + method @Nullable public V getOrDefault(int, @NonNull K, V); method public int indexOfKey(int); - method public int indexOfKey(int, @NonNull String); + method public int indexOfKey(int, @NonNull K); method public int keyAt(int); - method @NonNull public String keyAt(int, int); + method @NonNull public K keyAt(int, int); method public int numElementsForKey(int); method public int numMaps(); - method @Nullable public T valueAt(int, int); + method @Nullable public V valueAt(int, int); } public class TimeUtils { diff --git a/core/java/android/util/SparseArrayMap.java b/core/java/android/util/SparseArrayMap.java index 3ec6b810fda83..3287c279c87f2 100644 --- a/core/java/android/util/SparseArrayMap.java +++ b/core/java/android/util/SparseArrayMap.java @@ -26,16 +26,17 @@ import java.util.function.Consumer; * A sparse array of ArrayMaps, which is suitable for holding (userId, packageName)->object * associations. * - * @param Any class + * @param Any class + * @param Any class * @hide */ @TestApi -public class SparseArrayMap { - private final SparseArray> mData = new SparseArray<>(); +public class SparseArrayMap { + private final SparseArray> mData = new SparseArray<>(); - /** Add an entry associating obj with the int-String pair. */ - public void add(int key, @NonNull String mapKey, @Nullable T obj) { - ArrayMap data = mData.get(key); + /** Add an entry associating obj with the int-K pair. */ + public void add(int key, @NonNull K mapKey, @Nullable V obj) { + ArrayMap data = mData.get(key); if (data == null) { data = new ArrayMap<>(); mData.put(key, data); @@ -50,8 +51,8 @@ public class SparseArrayMap { } } - /** Return true if the structure contains an explicit entry for the int-String pair. */ - public boolean contains(int key, @NonNull String mapKey) { + /** Return true if the structure contains an explicit entry for the int-K pair. */ + public boolean contains(int key, @NonNull K mapKey) { return mData.contains(key) && mData.get(key).containsKey(mapKey); } @@ -66,8 +67,8 @@ public class SparseArrayMap { * @return Returns the value that was stored under the keys, or null if there was none. */ @Nullable - public T delete(int key, @NonNull String mapKey) { - ArrayMap data = mData.get(key); + public V delete(int key, @NonNull K mapKey) { + ArrayMap data = mData.get(key); if (data != null) { return data.remove(mapKey); } @@ -75,11 +76,11 @@ public class SparseArrayMap { } /** - * Get the value associated with the int-String pair. + * Get the value associated with the int-K pair. */ @Nullable - public T get(int key, @NonNull String mapKey) { - ArrayMap data = mData.get(key); + public V get(int key, @NonNull K mapKey) { + ArrayMap data = mData.get(key); if (data != null) { return data.get(mapKey); } @@ -91,9 +92,9 @@ public class SparseArrayMap { * map contains no mapping for them. */ @Nullable - public T getOrDefault(int key, @NonNull String mapKey, T defaultValue) { + public V getOrDefault(int key, @NonNull K mapKey, V defaultValue) { if (mData.contains(key)) { - ArrayMap data = mData.get(key); + ArrayMap data = mData.get(key); if (data != null && data.containsKey(mapKey)) { return data.get(mapKey); } @@ -111,8 +112,8 @@ public class SparseArrayMap { * * @see SparseArray#indexOfKey */ - public int indexOfKey(int key, @NonNull String mapKey) { - ArrayMap data = mData.get(key); + public int indexOfKey(int key, @NonNull K mapKey) { + ArrayMap data = mData.get(key); if (data != null) { return data.indexOfKey(mapKey); } @@ -126,7 +127,7 @@ public class SparseArrayMap { /** Returns the map's key at the given mapIndex for the given keyIndex. */ @NonNull - public String keyAt(int keyIndex, int mapIndex) { + public K keyAt(int keyIndex, int mapIndex) { return mData.valueAt(keyIndex).keyAt(mapIndex); } @@ -137,20 +138,20 @@ public class SparseArrayMap { /** Returns the number of elements in the map of the given key. */ public int numElementsForKey(int key) { - ArrayMap data = mData.get(key); + ArrayMap data = mData.get(key); return data == null ? 0 : data.size(); } - /** Returns the value T at the given key and map index. */ + /** Returns the value V at the given key and map index. */ @Nullable - public T valueAt(int keyIndex, int mapIndex) { + public V valueAt(int keyIndex, int mapIndex) { return mData.valueAt(keyIndex).valueAt(mapIndex); } - /** Iterate through all int-String pairs and operate on all of the values. */ - public void forEach(@NonNull Consumer consumer) { + /** Iterate through all int-K pairs and operate on all of the values. */ + public void forEach(@NonNull Consumer consumer) { for (int i = numMaps() - 1; i >= 0; --i) { - ArrayMap data = mData.valueAt(i); + ArrayMap data = mData.valueAt(i); for (int j = data.size() - 1; j >= 0; --j) { consumer.accept(data.valueAt(j)); } diff --git a/services/core/java/com/android/server/utils/quota/QuotaTracker.java b/services/core/java/com/android/server/utils/quota/QuotaTracker.java index 7a17c64795777..c1dfcf772f84f 100644 --- a/services/core/java/com/android/server/utils/quota/QuotaTracker.java +++ b/services/core/java/com/android/server/utils/quota/QuotaTracker.java @@ -95,7 +95,7 @@ abstract class QuotaTracker { /** "Free quota status" for apps. */ @GuardedBy("mLock") - private final SparseArrayMap mFreeQuota = new SparseArrayMap<>(); + private final SparseArrayMap mFreeQuota = new SparseArrayMap<>(); private final AlarmManager mAlarmManager; protected final Context mContext; diff --git a/services/core/java/com/android/server/utils/quota/UptcMap.java b/services/core/java/com/android/server/utils/quota/UptcMap.java index a3d6ee52db6a5..77a2a999fe2ab 100644 --- a/services/core/java/com/android/server/utils/quota/UptcMap.java +++ b/services/core/java/com/android/server/utils/quota/UptcMap.java @@ -31,7 +31,7 @@ import java.util.function.Function; * @see Uptc */ class UptcMap { - private final SparseArrayMap> mData = new SparseArrayMap<>(); + private final SparseArrayMap> mData = new SparseArrayMap<>(); public void add(int userId, @NonNull String packageName, @Nullable String tag, @Nullable T obj) {