Loading packages/SystemUI/customization/src/com/android/systemui/shared/clocks/ClockRegistry.kt +25 −9 Original line number Diff line number Diff line Loading @@ -51,9 +51,12 @@ open class ClockRegistry( defaultClockProvider: ClockProvider, val fallbackClockId: ClockId = DEFAULT_CLOCK_ID, ) { // Usually this would be a typealias, but a SAM provides better java interop fun interface ClockChangeListener { fun onClockChanged() interface ClockChangeListener { // Called when the active clock changes fun onCurrentClockChanged() {} // Called when the list of available clocks changes fun onAvailableClocksChanged() {} } private val availableClocks = mutableMapOf<ClockId, ClockInfo>() Loading Loading @@ -92,7 +95,7 @@ open class ClockRegistry( protected set(value) { if (field != value) { field = value scope.launch(mainDispatcher) { onClockChanged() } scope.launch(mainDispatcher) { onClockChanged { it.onCurrentClockChanged() } } } } Loading Loading @@ -164,9 +167,9 @@ open class ClockRegistry( Assert.isNotMainThread() } private fun onClockChanged() { private fun onClockChanged(func: (ClockChangeListener) -> Unit) { assertMainThread() clockChangeListeners.forEach { it.onClockChanged() } clockChangeListeners.forEach(func) } private fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) { Loading Loading @@ -241,6 +244,7 @@ open class ClockRegistry( } private fun connectClocks(provider: ClockProvider) { var isAvailableChanged = false val currentId = currentClockId for (clock in provider.getClocks()) { val id = clock.clockId Loading @@ -251,10 +255,11 @@ open class ClockRegistry( "Clock Id conflict: $id is registered by both " + "${provider::class.simpleName} and ${current.provider::class.simpleName}" ) return continue } availableClocks[id] = ClockInfo(clock, provider) isAvailableChanged = true if (DEBUG) { Log.i(TAG, "Added ${clock.clockId}") } Loading @@ -263,23 +268,34 @@ open class ClockRegistry( if (DEBUG) { Log.i(TAG, "Current clock ($currentId) was connected") } onClockChanged() onClockChanged { it.onCurrentClockChanged() } } } if (isAvailableChanged) { onClockChanged { it.onAvailableClocksChanged() } } } private fun disconnectClocks(provider: ClockProvider) { var isAvailableChanged = false val currentId = currentClockId for (clock in provider.getClocks()) { availableClocks.remove(clock.clockId) isAvailableChanged = true if (DEBUG) { Log.i(TAG, "Removed ${clock.clockId}") } if (currentId == clock.clockId) { Log.w(TAG, "Current clock ($currentId) was disconnected") onClockChanged() onClockChanged { it.onCurrentClockChanged() } } } if (isAvailableChanged) { onClockChanged { it.onAvailableClocksChanged() } } } Loading packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java +7 −2 Original line number Diff line number Diff line Loading @@ -151,8 +151,13 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS mLogBuffer = logBuffer; mView.setLogBuffer(mLogBuffer); mClockChangedListener = () -> { mClockChangedListener = new ClockRegistry.ClockChangeListener() { @Override public void onCurrentClockChanged() { setClock(mClockRegistry.createCurrentClock()); } @Override public void onAvailableClocksChanged() { } }; } Loading packages/SystemUI/src/com/android/systemui/keyguard/ui/preview/KeyguardPreviewRenderer.kt +6 −1 Original line number Diff line number Diff line Loading @@ -170,7 +170,12 @@ constructor( } private fun setUpClock(parentView: ViewGroup) { val clockChangeListener = ClockRegistry.ClockChangeListener { onClockChanged(parentView) } val clockChangeListener = object : ClockRegistry.ClockChangeListener { override fun onCurrentClockChanged() { onClockChanged(parentView) } } clockRegistry.registerClockChangeListener(clockChangeListener) disposables.add( DisposableHandle { clockRegistry.unregisterClockChangeListener(clockChangeListener) } Loading packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchControllerTest.java +1 −1 Original line number Diff line number Diff line Loading @@ -241,7 +241,7 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase { mController.init(); verify(mClockRegistry).registerClockChangeListener(listenerArgumentCaptor.capture()); listenerArgumentCaptor.getValue().onClockChanged(); listenerArgumentCaptor.getValue().onCurrentClockChanged(); verify(mView, times(2)).setClock(mClockController, StatusBarState.SHADE); verify(mClockEventController, times(2)).setClock(mClockController); } Loading packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt +22 −6 Original line number Diff line number Diff line Loading @@ -230,7 +230,7 @@ class ClockRegistryTest : SysuiTestCase() { } @Test fun pluginRemoved_clockChanged() { fun pluginRemoved_clockAndListChanged() { val plugin1 = FakeClockPlugin() .addClock("clock_1", "clock 1") .addClock("clock_2", "clock 2") Loading @@ -239,20 +239,36 @@ class ClockRegistryTest : SysuiTestCase() { .addClock("clock_3", "clock 3", { mockClock }) .addClock("clock_4", "clock 4") var changeCallCount = 0 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) pluginListener.onPluginConnected(plugin2, mockContext) assertEquals(0, changeCallCount) assertEquals(1, listChangeCallCount) var changeCallCount = 0 registry.registerClockChangeListener { changeCallCount++ } pluginListener.onPluginConnected(plugin2, mockContext) assertEquals(1, changeCallCount) assertEquals(2, listChangeCallCount) pluginListener.onPluginDisconnected(plugin1) assertEquals(0, changeCallCount) assertEquals(1, changeCallCount) assertEquals(3, listChangeCallCount) pluginListener.onPluginDisconnected(plugin2) assertEquals(1, changeCallCount) assertEquals(2, changeCallCount) assertEquals(4, listChangeCallCount) } @Test fun jsonDeserialization_gotExpectedObject() { val expected = ClockSettings("ID", null).apply { _applied_timestamp = 500 } Loading Loading
packages/SystemUI/customization/src/com/android/systemui/shared/clocks/ClockRegistry.kt +25 −9 Original line number Diff line number Diff line Loading @@ -51,9 +51,12 @@ open class ClockRegistry( defaultClockProvider: ClockProvider, val fallbackClockId: ClockId = DEFAULT_CLOCK_ID, ) { // Usually this would be a typealias, but a SAM provides better java interop fun interface ClockChangeListener { fun onClockChanged() interface ClockChangeListener { // Called when the active clock changes fun onCurrentClockChanged() {} // Called when the list of available clocks changes fun onAvailableClocksChanged() {} } private val availableClocks = mutableMapOf<ClockId, ClockInfo>() Loading Loading @@ -92,7 +95,7 @@ open class ClockRegistry( protected set(value) { if (field != value) { field = value scope.launch(mainDispatcher) { onClockChanged() } scope.launch(mainDispatcher) { onClockChanged { it.onCurrentClockChanged() } } } } Loading Loading @@ -164,9 +167,9 @@ open class ClockRegistry( Assert.isNotMainThread() } private fun onClockChanged() { private fun onClockChanged(func: (ClockChangeListener) -> Unit) { assertMainThread() clockChangeListeners.forEach { it.onClockChanged() } clockChangeListeners.forEach(func) } private fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) { Loading Loading @@ -241,6 +244,7 @@ open class ClockRegistry( } private fun connectClocks(provider: ClockProvider) { var isAvailableChanged = false val currentId = currentClockId for (clock in provider.getClocks()) { val id = clock.clockId Loading @@ -251,10 +255,11 @@ open class ClockRegistry( "Clock Id conflict: $id is registered by both " + "${provider::class.simpleName} and ${current.provider::class.simpleName}" ) return continue } availableClocks[id] = ClockInfo(clock, provider) isAvailableChanged = true if (DEBUG) { Log.i(TAG, "Added ${clock.clockId}") } Loading @@ -263,23 +268,34 @@ open class ClockRegistry( if (DEBUG) { Log.i(TAG, "Current clock ($currentId) was connected") } onClockChanged() onClockChanged { it.onCurrentClockChanged() } } } if (isAvailableChanged) { onClockChanged { it.onAvailableClocksChanged() } } } private fun disconnectClocks(provider: ClockProvider) { var isAvailableChanged = false val currentId = currentClockId for (clock in provider.getClocks()) { availableClocks.remove(clock.clockId) isAvailableChanged = true if (DEBUG) { Log.i(TAG, "Removed ${clock.clockId}") } if (currentId == clock.clockId) { Log.w(TAG, "Current clock ($currentId) was disconnected") onClockChanged() onClockChanged { it.onCurrentClockChanged() } } } if (isAvailableChanged) { onClockChanged { it.onAvailableClocksChanged() } } } Loading
packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java +7 −2 Original line number Diff line number Diff line Loading @@ -151,8 +151,13 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS mLogBuffer = logBuffer; mView.setLogBuffer(mLogBuffer); mClockChangedListener = () -> { mClockChangedListener = new ClockRegistry.ClockChangeListener() { @Override public void onCurrentClockChanged() { setClock(mClockRegistry.createCurrentClock()); } @Override public void onAvailableClocksChanged() { } }; } Loading
packages/SystemUI/src/com/android/systemui/keyguard/ui/preview/KeyguardPreviewRenderer.kt +6 −1 Original line number Diff line number Diff line Loading @@ -170,7 +170,12 @@ constructor( } private fun setUpClock(parentView: ViewGroup) { val clockChangeListener = ClockRegistry.ClockChangeListener { onClockChanged(parentView) } val clockChangeListener = object : ClockRegistry.ClockChangeListener { override fun onCurrentClockChanged() { onClockChanged(parentView) } } clockRegistry.registerClockChangeListener(clockChangeListener) disposables.add( DisposableHandle { clockRegistry.unregisterClockChangeListener(clockChangeListener) } Loading
packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchControllerTest.java +1 −1 Original line number Diff line number Diff line Loading @@ -241,7 +241,7 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase { mController.init(); verify(mClockRegistry).registerClockChangeListener(listenerArgumentCaptor.capture()); listenerArgumentCaptor.getValue().onClockChanged(); listenerArgumentCaptor.getValue().onCurrentClockChanged(); verify(mView, times(2)).setClock(mClockController, StatusBarState.SHADE); verify(mClockEventController, times(2)).setClock(mClockController); } Loading
packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt +22 −6 Original line number Diff line number Diff line Loading @@ -230,7 +230,7 @@ class ClockRegistryTest : SysuiTestCase() { } @Test fun pluginRemoved_clockChanged() { fun pluginRemoved_clockAndListChanged() { val plugin1 = FakeClockPlugin() .addClock("clock_1", "clock 1") .addClock("clock_2", "clock 2") Loading @@ -239,20 +239,36 @@ class ClockRegistryTest : SysuiTestCase() { .addClock("clock_3", "clock 3", { mockClock }) .addClock("clock_4", "clock 4") var changeCallCount = 0 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) pluginListener.onPluginConnected(plugin2, mockContext) assertEquals(0, changeCallCount) assertEquals(1, listChangeCallCount) var changeCallCount = 0 registry.registerClockChangeListener { changeCallCount++ } pluginListener.onPluginConnected(plugin2, mockContext) assertEquals(1, changeCallCount) assertEquals(2, listChangeCallCount) pluginListener.onPluginDisconnected(plugin1) assertEquals(0, changeCallCount) assertEquals(1, changeCallCount) assertEquals(3, listChangeCallCount) pluginListener.onPluginDisconnected(plugin2) assertEquals(1, changeCallCount) assertEquals(2, changeCallCount) assertEquals(4, listChangeCallCount) } @Test fun jsonDeserialization_gotExpectedObject() { val expected = ClockSettings("ID", null).apply { _applied_timestamp = 500 } Loading