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