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 
18 package com.android.systemui.keyguard.ui.view.layout.sections
19 
20 import android.content.Context
21 import android.view.View
22 import androidx.constraintlayout.widget.ConstraintLayout
23 import androidx.constraintlayout.widget.ConstraintSet
24 import com.android.systemui.Flags
25 import com.android.systemui.keyguard.shared.model.KeyguardSection
26 import com.android.systemui.keyguard.ui.binder.AccessibilityActionsViewBinder
27 import com.android.systemui.keyguard.ui.viewmodel.AccessibilityActionsViewModel
28 import com.android.systemui.res.R
29 import com.android.systemui.util.Utils
30 import javax.inject.Inject
31 import kotlinx.coroutines.DisposableHandle
32 
33 /**
34  * A placeholder section that provides shortcuts for navigating on the keyguard through
35  * accessibility actions.
36  */
37 class AccessibilityActionsSection
38 @Inject
39 constructor(
40     private val context: Context,
41     private val accessibilityActionsViewModel: AccessibilityActionsViewModel,
42 ) : KeyguardSection() {
43     private var accessibilityActionsViewHandle: DisposableHandle? = null
44 
addViewsnull45     override fun addViews(constraintLayout: ConstraintLayout) {
46         if (!communalEnabled(context)) {
47             return
48         }
49         val view = View(constraintLayout.context).apply { id = R.id.accessibility_actions_view }
50         constraintLayout.addView(view)
51     }
52 
bindDatanull53     override fun bindData(constraintLayout: ConstraintLayout) {
54         if (!communalEnabled(context)) {
55             return
56         }
57         accessibilityActionsViewHandle =
58             AccessibilityActionsViewBinder.bind(
59                 constraintLayout.requireViewById(R.id.accessibility_actions_view),
60                 accessibilityActionsViewModel,
61             )
62     }
63 
applyConstraintsnull64     override fun applyConstraints(constraintSet: ConstraintSet) {
65         val accessibilityActionsViewId = R.id.accessibility_actions_view
66         constraintSet.apply {
67             // Starts from the bottom of the status bar.
68             connect(
69                 accessibilityActionsViewId,
70                 ConstraintSet.TOP,
71                 ConstraintSet.PARENT_ID,
72                 ConstraintSet.TOP,
73                 Utils.getStatusBarHeaderHeightKeyguard(context)
74             )
75             connect(
76                 accessibilityActionsViewId,
77                 ConstraintSet.BOTTOM,
78                 ConstraintSet.PARENT_ID,
79                 ConstraintSet.BOTTOM,
80             )
81             // Full width
82             connect(
83                 accessibilityActionsViewId,
84                 ConstraintSet.START,
85                 ConstraintSet.PARENT_ID,
86                 ConstraintSet.START
87             )
88             connect(
89                 accessibilityActionsViewId,
90                 ConstraintSet.END,
91                 ConstraintSet.PARENT_ID,
92                 ConstraintSet.END
93             )
94         }
95     }
96 
removeViewsnull97     override fun removeViews(constraintLayout: ConstraintLayout) {
98         accessibilityActionsViewHandle?.dispose()
99         accessibilityActionsViewHandle = null
100         constraintLayout.removeView(R.id.accessibility_actions_view)
101     }
102 }
103 
communalEnablednull104 private fun communalEnabled(context: Context): Boolean {
105     return context.resources.getBoolean(R.bool.config_communalServiceEnabled) && Flags.communalHub()
106 }
107