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.screenshot.ui.binder
18 
19 import android.view.View
20 import android.widget.ImageView
21 import android.widget.LinearLayout
22 import android.widget.TextView
23 import com.android.systemui.res.R
24 import com.android.systemui.screenshot.ui.TransitioningIconDrawable
25 import com.android.systemui.screenshot.ui.viewmodel.ActionButtonViewModel
26 import javax.inject.Inject
27 
28 class ActionButtonViewBinder @Inject constructor() {
29     /** Binds the given view to the given view-model */
bindnull30     fun bind(view: View, viewModel: ActionButtonViewModel) {
31         val iconView = view.requireViewById<ImageView>(R.id.overlay_action_chip_icon)
32         val textView = view.requireViewById<TextView>(R.id.overlay_action_chip_text)
33         if (iconView.drawable == null) {
34             iconView.setImageDrawable(TransitioningIconDrawable())
35         }
36         val drawable = iconView.drawable as? TransitioningIconDrawable
37         // Note we never re-bind a view to a different ActionButtonViewModel, different view
38         // models would remove/create separate views.
39         drawable?.setIcon(viewModel.appearance.icon)
40         iconView.setImageDrawable(viewModel.appearance.icon)
41         if (!viewModel.appearance.tint) {
42             iconView.setImageTintList(null)
43         }
44         textView.text = viewModel.appearance.label
45 
46         viewModel.appearance.customBackground?.also {
47             if (it.canApplyTheme()) {
48                 it.applyTheme(view.rootView.context.theme)
49             }
50             view.background = it
51         }
52 
53         setMargins(iconView, textView, viewModel.appearance.label?.isNotEmpty() ?: false)
54         if (viewModel.onClicked != null) {
55             view.setOnClickListener { viewModel.onClicked.invoke() }
56         } else {
57             view.setOnClickListener(null)
58         }
59         view.tag = viewModel.id
60         view.contentDescription = viewModel.appearance.description
61         view.visibility = View.VISIBLE
62         view.alpha = 1f
63     }
64 
setMarginsnull65     private fun setMargins(iconView: View, textView: View, hasText: Boolean) {
66         val iconParams = iconView.layoutParams as LinearLayout.LayoutParams
67         val textParams = textView.layoutParams as LinearLayout.LayoutParams
68         if (hasText) {
69             iconParams.marginStart = iconView.dpToPx(R.dimen.overlay_action_chip_padding_start)
70             iconParams.marginEnd = iconView.dpToPx(R.dimen.overlay_action_chip_spacing)
71             textParams.marginStart = 0
72             textParams.marginEnd = textView.dpToPx(R.dimen.overlay_action_chip_padding_end)
73         } else {
74             val paddingHorizontal =
75                 iconView.dpToPx(R.dimen.overlay_action_chip_icon_only_padding_horizontal)
76             iconParams.marginStart = paddingHorizontal
77             iconParams.marginEnd = paddingHorizontal
78         }
79         iconView.layoutParams = iconParams
80         textView.layoutParams = textParams
81     }
82 
dpToPxnull83     private fun View.dpToPx(dimenId: Int): Int {
84         return this.resources.getDimensionPixelSize(dimenId)
85     }
86 }
87