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