• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.media.controls.ui.view
18 
19 import android.content.res.ColorStateList
20 import android.util.Log
21 import android.view.View
22 import android.view.ViewGroup
23 import android.widget.ImageButton
24 import android.widget.TextView
25 import com.android.systemui.Flags
26 import com.android.systemui.media.controls.ui.animation.accentPrimaryFromScheme
27 import com.android.systemui.media.controls.ui.animation.onPrimaryFromScheme
28 import com.android.systemui.media.controls.ui.animation.primaryFromScheme
29 import com.android.systemui.media.controls.ui.animation.surfaceFromScheme
30 import com.android.systemui.media.controls.ui.animation.textPrimaryFromScheme
31 import com.android.systemui.monet.ColorScheme
32 import com.android.systemui.res.R
33 
34 /**
35  * A view holder for the guts menu of a media player. The guts are shown when the user long-presses
36  * on the media player.
37  *
38  * Both [MediaViewHolder] and [RecommendationViewHolder] use the same guts menu layout, so this
39  * class helps share logic between the two.
40  */
41 class GutsViewHolder(itemView: View) {
42     val gutsText: TextView = itemView.requireViewById(R.id.remove_text)
43     val cancel: View = itemView.requireViewById(R.id.cancel)
44     val cancelText: TextView = itemView.requireViewById(R.id.cancel_text)
45     val dismiss: ViewGroup = itemView.requireViewById(R.id.dismiss)
46     val dismissText: TextView = itemView.requireViewById(R.id.dismiss_text)
47     val settings: ImageButton = itemView.requireViewById(R.id.settings)
48 
49     private var isDismissible: Boolean = true
50     // TODO(media_controls_a11y_colors): make private
51     var colorScheme: ColorScheme? = null
52     private var textColorFixed: Int? = null
53 
54     /** Marquees the main text of the guts menu. */
marqueenull55     fun marquee(start: Boolean, delay: Long, tag: String) {
56         val gutsTextHandler = gutsText.handler
57         if (gutsTextHandler == null) {
58             Log.d(tag, "marquee while longPressText.getHandler() is null", Exception())
59             return
60         }
61         gutsTextHandler.postDelayed({ gutsText.isSelected = start }, delay)
62     }
63 
64     /** Set whether this control can be dismissed, and update appearance to match */
setDismissiblenull65     fun setDismissible(dismissible: Boolean) {
66         if (isDismissible == dismissible) return
67 
68         isDismissible = dismissible
69         colorScheme?.let { setColors(it) }
70     }
71 
72     /** Sets the right colors on all the guts views based on the given [ColorScheme]. */
setColorsnull73     fun setColors(scheme: ColorScheme) {
74         colorScheme = scheme
75 
76         if (Flags.mediaControlsA11yColors()) {
77             textColorFixed?.let { setTextColor(it) }
78             setPrimaryColor(primaryFromScheme(scheme))
79             setOnPrimaryColor(onPrimaryFromScheme(scheme))
80         } else {
81             setSurfaceColor(surfaceFromScheme(scheme))
82             setTextPrimaryColor(textPrimaryFromScheme(scheme))
83             setAccentPrimaryColor(accentPrimaryFromScheme(scheme))
84         }
85     }
86 
setPrimaryColornull87     private fun setPrimaryColor(color: Int) {
88         val colorList = ColorStateList.valueOf(color)
89         dismissText.backgroundTintList = colorList
90         cancelText.backgroundTintList = colorList
91     }
92 
setOnPrimaryColornull93     private fun setOnPrimaryColor(color: Int) {
94         dismissText.setTextColor(color)
95         if (!isDismissible) {
96             cancelText.setTextColor(color)
97         }
98     }
99 
setTextColornull100     fun setTextColor(color: Int) {
101         textColorFixed = color
102         gutsText.setTextColor(color)
103         settings.imageTintList = ColorStateList.valueOf(color)
104 
105         if (isDismissible) {
106             cancelText.setTextColor(color)
107         }
108     }
109 
110     /** Sets the surface color on all guts views that use it. */
111     @Deprecated("Remove with media_controls_a11y_colors")
setSurfaceColornull112     fun setSurfaceColor(surfaceColor: Int) {
113         dismissText.setTextColor(surfaceColor)
114         if (!isDismissible) {
115             cancelText.setTextColor(surfaceColor)
116         }
117     }
118 
119     /** Sets the primary accent color on all guts views that use it. */
120     @Deprecated("Remove with media_controls_a11y_colors")
setAccentPrimaryColornull121     fun setAccentPrimaryColor(accentPrimary: Int) {
122         val accentColorList = ColorStateList.valueOf(accentPrimary)
123         settings.imageTintList = accentColorList
124         cancelText.backgroundTintList = accentColorList
125         dismissText.backgroundTintList = accentColorList
126     }
127 
128     /** Sets the primary text color on all guts views that use it. */
129     @Deprecated("Remove with media_controls_a11y_colors")
setTextPrimaryColornull130     fun setTextPrimaryColor(textPrimary: Int) {
131         val textColorList = ColorStateList.valueOf(textPrimary)
132         gutsText.setTextColor(textColorList)
133         if (isDismissible) {
134             cancelText.setTextColor(textColorList)
135         }
136     }
137 
138     companion object {
139         val ids = setOf(R.id.remove_text, R.id.cancel, R.id.dismiss, R.id.settings)
140     }
141 }
142