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

Commit e2a2e37c authored by Hawkwood Glazier's avatar Hawkwood Glazier Committed by Android (Google) Code Review
Browse files

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

parents f0d735e1 60b2a658
Loading
Loading
Loading
Loading
+25 −9
Original line number Diff line number Diff line
@@ -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>()
@@ -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() } }
            }
        }

@@ -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) {
@@ -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
@@ -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}")
            }
@@ -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() }
        }
    }

+7 −2
Original line number Diff line number Diff line
@@ -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() { }
        };
    }

+6 −1
Original line number Diff line number Diff line
@@ -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) }
+1 −1
Original line number Diff line number Diff line
@@ -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);
    }
+22 −6
Original line number Diff line number Diff line
@@ -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")
@@ -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 }