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

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

Merge "Move Clock Compose Elements into Plugin Libs" into main

parents cd7fa8f4 50d773d4
Loading
Loading
Loading
Loading
+20 −77
Original line number Diff line number Diff line
@@ -16,18 +16,10 @@

package com.android.systemui.keyguard.ui.composable.blueprint

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import com.android.compose.animation.scene.ElementKey
import com.android.compose.animation.scene.SceneKey
import com.android.compose.animation.scene.TransitionBuilder
import com.android.compose.animation.scene.transitions
import com.android.systemui.keyguard.ui.composable.blueprint.ClockElementKeys.largeClockElementKey
import com.android.systemui.keyguard.ui.composable.blueprint.ClockElementKeys.smallClockElementKey
import com.android.systemui.keyguard.ui.composable.blueprint.ClockElementKeys.smartspaceElementKey
import com.android.systemui.keyguard.ui.composable.blueprint.ClockTransition.transitioningToLargeClock
import com.android.systemui.keyguard.ui.composable.blueprint.ClockTransition.transitioningToSmallClock
import com.android.systemui.keyguard.ui.composable.blueprint.WeatherClockElementKeys.largeWeatherClockElementKeyList
import com.android.systemui.keyguard.ui.view.layout.sections.transitions.ClockSizeTransition.ClockFaceInTransition.Companion.CLOCK_IN_MILLIS
import com.android.systemui.keyguard.ui.view.layout.sections.transitions.ClockSizeTransition.ClockFaceInTransition.Companion.CLOCK_IN_START_DELAY_MILLIS
import com.android.systemui.keyguard.ui.view.layout.sections.transitions.ClockSizeTransition.ClockFaceOutTransition.Companion.CLOCK_OUT_MILLIS
@@ -35,92 +27,43 @@ import com.android.systemui.keyguard.ui.view.layout.sections.transitions.ClockSi
import com.android.systemui.keyguard.ui.view.layout.sections.transitions.ClockSizeTransition.SmartspaceMoveTransition.Companion.STATUS_AREA_MOVE_UP_MILLIS

object ClockTransition {
    val defaultClockTransitions = transitions {
        from(ClockScenes.smallClockScene, to = ClockScenes.largeClockScene) {
            transitioningToLargeClock(largeClockElements = listOf(largeClockElementKey))
        }
        from(ClockScenes.largeClockScene, to = ClockScenes.smallClockScene) {
            transitioningToSmallClock(largeClockElements = listOf(largeClockElementKey))
        }
        from(ClockScenes.splitShadeLargeClockScene, to = ClockScenes.largeClockScene) {
            spec = tween(300, easing = LinearEasing)
        }
    // TODO(b/432451019): Rebuild to be dependent on new lockscreen clock element list.
    // Possibly this makes sense to delegate fully to the clock plugins.
    val clockTransitions = transitions {}

        from(WeatherClockScenes.largeClockScene, to = ClockScenes.smallClockScene) {
            transitioningToSmallClock(largeClockElements = largeWeatherClockElementKeyList)
        }

        from(ClockScenes.smallClockScene, to = WeatherClockScenes.largeClockScene) {
            transitioningToLargeClock(largeClockElements = largeWeatherClockElementKeyList)
        }

        from(
            WeatherClockScenes.largeClockScene,
            to = WeatherClockScenes.splitShadeLargeClockScene
        ) {
            spec = tween(300, easing = LinearEasing)
        }
    }
    data class ElementKeys(
        val smallClock: List<ElementKey>,
        val largeClock: List<ElementKey>,
        val smartspace: List<ElementKey>,
    )

