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.media.controls.ui.animation.accentPrimaryFromScheme
26 import com.android.systemui.media.controls.ui.animation.surfaceFromScheme
27 import com.android.systemui.media.controls.ui.animation.textPrimaryFromScheme
28 import com.android.systemui.monet.ColorScheme
29 import com.android.systemui.res.R
30 
31 /**
32  * A view holder for the guts menu of a media player. The guts are shown when the user long-presses
33  * on the media player.
34  *
35  * Both [MediaViewHolder] and [RecommendationViewHolder] use the same guts menu layout, so this
36  * class helps share logic between the two.
37  */
38 class GutsViewHolder constructor(itemView: View) {
39     val gutsText: TextView = itemView.requireViewById(R.id.remove_text)
40     val cancel: View = itemView.requireViewById(R.id.cancel)
41     val cancelText: TextView = itemView.requireViewById(R.id.cancel_text)
42     val dismiss: ViewGroup = itemView.requireViewById(R.id.dismiss)
43     val dismissText: TextView = itemView.requireViewById(R.id.dismiss_text)
44     val settings: ImageButton = itemView.requireViewById(R.id.settings)
45 
46     private var isDismissible: Boolean = true
47     var colorScheme: ColorScheme? = null
48 
49     /** Marquees the main text of the guts menu. */
marqueenull50     fun marquee(start: Boolean, delay: Long, tag: String) {
51         val gutsTextHandler = gutsText.handler
52         if (gutsTextHandler == null) {
53             Log.d(tag, "marquee while longPressText.getHandler() is null", Exception())
54             return
55         }
56         gutsTextHandler.postDelayed({ gutsText.isSelected = start }, delay)
57     }
58 
59     /** Set whether this control can be dismissed, and update appearance to match */
setDismissiblenull60     fun setDismissible(dismissible: Boolean) {
61         if (isDismissible == dismissible) return
62 
63         isDismissible = dismissible
64         colorScheme?.let { setColors(it) }
65     }
66 
67     /** Sets the right colors on all the guts views based on the given [ColorScheme]. */
setColorsnull68     fun setColors(scheme: ColorScheme) {
69         colorScheme = scheme
70         setSurfaceColor(surfaceFromScheme(scheme))
71         setTextPrimaryColor(textPrimaryFromScheme(scheme))
72         setAccentPrimaryColor(accentPrimaryFromScheme(scheme))
73     }
74 
75     /** Sets the surface color on all guts views that use it. */
setSurfaceColornull76     fun setSurfaceColor(surfaceColor: Int) {
77         dismissText.setTextColor(surfaceColor)
78         if (!isDismissible) {
79             cancelText.setTextColor(surfaceColor)
80         }
81     }
82 
83     /** Sets the primary accent color on all guts views that use it. */
setAccentPrimaryColornull84     fun setAccentPrimaryColor(accentPrimary: Int) {
85         val accentColorList = ColorStateList.valueOf(accentPrimary)
86         settings.imageTintList = accentColorList
87         cancelText.backgroundTintList = accentColorList
88         dismissText.backgroundTintList = accentColorList
89     }
90 
91     /** Sets the primary text color on all guts views that use it. */
setTextPrimaryColornull92     fun setTextPrimaryColor(textPrimary: Int) {
93         val textColorList = ColorStateList.valueOf(textPrimary)
94         gutsText.setTextColor(textColorList)
95         if (isDismissible) {
96             cancelText.setTextColor(textColorList)
97         }
98     }
99 
100     companion object {
101         val ids = setOf(R.id.remove_text, R.id.cancel, R.id.dismiss, R.id.settings)
102     }
103 }
104