Loading src/com/android/permissioncontroller/permission/data/DataRepository.kt +19 −8 Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ package com.android.permissioncontroller.permission.data import android.app.ActivityManager import android.content.ComponentCallbacks2 import android.content.res.Configuration import androidx.annotation.GuardedBy import androidx.annotation.MainThread import com.android.permissioncontroller.PermissionControllerApplication import java.util.concurrent.TimeUnit Loading @@ -41,7 +42,9 @@ abstract class DataRepository<K, V : DataRepository.InactiveTimekeeper> : Compon private val TIME_THRESHOLD_TIGHT_NANOS: Long = TimeUnit.NANOSECONDS.convert(1, TimeUnit.MINUTES) private val TIME_THRESHOLD_ALL_NANOS: Long = 0 @GuardedBy("lock") protected val data = mutableMapOf<K, V>() protected val lock = Any() /** * Whether or not this data repository has been registered as a component callback yet Loading @@ -61,8 +64,10 @@ abstract class DataRepository<K, V : DataRepository.InactiveTimekeeper> : Compon * @return The cached or newly created Value for the given Key */ operator fun get(key: K): V { synchronized(lock) { return data.getOrPut(key) { newValue(key) } } } /** * Generate a new value type from the given data Loading Loading @@ -106,16 +111,20 @@ abstract class DataRepository<K, V : DataRepository.InactiveTimekeeper> : Compon } fun invalidateSingle(key: K) { synchronized(lock) { data.remove(key) } } private fun trimInactiveData(threshold: Long) { synchronized(lock) { data.keys.toList().forEach { key -> if (data[key]?.timeInactive?.let { it >= threshold } == true) { data.remove(key) } } } } /** * Interface which describes an object which can track how long it has been inactive, and if Loading Loading @@ -156,6 +165,7 @@ abstract class DataRepositoryForPackage<K, V : DataRepository.InactiveTimekeeper * @param packageName The package to be invalidated */ fun invalidateAllForPackage(packageName: String) { synchronized(lock) { for (key in data.keys.toSet()) { if (key is Pair<*, *> || key is Triple<*, *, *> && key.first == packageName) { data.remove(key) Loading @@ -163,6 +173,7 @@ abstract class DataRepositoryForPackage<K, V : DataRepository.InactiveTimekeeper } } } } /** * A convenience to retrieve data from a repository with a composite key Loading Loading
src/com/android/permissioncontroller/permission/data/DataRepository.kt +19 −8 Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ package com.android.permissioncontroller.permission.data import android.app.ActivityManager import android.content.ComponentCallbacks2 import android.content.res.Configuration import androidx.annotation.GuardedBy import androidx.annotation.MainThread import com.android.permissioncontroller.PermissionControllerApplication import java.util.concurrent.TimeUnit Loading @@ -41,7 +42,9 @@ abstract class DataRepository<K, V : DataRepository.InactiveTimekeeper> : Compon private val TIME_THRESHOLD_TIGHT_NANOS: Long = TimeUnit.NANOSECONDS.convert(1, TimeUnit.MINUTES) private val TIME_THRESHOLD_ALL_NANOS: Long = 0 @GuardedBy("lock") protected val data = mutableMapOf<K, V>() protected val lock = Any() /** * Whether or not this data repository has been registered as a component callback yet Loading @@ -61,8 +64,10 @@ abstract class DataRepository<K, V : DataRepository.InactiveTimekeeper> : Compon * @return The cached or newly created Value for the given Key */ operator fun get(key: K): V { synchronized(lock) { return data.getOrPut(key) { newValue(key) } } } /** * Generate a new value type from the given data Loading Loading @@ -106,16 +111,20 @@ abstract class DataRepository<K, V : DataRepository.InactiveTimekeeper> : Compon } fun invalidateSingle(key: K) { synchronized(lock) { data.remove(key) } } private fun trimInactiveData(threshold: Long) { synchronized(lock) { data.keys.toList().forEach { key -> if (data[key]?.timeInactive?.let { it >= threshold } == true) { data.remove(key) } } } } /** * Interface which describes an object which can track how long it has been inactive, and if Loading Loading @@ -156,6 +165,7 @@ abstract class DataRepositoryForPackage<K, V : DataRepository.InactiveTimekeeper * @param packageName The package to be invalidated */ fun invalidateAllForPackage(packageName: String) { synchronized(lock) { for (key in data.keys.toSet()) { if (key is Pair<*, *> || key is Triple<*, *, *> && key.first == packageName) { data.remove(key) Loading @@ -163,6 +173,7 @@ abstract class DataRepositoryForPackage<K, V : DataRepository.InactiveTimekeeper } } } } /** * A convenience to retrieve data from a repository with a composite key Loading