1 /*
2  * Copyright (C) 2019 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.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 mSupplementalIcon;
34     private View.OnClickListener mSupplementalIconOnClickListener;
35     private boolean mShowSupplementalIconDivider;
36     private int mGroupId;
37 
38     private int mMax;
39     private int mProgress;
40     private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;
41 
42     /**
43      * Called when {@link CarVolumeItem} is bound to its ViewHolder.
44      */
bind(CarVolumeItemViewHolder viewHolder)45     void bind(CarVolumeItemViewHolder viewHolder) {
46             viewHolder.bind(/* carVolumeItem= */ this);
47     }
48 
49     /** Sets progress of seekbar. */
setProgress(int progress)50     public void setProgress(int progress) {
51         mProgress = progress;
52     }
53 
54     /** Sets max value of seekbar. */
setMax(int max)55     public void setMax(int max) {
56         mMax = max;
57     }
58 
59     /** Sets {@link SeekBar.OnSeekBarChangeListener}. */
setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener)60     public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener) {
61         mOnSeekBarChangeListener = listener;
62     }
63 
64     /** Sets the primary icon. */
setPrimaryIcon(Drawable drawable)65     public void setPrimaryIcon(Drawable drawable) {
66         mPrimaryIcon = drawable;
67     }
68 
69     /** Sets the supplemental icon and the visibility of the supplemental icon divider. */
setSupplementalIcon(Drawable drawable, boolean showSupplementalIconDivider)70     public void setSupplementalIcon(Drawable drawable, boolean showSupplementalIconDivider) {
71         mSupplementalIcon = drawable;
72         mShowSupplementalIconDivider = showSupplementalIconDivider;
73     }
74 
75     /**
76      * Gets the group id associated.
77      */
getGroupId()78     public int getGroupId() {
79         return mGroupId;
80     }
81 
82     /**
83      * Sets the group id associated.
84      */
setGroupId(int groupId)85     public void setGroupId(int groupId) {
86         this.mGroupId = groupId;
87     }
88 
89     /** Sets {@code OnClickListener} for the supplemental icon. */
setSupplementalIconListener(View.OnClickListener listener)90     public void setSupplementalIconListener(View.OnClickListener listener) {
91         mSupplementalIconOnClickListener = listener;
92     }
93 
94     /** Defines the view holder which shows the information held by {@link CarVolumeItem}. */
95     public static class CarVolumeItemViewHolder extends RecyclerView.ViewHolder {
96 
97         private SeekBar mSeekBar;
98         private ImageView mPrimaryIcon;
99         private View mSupplementalIconDivider;
100         private ImageView mSupplementalIcon;
101 
CarVolumeItemViewHolder(@onNull View itemView)102         public CarVolumeItemViewHolder(@NonNull View itemView) {
103             super(itemView);
104 
105             mSeekBar = itemView.findViewById(R.id.seek_bar);
106             mPrimaryIcon = itemView.findViewById(R.id.primary_icon);
107             mSupplementalIcon = itemView.findViewById(R.id.supplemental_icon);
108             mSupplementalIconDivider = itemView.findViewById(R.id.supplemental_icon_divider);
109         }
110 
111         /**
112          * Binds {@link CarVolumeItem} to the {@link CarVolumeItemViewHolder}.
113          */
bind(CarVolumeItem carVolumeItem)114         void bind(CarVolumeItem carVolumeItem) {
115             // Progress bar
116             mSeekBar.setMax(carVolumeItem.mMax);
117             mSeekBar.setProgress(carVolumeItem.mProgress);
118             mSeekBar.setOnSeekBarChangeListener(carVolumeItem.mOnSeekBarChangeListener);
119 
120             // Primary icon
121             mPrimaryIcon.setVisibility(View.VISIBLE);
122             mPrimaryIcon.setImageDrawable(carVolumeItem.mPrimaryIcon);
123 
124             // Supplemental icon
125             mSupplementalIcon.setVisibility(View.VISIBLE);
126             mSupplementalIconDivider.setVisibility(
127                     carVolumeItem.mShowSupplementalIconDivider ? View.VISIBLE : View.INVISIBLE);
128             mSupplementalIcon.setImageDrawable(carVolumeItem.mSupplementalIcon);
129             mSupplementalIcon.setOnClickListener(
130                     carVolumeItem.mSupplementalIconOnClickListener);
131             mSupplementalIcon.setClickable(
132                     carVolumeItem.mSupplementalIconOnClickListener != null);
133         }
134     }
135 }
136