Merge "Add onAvailableClockChanged to ClockRegistry" into tm-qpr-dev

This commit is contained in:
Hawkwood Glazier
2023-02-15 14:49:20 +00:00
committed by Android (Google) Code Review
5 changed files with 62 additions and 20 deletions

View File

@@ -51,9 +51,12 @@ open class ClockRegistry(
defaultClockProvider: ClockProvider, defaultClockProvider: ClockProvider,
val fallbackClockId: ClockId = DEFAULT_CLOCK_ID, val fallbackClockId: ClockId = DEFAULT_CLOCK_ID,
) { ) {
// Usually this would be a typealias, but a SAM provides better java interop interface ClockChangeListener {
fun interface ClockChangeListener { // Called when the active clock changes
fun onClockChanged() fun onCurrentClockChanged() {}
// Called when the list of available clocks changes
fun onAvailableClocksChanged() {}
} }
private val availableClocks = mutableMapOf<ClockId, ClockInfo>() private val availableClocks = mutableMapOf<ClockId, ClockInfo>()
@@ -92,7 +95,7 @@ open class ClockRegistry(
protected set(value) { protected set(value) {
if (field != value) { if (field != value) {
field = value field = value
scope.launch(mainDispatcher) { onClockChanged() } scope.launch(mainDispatcher) { onClockChanged { it.onCurrentClockChanged() } }
} }
} }
@@ -164,9 +167,9 @@ open class ClockRegistry(
Assert.isNotMainThread() Assert.isNotMainThread()
} }
private fun onClockChanged() { private fun onClockChanged(func: (ClockChangeListener) -> Unit) {
assertMainThread() assertMainThread()
clockChangeListeners.forEach { it.onClockChanged() } clockChangeListeners.forEach(func)
} }
private fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) { private fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) {
@@ -241,6 +244,7 @@ open class ClockRegistry(
} }
private fun connectClocks(provider: ClockProvider) { private fun connectClocks(provider: ClockProvider) {
var isAvailableChanged = false
val currentId = currentClockId val currentId = currentClockId
for (clock in provider.getClocks()) { for (clock in provider.getClocks()) {
val id = clock.clockId val id = clock.clockId
@@ -251,10 +255,11 @@ open class ClockRegistry(
"Clock Id conflict: $id is registered by both " + "Clock Id conflict: $id is registered by both " +
"${provider::class.simpleName} and ${current.provider::class.simpleName}" "${provider::class.simpleName} and ${current.provider::class.simpleName}"
) )
return continue
} }
availableClocks[id] = ClockInfo(clock, provider) availableClocks[id] = ClockInfo(clock, provider)
isAvailableChanged = true
if (DEBUG) { if (DEBUG) {
Log.i(TAG, "Added ${clock.clockId}") Log.i(TAG, "Added ${clock.clockId}")
} }
@@ -263,24 +268,35 @@ open class ClockRegistry(
if (DEBUG) { if (DEBUG) {
Log.i(TAG, "Current clock ($currentId) was connected") Log.i(TAG, "Current clock ($currentId) was connected")
} }
onClockChanged() onClockChanged { it.onCurrentClockChanged() }
} }
} }
if (isAvailableChanged) {
onClockChanged { it.onAvailableClocksChanged() }
}
} }
private fun disconnectClocks(provider: ClockProvider) { private fun disconnectClocks(provider: ClockProvider) {
var isAvailableChanged = false
val currentId = currentClockId val currentId = currentClockId
for (clock in provider.getClocks()) { for (clock in provider.getClocks()) {
availableClocks.remove(clock.clockId) availableClocks.remove(clock.clockId)
isAvailableChanged = true
if (DEBUG) { if (DEBUG) {
Log.i(TAG, "Removed ${clock.clockId}") Log.i(TAG, "Removed ${clock.clockId}")
} }
if (currentId == clock.clockId) { if (currentId == clock.clockId) {
Log.w(TAG, "Current clock ($currentId) was disconnected") Log.w(TAG, "Current clock ($currentId) was disconnected")
onClockChanged() onClockChanged { it.onCurrentClockChanged() }
} }
} }
if (isAvailableChanged) {
onClockChanged { it.onAvailableClocksChanged() }
}
} }
fun getClocks(): List<ClockMetadata> { fun getClocks(): List<ClockMetadata> {

View File

@@ -151,8 +151,13 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
mLogBuffer = logBuffer; mLogBuffer = logBuffer;
mView.setLogBuffer(mLogBuffer); mView.setLogBuffer(mLogBuffer);
mClockChangedListener = () -> { mClockChangedListener = new ClockRegistry.ClockChangeListener() {
@Override
public void onCurrentClockChanged() {
setClock(mClockRegistry.createCurrentClock()); setClock(mClockRegistry.createCurrentClock());
}
@Override
public void onAvailableClocksChanged() { }
}; };
} }

