• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.android.systemui.keyguard.ui.binder
19 
20 import android.annotation.SuppressLint
21 import android.view.Gravity
22 import android.view.LayoutInflater
23 import android.view.View
24 import android.view.WindowManager
25 import android.widget.PopupWindow
26 import com.android.systemui.R
27 import com.android.systemui.common.ui.binder.IconViewBinder
28 import com.android.systemui.common.ui.binder.TextViewBinder
29 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSettingsPopupMenuViewModel
30 
31 object KeyguardLongPressPopupViewBinder {
32     @SuppressLint("InflateParams") // We don't care that the parent is null.
createAndShownull33     fun createAndShow(
34         container: View,
35         viewModel: KeyguardSettingsPopupMenuViewModel,
36         onDismissed: () -> Unit,
37     ): () -> Unit {
38         val contentView: View =
39             LayoutInflater.from(container.context)
40                 .inflate(
41                     R.layout.keyguard_settings_popup_menu,
42                     null,
43                 )
44 
45         contentView.setOnClickListener { viewModel.onClicked() }
46         IconViewBinder.bind(
47             icon = viewModel.icon,
48             view = contentView.requireViewById(R.id.icon),
49         )
50         TextViewBinder.bind(
51             view = contentView.requireViewById(R.id.text),
52             viewModel = viewModel.text,
53         )
54 
55         val popupWindow =
56             PopupWindow(container.context).apply {
57                 windowLayoutType = WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
58                 setBackgroundDrawable(null)
59                 animationStyle = com.android.internal.R.style.Animation_Dialog
60                 isOutsideTouchable = true
61                 isFocusable = true
62                 setContentView(contentView)
63                 setOnDismissListener { onDismissed() }
64                 contentView.measure(
65                     View.MeasureSpec.makeMeasureSpec(
66                         0,
67                         View.MeasureSpec.UNSPECIFIED,
68                     ),
69                     View.MeasureSpec.makeMeasureSpec(
70                         0,
71                         View.MeasureSpec.UNSPECIFIED,
72                     ),
73                 )
74                 showAtLocation(
75                     container,
76                     Gravity.NO_GRAVITY,
77                     viewModel.position.x - contentView.measuredWidth / 2,
78                     viewModel.position.y -
79                         contentView.measuredHeight -
80                         container.context.resources.getDimensionPixelSize(
81                             R.dimen.keyguard_long_press_settings_popup_vertical_offset
82                         ),
83                 )
84             }
85 
86         return { popupWindow.dismiss() }
87     }
88 }
89