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