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