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

Commit 13226348 authored by Nate Myren's avatar Nate Myren Committed by Android (Google) Code Review
Browse files

Merge "Synchronize DataRepository calls" into rvc-dev

parents e75d30d8 89252e0c
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -163,6 +173,7 @@ abstract class DataRepositoryForPackage<K, V : DataRepository.InactiveTimekeeper
            }
        }
    }
}

/**
 * A convenience to retrieve data from a repository with a composite key