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