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.communal.ui.view.layout.sections
19 
20 import android.content.res.Resources
21 import android.graphics.Typeface
22 import android.graphics.Typeface.NORMAL
23 import android.view.Gravity
24 import android.view.View
25 import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
26 import android.widget.TextView
27 import androidx.constraintlayout.widget.ConstraintLayout
28 import androidx.constraintlayout.widget.ConstraintSet
29 import androidx.core.content.res.ResourcesCompat
30 import com.android.systemui.communal.domain.interactor.CommunalInteractor
31 import com.android.systemui.communal.ui.binder.CommunalTutorialIndicatorViewBinder
32 import com.android.systemui.communal.ui.viewmodel.CommunalTutorialIndicatorViewModel
33 import com.android.systemui.dagger.qualifiers.Main
34 import com.android.systemui.keyguard.shared.model.KeyguardSection
35 import com.android.systemui.keyguard.ui.view.layout.sections.removeView
36 import com.android.systemui.res.R
37 import javax.inject.Inject
38 import kotlinx.coroutines.DisposableHandle
39 
40 class CommunalTutorialIndicatorSection
41 @Inject
42 constructor(
43     @Main private val resources: Resources,
44     private val communalTutorialIndicatorViewModel: CommunalTutorialIndicatorViewModel,
45     private val communalInteractor: CommunalInteractor,
46 ) : KeyguardSection() {
47     private var communalTutorialIndicatorHandle: DisposableHandle? = null
48 
addViewsnull49     override fun addViews(constraintLayout: ConstraintLayout) {
50         if (!communalInteractor.isCommunalEnabled.value) {
51             return
52         }
53         val padding =
54             constraintLayout.resources.getDimensionPixelSize(
55                 R.dimen.communal_tutorial_indicator_padding
56             )
57         val view =
58             TextView(constraintLayout.context).apply {
59                 id = R.id.communal_tutorial_indicator
60                 visibility = View.GONE
61                 background =
62                     ResourcesCompat.getDrawable(
63                         context.resources,
64                         R.drawable.keyguard_bottom_affordance_bg,
65                         context.theme
66                     )
67                 foreground =
68                     ResourcesCompat.getDrawable(
69                         context.resources,
70                         R.drawable.keyguard_bottom_affordance_selected_border,
71                         context.theme
72                     )
73                 gravity = Gravity.CENTER_VERTICAL
74                 typeface = Typeface.create("google-sans", NORMAL)
75                 text = constraintLayout.context.getString(R.string.communal_tutorial_indicator_text)
76                 setPadding(padding, padding, padding, padding)
77             }
78         constraintLayout.addView(view)
79     }
80 
bindDatanull81     override fun bindData(constraintLayout: ConstraintLayout) {
82         if (!communalInteractor.isCommunalEnabled.value) {
83             return
84         }
85         communalTutorialIndicatorHandle =
86             CommunalTutorialIndicatorViewBinder.bind(
87                 constraintLayout.requireViewById(R.id.communal_tutorial_indicator),
88                 communalTutorialIndicatorViewModel,
89             )
90     }
91 
applyConstraintsnull92     override fun applyConstraints(constraintSet: ConstraintSet) {
93         if (!communalInteractor.isCommunalEnabled.value) {
94             return
95         }
96         val tutorialIndicatorId = R.id.communal_tutorial_indicator
97         val width = resources.getDimensionPixelSize(R.dimen.communal_tutorial_indicator_fixed_width)
98         val horizontalOffsetMargin =
99             resources.getDimensionPixelSize(R.dimen.communal_tutorial_indicator_horizontal_offset)
100 
101         constraintSet.apply {
102             constrainWidth(tutorialIndicatorId, width)
103             constrainHeight(tutorialIndicatorId, WRAP_CONTENT)
104             connect(
105                 tutorialIndicatorId,
106                 ConstraintSet.RIGHT,
107                 ConstraintSet.PARENT_ID,
108                 ConstraintSet.RIGHT,
109                 horizontalOffsetMargin
110             )
111             connect(
112                 tutorialIndicatorId,
113                 ConstraintSet.TOP,
114                 ConstraintSet.PARENT_ID,
115                 ConstraintSet.TOP
116             )
117             connect(
118                 tutorialIndicatorId,
119                 ConstraintSet.BOTTOM,
120                 ConstraintSet.PARENT_ID,
121                 ConstraintSet.BOTTOM
122             )
123             setVisibility(tutorialIndicatorId, View.GONE)
124         }
125     }
126 
removeViewsnull127     override fun removeViews(constraintLayout: ConstraintLayout) {
128         communalTutorialIndicatorHandle?.dispose()
129         constraintLayout.removeView(R.id.communal_tutorial_indicator)
130     }
131 }
132