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

Commit a724e0b7 authored by Grace's avatar Grace Committed by Hawkwood Glazier
Browse files

[DO NOT MERGE] Update DefaultClockProvider colors

This is largely a cherry-pick of change
I6b32776dac1853eeab87c2a65da69478e4da5b75 but with
some notable changes to accomadate existing code
differences in tm-qpr-dev.

 - We use a boolean instead of an enum for clock region
   darkness as the enum is in a different place in tm-qpr-dev.
 - We clean up some of the initialization code in
   ClockEventController to prevent testing issues and provide
   a dependency injection point to the screenshot test.
 - Pull the clock color setting from the theme when region
   sampling is disabled instead of defaulting to always dark.

Test: Manually validated colors matched reference images on
light and dark backgrounds with the region sampling feature
both enabled and disabled.

Bug: 229771520
Change-Id: I7906850b5de7f512f55e47e1122dfaacbc9c9406
parent 417feefd
Loading
Loading
Loading
Loading
+3 −16
Original line number Diff line number Diff line
@@ -61,11 +61,7 @@ interface Clock {

    /** Initializes various rendering parameters. If never called, provides reasonable defaults. */
    fun initialize(resources: Resources, dozeFraction: Float, foldFraction: Float) {
        events.onColorPaletteChanged(
                resources,
                ClockDarkness.DEFAULT,
                ClockDarkness.DEFAULT
        )
        events.onColorPaletteChanged(resources, true, true)
        animations.doze(dozeFraction)
        animations.fold(foldFraction)
        events.onTimeTick()
@@ -95,8 +91,8 @@ interface ClockEvents {
    /** Call whenever the color palette should update */
    fun onColorPaletteChanged(
            resources: Resources,
            smallClockIsDark: ClockDarkness,
            largeClockIsDark: ClockDarkness
            smallClockIsDark: Boolean,
            largeClockIsDark: Boolean
    ) { }
}

@@ -120,12 +116,3 @@ data class ClockMetadata(
    val clockId: ClockId,
    val name: String
)

/**
 * Enum for whether clock region is dark or light.
 */
enum class ClockDarkness(val isDark: Boolean) {
    DEFAULT(true),
    DARK(true),
    LIGHT(false)
}
+13 −10
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.view.LayoutInflater
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.plugins.Clock
import com.android.systemui.plugins.ClockAnimations
import com.android.systemui.plugins.ClockDarkness
import com.android.systemui.plugins.ClockEvents
import com.android.systemui.plugins.ClockId
import com.android.systemui.plugins.ClockMetadata
@@ -84,11 +83,11 @@ class DefaultClock(
        resources.getFloat(R.dimen.keyguard_clock_line_spacing_scale_burmese)
    private val defaultLineSpacing = resources.getFloat(R.dimen.keyguard_clock_line_spacing_scale)

    private fun updateClockColor(clock: AnimatableClockView, darkValue: ClockDarkness) {
        val color = if (darkValue.isDark) {
            resources.getColor(android.R.color.system_accent2_600)
        } else {
    private fun updateClockColor(clock: AnimatableClockView, isRegionDark: Boolean) {
        val color = if (isRegionDark) {
            resources.getColor(android.R.color.system_accent1_100)
        } else {
            resources.getColor(android.R.color.system_accent2_600)
        }
        clock.setColors(DOZE_COLOR, color)
        clock.animateAppearOnLockscreen()
@@ -117,8 +116,8 @@ class DefaultClock(

        override fun onColorPaletteChanged(
                resources: Resources,
                smallClockIsDark: ClockDarkness,
                largeClockIsDark: ClockDarkness
                smallClockIsDark: Boolean,
                largeClockIsDark: Boolean
        ) {
            updateClockColor(smallClock, smallClockIsDark)
            updateClockColor(largeClock, largeClockIsDark)
@@ -199,13 +198,17 @@ class DefaultClock(
        clocks.forEach { it.setColors(DOZE_COLOR, DOZE_COLOR) }
    }

    override fun initialize(resources: Resources, dozeFraction: Float, foldFraction: Float) {
    override fun initialize(
            resources: Resources,
            dozeFraction: Float,
            foldFraction: Float
    ) {
        recomputePadding()
        animations = DefaultClockAnimations(dozeFraction, foldFraction)
        events.onColorPaletteChanged(
                resources,
                ClockDarkness.DEFAULT,
                ClockDarkness.DEFAULT
                true,
                true
        )
        events.onTimeTick()
    }
+61 −87
Original line number Diff line number Diff line
@@ -20,17 +20,16 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Resources
import android.graphics.Rect
import android.text.format.DateFormat
import android.util.TypedValue
import android.view.View
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.flags.FeatureFlags
import com.android.systemui.plugins.Clock
import com.android.systemui.plugins.ClockDarkness
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.shared.navigationbar.RegionSamplingHelper
import com.android.systemui.shared.regionsampling.RegionSamplingInstance
import com.android.systemui.statusbar.policy.BatteryController
import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback
import com.android.systemui.statusbar.policy.ConfigurationController
@@ -44,7 +43,7 @@ import javax.inject.Inject
 * Controller for a Clock provided by the registry and used on the keyguard. Instantiated by
 * [KeyguardClockSwitchController]. Functionality is forked from [AnimatableClockController].
 */
class ClockEventController @Inject constructor(
open class ClockEventController @Inject constructor(
        private val statusBarStateController: StatusBarStateController,
        private val broadcastDispatcher: BroadcastDispatcher,
        private val batteryController: BatteryController,
@@ -61,6 +60,7 @@ class ClockEventController @Inject constructor(
            field = value
            if (value != null) {
                value.initialize(resources, dozeAmount, 0f)
                updateRegionSamplers(value)
            }
        }

@@ -71,94 +71,71 @@ class ClockEventController @Inject constructor(
    private var dozeAmount = 0f
    private var isKeyguardShowing = false

    private var smallClockIsDark = ClockDarkness.DEFAULT
    private var largeClockIsDark = ClockDarkness.DEFAULT
    private var smallSamplingBounds = Rect()
    private var largeSamplingBounds = Rect()

    private val regionSamplingEnabled =
            featureFlags.isEnabled(com.android.systemui.flags.Flags.REGION_SAMPLING)

    private fun setClockDarkness(isRegionDark: Boolean): ClockDarkness {
        return if (isRegionDark) {
            ClockDarkness.DARK
    private val updateFun = object : RegionSamplingInstance.UpdateColorCallback {
        override fun updateColors() {
            if (regionSamplingEnabled) {
                smallClockIsDark = smallRegionSamplingInstance.currentRegionDarkness().isDark
                largeClockIsDark = largeRegionSamplingInstance.currentRegionDarkness().isDark
            } else {
            ClockDarkness.LIGHT
                val isLightTheme = TypedValue()
                context.theme.resolveAttribute(android.R.attr.isLightTheme, isLightTheme, true)
                smallClockIsDark = isLightTheme.data == 0
                largeClockIsDark = isLightTheme.data == 0
            }
            clock?.events?.onColorPaletteChanged(resources, smallClockIsDark, largeClockIsDark)
        }
    }

    // TODO: Abstract out the creation of RegionSampler and its fields
    var smallRegionSampling: RegionSamplingHelper? =
            if (!regionSamplingEnabled || clock == null) {
                null
            } else {
                RegionSamplingHelper(clock?.smallClock,
                        object : RegionSamplingHelper.SamplingCallback {
                            override fun onRegionDarknessChanged(isRegionDark: Boolean) {
                                smallClockIsDark = setClockDarkness(isRegionDark)
                                clock?.events?.onColorPaletteChanged(
                                        resources,
                                        smallClockIsDark,
                                        largeClockIsDark
    fun updateRegionSamplers(currentClock: Clock?) {
        smallRegionSamplingInstance = createRegionSampler(
                currentClock?.smallClock,
                mainExecutor,
                bgExecutor,
                regionSamplingEnabled,
                updateFun
        )
                            }

                            override fun getSampledRegion(sampledView: View): Rect {
                                smallSamplingBounds = Rect(
                                        sampledView.left,
                                        sampledView.top,
                                        sampledView.right,
                                        sampledView.bottom
        largeRegionSamplingInstance = createRegionSampler(
                currentClock?.largeClock,
                mainExecutor,
                bgExecutor,
                regionSamplingEnabled,
                updateFun
        )
                                return smallSamplingBounds
                            }

                            override fun isSamplingEnabled(): Boolean {
                                return regionSamplingEnabled
                            }
                        },
                        mainExecutor, bgExecutor)
            }
        smallRegionSamplingInstance.startRegionSampler()
        largeRegionSamplingInstance.startRegionSampler()

    var largeRegionSampling: RegionSamplingHelper? =
            if (!regionSamplingEnabled || clock == null) {
                null
            } else {
                RegionSamplingHelper(clock?.largeClock,
                        object : RegionSamplingHelper.SamplingCallback {
                            override fun onRegionDarknessChanged(isRegionDark: Boolean) {
                                largeClockIsDark = setClockDarkness(isRegionDark)
                                clock?.events?.onColorPaletteChanged(
                                        resources,
                                        smallClockIsDark,
                                        largeClockIsDark
                                )
        updateFun.updateColors()
    }

                            override fun getSampledRegion(sampledView: View): Rect {
                                largeSamplingBounds = Rect(
                                        sampledView.left,
                                        sampledView.top,
                                        sampledView.right,
                                        sampledView.bottom
                                )
                                return largeSamplingBounds
    protected open fun createRegionSampler(
            sampledView: View?,
            mainExecutor: Executor?,
            bgExecutor: Executor?,
            regionSamplingEnabled: Boolean,
            updateFun: RegionSamplingInstance.UpdateColorCallback
    ): RegionSamplingInstance {
        return RegionSamplingInstance(
            sampledView,
            mainExecutor,
            bgExecutor,
            regionSamplingEnabled,
            updateFun)
    }

                            override fun isSamplingEnabled(): Boolean {
                                return regionSamplingEnabled
                            }
                        },
                        mainExecutor, bgExecutor)
            }
    lateinit var smallRegionSamplingInstance: RegionSamplingInstance
    lateinit var largeRegionSamplingInstance: RegionSamplingInstance

    private var smallClockIsDark = true
    private var largeClockIsDark = true

    private val configListener = object : ConfigurationController.ConfigurationListener {
        override fun onThemeChanged() {
            clock?.events?.onColorPaletteChanged(
                    resources,
                    smallClockIsDark,
                    largeClockIsDark
            )
            updateFun.updateColors()
        }
    }

@@ -209,9 +186,6 @@ class ClockEventController @Inject constructor(

    init {
        isDozing = statusBarStateController.isDozing
        smallRegionSampling?.setWindowVisible(true)
        largeRegionSampling?.setWindowVisible(true)
        clock?.events?.onColorPaletteChanged(resources, smallClockIsDark, largeClockIsDark)
    }

    fun registerListeners() {
@@ -226,8 +200,8 @@ class ClockEventController @Inject constructor(
        batteryController.addCallback(batteryCallback)
        keyguardUpdateMonitor.registerCallback(keyguardUpdateMonitorCallback)
        statusBarStateController.addCallback(statusBarStateListener)
        smallRegionSampling?.start(smallSamplingBounds)
        largeRegionSampling?.start(largeSamplingBounds)
        smallRegionSamplingInstance.startRegionSampler()
        largeRegionSamplingInstance.startRegionSampler()
    }

    fun unregisterListeners() {
@@ -236,8 +210,8 @@ class ClockEventController @Inject constructor(
        batteryController.removeCallback(batteryCallback)
        keyguardUpdateMonitor.removeCallback(keyguardUpdateMonitorCallback)
        statusBarStateController.removeCallback(statusBarStateListener)
        smallRegionSampling?.stop()
        largeRegionSampling?.stop()
        smallRegionSamplingInstance.stopRegionSampler()
        largeRegionSamplingInstance.stopRegionSampler()
    }

    /**
@@ -246,8 +220,8 @@ class ClockEventController @Inject constructor(
    fun dump(pw: PrintWriter) {
        pw.println(this)
        clock?.dump(pw)
        smallRegionSampling?.dump(pw)
        largeRegionSampling?.dump(pw)
        smallRegionSamplingInstance.dump(pw)
        largeRegionSamplingInstance.dump(pw)
    }

    companion object {
+4 −1
Original line number Diff line number Diff line
@@ -107,13 +107,15 @@ class ClockEventControllerTest : SysuiTestCase() {
    @Test
    fun themeChanged_verifyClockPaletteUpdated() {
        clockEventController.clock = clock
        verify(events).onColorPaletteChanged(any(), any(), any())

        clockEventController.registerListeners()

        val captor = argumentCaptor<ConfigurationController.ConfigurationListener>()
        verify(configurationController).addCallback(capture(captor))
        captor.value.onThemeChanged()

        verify(events).onColorPaletteChanged(any(), any(), any())
        verify(events, times(2)).onColorPaletteChanged(any(), any(), any())
    }

    @Test
@@ -257,6 +259,7 @@ class ClockEventControllerTest : SysuiTestCase() {

    @Test
    fun unregisterListeners_validate() {
        clockEventController.clock = clock
        clockEventController.unregisterListeners()
        verify(broadcastDispatcher).unregisterReceiver(any())
        verify(configurationController).removeCallback(any())