1 /*
2  * Copyright (C) 2024 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.keyboard.stickykeys.ui
18 
19 import android.app.Dialog
20 import android.content.Context
21 import android.view.Gravity
22 import android.view.Window
23 import android.view.WindowInsets
24 import android.view.WindowManager
25 import android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND
26 import android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
27 import android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
28 import android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL
29 import androidx.activity.ComponentDialog
30 import com.android.systemui.dagger.SysUISingleton
31 import com.android.systemui.dagger.qualifiers.Application
32 import com.android.systemui.keyboard.stickykeys.ui.view.createStickyKeyIndicatorView
33 import com.android.systemui.keyboard.stickykeys.ui.viewmodel.StickyKeysIndicatorViewModel
34 import com.android.systemui.res.R
35 import javax.inject.Inject
36 
37 @SysUISingleton
38 class StickyKeyDialogFactory
39 @Inject
40 constructor(
41     @Application val context: Context,
42 ) {
43 
createnull44     fun create(viewModel: StickyKeysIndicatorViewModel): Dialog {
45         return createStickyKeyIndicator(viewModel)
46     }
47 
createStickyKeyIndicatornull48     private fun createStickyKeyIndicator(viewModel: StickyKeysIndicatorViewModel): Dialog {
49         return ComponentDialog(context, R.style.Theme_SystemUI_Dialog_StickyKeys).apply {
50             // because we're requesting window feature it must be called before setting content
51             window?.setStickyKeyWindowAttributes()
52             setContentView(createStickyKeyIndicatorView(context, viewModel))
53         }
54     }
55 
Windownull56     private fun Window.setStickyKeyWindowAttributes() {
57         requestFeature(Window.FEATURE_NO_TITLE)
58         setType(TYPE_STATUS_BAR_SUB_PANEL)
59         addFlags(FLAG_NOT_FOCUSABLE or FLAG_NOT_TOUCHABLE)
60         clearFlags(FLAG_DIM_BEHIND)
61         setGravity(Gravity.TOP or Gravity.END)
62         attributes =
63             WindowManager.LayoutParams().apply {
64                 copyFrom(attributes)
65                 // needed because we're above system bars windows, see [TYPE_STATUS_BAR_SUB_PANEL]
66                 receiveInsetsIgnoringZOrder = true
67                 fitInsetsTypes = WindowInsets.Type.systemBars()
68                 title = "StickyKeysIndicator"
69             }
70     }
71 }
72