1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.systemui.keyguard.ui.composable.blueprint
18
19 import androidx.compose.foundation.layout.WindowInsets
20 import androidx.compose.foundation.layout.displayCutout
21 import androidx.compose.foundation.layout.systemBars
22 import androidx.compose.foundation.layout.union
23 import androidx.compose.runtime.Composable
24 import androidx.compose.runtime.getValue
25 import androidx.compose.runtime.mutableStateOf
26 import androidx.compose.runtime.remember
27 import androidx.compose.ui.platform.LocalDensity
28 import androidx.lifecycle.compose.collectAsStateWithLifecycle
29 import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor
30 import com.android.systemui.keyguard.ui.viewmodel.BurnInParameters
31 import com.android.systemui.plugins.clocks.ClockController
32 import kotlin.math.min
33 import kotlin.math.roundToInt
34
35 /** Produces a [BurnInState] that can be used to query the `LockscreenBurnInViewModel` flows. */
36 @Composable
rememberBurnInnull37 fun rememberBurnIn(
38 clockInteractor: KeyguardClockInteractor,
39 ): BurnInState {
40 val clock by clockInteractor.currentClock.collectAsStateWithLifecycle()
41
42 val (smartspaceTop, onSmartspaceTopChanged) = remember { mutableStateOf<Float?>(null) }
43 val (smallClockTop, onSmallClockTopChanged) = remember { mutableStateOf<Float?>(null) }
44
45 val topmostTop =
46 when {
47 smartspaceTop != null && smallClockTop != null -> min(smartspaceTop, smallClockTop)
48 smartspaceTop != null -> smartspaceTop
49 smallClockTop != null -> smallClockTop
50 else -> 0f
51 }.roundToInt()
52
53 val params = rememberBurnInParameters(clock, topmostTop)
54
55 return remember(params, onSmartspaceTopChanged, onSmallClockTopChanged) {
56 BurnInState(
57 parameters = params,
58 onSmartspaceTopChanged = onSmartspaceTopChanged,
59 onSmallClockTopChanged = onSmallClockTopChanged,
60 )
61 }
62 }
63
64 @Composable
rememberBurnInParametersnull65 private fun rememberBurnInParameters(
66 clock: ClockController?,
67 topmostTop: Int,
68 ): BurnInParameters {
69 val density = LocalDensity.current
70 val topInset = WindowInsets.systemBars.union(WindowInsets.displayCutout).getTop(density)
71
72 return remember(clock, topInset, topmostTop) {
73 BurnInParameters(
74 topInset = topInset,
75 minViewY = topmostTop,
76 )
77 }
78 }
79
80 data class BurnInState(
81 /** Parameters for use with the `LockscreenBurnInViewModel. */
82 val parameters: BurnInParameters,
83 /**
84 * Callback to invoke when the top coordinate of the smartspace element is updated, pass `null`
85 * when the element is not shown.
86 */
87 val onSmartspaceTopChanged: (Float?) -> Unit,
88 /**
89 * Callback to invoke when the top coordinate of the small clock element is updated, pass `null`
90 * when the element is not shown.
91 */
92 val onSmallClockTopChanged: (Float?) -> Unit,
93 )
94