    private fun TransitionBuilder.transitioningToLargeClock(largeClockElements: List<ElementKey>) {
    private fun TransitionBuilder.transitioningToLargeClock(keys: ElementKeys) {
        spec = tween(durationMillis = STATUS_AREA_MOVE_UP_MILLIS.toInt())
        timestampRange(
            startMillis = CLOCK_IN_START_DELAY_MILLIS.toInt(),
            endMillis = (CLOCK_IN_START_DELAY_MILLIS + CLOCK_IN_MILLIS).toInt()
            endMillis = (CLOCK_IN_START_DELAY_MILLIS + CLOCK_IN_MILLIS).toInt(),
        ) {
            largeClockElements.forEach { fade(it) }
            keys.largeClock.forEach { fade(it) }
        }

        timestampRange(endMillis = CLOCK_OUT_MILLIS.toInt()) { fade(smallClockElementKey) }
        anchoredTranslate(smallClockElementKey, smartspaceElementKey)
        timestampRange(endMillis = CLOCK_OUT_MILLIS.toInt()) {
            keys.smallClock.forEach { fade(it) }
            keys.smartspace.forEach { fade(it) }
        }
    }

    private fun TransitionBuilder.transitioningToSmallClock(largeClockElements: List<ElementKey>) {
    private fun TransitionBuilder.transitioningToSmallClock(keys: ElementKeys) {
        spec = tween(durationMillis = STATUS_AREA_MOVE_DOWN_MILLIS.toInt())
        timestampRange(
            startMillis = CLOCK_IN_START_DELAY_MILLIS.toInt(),
            endMillis = (CLOCK_IN_START_DELAY_MILLIS + CLOCK_IN_MILLIS).toInt()
            endMillis = (CLOCK_IN_START_DELAY_MILLIS + CLOCK_IN_MILLIS).toInt(),
        ) {
            fade(smallClockElementKey)
            keys.smallClock.forEach { fade(it) }
            keys.smartspace.forEach { fade(it) }
        }

        timestampRange(endMillis = CLOCK_OUT_MILLIS.toInt()) {
            largeClockElements.forEach { fade(it) }
        }
        anchoredTranslate(smallClockElementKey, smartspaceElementKey)
            keys.largeClock.forEach { fade(it) }
        }
    }

object ClockScenes {
    val smallClockScene = SceneKey("small-clock-scene")
    val largeClockScene = SceneKey("large-clock-scene")
    val splitShadeSmallClockScene = SceneKey("split-shade-small-clock-scene")
    val splitShadeLargeClockScene = SceneKey("split-shade-large-clock-scene")
}

object ClockElementKeys {
    val largeClockElementKey = ElementKey("large-clock")
    val smallClockElementKey = ElementKey("small-clock")
    val smartspaceElementKey = ElementKey("smart-space")
}

object WeatherClockScenes {
    val largeClockScene = SceneKey("large-weather-clock-scene")
    val splitShadeLargeClockScene = SceneKey("split-shade-large-weather-clock-scene")
}

object WeatherClockElementKeys {
    val timeElementKey = ElementKey("weather-large-clock-time")
    val dateElementKey = ElementKey("weather-large-clock-date")
    val weatherIconElementKey = ElementKey("weather-large-clock-weather-icon")
    val temperatureElementKey = ElementKey("weather-large-clock-temperature")
    val dndAlarmElementKey = ElementKey("weather-large-clock-dnd-alarm")
    val largeWeatherClockElementKeyList =
        listOf(
            timeElementKey,
            dateElementKey,
            weatherIconElementKey,
            temperatureElementKey,
            dndAlarmElementKey
        )
}
+32 −38
Original line number Diff line number Diff line
@@ -20,25 +20,28 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.compose.animation.scene.ContentScope
import com.android.systemui.keyguard.ui.composable.LockscreenTouchHandling
import com.android.systemui.keyguard.ui.composable.element.AmbientIndicationElement
import com.android.systemui.keyguard.ui.composable.element.AodPromotedNotificationAreaElement
import com.android.systemui.keyguard.ui.composable.element.DateAndWeatherElement
import com.android.systemui.keyguard.ui.composable.element.IndicationAreaElement
import com.android.systemui.keyguard.ui.composable.element.LargeClockElement
import com.android.systemui.keyguard.ui.composable.element.LockElement
import com.android.systemui.keyguard.ui.composable.element.MediaCarouselElement
import com.android.systemui.keyguard.ui.composable.element.NotificationElement
import com.android.systemui.keyguard.ui.composable.element.SettingsMenuElement
import com.android.systemui.keyguard.ui.composable.element.ShortcutElement
import com.android.systemui.keyguard.ui.composable.element.SmallClockElement
import com.android.systemui.keyguard.ui.composable.element.SmartSpaceElement
import com.android.systemui.keyguard.ui.composable.element.SmartspaceElementProvider
import com.android.systemui.keyguard.ui.composable.element.StatusBarElement
import com.android.systemui.keyguard.ui.composable.layout.LockscreenSceneLayout
import com.android.systemui.keyguard.ui.composable.modifier.burnInAware
import com.android.systemui.keyguard.ui.viewmodel.AodBurnInViewModel
import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
import com.android.systemui.keyguard.ui.viewmodel.LockscreenContentViewModel
import com.android.systemui.plugins.clocks.LockscreenElementContext
import com.android.systemui.plugins.clocks.LockscreenElementFactory
import java.util.Optional
import javax.inject.Inject

@@ -49,6 +52,8 @@ import javax.inject.Inject
class DefaultBlueprint
@Inject
constructor(
    private val keyguardClockViewModel: KeyguardClockViewModel,
    private val aodBurnInViewModel: AodBurnInViewModel,
    private val statusBarElement: StatusBarElement,
    private val lockElement: LockElement,
    private val ambientIndicationElementOptional: Optional<AmbientIndicationElement>,
@@ -57,11 +62,7 @@ constructor(
    private val settingsMenuElement: SettingsMenuElement,
    private val notificationsElement: NotificationElement,
    private val aodPromotedNotificationAreaElement: AodPromotedNotificationAreaElement,
    private val smallClockElement: SmallClockElement,
    private val largeClockElement: LargeClockElement,
    private val keyguardClockViewModel: KeyguardClockViewModel,
    private val smartSpaceElement: SmartSpaceElement,
    private val dateAndWeatherElement: DateAndWeatherElement,
    private val smartspaceElementProvider: SmartspaceElementProvider,
    private val mediaCarouselElement: MediaCarouselElement,
) : ComposableLockscreenSceneBlueprint {

@@ -69,44 +70,37 @@ constructor(

    @Composable
    override fun ContentScope.Content(viewModel: LockscreenContentViewModel, modifier: Modifier) {
        val currentClock = keyguardClockViewModel.currentClock.collectAsStateWithLifecycle()
        val elementFactory =
            remember(currentClock, smartspaceElementProvider.elements) {
                LockscreenElementFactory.build { putAll ->
                    putAll(smartspaceElementProvider.elements)
                    currentClock.value?.apply {
                        putAll(largeClock.layout.elements)
                        putAll(smallClock.layout.elements)
                    }
                }
            }

        val burnIn = rememberBurnIn(keyguardClockViewModel)
        val elementContext =
            LockscreenElementContext(
                scope = this,
                burnInModifier =
                    Modifier.burnInAware(viewModel = aodBurnInViewModel, params = burnIn.parameters),
            )

        LockscreenTouchHandling(
            viewModelFactory = viewModel.touchHandlingFactory,
            modifier = modifier,
        ) { onSettingsMenuPlaced ->
            val burnIn = rememberBurnIn(keyguardClockViewModel)

            LockscreenSceneLayout(
                viewModel = viewModel.layout,
                elementFactory = elementFactory,
                elementContext = elementContext,
                statusBar = {
                    with(statusBarElement) { StatusBar(modifier = Modifier.fillMaxWidth()) }
                },
                smallClock = {
                    with(smallClockElement) {
                        SmallClock(
                            burnInParams = burnIn.parameters,
                            onTopChanged = burnIn.onSmallClockTopChanged,
                            onBottomChanged = { bottom -> viewModel.setSmallClockBottom(bottom) },
                        )
                    }
                },
                largeClock = {
                    with(largeClockElement) { LargeClock(burnInParams = burnIn.parameters) }
                },
                dateAndWeather = { orientation ->
                    with(dateAndWeatherElement) { DateAndWeather(orientation) }
                },
                smartSpace = {
                    with(smartSpaceElement) {
                        SmartSpace(
                            burnInParams = burnIn.parameters,
                            onTopChanged = burnIn.onSmartspaceTopChanged,
                            onBottomChanged = { bottom ->
                                viewModel.setSmartspaceCardBottom(bottom)
                            },
                            smartSpacePaddingTop = { 0 },
                        )
                    }
                },
                media = {
                    with(mediaCarouselElement) {
                        KeyguardMediaCarousel(
+0 −120
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.keyguard.ui.composable.element

import android.widget.FrameLayout
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.systemui.keyguard.ui.viewmodel.KeyguardSmartspaceViewModel
import com.android.systemui.statusbar.lockscreen.LockscreenSmartspaceController
import javax.inject.Inject

class DateAndWeatherElement
@Inject
constructor(
    private val lockscreenSmartspaceController: LockscreenSmartspaceController,
    private val keyguardSmartspaceViewModel: KeyguardSmartspaceViewModel,
) {

    @Composable
    fun DateAndWeather(orientation: Orientation, modifier: Modifier = Modifier) {
        if (!keyguardSmartspaceViewModel.isSmartspaceEnabled) {
            Box(modifier)
            return
        }

        when (orientation) {
            Orientation.Horizontal ->
                Row(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = modifier,
                ) {
                    Date()
                    Weather()
                }

            Orientation.Vertical ->
                Column(verticalArrangement = Arrangement.spacedBy(4.dp), modifier = modifier) {
                    Date()
                    Weather()
                }
        }
    }

    @Composable
    private fun Weather(modifier: Modifier = Modifier) {
        val isVisible by keyguardSmartspaceViewModel.isWeatherVisible.collectAsStateWithLifecycle()
        if (!isVisible) {
            return
        }

        AndroidView(
            factory = { context ->
                FrameLayout(context).apply {
                    addView(
                        lockscreenSmartspaceController
                            .buildAndConnectWeatherView(this, false)
                            .apply {
                                layoutParams =
                                    FrameLayout.LayoutParams(
                                        FrameLayout.LayoutParams.WRAP_CONTENT,
                                        FrameLayout.LayoutParams.WRAP_CONTENT,
                                    )
                            }
                    )
                }
            },
            modifier = modifier,
        )
    }

    @Composable
    private fun Date(modifier: Modifier = Modifier) {
        val isVisible by keyguardSmartspaceViewModel.isDateVisible.collectAsStateWithLifecycle()
        if (!isVisible) {
            return
        }

        AndroidView(
            factory = { context ->
                FrameLayout(context).apply {
                    addView(
                        lockscreenSmartspaceController.buildAndConnectDateView(this, false).apply {
                            layoutParams =
                                FrameLayout.LayoutParams(
                                    FrameLayout.LayoutParams.WRAP_CONTENT,
                                    FrameLayout.LayoutParams.WRAP_CONTENT,
                                )
                        }
                    )
                }
            },
            modifier = modifier,
        )
    }
}
+0 −92
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.keyguard.ui.composable.element

import android.content.res.Resources
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.compose.animation.scene.ContentScope
import com.android.systemui.keyguard.ui.composable.blueprint.ClockElementKeys.largeClockElementKey
import com.android.systemui.keyguard.ui.composable.blueprint.ClockScenes.largeClockScene
import com.android.systemui.keyguard.ui.composable.blueprint.ClockScenes.splitShadeLargeClockScene
import com.android.systemui.keyguard.ui.composable.modifier.burnInAware
import com.android.systemui.keyguard.ui.viewmodel.AodBurnInViewModel
import com.android.systemui.keyguard.ui.viewmodel.BurnInParameters
import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
import javax.inject.Inject

/** Provides small clock and large clock composables for the default clock face. */
class LargeClockElement
@Inject
constructor(
    private val viewModel: KeyguardClockViewModel,
    private val aodBurnInViewModel: AodBurnInViewModel,
) : ClockElement() {

    @Composable
    fun ContentScope.LargeClock(burnInParams: BurnInParameters, modifier: Modifier = Modifier) {
        val currentClock by viewModel.currentClock.collectAsStateWithLifecycle()
        if (currentClock?.largeClock?.view == null) {
            return
        }

        // Centering animation for clocks that have custom position animations.
        LaunchedEffect(layoutState.currentTransition?.progress) {
            val transition = layoutState.currentTransition ?: return@LaunchedEffect
            if (currentClock?.largeClock?.config?.hasCustomPositionUpdatedAnimation != true) {
                return@LaunchedEffect
            }

            // If we are not doing the centering animation, do not animate.
            val progress =
                if (transition.isTransitioningBetween(largeClockScene, splitShadeLargeClockScene)) {
                    transition.progress
                } else {
                    1f
                }

            val dir = if (transition.toContent == splitShadeLargeClockScene) -1f else 1f
            val distance = dir * getClockCenteringDistance()
            val largeClock = checkNotNull(currentClock).largeClock
            // TODO(b/418824686): Migrate stepping animation to compose and ensure it works in RTL
            // largeClock.animations.onPositionAnimated(distance = distance, fraction = progress)
        }

        Element(key = largeClockElementKey, modifier = modifier) {
            ClockView(
                checkNotNull(currentClock).largeClock.view,
                modifier =
                    Modifier.fillMaxSize()
                        .burnInAware(
                            viewModel = aodBurnInViewModel,
                            params = burnInParams,
                            isClock = true,
                        ),
            )
        }
    }

    companion object {
        private fun getClockCenteringDistance(): Float {
            return Resources.getSystem().displayMetrics.widthPixels / 4f
        }
    }
}
+0 −78
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.keyguard.ui.composable.element

import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.res.dimensionResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.compose.animation.scene.ContentScope
import com.android.compose.modifiers.padding
import com.android.systemui.customization.clocks.R as clocksR
import com.android.systemui.keyguard.ui.composable.blueprint.ClockElementKeys.smallClockElementKey
import com.android.systemui.keyguard.ui.composable.modifier.burnInAware
import com.android.systemui.keyguard.ui.composable.modifier.onTopPlacementChanged
import com.android.systemui.keyguard.ui.viewmodel.AodBurnInViewModel
import com.android.systemui.keyguard.ui.viewmodel.BurnInParameters
import com.android.systemui.keyguard.ui.viewmodel.KeyguardClockViewModel
import javax.inject.Inject

/** Provides small clock and large clock composables for the default clock face. */
class SmallClockElement
@Inject
constructor(
    private val viewModel: KeyguardClockViewModel,
    private val aodBurnInViewModel: AodBurnInViewModel,
) : ClockElement() {

    @Composable
    fun ContentScope.SmallClock(
        modifier: Modifier = Modifier,
        burnInParams: BurnInParameters,
        onTopChanged: (top: Float?) -> Unit,
        onBottomChanged: ((Float) -> Unit)? = null,
    ) {
        val currentClock by viewModel.currentClock.collectAsStateWithLifecycle()
        val smallTopMargin by
            viewModel.smallClockTopMargin.collectAsStateWithLifecycle(
                viewModel.getSmallClockTopMargin()
            )
        if (currentClock?.smallClock?.view == null) {
            return
        }

        ClockView(
            checkNotNull(currentClock).smallClock.view,
            modifier =
                modifier
                    .height(dimensionResource(clocksR.dimen.small_clock_height))
                    .padding(horizontal = dimensionResource(clocksR.dimen.clock_padding_start))
                    .padding(top = { smallTopMargin })
                    .onTopPlacementChanged(onTopChanged)
                    .burnInAware(viewModel = aodBurnInViewModel, params = burnInParams)
                    .element(smallClockElementKey)
                    .onGloballyPositioned { coordinates ->
                        onBottomChanged?.invoke(coordinates.boundsInWindow().bottom)
                    },
        )
    }
}
Loading