View File

@@ -170,7 +170,12 @@ constructor(
} }
private fun setUpClock(parentView: ViewGroup) { private fun setUpClock(parentView: ViewGroup) {
val clockChangeListener = ClockRegistry.ClockChangeListener { onClockChanged(parentView) } val clockChangeListener =
object : ClockRegistry.ClockChangeListener {
override fun onCurrentClockChanged() {
onClockChanged(parentView)
}
}
clockRegistry.registerClockChangeListener(clockChangeListener) clockRegistry.registerClockChangeListener(clockChangeListener)
disposables.add( disposables.add(
DisposableHandle { clockRegistry.unregisterClockChangeListener(clockChangeListener) } DisposableHandle { clockRegistry.unregisterClockChangeListener(clockChangeListener) }

View File

@@ -241,7 +241,7 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase {
mController.init(); mController.init();
verify(mClockRegistry).registerClockChangeListener(listenerArgumentCaptor.capture()); verify(mClockRegistry).registerClockChangeListener(listenerArgumentCaptor.capture());
listenerArgumentCaptor.getValue().onClockChanged(); listenerArgumentCaptor.getValue().onCurrentClockChanged();
verify(mView, times(2)).setClock(mClockController, StatusBarState.SHADE); verify(mView, times(2)).setClock(mClockController, StatusBarState.SHADE);
verify(mClockEventController, times(2)).setClock(mClockController); verify(mClockEventController, times(2)).setClock(mClockController);
} }

View File

@@ -230,7 +230,7 @@ class ClockRegistryTest : SysuiTestCase() {
} }
@Test @Test
fun pluginRemoved_clockChanged() { fun pluginRemoved_clockAndListChanged() {
val plugin1 = FakeClockPlugin() val plugin1 = FakeClockPlugin()
.addClock("clock_1", "clock 1") .addClock("clock_1", "clock 1")
.addClock("clock_2", "clock 2") .addClock("clock_2", "clock 2")
@@ -239,20 +239,36 @@ class ClockRegistryTest : SysuiTestCase() {
.addClock("clock_3", "clock 3", { mockClock }) .addClock("clock_3", "clock 3", { mockClock })
.addClock("clock_4", "clock 4") .addClock("clock_4", "clock 4")
registry.applySettings(ClockSettings("clock_3", null))
pluginListener.onPluginConnected(plugin1, mockContext)
pluginListener.onPluginConnected(plugin2, mockContext)
var changeCallCount = 0 var changeCallCount = 0
registry.registerClockChangeListener { changeCallCount++ } var listChangeCallCount = 0
registry.registerClockChangeListener(object : ClockRegistry.ClockChangeListener {
override fun onCurrentClockChanged() { changeCallCount++ }
override fun onAvailableClocksChanged() { listChangeCallCount++ }
})
registry.applySettings(ClockSettings("clock_3", null))
assertEquals(0, changeCallCount)
assertEquals(0, listChangeCallCount)
pluginListener.onPluginConnected(plugin1, mockContext)
assertEquals(0, changeCallCount)
assertEquals(1, listChangeCallCount)
pluginListener.onPluginConnected(plugin2, mockContext)
assertEquals(1, changeCallCount)
assertEquals(2, listChangeCallCount)
pluginListener.onPluginDisconnected(plugin1) pluginListener.onPluginDisconnected(plugin1)
assertEquals(0, changeCallCount) assertEquals(1, changeCallCount)
assertEquals(3, listChangeCallCount)
pluginListener.onPluginDisconnected(plugin2) pluginListener.onPluginDisconnected(plugin2)
assertEquals(1, changeCallCount) assertEquals(2, changeCallCount)
assertEquals(4, listChangeCallCount)
} }
@Test @Test
fun jsonDeserialization_gotExpectedObject() { fun jsonDeserialization_gotExpectedObject() {
val expected = ClockSettings("ID", null).apply { _applied_timestamp = 500 } val expected = ClockSettings("ID", null).apply { _applied_timestamp = 500 }