The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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.view; |
| 18 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 21 | import android.app.AlertDialog; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 22 | import android.app.Dialog; |
| 23 | import android.content.DialogInterface.OnDismissListener; |
| 24 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 26 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 28 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.res.Resources; |
| 30 | import android.media.AudioManager; |
| 31 | import android.media.AudioService; |
| 32 | import android.media.AudioSystem; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 33 | import android.media.RingtoneManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.media.ToneGenerator; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 35 | import android.media.VolumeController; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 36 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.Message; |
| 39 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Amith Yamasani | 284e630 | 2011-09-16 18:24:47 -0700 | [diff] [blame] | 41 | import android.view.WindowManager.LayoutParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.widget.ImageView; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 43 | import android.widget.SeekBar; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 44 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 45 | |
| 46 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Handle the volume up and down keys. |
| 50 | * |
| 51 | * This code really should be moved elsewhere. |
| 52 | * |
Dianne Hackborn | e8ecde1 | 2011-08-03 18:55:19 -0700 | [diff] [blame] | 53 | * Seriously, it really really should be moved elsewhere. This is used by |
| 54 | * android.media.AudioService, which actually runs in the system process, to |
| 55 | * show the volume dialog when the user changes the volume. What a mess. |
| 56 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | * @hide |
| 58 | */ |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 59 | public class VolumePanel extends Handler implements VolumeController { |
| 60 | private static final String TAG = VolumePanel.class.getSimpleName(); |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 61 | private static boolean LOGD = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * The delay before playing a sound. This small period exists so the user |
| 65 | * can press another key (non-volume keys, too) to have it NOT be audible. |
| 66 | * <p> |
| 67 | * PhoneWindow will implement this part. |
| 68 | */ |
| 69 | public static final int PLAY_SOUND_DELAY = 300; |
| 70 | |
| 71 | /** |
| 72 | * The delay before vibrating. This small period exists so if the user is |
| 73 | * moving to silent mode, it will not emit a short vibrate (it normally |
| 74 | * would since vibrate is between normal mode and silent mode using hardware |
| 75 | * keys). |
| 76 | */ |
| 77 | public static final int VIBRATE_DELAY = 300; |
| 78 | |
| 79 | private static final int VIBRATE_DURATION = 300; |
| 80 | private static final int BEEP_DURATION = 150; |
| 81 | private static final int MAX_VOLUME = 100; |
| 82 | private static final int FREE_DELAY = 10000; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 83 | private static final int TIMEOUT_DELAY = 3000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | private static final int MSG_VOLUME_CHANGED = 0; |
| 86 | private static final int MSG_FREE_RESOURCES = 1; |
| 87 | private static final int MSG_PLAY_SOUND = 2; |
| 88 | private static final int MSG_STOP_SOUNDS = 3; |
| 89 | private static final int MSG_VIBRATE = 4; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 90 | private static final int MSG_TIMEOUT = 5; |
| 91 | private static final int MSG_RINGER_MODE_CHANGED = 6; |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 92 | private static final int MSG_MUTE_CHANGED = 7; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 93 | private static final int MSG_REMOTE_VOLUME_CHANGED = 8; |
| 94 | private static final int MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN = 9; |
| 95 | private static final int MSG_SLIDER_VISIBILITY_CHANGED = 10; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 96 | private static final int MSG_DISPLAY_SAFE_VOLUME_WARNING = 11; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 98 | // Pseudo stream type for master volume |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 99 | private static final int STREAM_MASTER = -100; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 100 | // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | protected Context mContext; |
| 103 | private AudioManager mAudioManager; |
| 104 | protected AudioService mAudioService; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 105 | private boolean mRingIsSilent; |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 106 | private boolean mShowCombinedVolumes; |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 107 | private boolean mVoiceCapable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Christopher Tate | c4b78d2 | 2012-05-22 13:57:58 -0700 | [diff] [blame] | 109 | // True if we want to play tones on the system stream when the master stream is specified. |
| 110 | private final boolean mPlayMasterStreamTones; |
| 111 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 112 | /** Dialog containing all the sliders */ |
| 113 | private final Dialog mDialog; |
| 114 | /** Dialog's content view */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | private final View mView; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 116 | |
Amith Yamasani | baf6dbf | 2011-08-18 17:40:29 -0700 | [diff] [blame] | 117 | /** The visible portion of the volume overlay */ |
| 118 | private final ViewGroup mPanel; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 119 | /** Contains the sliders and their touchable icons */ |
| 120 | private final ViewGroup mSliderGroup; |
| 121 | /** The button that expands the dialog to show all sliders */ |
| 122 | private final View mMoreButton; |
| 123 | /** Dummy divider icon that needs to vanish with the more button */ |
| 124 | private final View mDivider; |
| 125 | |
| 126 | /** Currently active stream that shows up at the top of the list of sliders */ |
| 127 | private int mActiveStreamType = -1; |
| 128 | /** All the slider controls mapped by stream type */ |
| 129 | private HashMap<Integer,StreamControl> mStreamControls; |
| 130 | |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 131 | private enum StreamResources { |
| 132 | BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO, |
| 133 | R.string.volume_icon_description_bluetooth, |
| 134 | R.drawable.ic_audio_bt, |
| 135 | R.drawable.ic_audio_bt, |
| 136 | false), |
| 137 | RingerStream(AudioManager.STREAM_RING, |
| 138 | R.string.volume_icon_description_ringer, |
| 139 | R.drawable.ic_audio_ring_notif, |
| 140 | R.drawable.ic_audio_ring_notif_mute, |
| 141 | false), |
| 142 | VoiceStream(AudioManager.STREAM_VOICE_CALL, |
| 143 | R.string.volume_icon_description_incall, |
| 144 | R.drawable.ic_audio_phone, |
| 145 | R.drawable.ic_audio_phone, |
| 146 | false), |
Amith Yamasani | 92e1b2d | 2011-10-14 17:24:47 -0700 | [diff] [blame] | 147 | AlarmStream(AudioManager.STREAM_ALARM, |
| 148 | R.string.volume_alarm, |
| 149 | R.drawable.ic_audio_alarm, |
| 150 | R.drawable.ic_audio_alarm_mute, |
| 151 | false), |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 152 | MediaStream(AudioManager.STREAM_MUSIC, |
| 153 | R.string.volume_icon_description_media, |
| 154 | R.drawable.ic_audio_vol, |
| 155 | R.drawable.ic_audio_vol_mute, |
| 156 | true), |
| 157 | NotificationStream(AudioManager.STREAM_NOTIFICATION, |
| 158 | R.string.volume_icon_description_notification, |
| 159 | R.drawable.ic_audio_notification, |
| 160 | R.drawable.ic_audio_notification_mute, |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 161 | true), |
| 162 | // for now, use media resources for master volume |
| 163 | MasterStream(STREAM_MASTER, |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 164 | R.string.volume_icon_description_media, //FIXME should have its own description |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 165 | R.drawable.ic_audio_vol, |
| 166 | R.drawable.ic_audio_vol_mute, |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 167 | false), |
| 168 | RemoteStream(AudioService.STREAM_REMOTE_MUSIC, |
| 169 | R.string.volume_icon_description_media, //FIXME should have its own description |
| 170 | R.drawable.ic_media_route_on_holo_dark, |
| 171 | R.drawable.ic_media_route_disabled_holo_dark, |
| 172 | false);// will be dynamically updated |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 173 | |
| 174 | int streamType; |
| 175 | int descRes; |
| 176 | int iconRes; |
| 177 | int iconMuteRes; |
| 178 | // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested |
| 179 | boolean show; |
| 180 | |
| 181 | StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) { |
| 182 | this.streamType = streamType; |
| 183 | this.descRes = descRes; |
| 184 | this.iconRes = iconRes; |
| 185 | this.iconMuteRes = iconMuteRes; |
| 186 | this.show = show; |
| 187 | } |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 188 | } |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 189 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 190 | // List of stream types and their order |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 191 | private static final StreamResources[] STREAMS = { |
| 192 | StreamResources.BluetoothSCOStream, |
| 193 | StreamResources.RingerStream, |
| 194 | StreamResources.VoiceStream, |
| 195 | StreamResources.MediaStream, |
Amith Yamasani | 92e1b2d | 2011-10-14 17:24:47 -0700 | [diff] [blame] | 196 | StreamResources.NotificationStream, |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 197 | StreamResources.AlarmStream, |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 198 | StreamResources.MasterStream, |
| 199 | StreamResources.RemoteStream |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | /** Object that contains data for each slider */ |
| 203 | private class StreamControl { |
| 204 | int streamType; |
| 205 | ViewGroup group; |
| 206 | ImageView icon; |
| 207 | SeekBar seekbarView; |
| 208 | int iconRes; |
| 209 | int iconMuteRes; |
| 210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
| 212 | // Synchronize when accessing this |
| 213 | private ToneGenerator mToneGenerators[]; |
| 214 | private Vibrator mVibrator; |
| 215 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 216 | private static AlertDialog sConfirmSafeVolumeDialog; |
Eric Laurent | 0516a9e | 2012-09-19 11:53:03 -0700 | [diff] [blame] | 217 | private static Object sConfirmSafeVolumeLock = new Object(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 218 | |
| 219 | private static class WarningDialogReceiver extends BroadcastReceiver |
| 220 | implements DialogInterface.OnDismissListener { |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 221 | private final Context mContext; |
| 222 | private final Dialog mDialog; |
| 223 | private final VolumePanel mVolumePanel; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 224 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 225 | WarningDialogReceiver(Context context, Dialog dialog, VolumePanel volumePanel) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 226 | mContext = context; |
| 227 | mDialog = dialog; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 228 | mVolumePanel = volumePanel; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 229 | IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 230 | context.registerReceiver(this, filter); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onReceive(Context context, Intent intent) { |
| 235 | mDialog.cancel(); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 236 | cleanUp(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 239 | @Override |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 240 | public void onDismiss(DialogInterface unused) { |
| 241 | mContext.unregisterReceiver(this); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 242 | cleanUp(); |
| 243 | } |
| 244 | |
| 245 | private void cleanUp() { |
Eric Laurent | 0516a9e | 2012-09-19 11:53:03 -0700 | [diff] [blame] | 246 | synchronized (sConfirmSafeVolumeLock) { |
| 247 | sConfirmSafeVolumeDialog = null; |
| 248 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 249 | mVolumePanel.forceTimeout(); |
| 250 | mVolumePanel.updateStates(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 255 | public VolumePanel(Context context, AudioService volumeService) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | mContext = context; |
| 257 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 258 | mAudioService = volumeService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 260 | // For now, only show master volume if master volume is supported |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 261 | final Resources res = context.getResources(); |
| 262 | final boolean useMasterVolume = res.getBoolean(R.bool.config_useMasterVolume); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 263 | if (useMasterVolume) { |
| 264 | for (int i = 0; i < STREAMS.length; i++) { |
| 265 | StreamResources streamRes = STREAMS[i]; |
| 266 | streamRes.show = (streamRes.streamType == STREAM_MASTER); |
| 267 | } |
| 268 | } |
| 269 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 270 | mDialog = new Dialog(context) { |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 271 | @Override |
Amith Yamasani | 284e630 | 2011-09-16 18:24:47 -0700 | [diff] [blame] | 272 | public boolean onTouchEvent(MotionEvent event) { |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 273 | if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE && |
| 274 | sConfirmSafeVolumeDialog == null) { |
Amith Yamasani | 284e630 | 2011-09-16 18:24:47 -0700 | [diff] [blame] | 275 | forceTimeout(); |
| 276 | return true; |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | }; |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 281 | |
| 282 | // Change some window properties |
| 283 | final Window window = mDialog.getWindow(); |
| 284 | final LayoutParams lp = window.getAttributes(); |
| 285 | lp.token = null; |
| 286 | // Offset from the top |
| 287 | lp.y = res.getDimensionPixelOffset(R.dimen.volume_panel_top); |
| 288 | lp.type = LayoutParams.TYPE_VOLUME_OVERLAY; |
| 289 | lp.windowAnimations = R.style.Animation_VolumePanel; |
| 290 | window.setAttributes(lp); |
| 291 | window.setGravity(Gravity.TOP); |
| 292 | window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
| 293 | window.requestFeature(Window.FEATURE_NO_TITLE); |
| 294 | window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE |
| 295 | | LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 296 | | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH); |
| 297 | |
| 298 | mDialog.setCanceledOnTouchOutside(true); |
| 299 | mDialog.setContentView(R.layout.volume_adjust); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 300 | mDialog.setOnDismissListener(new OnDismissListener() { |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 301 | @Override |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 302 | public void onDismiss(DialogInterface dialog) { |
| 303 | mActiveStreamType = -1; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 304 | mAudioManager.forceVolumeControlStream(mActiveStreamType); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 305 | } |
| 306 | }); |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 307 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 308 | mDialog.create(); |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 309 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 310 | mView = window.findViewById(R.id.content); |
| 311 | mView.setOnTouchListener(new View.OnTouchListener() { |
| 312 | @Override |
| 313 | public boolean onTouch(View v, MotionEvent event) { |
| 314 | resetTimeout(); |
| 315 | return false; |
| 316 | } |
| 317 | }); |
| 318 | |
| 319 | mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel); |
| 320 | mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group); |
| 321 | mMoreButton = mView.findViewById(R.id.expand_button); |
| 322 | mDivider = mView.findViewById(R.id.expand_button_divider); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()]; |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 325 | mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 326 | mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable); |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 327 | |
| 328 | // If we don't want to show multiple volumes, hide the settings button |
| 329 | // and divider. |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 330 | mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume; |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 331 | if (!mShowCombinedVolumes) { |
| 332 | mMoreButton.setVisibility(View.GONE); |
| 333 | mDivider.setVisibility(View.GONE); |
| 334 | } else { |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 335 | mMoreButton.setOnClickListener(mClickListener); |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 338 | final boolean masterVolumeOnly = res.getBoolean(R.bool.config_useMasterVolume); |
| 339 | final boolean masterVolumeKeySounds = res.getBoolean(R.bool.config_useVolumeKeySounds); |
Christopher Tate | c4b78d2 | 2012-05-22 13:57:58 -0700 | [diff] [blame] | 340 | mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds; |
| 341 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 342 | listenToRingerMode(); |
| 343 | } |
| 344 | |
Fabrice Di Meglio | 8c02884 | 2013-01-09 18:20:38 -0800 | [diff] [blame] | 345 | public void setLayoutDirection(int layoutDirection) { |
| 346 | mPanel.setLayoutDirection(layoutDirection); |
| 347 | updateStates(); |
| 348 | } |
| 349 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 350 | private void listenToRingerMode() { |
| 351 | final IntentFilter filter = new IntentFilter(); |
| 352 | filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 353 | mContext.registerReceiver(new BroadcastReceiver() { |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 354 | @Override |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 355 | public void onReceive(Context context, Intent intent) { |
| 356 | final String action = intent.getAction(); |
| 357 | |
| 358 | if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) { |
| 359 | removeMessages(MSG_RINGER_MODE_CHANGED); |
| 360 | sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED)); |
| 361 | } |
| 362 | } |
| 363 | }, filter); |
| 364 | } |
| 365 | |
| 366 | private boolean isMuted(int streamType) { |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 367 | if (streamType == STREAM_MASTER) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 368 | return mAudioManager.isMasterMute(); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 369 | } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) { |
| 370 | return (mAudioService.getRemoteStreamVolume() <= 0); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 371 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 372 | return mAudioManager.isStreamMute(streamType); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | private int getStreamMaxVolume(int streamType) { |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 377 | if (streamType == STREAM_MASTER) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 378 | return mAudioManager.getMasterMaxVolume(); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 379 | } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) { |
| 380 | return mAudioService.getRemoteStreamMaxVolume(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 381 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 382 | return mAudioManager.getStreamMaxVolume(streamType); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | private int getStreamVolume(int streamType) { |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 387 | if (streamType == STREAM_MASTER) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 388 | return mAudioManager.getMasterVolume(); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 389 | } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) { |
| 390 | return mAudioService.getRemoteStreamVolume(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 391 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 392 | return mAudioManager.getStreamVolume(streamType); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | private void setStreamVolume(int streamType, int index, int flags) { |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 397 | if (streamType == STREAM_MASTER) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 398 | mAudioManager.setMasterVolume(index, flags); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 399 | } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) { |
| 400 | mAudioService.setRemoteStreamVolume(index); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 401 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 402 | mAudioManager.setStreamVolume(streamType, index, flags); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 403 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | private void createSliders() { |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 407 | final Resources res = mContext.getResources(); |
| 408 | final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 409 | Context.LAYOUT_INFLATER_SERVICE); |
| 410 | |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 411 | mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length); |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 412 | |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 413 | for (int i = 0; i < STREAMS.length; i++) { |
| 414 | StreamResources streamRes = STREAMS[i]; |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 415 | |
| 416 | final int streamType = streamRes.streamType; |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 417 | if (mVoiceCapable && streamRes == StreamResources.NotificationStream) { |
| 418 | streamRes = StreamResources.RingerStream; |
| 419 | } |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 420 | |
| 421 | final StreamControl sc = new StreamControl(); |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 422 | sc.streamType = streamType; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 423 | sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null); |
| 424 | sc.group.setTag(sc); |
| 425 | sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 426 | sc.icon.setTag(sc); |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 427 | sc.icon.setContentDescription(res.getString(streamRes.descRes)); |
| 428 | sc.iconRes = streamRes.iconRes; |
| 429 | sc.iconMuteRes = streamRes.iconMuteRes; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 430 | sc.icon.setImageResource(sc.iconRes); |
| 431 | sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar); |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 432 | final int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO || |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 433 | streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0; |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 434 | sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne); |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 435 | sc.seekbarView.setOnSeekBarChangeListener(mSeekListener); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 436 | sc.seekbarView.setTag(sc); |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 437 | mStreamControls.put(streamType, sc); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | private void reorderSliders(int activeStreamType) { |
| 442 | mSliderGroup.removeAllViews(); |
| 443 | |
Alan Viverette | 494fb7b | 2014-04-10 18:12:56 -0700 | [diff] [blame] | 444 | final StreamControl active = mStreamControls.get(activeStreamType); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 445 | if (active == null) { |
| 446 | Log.e("VolumePanel", "Missing stream type! - " + activeStreamType); |
| 447 | mActiveStreamType = -1; |
| 448 | } else { |
| 449 | mSliderGroup.addView(active.group); |
| 450 | mActiveStreamType = activeStreamType; |
| 451 | active.group.setVisibility(View.VISIBLE); |
| 452 | updateSlider(active); |
| 453 | } |
| 454 | |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 455 | addOtherVolumes(); |
| 456 | } |
| 457 | |
| 458 | private void addOtherVolumes() { |
| 459 | if (!mShowCombinedVolumes) return; |
| 460 | |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 461 | for (int i = 0; i < STREAMS.length; i++) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 462 | // Skip the phone specific ones and the active one |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 463 | final int streamType = STREAMS[i].streamType; |
| 464 | if (!STREAMS[i].show || streamType == mActiveStreamType) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | StreamControl sc = mStreamControls.get(streamType); |
| 468 | mSliderGroup.addView(sc.group); |
| 469 | updateSlider(sc); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /** Update the mute and progress state of a slider */ |
| 474 | private void updateSlider(StreamControl sc) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 475 | sc.seekbarView.setProgress(getStreamVolume(sc.streamType)); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 476 | final boolean muted = isMuted(sc.streamType); |
Fabrice Di Meglio | 8c02884 | 2013-01-09 18:20:38 -0800 | [diff] [blame] | 477 | // Force reloading the image resource |
| 478 | sc.icon.setImageDrawable(null); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 479 | sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes); |
Eric Laurent | 8a51aca | 2013-03-25 18:36:35 -0700 | [diff] [blame] | 480 | if (((sc.streamType == AudioManager.STREAM_RING) || |
| 481 | (sc.streamType == AudioManager.STREAM_NOTIFICATION)) && |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 482 | mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) { |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 483 | sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate); |
| 484 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 485 | if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) { |
| 486 | // never disable touch interactions for remote playback, the muting is not tied to |
| 487 | // the state of the phone. |
| 488 | sc.seekbarView.setEnabled(true); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 489 | } else if ((sc.streamType != mAudioManager.getMasterStreamType() && muted) || |
| 490 | (sConfirmSafeVolumeDialog != null)) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 491 | sc.seekbarView.setEnabled(false); |
| 492 | } else { |
| 493 | sc.seekbarView.setEnabled(true); |
| 494 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 497 | private void expand() { |
| 498 | final int count = mSliderGroup.getChildCount(); |
| 499 | for (int i = 0; i < count; i++) { |
| 500 | mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE); |
| 501 | } |
| 502 | mMoreButton.setVisibility(View.INVISIBLE); |
| 503 | mDivider.setVisibility(View.INVISIBLE); |
| 504 | } |
| 505 | |
| 506 | private void collapse() { |
| 507 | mMoreButton.setVisibility(View.VISIBLE); |
| 508 | mDivider.setVisibility(View.VISIBLE); |
| 509 | final int count = mSliderGroup.getChildCount(); |
| 510 | for (int i = 1; i < count; i++) { |
| 511 | mSliderGroup.getChildAt(i).setVisibility(View.GONE); |
| 512 | } |
| 513 | } |
| 514 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 515 | public void updateStates() { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 516 | final int count = mSliderGroup.getChildCount(); |
| 517 | for (int i = 0; i < count; i++) { |
| 518 | StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag(); |
| 519 | updateSlider(sc); |
| 520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | public void postVolumeChanged(int streamType, int flags) { |
| 524 | if (hasMessages(MSG_VOLUME_CHANGED)) return; |
Amith Yamasani | a654986 | 2012-05-30 17:29:28 -0700 | [diff] [blame] | 525 | synchronized (this) { |
| 526 | if (mStreamControls == null) { |
| 527 | createSliders(); |
| 528 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 529 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | removeMessages(MSG_FREE_RESOURCES); |
| 531 | obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget(); |
| 532 | } |
| 533 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 534 | @Override |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 535 | public void postRemoteVolumeChanged(int streamType, int flags) { |
| 536 | if (hasMessages(MSG_REMOTE_VOLUME_CHANGED)) return; |
| 537 | synchronized (this) { |
| 538 | if (mStreamControls == null) { |
| 539 | createSliders(); |
| 540 | } |
| 541 | } |
| 542 | removeMessages(MSG_FREE_RESOURCES); |
| 543 | obtainMessage(MSG_REMOTE_VOLUME_CHANGED, streamType, flags).sendToTarget(); |
| 544 | } |
| 545 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 546 | @Override |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 547 | public void postRemoteSliderVisibility(boolean visible) { |
| 548 | obtainMessage(MSG_SLIDER_VISIBILITY_CHANGED, |
| 549 | AudioService.STREAM_REMOTE_MUSIC, visible ? 1 : 0).sendToTarget(); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Called by AudioService when it has received new remote playback information that |
| 554 | * would affect the VolumePanel display (mainly volumes). The difference with |
| 555 | * {@link #postRemoteVolumeChanged(int, int)} is that the handling of the posted message |
| 556 | * (MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN) will only update the volume slider if it is being |
| 557 | * displayed. |
| 558 | * This special code path is due to the fact that remote volume updates arrive to AudioService |
| 559 | * asynchronously. So after AudioService has sent the volume update (which should be treated |
| 560 | * as a request to update the volume), the application will likely set a new volume. If the UI |
| 561 | * is still up, we need to refresh the display to show this new value. |
| 562 | */ |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 563 | @Override |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 564 | public void postHasNewRemotePlaybackInfo() { |
| 565 | if (hasMessages(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN)) return; |
| 566 | // don't create or prevent resources to be freed, if they disappear, this update came too |
| 567 | // late and shouldn't warrant the panel to be displayed longer |
| 568 | obtainMessage(MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN).sendToTarget(); |
| 569 | } |
| 570 | |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 571 | public void postMasterVolumeChanged(int flags) { |
| 572 | postVolumeChanged(STREAM_MASTER, flags); |
| 573 | } |
| 574 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 575 | public void postMuteChanged(int streamType, int flags) { |
| 576 | if (hasMessages(MSG_VOLUME_CHANGED)) return; |
Amith Yamasani | a654986 | 2012-05-30 17:29:28 -0700 | [diff] [blame] | 577 | synchronized (this) { |
| 578 | if (mStreamControls == null) { |
| 579 | createSliders(); |
| 580 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 581 | } |
| 582 | removeMessages(MSG_FREE_RESOURCES); |
| 583 | obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget(); |
| 584 | } |
| 585 | |
| 586 | public void postMasterMuteChanged(int flags) { |
| 587 | postMuteChanged(STREAM_MASTER, flags); |
| 588 | } |
| 589 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 590 | public void postDisplaySafeVolumeWarning(int flags) { |
Eric Laurent | 0516a9e | 2012-09-19 11:53:03 -0700 | [diff] [blame] | 591 | if (hasMessages(MSG_DISPLAY_SAFE_VOLUME_WARNING)) return; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 592 | obtainMessage(MSG_DISPLAY_SAFE_VOLUME_WARNING, flags, 0).sendToTarget(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 593 | } |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
| 596 | * Override this if you have other work to do when the volume changes (for |
| 597 | * example, vibrating, playing a sound, etc.). Make sure to call through to |
| 598 | * the superclass implementation. |
| 599 | */ |
| 600 | protected void onVolumeChanged(int streamType, int flags) { |
| 601 | |
| 602 | if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")"); |
| 603 | |
| 604 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0) { |
Amith Yamasani | a654986 | 2012-05-30 17:29:28 -0700 | [diff] [blame] | 605 | synchronized (this) { |
| 606 | if (mActiveStreamType != streamType) { |
| 607 | reorderSliders(streamType); |
| 608 | } |
| 609 | onShowVolumeChanged(streamType, flags); |
Amith Yamasani | e3361b8 | 2011-02-10 18:20:50 -0800 | [diff] [blame] | 610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 613 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | removeMessages(MSG_PLAY_SOUND); |
| 615 | sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY); |
| 616 | } |
| 617 | |
| 618 | if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) { |
| 619 | removeMessages(MSG_PLAY_SOUND); |
| 620 | removeMessages(MSG_VIBRATE); |
| 621 | onStopSounds(); |
| 622 | } |
| 623 | |
| 624 | removeMessages(MSG_FREE_RESOURCES); |
| 625 | sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 626 | resetTimeout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 629 | protected void onMuteChanged(int streamType, int flags) { |
| 630 | |
| 631 | if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")"); |
| 632 | |
| 633 | StreamControl sc = mStreamControls.get(streamType); |
| 634 | if (sc != null) { |
| 635 | sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes); |
| 636 | } |
| 637 | |
| 638 | onVolumeChanged(streamType, flags); |
| 639 | } |
| 640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | protected void onShowVolumeChanged(int streamType, int flags) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 642 | int index = getStreamVolume(streamType); |
Eric Laurent | d72d51c | 2011-02-03 18:47:47 -0800 | [diff] [blame] | 643 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 644 | mRingIsSilent = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | |
| 646 | if (LOGD) { |
| 647 | Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType |
| 648 | + ", flags: " + flags + "), index: " + index); |
| 649 | } |
| 650 | |
| 651 | // get max volume for progress bar |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 652 | |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 653 | int max = getStreamMaxVolume(streamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | |
| 655 | switch (streamType) { |
| 656 | |
| 657 | case AudioManager.STREAM_RING: { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 658 | // setRingerIcon(); |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 659 | Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri( |
| 660 | mContext, RingtoneManager.TYPE_RINGTONE); |
| 661 | if (ringuri == null) { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 662 | mRingIsSilent = true; |
| 663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | break; |
| 665 | } |
| 666 | |
| 667 | case AudioManager.STREAM_MUSIC: { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 668 | // Special case for when Bluetooth is active for music |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 669 | if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) & |
| 670 | (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP | |
| 671 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 672 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 673 | setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | } else { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 675 | setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | } |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | case AudioManager.STREAM_VOICE_CALL: { |
| 681 | /* |
| 682 | * For in-call voice call volume, there is no inaudible volume. |
| 683 | * Rescale the UI control so the progress bar doesn't go all |
| 684 | * the way to zero and don't show the mute icon. |
| 685 | */ |
| 686 | index++; |
| 687 | max++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | break; |
| 689 | } |
| 690 | |
| 691 | case AudioManager.STREAM_ALARM: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | |
| 695 | case AudioManager.STREAM_NOTIFICATION: { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 696 | Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri( |
| 697 | mContext, RingtoneManager.TYPE_NOTIFICATION); |
| 698 | if (ringuri == null) { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 699 | mRingIsSilent = true; |
| 700 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | |
| 704 | case AudioManager.STREAM_BLUETOOTH_SCO: { |
| 705 | /* |
| 706 | * For in-call voice call volume, there is no inaudible volume. |
| 707 | * Rescale the UI control so the progress bar doesn't go all |
| 708 | * the way to zero and don't show the mute icon. |
| 709 | */ |
| 710 | index++; |
| 711 | max++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | break; |
| 713 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 714 | |
| 715 | case AudioService.STREAM_REMOTE_MUSIC: { |
| 716 | if (LOGD) { Log.d(TAG, "showing remote volume "+index+" over "+ max); } |
| 717 | break; |
| 718 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 721 | StreamControl sc = mStreamControls.get(streamType); |
| 722 | if (sc != null) { |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 723 | if (sc.seekbarView.getMax() != max) { |
| 724 | sc.seekbarView.setMax(max); |
| 725 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 726 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 727 | sc.seekbarView.setProgress(index); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 728 | if (((flags & AudioManager.FLAG_FIXED_VOLUME) != 0) || |
| 729 | (streamType != mAudioManager.getMasterStreamType() && |
| 730 | streamType != AudioService.STREAM_REMOTE_MUSIC && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 731 | isMuted(streamType)) || |
| 732 | sConfirmSafeVolumeDialog != null) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 733 | sc.seekbarView.setEnabled(false); |
| 734 | } else { |
| 735 | sc.seekbarView.setEnabled(true); |
| 736 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 739 | if (!mDialog.isShowing()) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 740 | int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType; |
| 741 | // when the stream is for remote playback, use -1 to reset the stream type evaluation |
| 742 | mAudioManager.forceVolumeControlStream(stream); |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 743 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 744 | // Showing dialog - use collapsed state |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 745 | if (mShowCombinedVolumes) { |
| 746 | collapse(); |
| 747 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 748 | mDialog.show(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | } |
| 750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | // Do a little vibrate if applicable (only when going into vibrate mode) |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 752 | if ((streamType != AudioService.STREAM_REMOTE_MUSIC) && |
| 753 | ((flags & AudioManager.FLAG_VIBRATE) != 0) && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | mAudioService.isStreamAffectedByRingerMode(streamType) && |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 755 | mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY); |
| 757 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | protected void onPlaySound(int streamType, int flags) { |
| 761 | |
| 762 | if (hasMessages(MSG_STOP_SOUNDS)) { |
| 763 | removeMessages(MSG_STOP_SOUNDS); |
| 764 | // Force stop right now |
| 765 | onStopSounds(); |
| 766 | } |
| 767 | |
| 768 | synchronized (this) { |
| 769 | ToneGenerator toneGen = getOrCreateToneGenerator(streamType); |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 770 | if (toneGen != null) { |
| 771 | toneGen.startTone(ToneGenerator.TONE_PROP_BEEP); |
| 772 | sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION); |
| 773 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | protected void onStopSounds() { |
| 778 | |
| 779 | synchronized (this) { |
| 780 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 781 | for (int i = numStreamTypes - 1; i >= 0; i--) { |
| 782 | ToneGenerator toneGen = mToneGenerators[i]; |
| 783 | if (toneGen != null) { |
| 784 | toneGen.stopTone(); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | protected void onVibrate() { |
| 791 | |
| 792 | // Make sure we ended up in vibrate ringer mode |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 793 | if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
John Spurlock | f9e1a0b | 2014-03-19 22:09:06 -0400 | [diff] [blame] | 797 | mVibrator.vibrate(VIBRATE_DURATION, AudioManager.STREAM_SYSTEM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 800 | protected void onRemoteVolumeChanged(int streamType, int flags) { |
| 801 | // streamType is the real stream type being affected, but for the UI sliders, we |
| 802 | // refer to AudioService.STREAM_REMOTE_MUSIC. We still play the beeps on the real |
| 803 | // stream type. |
| 804 | if (LOGD) Log.d(TAG, "onRemoteVolumeChanged(stream:"+streamType+", flags: " + flags + ")"); |
| 805 | |
| 806 | if (((flags & AudioManager.FLAG_SHOW_UI) != 0) || mDialog.isShowing()) { |
| 807 | synchronized (this) { |
| 808 | if (mActiveStreamType != AudioService.STREAM_REMOTE_MUSIC) { |
| 809 | reorderSliders(AudioService.STREAM_REMOTE_MUSIC); |
| 810 | } |
| 811 | onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, flags); |
| 812 | } |
| 813 | } else { |
| 814 | if (LOGD) Log.d(TAG, "not calling onShowVolumeChanged(), no FLAG_SHOW_UI or no UI"); |
| 815 | } |
| 816 | |
| 817 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) { |
| 818 | removeMessages(MSG_PLAY_SOUND); |
| 819 | sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY); |
| 820 | } |
| 821 | |
| 822 | if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) { |
| 823 | removeMessages(MSG_PLAY_SOUND); |
| 824 | removeMessages(MSG_VIBRATE); |
| 825 | onStopSounds(); |
| 826 | } |
| 827 | |
| 828 | removeMessages(MSG_FREE_RESOURCES); |
| 829 | sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 830 | resetTimeout(); |
| 831 | } |
| 832 | |
| 833 | protected void onRemoteVolumeUpdateIfShown() { |
| 834 | if (LOGD) Log.d(TAG, "onRemoteVolumeUpdateIfShown()"); |
| 835 | if (mDialog.isShowing() |
| 836 | && (mActiveStreamType == AudioService.STREAM_REMOTE_MUSIC) |
| 837 | && (mStreamControls != null)) { |
| 838 | onShowVolumeChanged(AudioService.STREAM_REMOTE_MUSIC, 0); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | |
| 843 | /** |
| 844 | * Handler for MSG_SLIDER_VISIBILITY_CHANGED |
| 845 | * Hide or show a slider |
| 846 | * @param streamType can be a valid stream type value, or VolumePanel.STREAM_MASTER, |
| 847 | * or AudioService.STREAM_REMOTE_MUSIC |
| 848 | * @param visible |
| 849 | */ |
| 850 | synchronized protected void onSliderVisibilityChanged(int streamType, int visible) { |
| 851 | if (LOGD) Log.d(TAG, "onSliderVisibilityChanged(stream="+streamType+", visi="+visible+")"); |
| 852 | boolean isVisible = (visible == 1); |
| 853 | for (int i = STREAMS.length - 1 ; i >= 0 ; i--) { |
| 854 | StreamResources streamRes = STREAMS[i]; |
| 855 | if (streamRes.streamType == streamType) { |
| 856 | streamRes.show = isVisible; |
| 857 | if (!isVisible && (mActiveStreamType == streamType)) { |
| 858 | mActiveStreamType = -1; |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 865 | protected void onDisplaySafeVolumeWarning(int flags) { |
| 866 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 || mDialog.isShowing()) { |
| 867 | synchronized (sConfirmSafeVolumeLock) { |
| 868 | if (sConfirmSafeVolumeDialog != null) { |
| 869 | return; |
| 870 | } |
| 871 | sConfirmSafeVolumeDialog = new AlertDialog.Builder(mContext) |
| 872 | .setMessage(com.android.internal.R.string.safe_media_volume_warning) |
| 873 | .setPositiveButton(com.android.internal.R.string.yes, |
| 874 | new DialogInterface.OnClickListener() { |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 875 | @Override |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 876 | public void onClick(DialogInterface dialog, int which) { |
| 877 | mAudioService.disableSafeMediaVolume(); |
| 878 | } |
| 879 | }) |
| 880 | .setNegativeButton(com.android.internal.R.string.no, null) |
| 881 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 882 | .create(); |
| 883 | final WarningDialogReceiver warning = new WarningDialogReceiver(mContext, |
| 884 | sConfirmSafeVolumeDialog, this); |
Eric Laurent | 0516a9e | 2012-09-19 11:53:03 -0700 | [diff] [blame] | 885 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 886 | sConfirmSafeVolumeDialog.setOnDismissListener(warning); |
| 887 | sConfirmSafeVolumeDialog.getWindow().setType( |
| 888 | WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 889 | sConfirmSafeVolumeDialog.show(); |
| 890 | } |
| 891 | updateStates(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 892 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 893 | resetTimeout(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 894 | } |
| 895 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 896 | /** |
| 897 | * Lock on this VolumePanel instance as long as you use the returned ToneGenerator. |
| 898 | */ |
| 899 | private ToneGenerator getOrCreateToneGenerator(int streamType) { |
Christopher Tate | c4b78d2 | 2012-05-22 13:57:58 -0700 | [diff] [blame] | 900 | if (streamType == STREAM_MASTER) { |
| 901 | // For devices that use the master volume setting only but still want to |
| 902 | // play a volume-changed tone, direct the master volume pseudostream to |
| 903 | // the system stream's tone generator. |
| 904 | if (mPlayMasterStreamTones) { |
| 905 | streamType = AudioManager.STREAM_SYSTEM; |
| 906 | } else { |
| 907 | return null; |
| 908 | } |
| 909 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | synchronized (this) { |
| 911 | if (mToneGenerators[streamType] == null) { |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 912 | try { |
| 913 | mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME); |
| 914 | } catch (RuntimeException e) { |
| 915 | if (LOGD) { |
| 916 | Log.d(TAG, "ToneGenerator constructor failed with " |
| 917 | + "RuntimeException: " + e); |
| 918 | } |
| 919 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | } |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 921 | return mToneGenerators[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | |
| 926 | /** |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 927 | * Switch between icons because Bluetooth music is same as music volume, but with |
| 928 | * different icons. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | */ |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 930 | private void setMusicIcon(int resId, int resMuteId) { |
| 931 | StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC); |
| 932 | if (sc != null) { |
| 933 | sc.iconRes = resId; |
| 934 | sc.iconMuteRes = resMuteId; |
| 935 | sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | protected void onFreeResources() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | synchronized (this) { |
| 941 | for (int i = mToneGenerators.length - 1; i >= 0; i--) { |
| 942 | if (mToneGenerators[i] != null) { |
| 943 | mToneGenerators[i].release(); |
| 944 | } |
| 945 | mToneGenerators[i] = null; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | @Override |
| 951 | public void handleMessage(Message msg) { |
| 952 | switch (msg.what) { |
| 953 | |
| 954 | case MSG_VOLUME_CHANGED: { |
| 955 | onVolumeChanged(msg.arg1, msg.arg2); |
| 956 | break; |
| 957 | } |
| 958 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 959 | case MSG_MUTE_CHANGED: { |
| 960 | onMuteChanged(msg.arg1, msg.arg2); |
| 961 | break; |
| 962 | } |
| 963 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | case MSG_FREE_RESOURCES: { |
| 965 | onFreeResources(); |
| 966 | break; |
| 967 | } |
| 968 | |
| 969 | case MSG_STOP_SOUNDS: { |
| 970 | onStopSounds(); |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | case MSG_PLAY_SOUND: { |
| 975 | onPlaySound(msg.arg1, msg.arg2); |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | case MSG_VIBRATE: { |
| 980 | onVibrate(); |
| 981 | break; |
| 982 | } |
| 983 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 984 | case MSG_TIMEOUT: { |
| 985 | if (mDialog.isShowing()) { |
| 986 | mDialog.dismiss(); |
| 987 | mActiveStreamType = -1; |
| 988 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 989 | synchronized (sConfirmSafeVolumeLock) { |
| 990 | if (sConfirmSafeVolumeDialog != null) { |
| 991 | sConfirmSafeVolumeDialog.dismiss(); |
| 992 | } |
| 993 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 994 | break; |
| 995 | } |
| 996 | case MSG_RINGER_MODE_CHANGED: { |
| 997 | if (mDialog.isShowing()) { |
| 998 | updateStates(); |
| 999 | } |
| 1000 | break; |
| 1001 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1002 | |
| 1003 | case MSG_REMOTE_VOLUME_CHANGED: { |
| 1004 | onRemoteVolumeChanged(msg.arg1, msg.arg2); |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | case MSG_REMOTE_VOLUME_UPDATE_IF_SHOWN: |
| 1009 | onRemoteVolumeUpdateIfShown(); |
| 1010 | break; |
| 1011 | |
| 1012 | case MSG_SLIDER_VISIBILITY_CHANGED: |
| 1013 | onSliderVisibilityChanged(msg.arg1, msg.arg2); |
| 1014 | break; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 1015 | |
| 1016 | case MSG_DISPLAY_SAFE_VOLUME_WARNING: |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1017 | onDisplaySafeVolumeWarning(msg.arg1); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 1018 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 1022 | private void resetTimeout() { |
| 1023 | removeMessages(MSG_TIMEOUT); |
| 1024 | sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY); |
| 1025 | } |
| 1026 | |
Amith Yamasani | baf6dbf | 2011-08-18 17:40:29 -0700 | [diff] [blame] | 1027 | private void forceTimeout() { |
| 1028 | removeMessages(MSG_TIMEOUT); |
| 1029 | sendMessage(obtainMessage(MSG_TIMEOUT)); |
| 1030 | } |
| 1031 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 1032 | private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() { |
| 1033 | @Override |
| 1034 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 1035 | final Object tag = seekBar.getTag(); |
| 1036 | if (fromUser && tag instanceof StreamControl) { |
| 1037 | StreamControl sc = (StreamControl) tag; |
| 1038 | if (getStreamVolume(sc.streamType) != progress) { |
| 1039 | setStreamVolume(sc.streamType, progress, 0); |
| 1040 | } |
| 1041 | } |
| 1042 | resetTimeout(); |
| 1043 | } |
| 1044 | |
| 1045 | @Override |
| 1046 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 1047 | } |
| 1048 | |
| 1049 | @Override |
| 1050 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 1051 | final Object tag = seekBar.getTag(); |
| 1052 | if (tag instanceof StreamControl) { |
| 1053 | StreamControl sc = (StreamControl) tag; |
| 1054 | // Because remote volume updates are asynchronous, AudioService |
| 1055 | // might have received a new remote volume value since the |
| 1056 | // finger adjusted the slider. So when the progress of the |
| 1057 | // slider isn't being tracked anymore, adjust the slider to the |
| 1058 | // last "published" remote volume value, so the UI reflects the |
| 1059 | // actual volume. |
| 1060 | if (sc.streamType == AudioService.STREAM_REMOTE_MUSIC) { |
| 1061 | seekBar.setProgress(getStreamVolume(AudioService.STREAM_REMOTE_MUSIC)); |
| 1062 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 1065 | }; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 1066 | |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 1067 | private final View.OnClickListener mClickListener = new View.OnClickListener() { |
| 1068 | @Override |
| 1069 | public void onClick(View v) { |
| 1070 | if (v == mMoreButton) { |
| 1071 | expand(); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1072 | } |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 1073 | resetTimeout(); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1074 | } |
Alan Viverette | e8ebaf3 | 2014-04-11 15:44:15 -0700 | [diff] [blame^] | 1075 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | } |