1 /*
<lambda>null2  * 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.view.LayoutInflater
20 import androidx.compose.foundation.layout.padding
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.mutableStateOf
23 import androidx.compose.runtime.remember
24 import androidx.compose.ui.Modifier
25 import androidx.compose.ui.geometry.Rect
26 import androidx.compose.ui.layout.onPlaced
27 import androidx.compose.ui.layout.positionInParent
28 import androidx.compose.ui.res.dimensionResource
29 import androidx.compose.ui.unit.toSize
30 import androidx.compose.ui.viewinterop.AndroidView
31 import androidx.core.view.isVisible
32 import com.android.systemui.keyguard.ui.binder.KeyguardSettingsViewBinder
33 import com.android.systemui.keyguard.ui.viewmodel.KeyguardLongPressViewModel
34 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSettingsMenuViewModel
35 import com.android.systemui.plugins.ActivityStarter
36 import com.android.systemui.res.R
37 import com.android.systemui.statusbar.VibratorHelper
38 import javax.inject.Inject
39 import kotlinx.coroutines.DisposableHandle
40 
41 class SettingsMenuSection
42 @Inject
43 constructor(
44     private val viewModel: KeyguardSettingsMenuViewModel,
45     private val longPressViewModel: KeyguardLongPressViewModel,
46     private val vibratorHelper: VibratorHelper,
47     private val activityStarter: ActivityStarter,
48 ) {
49     @Composable
50     @SuppressWarnings("InflateParams") // null is passed into the inflate call, on purpose.
51     fun SettingsMenu(
52         onPlaced: (Rect?) -> Unit,
53         modifier: Modifier = Modifier,
54     ) {
55         val (disposableHandle, setDisposableHandle) =
56             remember { mutableStateOf<DisposableHandle?>(null) }
57         AndroidView(
58             factory = { context ->
59                 LayoutInflater.from(context)
60                     .inflate(
61                         R.layout.keyguard_settings_popup_menu,
62                         null,
63                     )
64                     .apply {
65                         isVisible = false
66                         alpha = 0f
67 
68                         setDisposableHandle(
69                             KeyguardSettingsViewBinder.bind(
70                                 view = this,
71                                 viewModel = viewModel,
72                                 longPressViewModel = longPressViewModel,
73                                 rootViewModel = null,
74                                 vibratorHelper = vibratorHelper,
75                                 activityStarter = activityStarter,
76                             )
77                         )
78                     }
79             },
80             onRelease = { disposableHandle?.dispose() },
81             modifier =
82                 modifier
83                     .padding(
84                         bottom = dimensionResource(R.dimen.keyguard_affordance_vertical_offset),
85                     )
86                     .padding(
87                         horizontal =
88                             dimensionResource(R.dimen.keyguard_affordance_horizontal_offset),
89                     )
90                     .onPlaced { coordinates ->
91                         onPlaced(
92                             if (!coordinates.size.toSize().isEmpty()) {
93                                 Rect(coordinates.positionInParent(), coordinates.size.toSize())
94                             } else {
95                                 null
96                             }
97                         )
98                     },
99         )
100     }
101 }
102