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