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

Commit e5174f3f authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Record Trace when clock changes" into main

parents b5ae989a d1814115
Loading
Loading
Loading
Loading
+63 −12
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import android.app.UserSwitchObserver
import android.content.Context
import android.database.ContentObserver
import android.net.Uri
import android.os.Trace
import android.os.UserHandle
import android.provider.Settings
import androidx.annotation.OpenForTesting
@@ -29,6 +30,7 @@ import com.android.systemui.plugins.PluginLifecycleManager
import com.android.systemui.plugins.PluginListener
import com.android.systemui.plugins.PluginManager
import com.android.systemui.plugins.clocks.ClockController
import com.android.systemui.plugins.clocks.ClockEventListener
import com.android.systemui.plugins.clocks.ClockId
import com.android.systemui.plugins.clocks.ClockMessageBuffers
import com.android.systemui.plugins.clocks.ClockMetadata
@@ -36,6 +38,7 @@ import com.android.systemui.plugins.clocks.ClockPickerConfig
import com.android.systemui.plugins.clocks.ClockProvider
import com.android.systemui.plugins.clocks.ClockProviderPlugin
import com.android.systemui.plugins.clocks.ClockSettings
import com.android.systemui.plugins.clocks.VRectF
import com.android.systemui.util.ThreadAssert
import java.io.PrintWriter
import java.util.concurrent.ConcurrentHashMap
@@ -62,6 +65,8 @@ private val KNOWN_PLUGINS =
            listOf(ClockMetadata("DIGITAL_CLOCK_NUMBEROVERLAP")),
        "com.android.systemui.clocks.weather" to listOf(ClockMetadata("DIGITAL_CLOCK_WEATHER")),
    )
private val TRACE_CLOCK_CHANGE = "LOCKSCREEN_CLOCK_CHANGE"
private val TRACE_STYLE_CHANGE = "LOCKSCREEN_CLOCK_STYLE_CHANGE"

