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 
18 package com.android.systemui.keyguard.ui.view.layout.sections
19 
20 import android.content.res.Resources
21 import android.view.WindowInsets
22 import androidx.constraintlayout.widget.ConstraintLayout
23 import androidx.constraintlayout.widget.ConstraintSet
24 import androidx.constraintlayout.widget.ConstraintSet.BOTTOM
25 import androidx.constraintlayout.widget.ConstraintSet.LEFT
26 import androidx.constraintlayout.widget.ConstraintSet.PARENT_ID
27 import androidx.constraintlayout.widget.ConstraintSet.RIGHT
28 import androidx.constraintlayout.widget.ConstraintSet.VISIBILITY_MODE_IGNORE
29 import com.android.systemui.animation.view.LaunchableImageView
30 import com.android.systemui.dagger.qualifiers.Main
31 import com.android.systemui.keyguard.KeyguardBottomAreaRefactor
32 import com.android.systemui.keyguard.domain.interactor.KeyguardBlueprintInteractor
33 import com.android.systemui.keyguard.ui.binder.KeyguardQuickAffordanceViewBinder
34 import com.android.systemui.keyguard.ui.view.layout.blueprints.transitions.IntraBlueprintTransition
35 import com.android.systemui.keyguard.ui.viewmodel.KeyguardQuickAffordancesCombinedViewModel
36 import com.android.systemui.keyguard.ui.viewmodel.KeyguardRootViewModel
37 import com.android.systemui.plugins.FalsingManager
38 import com.android.systemui.res.R
39 import com.android.systemui.statusbar.KeyguardIndicationController
40 import com.android.systemui.statusbar.VibratorHelper
41 import dagger.Lazy
42 import javax.inject.Inject
43 
44 class DefaultShortcutsSection
45 @Inject
46 constructor(
47     @Main private val resources: Resources,
48     private val keyguardQuickAffordancesCombinedViewModel:
49         KeyguardQuickAffordancesCombinedViewModel,
50     private val keyguardRootViewModel: KeyguardRootViewModel,
51     private val falsingManager: FalsingManager,
52     private val indicationController: KeyguardIndicationController,
53     private val vibratorHelper: VibratorHelper,
54     private val keyguardBlueprintInteractor: Lazy<KeyguardBlueprintInteractor>,
55 ) : BaseShortcutSection() {
56 
57     // Amount to increase the bottom margin by to avoid colliding with inset
58     private var safeInsetBottom = 0
59 
60     override fun addViews(constraintLayout: ConstraintLayout) {
61         if (KeyguardBottomAreaRefactor.isEnabled) {
62             addLeftShortcut(constraintLayout)
63             addRightShortcut(constraintLayout)
64 
65             constraintLayout
66                 .requireViewById<LaunchableImageView>(R.id.start_button)
67                 .setOnApplyWindowInsetsListener { _, windowInsets ->
68                     val tempSafeInset = windowInsets?.displayCutout?.safeInsetBottom ?: 0
69                     if (safeInsetBottom != tempSafeInset) {
70                         safeInsetBottom = tempSafeInset
71                         keyguardBlueprintInteractor
72                             .get()
73                             .refreshBlueprint(IntraBlueprintTransition.Type.DefaultTransition)
74                     }
75                     WindowInsets.CONSUMED
76                 }
77         }
78     }
79 
80     override fun bindData(constraintLayout: ConstraintLayout) {
81         if (KeyguardBottomAreaRefactor.isEnabled) {
82             leftShortcutHandle =
83                 KeyguardQuickAffordanceViewBinder.bind(
84                     constraintLayout.requireViewById(R.id.start_button),
85                     keyguardQuickAffordancesCombinedViewModel.startButton,
86                     keyguardQuickAffordancesCombinedViewModel.transitionAlpha,
87                     falsingManager,
88                     vibratorHelper,
89                 ) {
90                     indicationController.showTransientIndication(it)
91                 }
92             rightShortcutHandle =
93                 KeyguardQuickAffordanceViewBinder.bind(
94                     constraintLayout.requireViewById(R.id.end_button),
95                     keyguardQuickAffordancesCombinedViewModel.endButton,
96                     keyguardQuickAffordancesCombinedViewModel.transitionAlpha,
97                     falsingManager,
98                     vibratorHelper,
99                 ) {
100                     indicationController.showTransientIndication(it)
101                 }
102         }
103     }
104 
105     override fun applyConstraints(constraintSet: ConstraintSet) {
106         val width = resources.getDimensionPixelSize(R.dimen.keyguard_affordance_fixed_width)
107         val height = resources.getDimensionPixelSize(R.dimen.keyguard_affordance_fixed_height)
108         val horizontalOffsetMargin =
109             resources.getDimensionPixelSize(R.dimen.keyguard_affordance_horizontal_offset)
110         val verticalOffsetMargin =
111             resources.getDimensionPixelSize(R.dimen.keyguard_affordance_vertical_offset)
112 
113         constraintSet.apply {
114             constrainWidth(R.id.start_button, width)
115             constrainHeight(R.id.start_button, height)
116             connect(R.id.start_button, LEFT, PARENT_ID, LEFT, horizontalOffsetMargin)
117             connect(
118                 R.id.start_button,
119                 BOTTOM,
120                 PARENT_ID,
121                 BOTTOM,
122                 verticalOffsetMargin + safeInsetBottom
123             )
124 
125             constrainWidth(R.id.end_button, width)
126             constrainHeight(R.id.end_button, height)
127             connect(R.id.end_button, RIGHT, PARENT_ID, RIGHT, horizontalOffsetMargin)
128             connect(
129                 R.id.end_button,
130                 BOTTOM,
131                 PARENT_ID,
132                 BOTTOM,
133                 verticalOffsetMargin + safeInsetBottom
134             )
135 
136             // The constraint set visibility for start and end button are default visible, set to
137             // ignore so the view's own initial visibility (invisible) is used
138             setVisibilityMode(R.id.start_button, VISIBILITY_MODE_IGNORE)
139             setVisibilityMode(R.id.end_button, VISIBILITY_MODE_IGNORE)
140         }
141     }
142 }
143