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 package com.android.wallpaper.picker.preview.ui.view
17 
18 import android.content.Context
19 import android.util.AttributeSet
20 import android.view.LayoutInflater
21 import android.view.View
22 import android.widget.FrameLayout
23 import android.widget.ToggleButton
24 import androidx.appcompat.content.res.AppCompatResources
25 import androidx.core.view.isVisible
26 import com.android.wallpaper.R
27 import com.android.wallpaper.picker.preview.ui.viewmodel.Action
28 
29 /** Custom layout for a group of wallpaper preview actions. */
30 class PreviewActionGroup(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
31 
32     private val informationButton: ToggleButton
33     private val downloadButton: FrameLayout
34     private val downloadButtonToggle: ToggleButton
35     private val downloadButtonProgress: FrameLayout
36     private val deleteButton: ToggleButton
37     private val editButton: ToggleButton
38     private val customizeButton: ToggleButton
39     private val effectsButton: ToggleButton
40     private val shareButton: ToggleButton
41 
42     init {
43         LayoutInflater.from(context).inflate(R.layout.preview_action_group, this, true)
44         informationButton = requireViewById(R.id.information_button)
45         downloadButton = requireViewById(R.id.download_button)
46         downloadButtonToggle = requireViewById(R.id.download_button_toggle)
47         downloadButtonProgress = requireViewById(R.id.download_button_progress)
48         deleteButton = requireViewById(R.id.delete_button)
49         editButton = requireViewById(R.id.edit_button)
50         customizeButton = requireViewById(R.id.customize_button)
51         effectsButton = requireViewById(R.id.effects_button)
52         shareButton = requireViewById(R.id.share_button)
53     }
54 
setIsCheckednull55     fun setIsChecked(action: Action, isChecked: Boolean) {
56         // Updates only when the view state is different from the input isChecked
57         if (getActionToggle(action).isChecked != isChecked) {
58             getActionToggle(action).isChecked = isChecked
59         }
60     }
61 
setIsVisiblenull62     fun setIsVisible(action: Action, isVisible: Boolean) {
63         getActionView(action).isVisible = isVisible
64     }
65 
setClickListenernull66     fun setClickListener(action: Action, listener: (() -> Unit)?) {
67         getActionToggle(action)
68             .setOnClickListener(
69                 if (listener != null) {
70                     { listener.invoke() }
71                 } else null
72             )
73     }
74 
setIsDownloadingnull75     fun setIsDownloading(isDownloading: Boolean) {
76         downloadButtonProgress.isVisible = isDownloading
77         downloadButtonToggle.isVisible = !isDownloading
78     }
79 
getActionViewnull80     private fun getActionView(action: Action): View {
81         return when (action) {
82             Action.INFORMATION -> informationButton
83             Action.DOWNLOAD -> downloadButton
84             Action.DELETE -> deleteButton
85             Action.EDIT -> editButton
86             Action.CUSTOMIZE -> customizeButton
87             Action.EFFECTS -> effectsButton
88             Action.SHARE -> shareButton
89         }
90     }
91 
getActionTogglenull92     private fun getActionToggle(action: Action): ToggleButton {
93         return when (action) {
94             Action.INFORMATION -> informationButton
95             Action.DOWNLOAD -> downloadButtonToggle
96             Action.DELETE -> deleteButton
97             Action.EDIT -> editButton
98             Action.CUSTOMIZE -> customizeButton
99             Action.EFFECTS -> effectsButton
100             Action.SHARE -> shareButton
101         }
102     }
103 
104     /** Update the background color in case the context theme has changed. */
updateBackgroundColornull105     fun updateBackgroundColor() {
106         val context = context ?: return
107         informationButton.foreground = null
108         downloadButtonToggle.foreground = null
109         downloadButtonProgress.background = null
110         deleteButton.foreground = null
111         editButton.foreground = null
112         customizeButton.foreground = null
113         effectsButton.foreground = null
114         shareButton.foreground = null
115         informationButton.foreground =
116             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_info)
117         downloadButtonToggle.foreground =
118             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_download)
119         downloadButtonProgress.background =
120             AppCompatResources.getDrawable(
121                 context,
122                 R.drawable.wallpaper_control_button_off_background
123             )
124         deleteButton.foreground =
125             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_delete)
126         editButton.foreground =
127             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_edit)
128         customizeButton.foreground =
129             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_customize)
130         effectsButton.foreground =
131             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_effect)
132         shareButton.foreground =
133             AppCompatResources.getDrawable(context, R.drawable.wallpaper_control_button_share)
134     }
135 }
136