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