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