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.car.developeroptions.notification;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.media.AudioManager;
22 import android.net.Uri;
23 import android.preference.SeekBarVolumizer;
24 import android.text.TextUtils;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.widget.ImageView;
28 import android.widget.SeekBar;
29 import android.widget.TextView;
30 
31 import androidx.annotation.VisibleForTesting;
32 import androidx.preference.PreferenceViewHolder;
33 
34 import com.android.car.developeroptions.R;
35 import com.android.car.developeroptions.widget.SeekBarPreference;
36 
37 import java.util.Objects;
38 
39 /** A slider preference that directly controls an audio stream volume (no dialog) **/
40 public class VolumeSeekBarPreference extends SeekBarPreference {
41     private static final String TAG = "VolumeSeekBarPreference";
42 
43     protected SeekBar mSeekBar;
44     private int mStream;
45     private SeekBarVolumizer mVolumizer;
46     private Callback mCallback;
47     private ImageView mIconView;
48     private TextView mSuppressionTextView;
49     private String mSuppressionText;
50     private boolean mMuted;
51     private boolean mZenMuted;
52     private int mIconResId;
53     private int mMuteIconResId;
54     private boolean mStopped;
55     @VisibleForTesting
56     AudioManager mAudioManager;
57 
VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)58     public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
59             int defStyleRes) {
60         super(context, attrs, defStyleAttr, defStyleRes);
61         setLayoutResource(R.layout.preference_volume_slider);
62         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
63     }
64 
VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr)65     public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
66         super(context, attrs, defStyleAttr);
67         setLayoutResource(R.layout.preference_volume_slider);
68         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
69     }
70 
VolumeSeekBarPreference(Context context, AttributeSet attrs)71     public VolumeSeekBarPreference(Context context, AttributeSet attrs) {
72         super(context, attrs);
73         setLayoutResource(R.layout.preference_volume_slider);
74         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
75     }
76 
VolumeSeekBarPreference(Context context)77     public VolumeSeekBarPreference(Context context) {
78         super(context);
79         setLayoutResource(R.layout.preference_volume_slider);
80         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
81     }
82 
83     @Override
isSelectable()84     public boolean isSelectable() {
85         return false;
86     }
87 
setStream(int stream)88     public void setStream(int stream) {
89         mStream = stream;
90         setMax(mAudioManager.getStreamMaxVolume(mStream));
91         // Use getStreamMinVolumeInt for non-public stream type
92         // eg: AudioManager.STREAM_BLUETOOTH_SCO
93         setMin(mAudioManager.getStreamMinVolumeInt(mStream));
94         setProgress(mAudioManager.getStreamVolume(mStream));
95     }
96 
setCallback(Callback callback)97     public void setCallback(Callback callback) {
98         mCallback = callback;
99     }
100 
onActivityResume()101     public void onActivityResume() {
102         if (mStopped) {
103             init();
104         }
105     }
106 
onActivityPause()107     public void onActivityPause() {
108         mStopped = true;
109         if (mVolumizer != null) {
110             mVolumizer.stop();
111             mVolumizer = null;
112         }
113     }
114 
115     @Override
onBindViewHolder(PreferenceViewHolder view)116     public void onBindViewHolder(PreferenceViewHolder view) {
117         super.onBindViewHolder(view);
118         mSeekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
119         mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
120         mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text);
121         init();
122     }
123 
init()124     protected void init() {
125         if (mSeekBar == null) return;
126         final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
127             @Override
128             public void onSampleStarting(SeekBarVolumizer sbv) {
129                 if (mCallback != null) {
130                     mCallback.onSampleStarting(sbv);
131                 }
132             }
133             @Override
134             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
135                 if (mCallback != null) {
136                     mCallback.onStreamValueChanged(mStream, progress);
137                 }
138             }
139             @Override
140             public void onMuted(boolean muted, boolean zenMuted) {
141                 if (mMuted == muted && mZenMuted == zenMuted) return;
142                 mMuted = muted;
143                 mZenMuted = zenMuted;
144                 updateIconView();
145             }
146         };
147         final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
148         if (mVolumizer == null) {
149             mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
150         }
151         mVolumizer.start();
152         mVolumizer.setSeekBar(mSeekBar);
153         updateIconView();
154         updateSuppressionText();
155         if (!isEnabled()) {
156             mSeekBar.setEnabled(false);
157             mVolumizer.stop();
158         }
159     }
160 
updateIconView()161     protected void updateIconView() {
162         if (mIconView == null) return;
163         if (mIconResId != 0) {
164             mIconView.setImageResource(mIconResId);
165         } else if (mMuteIconResId != 0 && mMuted && !mZenMuted) {
166             mIconView.setImageResource(mMuteIconResId);
167         } else {
168             mIconView.setImageDrawable(getIcon());
169         }
170     }
171 
showIcon(int resId)172     public void showIcon(int resId) {
173         // Instead of using setIcon, which will trigger listeners, this just decorates the
174         // preference temporarily with a new icon.
175         if (mIconResId == resId) return;
176         mIconResId = resId;
177         updateIconView();
178     }
179 
setMuteIcon(int resId)180     public void setMuteIcon(int resId) {
181         if (mMuteIconResId == resId) return;
182         mMuteIconResId = resId;
183         updateIconView();
184     }
185 
getMediaVolumeUri()186     private Uri getMediaVolumeUri() {
187         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
188                 + getContext().getPackageName()
189                 + "/" + R.raw.media_volume);
190     }
191 
setSuppressionText(String text)192     public void setSuppressionText(String text) {
193         if (Objects.equals(text, mSuppressionText)) return;
194         mSuppressionText = text;
195         updateSuppressionText();
196     }
197 
updateSuppressionText()198     protected void updateSuppressionText() {
199         if (mSuppressionTextView != null && mSeekBar != null) {
200             mSuppressionTextView.setText(mSuppressionText);
201             final boolean showSuppression = !TextUtils.isEmpty(mSuppressionText);
202             mSuppressionTextView.setVisibility(showSuppression ? View.VISIBLE : View.GONE);
203         }
204     }
205 
206     public interface Callback {
onSampleStarting(SeekBarVolumizer sbv)207         void onSampleStarting(SeekBarVolumizer sbv);
onStreamValueChanged(int stream, int progress)208         void onStreamValueChanged(int stream, int progress);
209     }
210 }
211