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