From 0e0b36e2e96bfe8f032bee8e918313196596d6f4 Mon Sep 17 00:00:00 2001 From: Hawkwood Glazier Date: Thu, 17 Aug 2023 15:47:48 +0000 Subject: [PATCH] Select the default clock if current clock id was not found Bug: 295371293 Test: Manually tested invalid clock ids Change-Id: I98d96bf72cbe09895b644a5a53f86499bf74bfab --- .../systemui/shared/clocks/ClockRegistry.kt | 12 ++++++++++++ .../shared/clocks/ClockRegistryTest.kt | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/ClockRegistry.kt b/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/ClockRegistry.kt index d65edae1caf00..0fa4ebd69bf2b 100644 --- a/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/ClockRegistry.kt +++ b/packages/SystemUI/customization/src/com/android/systemui/shared/clocks/ClockRegistry.kt @@ -409,6 +409,18 @@ open class ClockRegistry( scope.launch(bgDispatcher) { mutateSetting { it.copy(seedColor = value) } } } + // Returns currentClockId if clock is connected, otherwise DEFAULT_CLOCK_ID. Since this + // is dependent on which clocks are connected, it may change when a clock is installed or + // removed from the device (unlike currentClockId). + // TODO: Merge w/ CurrentClockId when we convert to a flow. We shouldn't need both behaviors. + val activeClockId: String + get() { + if (!availableClocks.containsKey(currentClockId)) { + return DEFAULT_CLOCK_ID + } + return currentClockId + } + init { // Register default clock designs for (clock in defaultClockProvider.getClocks()) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt index 9495fdd50a7e3..ecaf13711b52e 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt @@ -226,6 +226,25 @@ class ClockRegistryTest : SysuiTestCase() { assertEquals(mockClock, clock) } + @Test + fun activeClockId_changeAfterPluginConnected() { + val plugin1 = FakeClockPlugin() + .addClock("clock_1", "clock 1") + .addClock("clock_2", "clock 2") + + val plugin2 = FakeClockPlugin() + .addClock("clock_3", "clock 3", { mockClock }) + .addClock("clock_4", "clock 4") + + registry.applySettings(ClockSettings("clock_3", null)) + + pluginListener.onPluginLoaded(plugin1, mockContext, mockPluginLifecycle) + assertEquals(DEFAULT_CLOCK_ID, registry.activeClockId) + + pluginListener.onPluginLoaded(plugin2, mockContext, mockPluginLifecycle) + assertEquals("clock_3", registry.activeClockId) + } + @Test fun createDefaultClock_pluginDisconnected() { val plugin1 = FakeClockPlugin()