John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 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 | |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 19 | import android.app.NotificationManager; |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 21 | import android.content.Context; |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 24 | import android.database.ContentObserver; |
John Spurlock | bbfd31a | 2015-02-18 11:58:14 -0500 | [diff] [blame] | 25 | import android.media.AudioAttributes; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 26 | import android.media.AudioManager; |
| 27 | import android.media.Ringtone; |
| 28 | import android.media.RingtoneManager; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Handler; |
| 31 | import android.os.HandlerThread; |
| 32 | import android.os.Message; |
| 33 | import android.preference.VolumePreference.VolumeStore; |
| 34 | import android.provider.Settings; |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 35 | import android.provider.Settings.Global; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 36 | import android.provider.Settings.System; |
| 37 | import android.util.Log; |
| 38 | import android.widget.SeekBar; |
| 39 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 40 | |
| 41 | /** |
| 42 | * Turns a {@link SeekBar} into a volume control. |
| 43 | * @hide |
| 44 | */ |
| 45 | public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callback { |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 46 | private static final String TAG = "SeekBarVolumizer"; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 47 | |
| 48 | public interface Callback { |
| 49 | void onSampleStarting(SeekBarVolumizer sbv); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 50 | void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch); |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 51 | void onMuted(boolean muted, boolean zenMuted); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 52 | } |
| 53 | |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 54 | private final Context mContext; |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 55 | private final H mUiHandler = new H(); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 56 | private final Callback mCallback; |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 57 | private final Uri mDefaultUri; |
| 58 | private final AudioManager mAudioManager; |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 59 | private final NotificationManager mNotificationManager; |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 60 | private final int mStreamType; |
| 61 | private final int mMaxStreamVolume; |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 62 | private boolean mAffectedByRingerMode; |
| 63 | private boolean mNotificationOrRing; |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 64 | private final Receiver mReceiver = new Receiver(); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 65 | |
John Spurlock | 0e588ea | 2014-10-08 12:33:22 -0400 | [diff] [blame] | 66 | private Handler mHandler; |
| 67 | private Observer mVolumeObserver; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 68 | private int mOriginalStreamVolume; |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 69 | private int mLastAudibleStreamVolume; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 70 | private Ringtone mRingtone; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 71 | private int mLastProgress = -1; |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 72 | private boolean mMuted; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 73 | private SeekBar mSeekBar; |
| 74 | private int mVolumeBeforeMute = -1; |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 75 | private int mRingerMode; |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 76 | private int mZenMode; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 77 | |
| 78 | private static final int MSG_SET_STREAM_VOLUME = 0; |
| 79 | private static final int MSG_START_SAMPLE = 1; |
| 80 | private static final int MSG_STOP_SAMPLE = 2; |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 81 | private static final int MSG_INIT_SAMPLE = 3; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 82 | private static final int CHECK_RINGTONE_PLAYBACK_DELAY_MS = 1000; |
| 83 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 84 | public SeekBarVolumizer(Context context, int streamType, Uri defaultUri, Callback callback) { |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 85 | mContext = context; |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 86 | mAudioManager = context.getSystemService(AudioManager.class); |
| 87 | mNotificationManager = context.getSystemService(NotificationManager.class); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 88 | mStreamType = streamType; |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 89 | mAffectedByRingerMode = mAudioManager.isStreamAffectedByRingerMode(mStreamType); |
| 90 | mNotificationOrRing = isNotificationOrRing(mStreamType); |
| 91 | if (mNotificationOrRing) { |
| 92 | mRingerMode = mAudioManager.getRingerModeInternal(); |
| 93 | } |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 94 | mZenMode = mNotificationManager.getZenMode(); |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 95 | mMaxStreamVolume = mAudioManager.getStreamMaxVolume(mStreamType); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 96 | mCallback = callback; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 97 | mOriginalStreamVolume = mAudioManager.getStreamVolume(mStreamType); |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 98 | mLastAudibleStreamVolume = mAudioManager.getLastAudibleStreamVolume(mStreamType); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 99 | mMuted = mAudioManager.isStreamMute(mStreamType); |
| 100 | if (mCallback != null) { |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 101 | mCallback.onMuted(mMuted, isZenMuted()); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 102 | } |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 103 | if (defaultUri == null) { |
| 104 | if (mStreamType == AudioManager.STREAM_RING) { |
| 105 | defaultUri = Settings.System.DEFAULT_RINGTONE_URI; |
| 106 | } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) { |
| 107 | defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 108 | } else { |
| 109 | defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI; |
| 110 | } |
| 111 | } |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 112 | mDefaultUri = defaultUri; |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 113 | } |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 114 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 115 | private static boolean isNotificationOrRing(int stream) { |
| 116 | return stream == AudioManager.STREAM_RING || stream == AudioManager.STREAM_NOTIFICATION; |
| 117 | } |
| 118 | |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 119 | public void setSeekBar(SeekBar seekBar) { |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 120 | if (mSeekBar != null) { |
| 121 | mSeekBar.setOnSeekBarChangeListener(null); |
| 122 | } |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 123 | mSeekBar = seekBar; |
| 124 | mSeekBar.setOnSeekBarChangeListener(null); |
| 125 | mSeekBar.setMax(mMaxStreamVolume); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 126 | updateSeekBar(); |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 127 | mSeekBar.setOnSeekBarChangeListener(this); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 128 | } |
| 129 | |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 130 | private boolean isZenMuted() { |
| 131 | return mNotificationOrRing && mZenMode == Global.ZEN_MODE_ALARMS |
| 132 | || mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; |
| 133 | } |
| 134 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 135 | protected void updateSeekBar() { |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 136 | final boolean zenMuted = isZenMuted(); |
| 137 | mSeekBar.setEnabled(!zenMuted); |
| 138 | if (zenMuted) { |
| 139 | mSeekBar.setProgress(mLastAudibleStreamVolume); |
| 140 | } else if (mNotificationOrRing && mRingerMode == AudioManager.RINGER_MODE_VIBRATE) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 141 | mSeekBar.setProgress(0); |
| 142 | } else if (mMuted) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 143 | mSeekBar.setProgress(0); |
| 144 | } else { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 145 | mSeekBar.setProgress(mLastProgress > -1 ? mLastProgress : mOriginalStreamVolume); |
| 146 | } |
| 147 | } |
| 148 | |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 149 | @Override |
| 150 | public boolean handleMessage(Message msg) { |
| 151 | switch (msg.what) { |
| 152 | case MSG_SET_STREAM_VOLUME: |
John Spurlock | 721d457 | 2015-04-16 11:01:15 -0400 | [diff] [blame] | 153 | if (mMuted && mLastProgress > 0) { |
| 154 | mAudioManager.adjustStreamVolume(mStreamType, AudioManager.ADJUST_UNMUTE, 0); |
| 155 | } else if (!mMuted && mLastProgress == 0) { |
| 156 | mAudioManager.adjustStreamVolume(mStreamType, AudioManager.ADJUST_MUTE, 0); |
| 157 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 158 | mAudioManager.setStreamVolume(mStreamType, mLastProgress, |
| 159 | AudioManager.FLAG_SHOW_UI_WARNINGS); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 160 | break; |
| 161 | case MSG_START_SAMPLE: |
| 162 | onStartSample(); |
| 163 | break; |
| 164 | case MSG_STOP_SAMPLE: |
| 165 | onStopSample(); |
| 166 | break; |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 167 | case MSG_INIT_SAMPLE: |
| 168 | onInitSample(); |
| 169 | break; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 170 | default: |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 171 | Log.e(TAG, "invalid SeekBarVolumizer message: "+msg.what); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 176 | private void onInitSample() { |
| 177 | mRingtone = RingtoneManager.getRingtone(mContext, mDefaultUri); |
| 178 | if (mRingtone != null) { |
| 179 | mRingtone.setStreamType(mStreamType); |
| 180 | } |
| 181 | } |
| 182 | |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 183 | private void postStartSample() { |
John Spurlock | 735f9eb | 2014-10-21 14:23:24 -0400 | [diff] [blame] | 184 | if (mHandler == null) return; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 185 | mHandler.removeMessages(MSG_START_SAMPLE); |
| 186 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_SAMPLE), |
| 187 | isSamplePlaying() ? CHECK_RINGTONE_PLAYBACK_DELAY_MS : 0); |
| 188 | } |
| 189 | |
| 190 | private void onStartSample() { |
| 191 | if (!isSamplePlaying()) { |
| 192 | if (mCallback != null) { |
| 193 | mCallback.onSampleStarting(this); |
| 194 | } |
| 195 | if (mRingtone != null) { |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 196 | try { |
John Spurlock | bbfd31a | 2015-02-18 11:58:14 -0500 | [diff] [blame] | 197 | mRingtone.setAudioAttributes(new AudioAttributes.Builder(mRingtone |
| 198 | .getAudioAttributes()) |
| 199 | .setFlags(AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY | |
| 200 | AudioAttributes.FLAG_BYPASS_MUTE) |
| 201 | .build()); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 202 | mRingtone.play(); |
| 203 | } catch (Throwable e) { |
| 204 | Log.w(TAG, "Error playing ringtone, stream " + mStreamType, e); |
| 205 | } |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
John Spurlock | 735f9eb | 2014-10-21 14:23:24 -0400 | [diff] [blame] | 210 | private void postStopSample() { |
| 211 | if (mHandler == null) return; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 212 | // remove pending delayed start messages |
| 213 | mHandler.removeMessages(MSG_START_SAMPLE); |
| 214 | mHandler.removeMessages(MSG_STOP_SAMPLE); |
| 215 | mHandler.sendMessage(mHandler.obtainMessage(MSG_STOP_SAMPLE)); |
| 216 | } |
| 217 | |
| 218 | private void onStopSample() { |
| 219 | if (mRingtone != null) { |
| 220 | mRingtone.stop(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | public void stop() { |
John Spurlock | 0e588ea | 2014-10-08 12:33:22 -0400 | [diff] [blame] | 225 | if (mHandler == null) return; // already stopped |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 226 | postStopSample(); |
| 227 | mContext.getContentResolver().unregisterContentObserver(mVolumeObserver); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 228 | mReceiver.setListening(false); |
John Spurlock | 0e588ea | 2014-10-08 12:33:22 -0400 | [diff] [blame] | 229 | mSeekBar.setOnSeekBarChangeListener(null); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 230 | mHandler.getLooper().quitSafely(); |
John Spurlock | 0e588ea | 2014-10-08 12:33:22 -0400 | [diff] [blame] | 231 | mHandler = null; |
| 232 | mVolumeObserver = null; |
| 233 | } |
| 234 | |
| 235 | public void start() { |
| 236 | if (mHandler != null) return; // already started |
| 237 | HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler"); |
| 238 | thread.start(); |
| 239 | mHandler = new Handler(thread.getLooper(), this); |
| 240 | mHandler.sendEmptyMessage(MSG_INIT_SAMPLE); |
| 241 | mVolumeObserver = new Observer(mHandler); |
| 242 | mContext.getContentResolver().registerContentObserver( |
| 243 | System.getUriFor(System.VOLUME_SETTINGS[mStreamType]), |
| 244 | false, mVolumeObserver); |
| 245 | mReceiver.setListening(true); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | public void revertVolume() { |
| 249 | mAudioManager.setStreamVolume(mStreamType, mOriginalStreamVolume, 0); |
| 250 | } |
| 251 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 252 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { |
| 253 | if (fromTouch) { |
| 254 | postSetVolume(progress); |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 255 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 256 | if (mCallback != null) { |
| 257 | mCallback.onProgressChanged(seekBar, progress, fromTouch); |
| 258 | } |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 259 | } |
| 260 | |
John Spurlock | 735f9eb | 2014-10-21 14:23:24 -0400 | [diff] [blame] | 261 | private void postSetVolume(int progress) { |
| 262 | if (mHandler == null) return; |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 263 | // Do the volume changing separately to give responsive UI |
| 264 | mLastProgress = progress; |
| 265 | mHandler.removeMessages(MSG_SET_STREAM_VOLUME); |
| 266 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_STREAM_VOLUME)); |
| 267 | } |
| 268 | |
| 269 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 270 | } |
| 271 | |
| 272 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 273 | postStartSample(); |
| 274 | } |
| 275 | |
| 276 | public boolean isSamplePlaying() { |
| 277 | return mRingtone != null && mRingtone.isPlaying(); |
| 278 | } |
| 279 | |
| 280 | public void startSample() { |
| 281 | postStartSample(); |
| 282 | } |
| 283 | |
| 284 | public void stopSample() { |
| 285 | postStopSample(); |
| 286 | } |
| 287 | |
| 288 | public SeekBar getSeekBar() { |
| 289 | return mSeekBar; |
| 290 | } |
| 291 | |
| 292 | public void changeVolumeBy(int amount) { |
| 293 | mSeekBar.incrementProgressBy(amount); |
| 294 | postSetVolume(mSeekBar.getProgress()); |
| 295 | postStartSample(); |
| 296 | mVolumeBeforeMute = -1; |
| 297 | } |
| 298 | |
| 299 | public void muteVolume() { |
| 300 | if (mVolumeBeforeMute != -1) { |
| 301 | mSeekBar.setProgress(mVolumeBeforeMute); |
| 302 | postSetVolume(mVolumeBeforeMute); |
| 303 | postStartSample(); |
| 304 | mVolumeBeforeMute = -1; |
| 305 | } else { |
| 306 | mVolumeBeforeMute = mSeekBar.getProgress(); |
| 307 | mSeekBar.setProgress(0); |
| 308 | postStopSample(); |
| 309 | postSetVolume(0); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | public void onSaveInstanceState(VolumeStore volumeStore) { |
| 314 | if (mLastProgress >= 0) { |
| 315 | volumeStore.volume = mLastProgress; |
| 316 | volumeStore.originalVolume = mOriginalStreamVolume; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public void onRestoreInstanceState(VolumeStore volumeStore) { |
| 321 | if (volumeStore.volume != -1) { |
| 322 | mOriginalStreamVolume = volumeStore.originalVolume; |
| 323 | mLastProgress = volumeStore.volume; |
| 324 | postSetVolume(mLastProgress); |
| 325 | } |
| 326 | } |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 327 | |
| 328 | private final class H extends Handler { |
| 329 | private static final int UPDATE_SLIDER = 1; |
| 330 | |
| 331 | @Override |
| 332 | public void handleMessage(Message msg) { |
| 333 | if (msg.what == UPDATE_SLIDER) { |
| 334 | if (mSeekBar != null) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 335 | mLastProgress = msg.arg1; |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 336 | mLastAudibleStreamVolume = Math.abs(msg.arg2); |
| 337 | final boolean muted = msg.arg2 < 0; |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 338 | if (muted != mMuted) { |
| 339 | mMuted = muted; |
| 340 | if (mCallback != null) { |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 341 | mCallback.onMuted(mMuted, isZenMuted()); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | updateSeekBar(); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 349 | public void postUpdateSlider(int volume, int lastAudibleVolume, boolean mute) { |
| 350 | final int arg2 = lastAudibleVolume * (mute ? -1 : 1); |
| 351 | obtainMessage(UPDATE_SLIDER, volume, arg2).sendToTarget(); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | private void updateSlider() { |
| 356 | if (mSeekBar != null && mAudioManager != null) { |
| 357 | final int volume = mAudioManager.getStreamVolume(mStreamType); |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 358 | final int lastAudibleVolume = mAudioManager.getLastAudibleStreamVolume(mStreamType); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 359 | final boolean mute = mAudioManager.isStreamMute(mStreamType); |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 360 | mUiHandler.postUpdateSlider(volume, lastAudibleVolume, mute); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | private final class Observer extends ContentObserver { |
| 365 | public Observer(Handler handler) { |
| 366 | super(handler); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void onChange(boolean selfChange) { |
| 371 | super.onChange(selfChange); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 372 | updateSlider(); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | private final class Receiver extends BroadcastReceiver { |
| 377 | private boolean mListening; |
| 378 | |
| 379 | public void setListening(boolean listening) { |
| 380 | if (mListening == listening) return; |
| 381 | mListening = listening; |
| 382 | if (listening) { |
| 383 | final IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 384 | filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 385 | filter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Julia Reynolds | 3a10b10 | 2015-10-14 16:13:28 -0400 | [diff] [blame] | 386 | filter.addAction(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 387 | mContext.registerReceiver(this, filter); |
| 388 | } else { |
| 389 | mContext.unregisterReceiver(this); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void onReceive(Context context, Intent intent) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 395 | final String action = intent.getAction(); |
| 396 | if (AudioManager.VOLUME_CHANGED_ACTION.equals(action)) { |
| 397 | int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 398 | int streamValue = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1); |
Julia Reynolds | 3a10b10 | 2015-10-14 16:13:28 -0400 | [diff] [blame] | 399 | updateVolumeSlider(streamType, streamValue); |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 400 | } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { |
| 401 | if (mNotificationOrRing) { |
| 402 | mRingerMode = mAudioManager.getRingerModeInternal(); |
| 403 | } |
| 404 | if (mAffectedByRingerMode) { |
| 405 | updateSlider(); |
| 406 | } |
Julia Reynolds | 3a10b10 | 2015-10-14 16:13:28 -0400 | [diff] [blame] | 407 | } else if (AudioManager.STREAM_DEVICES_CHANGED_ACTION.equals(action)) { |
| 408 | int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 409 | int streamVolume = mAudioManager.getStreamVolume(streamType); |
| 410 | updateVolumeSlider(streamType, streamVolume); |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 411 | } else if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED.equals(action)) { |
| 412 | mZenMode = mNotificationManager.getZenMode(); |
| 413 | updateSlider(); |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 414 | } |
| 415 | } |
Julia Reynolds | 3a10b10 | 2015-10-14 16:13:28 -0400 | [diff] [blame] | 416 | |
| 417 | private void updateVolumeSlider(int streamType, int streamValue) { |
| 418 | final boolean streamMatch = mNotificationOrRing ? isNotificationOrRing(streamType) |
| 419 | : (streamType == mStreamType); |
| 420 | if (mSeekBar != null && streamMatch && streamValue != -1) { |
| 421 | final boolean muted = mAudioManager.isStreamMute(mStreamType) |
| 422 | || streamValue == 0; |
| 423 | mUiHandler.postUpdateSlider(streamValue, mLastAudibleStreamVolume, muted); |
| 424 | } |
| 425 | } |
John Spurlock | 0dbf1a6 | 2014-06-01 13:01:23 -0400 | [diff] [blame] | 426 | } |
| 427 | } |