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.section
18 
19 import android.annotation.SuppressLint
20 import android.view.LayoutInflater
21 import android.view.View
22 import android.view.ViewGroup
23 import androidx.compose.foundation.layout.fillMaxWidth
24 import androidx.compose.foundation.layout.padding
25 import androidx.compose.runtime.Composable
26 import androidx.compose.runtime.remember
27 import androidx.compose.ui.Modifier
28 import androidx.compose.ui.platform.LocalContext
29 import androidx.compose.ui.unit.dp
30 import androidx.compose.ui.viewinterop.AndroidView
31 import com.android.compose.animation.scene.ElementKey
32 import com.android.compose.animation.scene.SceneScope
33 import com.android.compose.modifiers.height
34 import com.android.keyguard.dagger.KeyguardStatusBarViewComponent
35 import com.android.systemui.common.ui.compose.windowinsets.LocalDisplayCutout
36 import com.android.systemui.res.R
37 import com.android.systemui.shade.NotificationPanelView
38 import com.android.systemui.shade.ShadeViewStateProvider
39 import com.android.systemui.statusbar.phone.KeyguardStatusBarView
40 import com.android.systemui.util.Utils
41 import dagger.Lazy
42 import javax.inject.Inject
43 
44 class StatusBarSection
45 @Inject
46 constructor(
47     private val componentFactory: KeyguardStatusBarViewComponent.Factory,
48     private val notificationPanelView: Lazy<NotificationPanelView>,
49 ) {
50     @Composable
SceneScopenull51     fun SceneScope.StatusBar(modifier: Modifier = Modifier) {
52         val context = LocalContext.current
53         val viewDisplayCutout = LocalDisplayCutout.current.viewDisplayCutoutKeyguardStatusBarView
54         @SuppressLint("InflateParams")
55         val view =
56             remember(context) {
57                 (LayoutInflater.from(context)
58                         .inflate(
59                             R.layout.keyguard_status_bar,
60                             null,
61                             false,
62                         ) as KeyguardStatusBarView)
63                     .also {
64                         it.layoutParams =
65                             ViewGroup.LayoutParams(
66                                 ViewGroup.LayoutParams.MATCH_PARENT,
67                                 ViewGroup.LayoutParams.WRAP_CONTENT
68                             )
69                     }
70             }
71         val viewController =
72             remember(view) {
73                 val provider =
74                     object : ShadeViewStateProvider {
75                         override val lockscreenShadeDragProgress: Float = 0f
76                         override val panelViewExpandedHeight: Float = 0f
77 
78                         override fun shouldHeadsUpBeVisible(): Boolean {
79                             return false
80                         }
81                     }
82 
83                 componentFactory.build(view, provider).keyguardStatusBarViewController
84             }
85 
86         MovableElement(
87             key = StatusBarElementKey,
88             modifier = modifier,
89         ) {
90             content {
91                 AndroidView(
92                     factory = {
93                         notificationPanelView.get().findViewById<View>(R.id.keyguard_header)?.let {
94                             (it.parent as ViewGroup).removeView(it)
95                         }
96 
97                         viewController.init()
98                         view
99                     },
100                     modifier =
101                         Modifier.fillMaxWidth().padding(horizontal = 16.dp).height {
102                             Utils.getStatusBarHeaderHeightKeyguard(context)
103                         },
104                     update = { viewController.setDisplayCutout(viewDisplayCutout) }
105                 )
106             }
107         }
108     }
109 }
110 
111 private val StatusBarElementKey = ElementKey("StatusBar")
112