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