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

Commit c027cc05 authored by Hawkwood's avatar Hawkwood
Browse files

Add createPreviewClock to load target clock in SystemUI for KeyguardPreview

Bug: 415755368
Flag: NONE - Flagged at preview callsite
Test: Manually checked using WPP instance of preview
Change-Id: I81a3f24cab5d0fb65c5b80055cc70f66e9917aea
parent 216b37b7
Loading
Loading
Loading
Loading
+47 −13
Original line number Diff line number Diff line
@@ -45,6 +45,8 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
@@ -615,8 +617,33 @@ open class ClockRegistry(
        return availableClocks[clockId]?.provider?.getClockPickerConfig(clockSettings)
    }

    fun createExampleClock(ctx: Context, clockId: ClockId): ClockController? =
        createClock(ctx, clockId)
    fun createExampleClock(ctx: Context, clockId: ClockId): ClockController? {
        return createClock(ctx, clockId)
    }

    suspend fun createPreviewClockAsync(
        ctx: Context,
        settings: ClockSettings,
    ): Deferred<ClockController> {
        return withContext(bgDispatcher) {
            async {
                synchronized(availableClocks) {
                    availableClocks[settings.clockId]?.manager?.loadPlugin()
                    val result = createClock(ctx, settings)
                    verifyLoadedProviders()

                    if (result != null) {
                        return@async result
                    }

                    logger.e({ "Failed to create preview clock: '$str1'; using default" }) {
                        str1 = settings.clockId
                    }
                    return@async createDefaultClock(ctx)
                }
            }
        }
    }

    /**
     * Adds [listener] to receive future clock changes.
@@ -638,21 +665,24 @@ open class ClockRegistry(
        clockChangeListeners.remove(listener)
    }

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

    fun createCurrentClock(ctx: Context): ClockController {
        val clockId = currentClockId
        if (clockId.isEmpty()) {
            return createDefault { attachEndChangeTrace(it) }
            return createDefaultClock(ctx) { attachEndChangeTrace(this) }
        }

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

        val clock = createClock(ctx, clockId)
@@ -664,11 +694,11 @@ open class ClockRegistry(

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

        logger.e({ "Clock $str1 not found; using default" }) { str1 = clockId }
        return createDefault { attachEndChangeTrace(it) }
        return createDefaultClock(ctx) { attachEndChangeTrace(this) }
    }

    private fun createClock(ctx: Context, targetClockId: ClockId): ClockController? {
@@ -676,7 +706,11 @@ open class ClockRegistry(
        if (targetClockId != settings.clockId) {
            settings = settings.copy(clockId = targetClockId)
        }
        return availableClocks[targetClockId]?.provider?.createClock(ctx, settings)
        return createClock(ctx, settings)
    }

    private fun createClock(ctx: Context, settings: ClockSettings): ClockController? {
        return availableClocks[settings.clockId]?.provider?.createClock(ctx, settings)
    }

    fun dump(pw: PrintWriter, args: Array<out String>) {
+15 −2
Original line number Diff line number Diff line
@@ -18,6 +18,20 @@
package com.android.systemui.shared.quickaffordance.shared.model

object KeyguardPreviewConstants {
    // Initialization bundle keys
    const val KEY_CLOCK_ID = "clock_id"
    const val KEY_CLOCK_STYLE = "clock_style"
    const val KEY_HIDE_CLOCK = "hide_clock"
    const val KEY_HIGHLIGHT_QUICK_AFFORDANCES = "highlight_quick_affordances"

    // TODO(b/422462849): Can SurfaceViewUtils share these constants?
    const val KEY_HOST_TOKEN = "host_token"
    const val KEY_VIEW_WIDTH = "width"
    const val KEY_VIEW_HEIGHT = "height"
    const val KEY_DISPLAY_ID = "display_id"
    const val KEY_COLORS = "wallpaper_colors"

    // Message IDs
    const val MESSAGE_ID_DEFAULT_PREVIEW = 707
    const val MESSAGE_ID_HIDE_SMART_SPACE = 1111
    const val MESSAGE_ID_PREVIEW_QUICK_AFFORDANCE_SELECTED = 1988
@@ -25,13 +39,12 @@ object KeyguardPreviewConstants {
    const val MESSAGE_ID_START_CUSTOMIZING_QUICK_AFFORDANCES = 214
    const val MESSAGE_ID_PREVIEW_CLOCK_SIZE = 1119

    // Message Keys
    const val KEY_HIDE_SMART_SPACE = "hide_smart_space"
    const val KEY_HIGHLIGHT_QUICK_AFFORDANCES = "highlight_quick_affordances"
    const val KEY_INITIALLY_SELECTED_SLOT_ID = "initially_selected_slot_id"
    const val KEY_QUICK_AFFORDANCE_ID = "quick_affordance_id"
    const val KEY_SLOT_ID = "slot_id"
    const val KEY_CLOCK_SIZE = "clock_size"
    const val KEY_HIDE_CLOCK = "hide_clock"

    const val KEYGUARD_QUICK_AFFORDANCE_ID_NONE = "none"
    const val CLOCK_SIZE_DYNAMIC = "clock_size_dynamic"
+33 −12
Original line number Diff line number Diff line
@@ -26,15 +26,25 @@ import android.view.Display
import android.view.Display.DEFAULT_DISPLAY
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.keyguard.shared.model.ClockSizeSetting
import com.android.systemui.plugins.clocks.ClockAxisStyle
import com.android.systemui.plugins.clocks.ClockController
import com.android.systemui.plugins.clocks.ClockSettings
import com.android.systemui.shared.clocks.ClockRegistry
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_CLOCK_ID
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_CLOCK_STYLE
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_COLORS
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_DISPLAY_ID
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_HIDE_CLOCK
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_HIGHLIGHT_QUICK_AFFORDANCES
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_HOST_TOKEN
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_VIEW_HEIGHT
import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants.KEY_VIEW_WIDTH
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map

@AssistedFactory
@@ -69,18 +79,29 @@ constructor(

    val requestedClockSize: MutableStateFlow<ClockSizeSetting?> = MutableStateFlow(null)

    val overrideClockSettings: ClockSettings? = run {
        val clockId = request.getString(KEY_CLOCK_ID)
        if (clockId == null) return@run null

        // clock seed color handled by KeyguardPreviewRenderer.updateClockAppearance
        return@run ClockSettings(
            clockId = clockId,
            axes =
                ClockAxisStyle {
                    request.getBundle(KEY_CLOCK_STYLE)?.let { bundle ->
                        bundle.keySet().forEach { key -> put(key, bundle.getFloat(key)) }
                    }
                },
        )
    }

    val previewClock: Flow<ClockController> =
        clockRepository.currentClockId.map {
        overrideClockSettings?.let { settings ->
            flow { emit(clockRegistry.createPreviewClockAsync(previewContext, settings).await()) }
        }
            ?: clockRepository.currentClockId.map {
                // We should create a new instance for each collect call cause in preview, the same
                // clock will be attached to a different parent view at the same time.
                clockRegistry.createCurrentClock(previewContext)
            }

    companion object {
        private const val KEY_HOST_TOKEN = "host_token"
        private const val KEY_VIEW_WIDTH = "width"
        private const val KEY_VIEW_HEIGHT = "height"
        private const val KEY_DISPLAY_ID = "display_id"
        private const val KEY_COLORS = "wallpaper_colors"
    }
}