1 /*
2  * Copyright (C) 2021 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.car.volume;
18 
19 import android.annotation.ColorInt;
20 import android.graphics.drawable.Drawable;
21 import android.view.View;
22 import android.widget.ImageView;
23 import android.widget.SeekBar;
24 
25 import androidx.annotation.NonNull;
26 import androidx.recyclerview.widget.RecyclerView;
27 
28 import com.android.systemui.R;
29 
30 /** Holds all related data to represent a volume group. */
31 public class CarVolumeItem {
32 
33     private Drawable mPrimaryIcon;
34     private Drawable mPrimaryMuteIcon;
35     private Drawable mSupplementalIcon;
36     private View.OnClickListener mSupplementalIconOnClickListener;
37     private boolean mShowSupplementalIconDivider;
38     private int mGroupId;
39 
40     private int mMax;
41     private int mProgress;
42     private boolean mIsMuted;
43     private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;
44 
45     /**
46      * Called when {@link CarVolumeItem} is bound to its ViewHolder.
47      */
bind(CarVolumeItemViewHolder viewHolder)48     void bind(CarVolumeItemViewHolder viewHolder) {
49         viewHolder.bind(/* carVolumeItem= */ this);
50     }
51 
52     /** Sets progress of seekbar. */
setProgress(int progress)53     public void setProgress(int progress) {
54         mProgress = progress;
55     }
56 
57     /** Sets mute state of seekbar. */
setIsMuted(boolean isMuted)58     public void setIsMuted(boolean isMuted) {
59         mIsMuted = isMuted;
60     }
61 
62     /** Sets max value of seekbar. */
setMax(int max)63     public void setMax(int max) {
64         mMax = max;
65     }
66 
67     /** Sets {@link SeekBar.OnSeekBarChangeListener}. */
setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener)68     public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener) {
69         mOnSeekBarChangeListener = listener;
70     }
71 
72     /** Sets the primary icon. */
setPrimaryIcon(Drawable drawable)73     public void setPrimaryIcon(Drawable drawable) {
74         mPrimaryIcon = drawable;
75     }
76 
77     /** Sets the primary mute icon. */
setPrimaryMuteIcon(Drawable drawable)78     public void setPrimaryMuteIcon(Drawable drawable) {
79         mPrimaryMuteIcon = drawable;
80     }
81 
82     /** Sets the supplemental icon and the visibility of the supplemental icon divider. */
setSupplementalIcon(Drawable drawable, boolean showSupplementalIconDivider)83     public void setSupplementalIcon(Drawable drawable, boolean showSupplementalIconDivider) {
84         mSupplementalIcon = drawable;
85         mShowSupplementalIconDivider = showSupplementalIconDivider;
86     }
87 
88     /**
89      * Gets the group id associated.
90      */
getGroupId()91     public int getGroupId() {
92         return mGroupId;
93     }
94 
95     /**
96      * Sets the group id associated.
97      */
setGroupId(int groupId)98     public void setGroupId(int groupId) {
99         this.mGroupId = groupId;
100     }
101 
102     /** Sets {@code OnClickListener} for the supplemental icon. */
setSupplementalIconListener(View.OnClickListener listener)103     public void setSupplementalIconListener(View.OnClickListener listener) {
104         mSupplementalIconOnClickListener = listener;
105     }
106 
107     /** Defines the view holder which shows the information held by {@link CarVolumeItem}. */
108     public static class CarVolumeItemViewHolder extends RecyclerView.ViewHolder {
109 
110         private SeekBar mSeekBar;
111         private ImageView mPrimaryIcon;
112         private View mSupplementalIconDivider;
113         private ImageView mSupplementalIcon;
114 
CarVolumeItemViewHolder(@onNull View itemView)115         public CarVolumeItemViewHolder(@NonNull View itemView) {
116             super(itemView);
117 
118             mSeekBar = itemView.findViewById(R.id.volume_seek_bar);
119             mPrimaryIcon = itemView.findViewById(R.id.primary_icon);
120             mSupplementalIcon = itemView.findViewById(R.id.supplemental_icon);
121             mSupplementalIconDivider = itemView.findViewById(R.id.supplemental_icon_divider);
122         }
123 
124         /**
125          * Binds {@link CarVolumeItem} to the {@link CarVolumeItemViewHolder}.
126          */
bind(CarVolumeItem carVolumeItem)127         void bind(CarVolumeItem carVolumeItem) {
128             // Progress bar
129             mSeekBar.setMax(carVolumeItem.mMax);
130             mSeekBar.setProgress(carVolumeItem.mProgress);
131             mSeekBar.setOnSeekBarChangeListener(carVolumeItem.mOnSeekBarChangeListener);
132 
133             // Primary icon
134             mPrimaryIcon.setVisibility(View.VISIBLE);
135             if (carVolumeItem.mIsMuted) {
136                 mPrimaryIcon.setImageDrawable(carVolumeItem.mPrimaryMuteIcon);
137             } else {
138                 mPrimaryIcon.setImageDrawable(carVolumeItem.mPrimaryIcon);
139             }
140 
141             // Supplemental icon
142             mSupplementalIcon.setVisibility(View.VISIBLE);
143             mSupplementalIconDivider.setVisibility(
144                     carVolumeItem.mShowSupplementalIconDivider ? View.VISIBLE : View.INVISIBLE);
145             mSupplementalIcon.setImageDrawable(carVolumeItem.mSupplementalIcon);
146             mSupplementalIcon.setOnClickListener(
147                     carVolumeItem.mSupplementalIconOnClickListener);
148             mSupplementalIcon.setClickable(
149                     carVolumeItem.mSupplementalIconOnClickListener != null);
150         }
151 
152         /** Refresh the icons drawable color. */
setIconDrawableColor(@olorInt int color)153         void setIconDrawableColor(@ColorInt int color) {
154             Drawable primaryIconDrawable = mPrimaryIcon.getDrawable();
155             primaryIconDrawable.mutate().setTint(color);
156             mPrimaryIcon.setImageDrawable(primaryIconDrawable);
157 
158             if (mSupplementalIcon != null && mSupplementalIcon.getDrawable() != null) {
159                 Drawable supplementalIconDrawable = mSupplementalIcon.getDrawable();
160                 supplementalIconDrawable.mutate().setTint(color);
161                 mSupplementalIcon.setImageDrawable(supplementalIconDrawable);
162             }
163         }
164     }
165 }
166