Debian Codegraph @

core/java/android/preference/SeekBarVolumizer.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 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 android.preference;
18
19 import android.annotation.NonNull;
20 import android.app.NotificationManager;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.database.ContentObserver;
27 import android.media.AudioAttributes;
28 import android.media.AudioManager;
29 import android.media.Ringtone;
30 import android.media.RingtoneManager;
31 import android.media.audiopolicy.AudioProductStrategy;
32 import android.media.audiopolicy.AudioVolumeGroup;
33 import android.net.Uri;
34 import android.os.Handler;
35 import android.os.HandlerThread;
36 import android.os.Message;
37 import android.preference.VolumePreference.VolumeStore;
38 import android.provider.Settings;
39 import android.provider.Settings.Global;
40 import android.provider.Settings.System;
41 import android.service.notification.ZenModeConfig;
42 import android.util.Log;
43 import android.widget.SeekBar;
44 import android.widget.SeekBar.OnSeekBarChangeListener;
45
46 import com.android.internal.annotations.GuardedBy;
47 import com.android.internal.os.SomeArgs;
48
49 import java.util.concurrent.TimeUnit;
50
51 /**
52 * Turns a {@link SeekBar} into a volume control.
53 * @hide
54 *
55 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
56 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
57 * Preference Library</a> for consistent behavior across all devices. For more information on
58 * using the AndroidX Preference Library see
59 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
60 */
61 @Deprecated
62 public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callback {
63 private static final String TAG = "SeekBarVolumizer";
64
65 public interface Callback {
66 void onSampleStarting(SeekBarVolumizer sbv);
67 void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch);
68 void onMuted(boolean muted, boolean zenMuted);
69 /**
70 * Callback reporting that the seek bar is start tracking.
71 *
72 * @param sbv - The seek bar that start tracking
73 */
74 void onStartTrackingTouch(SeekBarVolumizer sbv);
75
76 /**
77 * Callback reporting that the seek bar is stop tracking.
78 *
79 * @param sbv - The seek bar that stop tracking
80 */
81 default void onStopTrackingTouch(SeekBarVolumizer sbv) {
82 }
83 }
84
85 private static final int MSG_GROUP_VOLUME_CHANGED = 1;
86 private static long sStopVolumeTime = 0L;
87 private final Handler mVolumeHandler = new VolumeHandler();
88 private AudioAttributes mAttributes;
89 private int mVolumeGroupId;
90
91 private final AudioManager.VolumeGroupCallback mVolumeGroupCallback =
92 new AudioManager.VolumeGroupCallback() {
93 @Override
94 public void onAudioVolumeGroupChanged(int group, int flags) {
95 if (mHandler == null) {
96 return;
97 }
98 SomeArgs args = SomeArgs.obtain();
99 args.arg1 = group;
100 args.arg2 = flags;
101 mVolumeHandler.sendMessage(mHandler.obtainMessage(MSG_GROUP_VOLUME_CHANGED, args));
102 }
103 };
104
105 @UnsupportedAppUsage
106 private final Context mContext;
107 private final H mUiHandler = new H();
108 private final Callback mCallback;
109 private final Uri mDefaultUri;
110 @UnsupportedAppUsage
111 private final AudioManager mAudioManager;
112 private final NotificationManager mNotificationManager;
113 @UnsupportedAppUsage
114 private final int mStreamType;
115 private final int mMaxStreamVolume;
116 private boolean mAffectedByRingerMode;
117 private boolean mNotificationOrRing;
118 private final boolean mNotifAliasRing;
119 private final Receiver mReceiver = new Receiver();
120
121 private Handler mHandler;
122 private Observer mVolumeObserver;
123 @UnsupportedAppUsage
124 private int mOriginalStreamVolume;
125 private int mLastAudibleStreamVolume;
126 // When the old handler is destroyed and a new one is created, there could be a situation where
127 // this is accessed at the same time in different handlers. So, access to this field needs to be
128 // synchronized.
129 @GuardedBy("this")
130 @UnsupportedAppUsage
131 private Ringtone mRingtone;
132 @UnsupportedAppUsage
133 private int mLastProgress = -1;
134 private boolean mMuted;
135 @UnsupportedAppUsage
136 private SeekBar mSeekBar;
137 private int mVolumeBeforeMute = -1;
138 private int mRingerMode;
139 private int mZenMode;
140 private boolean mPlaySample;
141
142 private static final int MSG_SET_STREAM_VOLUME = 0;
143 private static final int MSG_START_SAMPLE = 1;
144 private static final int MSG_STOP_SAMPLE = 2;
145 private static final int MSG_INIT_SAMPLE = 3;
146 private static final int CHECK_RINGTONE_PLAYBACK_DELAY_MS = 1000;
147 private static final long SET_STREAM_VOLUME_DELAY_MS = TimeUnit.MILLISECONDS.toMillis(500);
148 private static final long START_SAMPLE_DELAY_MS = TimeUnit.MILLISECONDS.toMillis(500);
149 private static final long DURATION_TO_START_DELAYING = TimeUnit.MILLISECONDS.toMillis(2000);
150
151 private NotificationManager.Policy mNotificationPolicy;
152 private boolean mAllowAlarms;
153 private boolean mAllowMedia;
154 private boolean mAllowRinger;
155
156 @UnsupportedAppUsage
157 public SeekBarVolumizer(Context context, int streamType, Uri defaultUri, Callback callback) {
158 this(context, streamType, defaultUri, callback, true /* playSample */);
159 }
160
161 public SeekBarVolumizer(
162 Context context,
163 int streamType,
164 Uri defaultUri,
165 Callback callback,
166 boolean playSample) {
167 mContext = context;
168 mAudioManager = context.getSystemService(AudioManager.class);
169 mNotificationManager = context.getSystemService(NotificationManager.class);
170 mNotificationPolicy = mNotificationManager.getConsolidatedNotificationPolicy();
171 mAllowAlarms = (mNotificationPolicy.priorityCategories & NotificationManager.Policy
172 .PRIORITY_CATEGORY_ALARMS) != 0;
173 mAllowMedia = (mNotificationPolicy.priorityCategories & NotificationManager.Policy
174 .PRIORITY_CATEGORY_MEDIA) != 0;
175 mAllowRinger = !ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(
176 mNotificationPolicy);
177 mStreamType = streamType;
178 mAffectedByRingerMode = mAudioManager.isStreamAffectedByRingerMode(mStreamType);
179 mNotificationOrRing = isNotificationOrRing(mStreamType);
180 if (mNotificationOrRing) {
181 mRingerMode = mAudioManager.getRingerModeInternal();
182 }
183 mNotifAliasRing = mContext.getResources().getBoolean(
184 com.android.internal.R.bool.config_alias_ring_notif_stream_types);
185 mZenMode = mNotificationManager.getZenMode();
186
187 if (hasAudioProductStrategies()) {
188 mVolumeGroupId = getVolumeGroupIdForLegacyStreamType(mStreamType);
189 mAttributes = getAudioAttributesForLegacyStreamType(
190 mStreamType);
191 }
192
193 mMaxStreamVolume = mAudioManager.getStreamMaxVolume(mStreamType);
194 mCallback = callback;
195 mOriginalStreamVolume = mAudioManager.getStreamVolume(mStreamType);
196 mLastAudibleStreamVolume = mAudioManager.getLastAudibleStreamVolume(mStreamType);
197 mMuted = mAudioManager.isStreamMute(mStreamType);
198 mPlaySample = playSample;
199 if (mCallback != null) {
200 mCallback.onMuted(mMuted, isZenMuted());
201 }
202 if (defaultUri == null) {
203 if (mStreamType == AudioManager.STREAM_RING) {
204 defaultUri = Settings.System.DEFAULT_RINGTONE_URI;
205 } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) {
206 defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI;
207 } else {
208 defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
209 }
210 }
211 mDefaultUri = defaultUri;
212 }
213
214 private boolean hasAudioProductStrategies() {
215 return AudioManager.getAudioProductStrategies().size() > 0;
216 }
217
218 private int getVolumeGroupIdForLegacyStreamType(int streamType) {
219 for (final AudioProductStrategy productStrategy :
220 AudioManager.getAudioProductStrategies()) {
221 int volumeGroupId = productStrategy.getVolumeGroupIdForLegacyStreamType(streamType);
222 if (volumeGroupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
223 return volumeGroupId;
224 }
225 }
226
227 return AudioManager.getAudioProductStrategies().stream()
228 .map(strategy -> strategy.getVolumeGroupIdForAudioAttributes(
229 AudioProductStrategy.getDefaultAttributes()))
230 .filter(volumeGroupId -> volumeGroupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP)
231 .findFirst()
232 .orElse(AudioVolumeGroup.DEFAULT_VOLUME_GROUP);
233 }
234
235 private @NonNull AudioAttributes getAudioAttributesForLegacyStreamType(int streamType) {
236 for (final AudioProductStrategy productStrategy :
237 AudioManager.getAudioProductStrategies()) {
238 AudioAttributes aa = productStrategy.getAudioAttributesForLegacyStreamType(streamType);
239 if (aa != null) {
240 return aa;
241 }
242 }
243 return new AudioAttributes.Builder()
244 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
245 .setUsage(AudioAttributes.USAGE_UNKNOWN).build();
246 }
247
248 private static boolean isNotificationOrRing(int stream) {
249 return stream == AudioManager.STREAM_RING || stream == AudioManager.STREAM_NOTIFICATION;
250 }
251
252 private static boolean isAlarmsStream(int stream) {
253 return stream == AudioManager.STREAM_ALARM;
254 }
255
256 private static boolean isMediaStream(int stream) {
257 return stream == AudioManager.STREAM_MUSIC;
258 }
259
260 public void setSeekBar(SeekBar seekBar) {
261 if (mSeekBar != null) {
262 mSeekBar.setOnSeekBarChangeListener(null);
263 }
264 mSeekBar = seekBar;
265 mSeekBar.setOnSeekBarChangeListener(null);
266 mSeekBar.setMax(mMaxStreamVolume);
267 updateSeekBar();
268 mSeekBar.setOnSeekBarChangeListener(this);
269 }
270
271 private boolean isZenMuted() {
272 return mNotificationOrRing && mZenMode == Global.ZEN_MODE_ALARMS
273 || mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS
274 || (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
275 && ((!mAllowAlarms && isAlarmsStream(mStreamType))
276 || (!mAllowMedia && isMediaStream(mStreamType))
277 || (!mAllowRinger && isNotificationOrRing(mStreamType))));
278 }
279
280 protected void updateSeekBar() {
281 final boolean zenMuted = isZenMuted();
282 mSeekBar.setEnabled(!zenMuted);
283 if (zenMuted) {
284 mSeekBar.setProgress(mLastAudibleStreamVolume, true);
285 } else if (mNotificationOrRing && mRingerMode == AudioManager.RINGER_MODE_VIBRATE) {
286 /**
287 * the first variable above is preserved and the conditions below are made explicit
288 * so that when user attempts to slide the notification seekbar out of vibrate the
289 * seekbar doesn't wrongly snap back to 0 when the streams aren't aliased
290 */
291 if (mNotifAliasRing || mStreamType == AudioManager.STREAM_RING
292 || (mStreamType == AudioManager.STREAM_NOTIFICATION && mMuted)) {
293 mSeekBar.setProgress(0, true);
294 }
295 } else if (mMuted) {
296 mSeekBar.setProgress(0, true);
297 } else {
298 mSeekBar.setProgress(mLastProgress > -1 ? mLastProgress : mOriginalStreamVolume, true);
299 }
300 }
301
302 @Override
303 public boolean handleMessage(Message msg) {
304 switch (msg.what) {
305 case MSG_SET_STREAM_VOLUME:
306 if (mMuted && mLastProgress > 0) {
307 mAudioManager.adjustStreamVolume(mStreamType, AudioManager.ADJUST_UNMUTE, 0);
308 } else if (!mMuted && mLastProgress == 0) {
309 mAudioManager.adjustStreamVolume(mStreamType, AudioManager.ADJUST_MUTE, 0);
310 }
311 mAudioManager.setStreamVolume(mStreamType, mLastProgress,
312 AudioManager.FLAG_SHOW_UI_WARNINGS);
313 break;
314 case MSG_START_SAMPLE:
315 if (mPlaySample) {
316 onStartSample();
317 }
318 break;
319 case MSG_STOP_SAMPLE:
320 if (mPlaySample) {
321 onStopSample();
322 }
323 break;
324 case MSG_INIT_SAMPLE:
325 if (mPlaySample) {
326 onInitSample();
327 }
328 break;
329 default:
330 Log.e(TAG, "invalid SeekBarVolumizer message: "+msg.what);
331 }
332 return true;
333 }
334
335 private void onInitSample() {
336 synchronized (this) {
337 mRingtone = RingtoneManager.getRingtone(mContext, mDefaultUri);
338 if (mRingtone != null) {
339 mRingtone.setStreamType(mStreamType);
340 }
341 }
342 }
343
344 private void postStartSample() {
345 if (mHandler == null) return;
346 mHandler.removeMessages(MSG_START_SAMPLE);
347 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_SAMPLE),
348 isSamplePlaying() ? CHECK_RINGTONE_PLAYBACK_DELAY_MS
349 : isDelay() ? START_SAMPLE_DELAY_MS : 0);
350 }
351
352 // After stop volume it needs to add a small delay when playing volume or set stream.
353 // It is because the call volume is from the earpiece and the alarm/ring/media
354 // is from the speaker. If play the alarm volume or set alarm stream right after stop
355 // call volume, the alarm volume on earpiece is returned then cause the volume value incorrect.
356 // It needs a small delay after stop call volume to get alarm volume on speaker.
357 // e.g. : If the ring volume has adjusted right after call volume stopped in 2 second
358 // then delay 0.5 second to set stream or play volume ringtone.
359 private boolean isDelay() {
360 final long durationTime = java.lang.System.currentTimeMillis() - sStopVolumeTime;
361 return durationTime >= 0 && durationTime < DURATION_TO_START_DELAYING;
362 }
363
364 private void setStopVolumeTime() {
365 // set the time of stop volume
366 if ((mStreamType == AudioManager.STREAM_VOICE_CALL
367 || mStreamType == AudioManager.STREAM_RING
368 || (!mNotifAliasRing && mStreamType == AudioManager.STREAM_NOTIFICATION)
369 || mStreamType == AudioManager.STREAM_ALARM)) {
370 sStopVolumeTime = java.lang.System.currentTimeMillis();
371 }
372 }
373
374 private void onStartSample() {
375 if (!isSamplePlaying()) {
376 if (mCallback != null) {
377 mCallback.onSampleStarting(this);
378 }
379
380 synchronized (this) {
381 if (mRingtone != null) {
382 try {
383 mRingtone.setAudioAttributes(new AudioAttributes.Builder(mRingtone
384 .getAudioAttributes())
385 .setFlags(AudioAttributes.FLAG_BYPASS_MUTE)
386 .build());
387 mRingtone.play();
388 } catch (Throwable e) {
389 Log.w(TAG, "Error playing ringtone, stream " + mStreamType, e);
390 }
391 }
392 }
393 }
394 }
395
396 private void postStopSample() {
397 if (mHandler == null) return;
398 setStopVolumeTime();
399 // remove pending delayed start messages
400 mHandler.removeMessages(MSG_START_SAMPLE);
401 mHandler.removeMessages(MSG_STOP_SAMPLE);
402 mHandler.sendMessage(mHandler.obtainMessage(MSG_STOP_SAMPLE));
403 }
404
405 private void onStopSample() {
406 synchronized (this) {
407 if (mRingtone != null) {
408 mRingtone.stop();
409 }
410 }
411 }
412
413 @UnsupportedAppUsage
414 public void stop() {
415 if (mHandler == null) return; // already stopped
416 postStopSample();
417 mContext.getContentResolver().unregisterContentObserver(mVolumeObserver);
418 mReceiver.setListening(false);
419 if (hasAudioProductStrategies()) {
420 unregisterVolumeGroupCb();
421 }
422 mSeekBar.setOnSeekBarChangeListener(null);
423 mHandler.getLooper().quitSafely();
424 mHandler = null;
425 mVolumeObserver = null;
426 }
427
428 public void start() {
429 if (mHandler != null) return; // already started
430 HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler");
431 thread.start();
432 mHandler = new Handler(thread.getLooper(), this);
433 mHandler.sendEmptyMessage(MSG_INIT_SAMPLE);
434 mVolumeObserver = new Observer(mHandler);
435 mContext.getContentResolver().registerContentObserver(
436 System.getUriFor(System.VOLUME_SETTINGS_INT[mStreamType]),
437 false, mVolumeObserver);
438 mReceiver.setListening(true);
439 if (hasAudioProductStrategies()) {
440 registerVolumeGroupCb();
441 }
442 }
443
444 public void revertVolume() {
445 mAudioManager.setStreamVolume(mStreamType, mOriginalStreamVolume, 0);
446 }
447
448 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
449 if (fromTouch) {
450 postSetVolume(progress);
451 }
452 if (mCallback != null) {
453 mCallback.onProgressChanged(seekBar, progress, fromTouch);
454 }
455 }
456
457 private void postSetVolume(int progress) {
458 if (mHandler == null) return;
459 // Do the volume changing separately to give responsive UI
460 mLastProgress = progress;
461 mHandler.removeMessages(MSG_SET_STREAM_VOLUME);
462 mHandler.removeMessages(MSG_START_SAMPLE);
463 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_STREAM_VOLUME),
464 isDelay() ? SET_STREAM_VOLUME_DELAY_MS : 0);
465 }
466
467 public void onStartTrackingTouch(SeekBar seekBar) {
468 if (mCallback != null) {
469 mCallback.onStartTrackingTouch(this);
470 }
471 }
472
473 public void onStopTrackingTouch(SeekBar seekBar) {
474 postStartSample();
475 if (mCallback != null) {
476 mCallback.onStopTrackingTouch(this);
477 }
478 }
479
480 public boolean isSamplePlaying() {
481 synchronized (this) {
482 return mRingtone != null && mRingtone.isPlaying();
483 }
484 }
485
486 public void startSample() {
487 postStartSample();
488 }
489
490 public void stopSample() {
491 postStopSample();
492 }
493
494 public SeekBar getSeekBar() {
495 return mSeekBar;
496 }
497
498 public void changeVolumeBy(int amount) {
499 mSeekBar.incrementProgressBy(amount);
500 postSetVolume(mSeekBar.getProgress());
501 postStartSample();
502 mVolumeBeforeMute = -1;
503 }
504
505 public void muteVolume() {
506 if (mVolumeBeforeMute != -1) {
507 mSeekBar.setProgress(mVolumeBeforeMute, true);
508 postSetVolume(mVolumeBeforeMute);
509 postStartSample();
510 mVolumeBeforeMute = -1;
511 } else {
512 mVolumeBeforeMute = mSeekBar.getProgress();
513 mSeekBar.setProgress(0, true);
514 postStopSample();
515 postSetVolume(0);
516 }
517 }
518
519 public void onSaveInstanceState(VolumeStore volumeStore) {
520 if (mLastProgress >= 0) {
521 volumeStore.volume = mLastProgress;
522 volumeStore.originalVolume = mOriginalStreamVolume;
523 }
524 }
525
526 public void onRestoreInstanceState(VolumeStore volumeStore) {
527 if (volumeStore.volume != -1) {
528 mOriginalStreamVolume = volumeStore.originalVolume;
529 mLastProgress = volumeStore.volume;
530 postSetVolume(mLastProgress);
531 }
532 }
533
534 private final class H extends Handler {
535 private static final int UPDATE_SLIDER = 1;
536
537 @Override
538 public void handleMessage(Message msg) {
539 if (msg.what == UPDATE_SLIDER) {
540 if (mSeekBar != null) {
541 mLastProgress = msg.arg1;
542 mLastAudibleStreamVolume = msg.arg2;
543 final boolean muted = ((Boolean)msg.obj).booleanValue();
544 if (muted != mMuted) {
545 mMuted = muted;
546 if (mCallback != null) {
547 mCallback.onMuted(mMuted, isZenMuted());
548 }
549 }
550 updateSeekBar();
551 }
552 }
553 }
554
555 public void postUpdateSlider(int volume, int lastAudibleVolume, boolean mute) {
556 obtainMessage(UPDATE_SLIDER, volume, lastAudibleVolume, Boolean.valueOf(mute))
557 .sendToTarget();
558 }
559 }
560
561 private void updateSlider() {
562 if (mSeekBar != null && mAudioManager != null) {
563 final int volume = mAudioManager.getStreamVolume(mStreamType);
564 final int lastAudibleVolume = mAudioManager.getLastAudibleStreamVolume(mStreamType);
565 final boolean mute = mAudioManager.isStreamMute(mStreamType);
566 mUiHandler.postUpdateSlider(volume, lastAudibleVolume, mute);
567 }
568 }
569
570 private final class Observer extends ContentObserver {
571 public Observer(Handler handler) {
572 super(handler);
573 }
574
575 @Override
576 public void onChange(boolean selfChange) {
577 super.onChange(selfChange);
578 updateSlider();
579 }
580 }
581
582 private final class Receiver extends BroadcastReceiver {
583 private boolean mListening;
584
585 public void setListening(boolean listening) {
586 if (mListening == listening) return;
587 mListening = listening;
588 if (listening) {
589 final IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
590 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
591 filter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
592 filter.addAction(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED);
593 filter.addAction(AudioManager.STREAM_DEVICES_CHANGED_ACTION);
594 mContext.registerReceiver(this, filter);
595 } else {
596 mContext.unregisterReceiver(this);
597 }
598 }
599
600 @Override
601 public void onReceive(Context context, Intent intent) {
602 final String action = intent.getAction();
603 if (AudioManager.VOLUME_CHANGED_ACTION.equals(action)) {
604 int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
605 int streamValue = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1);
606 if (hasAudioProductStrategies() && !isDelay()) {
607 updateVolumeSlider(streamType, streamValue);
608 }
609 } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) {
610 if (mNotificationOrRing) {
611 mRingerMode = mAudioManager.getRingerModeInternal();
612 }
613 if (mAffectedByRingerMode) {
614 updateSlider();
615 }
616 } else if (AudioManager.STREAM_DEVICES_CHANGED_ACTION.equals(action)) {
617 int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
618 if (hasAudioProductStrategies() && !isDelay()) {
619 int streamVolume = mAudioManager.getStreamVolume(streamType);
620 updateVolumeSlider(streamType, streamVolume);
621 } else {
622 int volumeGroup = getVolumeGroupIdForLegacyStreamType(streamType);
623 if (volumeGroup != AudioVolumeGroup.DEFAULT_VOLUME_GROUP
624 && volumeGroup == mVolumeGroupId) {
625 int streamVolume = mAudioManager.getStreamVolume(streamType);
626 if (!isDelay()) {
627 updateVolumeSlider(streamType, streamVolume);
628 }
629 }
630 }
631 } else if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED.equals(action)) {
632 mZenMode = mNotificationManager.getZenMode();
633 updateSlider();
634 } else if (NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED.equals(action)) {
635 mNotificationPolicy = mNotificationManager.getConsolidatedNotificationPolicy();
636 mAllowAlarms = (mNotificationPolicy.priorityCategories & NotificationManager.Policy
637 .PRIORITY_CATEGORY_ALARMS) != 0;
638 mAllowMedia = (mNotificationPolicy.priorityCategories
639 & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) != 0;
640 mAllowRinger = !ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(
641 mNotificationPolicy);
642 updateSlider();
643 }
644 }
645
646 private void updateVolumeSlider(int streamType, int streamValue) {
647 final boolean streamMatch = mNotifAliasRing && mNotificationOrRing
648 ? isNotificationOrRing(streamType) : streamType == mStreamType;
649 if (mSeekBar != null && streamMatch && streamValue != -1) {
650 final boolean muted = mAudioManager.isStreamMute(mStreamType)
651 || streamValue == 0;
652 mUiHandler.postUpdateSlider(streamValue, mLastAudibleStreamVolume, muted);
653 }
654 }
655 }
656
657 private void registerVolumeGroupCb() {
658 if (mVolumeGroupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
659 mAudioManager.registerVolumeGroupCallback(Runnable::run, mVolumeGroupCallback);
660 updateSlider();
661 }
662 }
663
664 private void unregisterVolumeGroupCb() {
665 if (mVolumeGroupId != AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
666 mAudioManager.unregisterVolumeGroupCallback(mVolumeGroupCallback);
667 }
668 }
669
670 private class VolumeHandler extends Handler {
671 @Override
672 public void handleMessage(Message msg) {
673 SomeArgs args = (SomeArgs) msg.obj;
674 switch (msg.what) {
675 case MSG_GROUP_VOLUME_CHANGED:
676 int group = (int) args.arg1;
677 if (mVolumeGroupId != group
678 || mVolumeGroupId == AudioVolumeGroup.DEFAULT_VOLUME_GROUP) {
679 return;
680 }
681 updateSlider();
682 break;
683 }
684 }
685 }
686 }