private fun <TKey : Any, TVal : Any> ConcurrentHashMap<TKey, TVal>.concurrentGetOrPut(
    key: TKey,
@@ -277,12 +282,43 @@ open class ClockRegistry(
        get() = field
        protected set(value) {
            if (field != value) {
                beginChangeTrace(field?.clockId, value?.clockId)
                field = value
                verifyLoadedProviders()
                triggerOnCurrentClockChanged()
            }
        }

    private var nextTraceCookie = 0
    private var endChangeTrace: (() -> Unit)? = null

    private fun beginChangeTrace(current: ClockId?, next: ClockId?) {
        if (keepAllLoaded) return
        val label = if (current != next) TRACE_CLOCK_CHANGE else TRACE_STYLE_CHANGE
        val cookie = nextTraceCookie++
        var isEnded = false

        Trace.beginAsyncSection(label, cookie)
        endChangeTrace = {
            if (!isEnded) {
                Trace.endAsyncSection(label, cookie)
                isEnded = true
            }
        }
    }

    private fun attachEndChangeTrace(clock: ClockController) {
        if (keepAllLoaded) return
        val onComplete = endChangeTrace?.also { endChangeTrace = null } ?: return
        clock.eventListeners.attach(
            object : ClockEventListener {
                override fun onBoundsChanged(bounds: VRectF) {}

                override fun onChangeComplete() = onComplete()
            }
        )
    }

    var isRegistered: Boolean = false
        private set

@@ -604,21 +640,36 @@ open class ClockRegistry(
    }

    fun createCurrentClock(ctx: Context): ClockController {
        fun createDefault(func: (ClockController) -> Unit = {}): ClockController {
            val clock = createClock(ctx, DEFAULT_CLOCK_ID)!!
            func(clock)
            return clock
        }

        val clockId = currentClockId
        if (isEnabled && clockId.isNotEmpty()) {
        if (clockId.isEmpty()) {
            return createDefault { attachEndChangeTrace(it) }
        }

        if (!isEnabled) {
            logger.i("Customized clocks disabled")
            return createDefault { attachEndChangeTrace(it) }
        }

        val clock = createClock(ctx, clockId)
        if (clock != null) {
            logger.i({ "Rendering clock $str1" }) { str1 = clockId }
            attachEndChangeTrace(clock)
            return clock
            } else if (availableClocks.containsKey(clockId)) {
                logger.w({ "Clock $str1 not loaded; using default" }) { str1 = clockId }
                verifyLoadedProviders()
            } else {
                logger.e({ "Clock $str1 not found; using default" }) { str1 = clockId }
        }

        if (availableClocks.containsKey(clockId)) {
            logger.w({ "Clock $str1 not loaded; using default" }) { str1 = clockId }
            return createDefault { verifyLoadedProviders() }
        }

        return createClock(ctx, DEFAULT_CLOCK_ID)!!
        logger.e({ "Clock $str1 not found; using default" }) { str1 = clockId }
        return createDefault { attachEndChangeTrace(it) }
    }

    private fun createClock(ctx: Context, targetClockId: ClockId): ClockController? {
+4 −7
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import com.android.systemui.plugins.clocks.ClockAnimations
import com.android.systemui.plugins.clocks.ClockAxisStyle
import com.android.systemui.plugins.clocks.ClockConfig
import com.android.systemui.plugins.clocks.ClockController
import com.android.systemui.plugins.clocks.ClockEventListener
import com.android.systemui.plugins.clocks.ClockEventListeners
import com.android.systemui.plugins.clocks.ClockEvents
import com.android.systemui.plugins.clocks.ClockFaceConfig
import com.android.systemui.plugins.clocks.ClockFaceController
@@ -102,12 +102,9 @@ class DefaultClockController(
        events.onLocaleChanged(Locale.getDefault())
    }

    override fun initialize(
        isDarkTheme: Boolean,
        dozeFraction: Float,
        foldFraction: Float,
        clockListener: ClockEventListener?,
    ) {
    override val eventListeners = ClockEventListeners()

    override fun initialize(isDarkTheme: Boolean, dozeFraction: Float, foldFraction: Float) {
        largeClock.recomputePadding(null)

        largeClock.animations = LargeClockAnimations(largeClock.view, dozeFraction, foldFraction)
+6 −9
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import com.android.systemui.plugins.clocks.AxisType
import com.android.systemui.plugins.clocks.ClockAxisStyle
import com.android.systemui.plugins.clocks.ClockConfig
import com.android.systemui.plugins.clocks.ClockController
import com.android.systemui.plugins.clocks.ClockEventListener
import com.android.systemui.plugins.clocks.ClockEventListeners
import com.android.systemui.plugins.clocks.ClockEvents
import com.android.systemui.plugins.clocks.ClockFontAxis
import com.android.systemui.plugins.clocks.ClockFontAxis.Companion.merge
@@ -101,14 +101,11 @@ class FlexClockController(private val clockCtx: ClockContext) : ClockController
            }
        }

    override fun initialize(
        isDarkTheme: Boolean,
        dozeFraction: Float,
        foldFraction: Float,
        clockListener: ClockEventListener?,
    ) {
    override val eventListeners = ClockEventListeners()

    override fun initialize(isDarkTheme: Boolean, dozeFraction: Float, foldFraction: Float) {
        smallClock.run {
            layerController.onViewBoundsChanged = { clockListener?.onBoundsChanged(it) }
            layerController.onViewBoundsChanged = { eventListeners.fire { onBoundsChanged(it) } }
            events.onThemeChanged(theme.copy(isDarkTheme = isDarkTheme))
            animations.onFontAxesChanged(clockCtx.settings.axes)
            animations.doze(dozeFraction)
@@ -117,7 +114,7 @@ class FlexClockController(private val clockCtx: ClockContext) : ClockController
        }

        largeClock.run {
            layerController.onViewBoundsChanged = { clockListener?.onBoundsChanged(it) }
            layerController.onViewBoundsChanged = { eventListeners.fire { onBoundsChanged(it) } }
            events.onThemeChanged(theme.copy(isDarkTheme = isDarkTheme))
            animations.onFontAxesChanged(clockCtx.settings.axes)
            animations.doze(dozeFraction)
+2 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import com.android.systemui.plugins.PluginListener
import com.android.systemui.plugins.PluginManager
import com.android.systemui.plugins.clocks.ClockAxisStyle
import com.android.systemui.plugins.clocks.ClockController
import com.android.systemui.plugins.clocks.ClockEventListeners
import com.android.systemui.plugins.clocks.ClockId
import com.android.systemui.plugins.clocks.ClockMessageBuffers
import com.android.systemui.plugins.clocks.ClockMetadata
@@ -176,6 +177,7 @@ class ClockRegistryTest : SysuiTestCase() {
        fakeDefaultProvider =
            FakeClockPlugin().addClock(DEFAULT_CLOCK_ID, { mockDefaultClock }, { pickerConfig })
        whenever(mockContext.contentResolver).thenReturn(mockContentResolver)
        whenever(mockClock.eventListeners).thenReturn(ClockEventListeners())

        val captor = argumentCaptor<PluginListener<ClockProviderPlugin>>()
        registry =
+2 −2
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ class DefaultClockProviderTest : SysuiTestCase() {
        verify(mockSmallClockView).setColors(DOZE_COLOR, Color.MAGENTA)
        verify(mockLargeClockView).setColors(DOZE_COLOR, Color.MAGENTA)

        clock.initialize(true, 0f, 0f, null)
        clock.initialize(true, 0f, 0f)

        // This is the default darkTheme color
        val expectedColor = context.resources.getColor(android.R.color.system_accent1_100)
@@ -137,7 +137,7 @@ class DefaultClockProviderTest : SysuiTestCase() {
        verify(mockSmallClockView).setColors(expectedAodColor, Color.MAGENTA)
        verify(mockLargeClockView).setColors(expectedAodColor, Color.MAGENTA)

        clock.initialize(true, 0f, 0f, null)
        clock.initialize(true, 0f, 0f)

        val expectedColor = Color.MAGENTA
        verify(mockSmallClockView).setColors(expectedAodColor, expectedColor)
Loading