Merge "Fix race condition when accessing ClockChangeListener" into udc-dev am: cec1c35e34

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23356839

Change-Id: I16ac0a71c3474f0aa488674fbc091fd583e16d97
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Austin Wang
2023-05-30 20:41:07 +00:00
committed by Automerger Merge Worker

View File

@@ -45,6 +45,7 @@ import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
private val KEY_TIMESTAMP = "appliedTimestamp" private val KEY_TIMESTAMP = "appliedTimestamp"
@@ -320,20 +321,20 @@ open class ClockRegistry(
} }
} }
public fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) { public suspend fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) {
scope.launch(bgDispatcher) { applySettings(mutator(settings ?: ClockSettings())) } withContext(bgDispatcher) { applySettings(mutator(settings ?: ClockSettings())) }
} }
var currentClockId: ClockId var currentClockId: ClockId
get() = settings?.clockId ?: fallbackClockId get() = settings?.clockId ?: fallbackClockId
set(value) { set(value) {
mutateSetting { it.copy(clockId = value) } scope.launch(bgDispatcher) { mutateSetting { it.copy(clockId = value) } }
} }
var seedColor: Int? var seedColor: Int?
get() = settings?.seedColor get() = settings?.seedColor
set(value) { set(value) {
mutateSetting { it.copy(seedColor = value) } scope.launch(bgDispatcher) { mutateSetting { it.copy(seedColor = value) } }
} }
init { init {
@@ -501,11 +502,25 @@ open class ClockRegistry(
fun createExampleClock(clockId: ClockId): ClockController? = createClock(clockId) fun createExampleClock(clockId: ClockId): ClockController? = createClock(clockId)
fun registerClockChangeListener(listener: ClockChangeListener) = /**
* Adds [listener] to receive future clock changes.
*
* Calling from main thread to make sure the access is thread safe.
*/
fun registerClockChangeListener(listener: ClockChangeListener) {
assertMainThread()
clockChangeListeners.add(listener) clockChangeListeners.add(listener)
}
fun unregisterClockChangeListener(listener: ClockChangeListener) = /**
* Removes [listener] from future clock changes.
*
* Calling from main thread to make sure the access is thread safe.
*/
fun unregisterClockChangeListener(listener: ClockChangeListener) {
assertMainThread()
clockChangeListeners.remove(listener) clockChangeListeners.remove(listener)
}
fun createCurrentClock(): ClockController { fun createCurrentClock(): ClockController {
val clockId = currentClockId val clockId = currentClockId