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 | |
| 17 | package android.view; |
| 18 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
| 21 | import android.app.Dialog; |
| 22 | import android.content.DialogInterface.OnDismissListener; |
| 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 27 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.media.AudioManager; |
| 30 | import android.media.AudioService; |
| 31 | import android.media.AudioSystem; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 32 | import android.media.RingtoneManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.media.ToneGenerator; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.Handler; |
| 36 | import android.os.Message; |
| 37 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | import android.widget.ImageView; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 40 | import android.widget.SeekBar; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 41 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 42 | |
| 43 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Handle the volume up and down keys. |
| 47 | * |
| 48 | * This code really should be moved elsewhere. |
| 49 | * |
Dianne Hackborn | e8ecde1 | 2011-08-03 18:55:19 -0700 | [diff] [blame^] | 50 | * Seriously, it really really should be moved elsewhere. This is used by |
| 51 | * android.media.AudioService, which actually runs in the system process, to |
| 52 | * show the volume dialog when the user changes the volume. What a mess. |
| 53 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * @hide |
| 55 | */ |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 56 | public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | { |
| 58 | private static final String TAG = "VolumePanel"; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 59 | private static boolean LOGD = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * The delay before playing a sound. This small period exists so the user |
| 63 | * can press another key (non-volume keys, too) to have it NOT be audible. |
| 64 | * <p> |
| 65 | * PhoneWindow will implement this part. |
| 66 | */ |
| 67 | public static final int PLAY_SOUND_DELAY = 300; |
| 68 | |
| 69 | /** |
| 70 | * The delay before vibrating. This small period exists so if the user is |
| 71 | * moving to silent mode, it will not emit a short vibrate (it normally |
| 72 | * would since vibrate is between normal mode and silent mode using hardware |
| 73 | * keys). |
| 74 | */ |
| 75 | public static final int VIBRATE_DELAY = 300; |
| 76 | |
| 77 | private static final int VIBRATE_DURATION = 300; |
| 78 | private static final int BEEP_DURATION = 150; |
| 79 | private static final int MAX_VOLUME = 100; |
| 80 | private static final int FREE_DELAY = 10000; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 81 | private static final int TIMEOUT_DELAY = 3000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| 83 | private static final int MSG_VOLUME_CHANGED = 0; |
| 84 | private static final int MSG_FREE_RESOURCES = 1; |
| 85 | private static final int MSG_PLAY_SOUND = 2; |
| 86 | private static final int MSG_STOP_SOUNDS = 3; |
| 87 | private static final int MSG_VIBRATE = 4; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 88 | private static final int MSG_TIMEOUT = 5; |
| 89 | private static final int MSG_RINGER_MODE_CHANGED = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | protected Context mContext; |
| 92 | private AudioManager mAudioManager; |
| 93 | protected AudioService mAudioService; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 94 | private boolean mRingIsSilent; |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 95 | private boolean mShowCombinedVolumes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 97 | /** Dialog containing all the sliders */ |
| 98 | private final Dialog mDialog; |
| 99 | /** Dialog's content view */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | private final View mView; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 101 | |
| 102 | /** Contains the sliders and their touchable icons */ |
| 103 | private final ViewGroup mSliderGroup; |
| 104 | /** The button that expands the dialog to show all sliders */ |
| 105 | private final View mMoreButton; |
| 106 | /** Dummy divider icon that needs to vanish with the more button */ |
| 107 | private final View mDivider; |
| 108 | |
| 109 | /** Currently active stream that shows up at the top of the list of sliders */ |
| 110 | private int mActiveStreamType = -1; |
| 111 | /** All the slider controls mapped by stream type */ |
| 112 | private HashMap<Integer,StreamControl> mStreamControls; |
| 113 | |
| 114 | // List of stream types and their order |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 115 | // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 116 | private static final int [] STREAM_TYPES = { |
Eric Laurent | e2ad901 | 2011-02-17 17:31:51 -0800 | [diff] [blame] | 117 | AudioManager.STREAM_BLUETOOTH_SCO, |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 118 | AudioManager.STREAM_RING, |
| 119 | AudioManager.STREAM_VOICE_CALL, |
| 120 | AudioManager.STREAM_MUSIC, |
| 121 | AudioManager.STREAM_NOTIFICATION |
| 122 | }; |
| 123 | |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 124 | private static final int [] CONTENT_DESCRIPTIONS = { |
| 125 | R.string.volume_icon_description_bluetooth, |
| 126 | R.string.volume_icon_description_ringer, |
| 127 | R.string.volume_icon_description_incall, |
| 128 | R.string.volume_icon_description_media, |
| 129 | R.string.volume_icon_description_notification |
| 130 | }; |
| 131 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 132 | // These icons need to correspond to the ones above. |
| 133 | private static final int [] STREAM_ICONS_NORMAL = { |
Eric Laurent | e2ad901 | 2011-02-17 17:31:51 -0800 | [diff] [blame] | 134 | R.drawable.ic_audio_bt, |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 135 | R.drawable.ic_audio_ring_notif, |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 136 | R.drawable.ic_audio_phone, |
| 137 | R.drawable.ic_audio_vol, |
| 138 | R.drawable.ic_audio_notification, |
| 139 | }; |
| 140 | |
| 141 | // These icons need to correspond to the ones above. |
| 142 | private static final int [] STREAM_ICONS_MUTED = { |
Eric Laurent | e2ad901 | 2011-02-17 17:31:51 -0800 | [diff] [blame] | 143 | R.drawable.ic_audio_bt, |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 144 | R.drawable.ic_audio_ring_notif_mute, |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 145 | R.drawable.ic_audio_phone, |
| 146 | R.drawable.ic_audio_vol_mute, |
| 147 | R.drawable.ic_audio_notification_mute, |
| 148 | }; |
| 149 | |
| 150 | /** Object that contains data for each slider */ |
| 151 | private class StreamControl { |
| 152 | int streamType; |
| 153 | ViewGroup group; |
| 154 | ImageView icon; |
| 155 | SeekBar seekbarView; |
| 156 | int iconRes; |
| 157 | int iconMuteRes; |
| 158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
| 160 | // Synchronize when accessing this |
| 161 | private ToneGenerator mToneGenerators[]; |
| 162 | private Vibrator mVibrator; |
| 163 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 164 | public VolumePanel(final Context context, AudioService volumeService) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | mContext = context; |
| 166 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 167 | mAudioService = volumeService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
| 169 | LayoutInflater inflater = (LayoutInflater) context |
| 170 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 171 | View view = mView = inflater.inflate(R.layout.volume_adjust, null); |
| 172 | mView.setOnTouchListener(new View.OnTouchListener() { |
| 173 | public boolean onTouch(View v, MotionEvent event) { |
| 174 | resetTimeout(); |
| 175 | return true; |
| 176 | } |
| 177 | }); |
| 178 | mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group); |
| 179 | mMoreButton = (ImageView) mView.findViewById(R.id.expand_button); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 180 | mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider); |
| 181 | |
| 182 | mDialog = new Dialog(context, R.style.Theme_Panel_Volume); |
| 183 | mDialog.setTitle("Volume control"); // No need to localize |
| 184 | mDialog.setContentView(mView); |
| 185 | mDialog.setOnDismissListener(new OnDismissListener() { |
| 186 | public void onDismiss(DialogInterface dialog) { |
| 187 | mActiveStreamType = -1; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 188 | mAudioManager.forceVolumeControlStream(mActiveStreamType); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 189 | } |
| 190 | }); |
| 191 | // Change some window properties |
| 192 | Window window = mDialog.getWindow(); |
| 193 | window.setGravity(Gravity.TOP); |
| 194 | WindowManager.LayoutParams lp = window.getAttributes(); |
| 195 | lp.token = null; |
Dianne Hackborn | e8ecde1 | 2011-08-03 18:55:19 -0700 | [diff] [blame^] | 196 | lp.type = WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 197 | window.setAttributes(lp); |
| 198 | window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); |
| 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()]; |
| 201 | mVibrator = new Vibrator(); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 202 | |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 203 | mShowCombinedVolumes = !context.getResources().getBoolean(R.bool.config_voice_capable); |
| 204 | // If we don't want to show multiple volumes, hide the settings button and divider |
| 205 | if (!mShowCombinedVolumes) { |
| 206 | mMoreButton.setVisibility(View.GONE); |
| 207 | mDivider.setVisibility(View.GONE); |
| 208 | } else { |
| 209 | mMoreButton.setOnClickListener(this); |
| 210 | } |
| 211 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 212 | listenToRingerMode(); |
| 213 | } |
| 214 | |
| 215 | private void listenToRingerMode() { |
| 216 | final IntentFilter filter = new IntentFilter(); |
| 217 | filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 218 | mContext.registerReceiver(new BroadcastReceiver() { |
| 219 | |
| 220 | public void onReceive(Context context, Intent intent) { |
| 221 | final String action = intent.getAction(); |
| 222 | |
| 223 | if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) { |
| 224 | removeMessages(MSG_RINGER_MODE_CHANGED); |
| 225 | sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED)); |
| 226 | } |
| 227 | } |
| 228 | }, filter); |
| 229 | } |
| 230 | |
| 231 | private boolean isMuted(int streamType) { |
| 232 | return mAudioManager.isStreamMute(streamType); |
| 233 | } |
| 234 | |
| 235 | private void createSliders() { |
| 236 | LayoutInflater inflater = (LayoutInflater) mContext |
| 237 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 238 | mStreamControls = new HashMap<Integer,StreamControl>(STREAM_TYPES.length); |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 239 | Resources res = mContext.getResources(); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 240 | for (int i = 0; i < STREAM_TYPES.length; i++) { |
| 241 | StreamControl sc = new StreamControl(); |
| 242 | sc.streamType = STREAM_TYPES[i]; |
| 243 | sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null); |
| 244 | sc.group.setTag(sc); |
| 245 | sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon); |
| 246 | sc.icon.setOnClickListener(this); |
| 247 | sc.icon.setTag(sc); |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 248 | sc.icon.setContentDescription(res.getString(CONTENT_DESCRIPTIONS[i])); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 249 | sc.iconRes = STREAM_ICONS_NORMAL[i]; |
| 250 | sc.iconMuteRes = STREAM_ICONS_MUTED[i]; |
| 251 | sc.icon.setImageResource(sc.iconRes); |
| 252 | sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar); |
| 253 | sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(STREAM_TYPES[i])); |
| 254 | sc.seekbarView.setOnSeekBarChangeListener(this); |
| 255 | sc.seekbarView.setTag(sc); |
| 256 | mStreamControls.put(STREAM_TYPES[i], sc); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private void reorderSliders(int activeStreamType) { |
| 261 | mSliderGroup.removeAllViews(); |
| 262 | |
| 263 | StreamControl active = mStreamControls.get(activeStreamType); |
| 264 | if (active == null) { |
| 265 | Log.e("VolumePanel", "Missing stream type! - " + activeStreamType); |
| 266 | mActiveStreamType = -1; |
| 267 | } else { |
| 268 | mSliderGroup.addView(active.group); |
| 269 | mActiveStreamType = activeStreamType; |
| 270 | active.group.setVisibility(View.VISIBLE); |
| 271 | updateSlider(active); |
| 272 | } |
| 273 | |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 274 | addOtherVolumes(); |
| 275 | } |
| 276 | |
| 277 | private void addOtherVolumes() { |
| 278 | if (!mShowCombinedVolumes) return; |
| 279 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 280 | for (int i = 0; i < STREAM_TYPES.length; i++) { |
| 281 | // Skip the phone specific ones and the active one |
| 282 | final int streamType = STREAM_TYPES[i]; |
| 283 | if (streamType == AudioManager.STREAM_RING |
| 284 | || streamType == AudioManager.STREAM_VOICE_CALL |
Eric Laurent | e2ad901 | 2011-02-17 17:31:51 -0800 | [diff] [blame] | 285 | || streamType == AudioManager.STREAM_BLUETOOTH_SCO |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 286 | || streamType == mActiveStreamType) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | StreamControl sc = mStreamControls.get(streamType); |
| 290 | mSliderGroup.addView(sc.group); |
| 291 | updateSlider(sc); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** Update the mute and progress state of a slider */ |
| 296 | private void updateSlider(StreamControl sc) { |
| 297 | sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType)); |
| 298 | final boolean muted = isMuted(sc.streamType); |
| 299 | sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes); |
| 300 | sc.seekbarView.setEnabled(!muted); |
| 301 | } |
| 302 | |
| 303 | private boolean isExpanded() { |
| 304 | return mMoreButton.getVisibility() != View.VISIBLE; |
| 305 | } |
| 306 | |
| 307 | private void expand() { |
| 308 | final int count = mSliderGroup.getChildCount(); |
| 309 | for (int i = 0; i < count; i++) { |
| 310 | mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE); |
| 311 | } |
| 312 | mMoreButton.setVisibility(View.INVISIBLE); |
| 313 | mDivider.setVisibility(View.INVISIBLE); |
| 314 | } |
| 315 | |
| 316 | private void collapse() { |
| 317 | mMoreButton.setVisibility(View.VISIBLE); |
| 318 | mDivider.setVisibility(View.VISIBLE); |
| 319 | final int count = mSliderGroup.getChildCount(); |
| 320 | for (int i = 1; i < count; i++) { |
| 321 | mSliderGroup.getChildAt(i).setVisibility(View.GONE); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | private void updateStates() { |
| 326 | final int count = mSliderGroup.getChildCount(); |
| 327 | for (int i = 0; i < count; i++) { |
| 328 | StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag(); |
| 329 | updateSlider(sc); |
| 330 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | public void postVolumeChanged(int streamType, int flags) { |
| 334 | if (hasMessages(MSG_VOLUME_CHANGED)) return; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 335 | if (mStreamControls == null) { |
| 336 | createSliders(); |
| 337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | removeMessages(MSG_FREE_RESOURCES); |
| 339 | obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget(); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Override this if you have other work to do when the volume changes (for |
| 344 | * example, vibrating, playing a sound, etc.). Make sure to call through to |
| 345 | * the superclass implementation. |
| 346 | */ |
| 347 | protected void onVolumeChanged(int streamType, int flags) { |
| 348 | |
| 349 | if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")"); |
| 350 | |
| 351 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0) { |
Amith Yamasani | e3361b8 | 2011-02-10 18:20:50 -0800 | [diff] [blame] | 352 | if (mActiveStreamType == -1) { |
| 353 | reorderSliders(streamType); |
| 354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | onShowVolumeChanged(streamType, flags); |
| 356 | } |
| 357 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 358 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | removeMessages(MSG_PLAY_SOUND); |
| 360 | sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY); |
| 361 | } |
| 362 | |
| 363 | if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) { |
| 364 | removeMessages(MSG_PLAY_SOUND); |
| 365 | removeMessages(MSG_VIBRATE); |
| 366 | onStopSounds(); |
| 367 | } |
| 368 | |
| 369 | removeMessages(MSG_FREE_RESOURCES); |
| 370 | sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 371 | |
| 372 | resetTimeout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | protected void onShowVolumeChanged(int streamType, int flags) { |
Eric Laurent | d72d51c | 2011-02-03 18:47:47 -0800 | [diff] [blame] | 376 | int index = mAudioService.isStreamMute(streamType) ? |
| 377 | mAudioService.getLastAudibleStreamVolume(streamType) |
| 378 | : mAudioService.getStreamVolume(streamType); |
| 379 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 380 | // int message = UNKNOWN_VOLUME_TEXT; |
| 381 | // int additionalMessage = 0; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 382 | mRingIsSilent = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
| 384 | if (LOGD) { |
| 385 | Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType |
| 386 | + ", flags: " + flags + "), index: " + index); |
| 387 | } |
| 388 | |
| 389 | // get max volume for progress bar |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | int max = mAudioService.getStreamMaxVolume(streamType); |
| 392 | |
| 393 | switch (streamType) { |
| 394 | |
| 395 | case AudioManager.STREAM_RING: { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 396 | // setRingerIcon(); |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 397 | Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri( |
| 398 | mContext, RingtoneManager.TYPE_RINGTONE); |
| 399 | if (ringuri == null) { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 400 | mRingIsSilent = true; |
| 401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | |
| 405 | case AudioManager.STREAM_MUSIC: { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 406 | // Special case for when Bluetooth is active for music |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 407 | if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) & |
| 408 | (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP | |
| 409 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 410 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 411 | 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] | 412 | } else { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 413 | 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] | 414 | } |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | case AudioManager.STREAM_VOICE_CALL: { |
| 419 | /* |
| 420 | * For in-call voice call volume, there is no inaudible volume. |
| 421 | * Rescale the UI control so the progress bar doesn't go all |
| 422 | * the way to zero and don't show the mute icon. |
| 423 | */ |
| 424 | index++; |
| 425 | max++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | break; |
| 427 | } |
| 428 | |
| 429 | case AudioManager.STREAM_ALARM: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | |
| 433 | case AudioManager.STREAM_NOTIFICATION: { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 434 | Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri( |
| 435 | mContext, RingtoneManager.TYPE_NOTIFICATION); |
| 436 | if (ringuri == null) { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 437 | mRingIsSilent = true; |
| 438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | |
| 442 | case AudioManager.STREAM_BLUETOOTH_SCO: { |
| 443 | /* |
| 444 | * For in-call voice call volume, there is no inaudible volume. |
| 445 | * Rescale the UI control so the progress bar doesn't go all |
| 446 | * the way to zero and don't show the mute icon. |
| 447 | */ |
| 448 | index++; |
| 449 | max++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 454 | StreamControl sc = mStreamControls.get(streamType); |
| 455 | if (sc != null) { |
| 456 | sc.seekbarView.setProgress(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 459 | if (!mDialog.isShowing()) { |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 460 | mAudioManager.forceVolumeControlStream(streamType); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 461 | mDialog.setContentView(mView); |
| 462 | // Showing dialog - use collapsed state |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 463 | if (mShowCombinedVolumes) { |
| 464 | collapse(); |
| 465 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 466 | mDialog.show(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
| 468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | // Do a little vibrate if applicable (only when going into vibrate mode) |
| 470 | if ((flags & AudioManager.FLAG_VIBRATE) != 0 && |
| 471 | mAudioService.isStreamAffectedByRingerMode(streamType) && |
| 472 | mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE && |
| 473 | mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) { |
| 474 | sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY); |
| 475 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | protected void onPlaySound(int streamType, int flags) { |
| 479 | |
| 480 | if (hasMessages(MSG_STOP_SOUNDS)) { |
| 481 | removeMessages(MSG_STOP_SOUNDS); |
| 482 | // Force stop right now |
| 483 | onStopSounds(); |
| 484 | } |
| 485 | |
| 486 | synchronized (this) { |
| 487 | ToneGenerator toneGen = getOrCreateToneGenerator(streamType); |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 488 | if (toneGen != null) { |
| 489 | toneGen.startTone(ToneGenerator.TONE_PROP_BEEP); |
| 490 | sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION); |
| 491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | protected void onStopSounds() { |
| 496 | |
| 497 | synchronized (this) { |
| 498 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 499 | for (int i = numStreamTypes - 1; i >= 0; i--) { |
| 500 | ToneGenerator toneGen = mToneGenerators[i]; |
| 501 | if (toneGen != null) { |
| 502 | toneGen.stopTone(); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | protected void onVibrate() { |
| 509 | |
| 510 | // Make sure we ended up in vibrate ringer mode |
| 511 | if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) { |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | mVibrator.vibrate(VIBRATE_DURATION); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Lock on this VolumePanel instance as long as you use the returned ToneGenerator. |
| 520 | */ |
| 521 | private ToneGenerator getOrCreateToneGenerator(int streamType) { |
| 522 | synchronized (this) { |
| 523 | if (mToneGenerators[streamType] == null) { |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 524 | try { |
| 525 | mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME); |
| 526 | } catch (RuntimeException e) { |
| 527 | if (LOGD) { |
| 528 | Log.d(TAG, "ToneGenerator constructor failed with " |
| 529 | + "RuntimeException: " + e); |
| 530 | } |
| 531 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | } |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 533 | return mToneGenerators[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 537 | // /** |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 538 | // * Makes the ringer icon visible with an icon that is chosen |
| 539 | // * based on the current ringer mode. |
| 540 | // */ |
| 541 | // private void setRingerIcon() { |
| 542 | // mSmallStreamIcon.setVisibility(View.GONE); |
| 543 | // mLargeStreamIcon.setVisibility(View.VISIBLE); |
| 544 | // |
| 545 | // int ringerMode = mAudioService.getRingerMode(); |
| 546 | // int icon; |
| 547 | // |
| 548 | // if (LOGD) Log.d(TAG, "setRingerIcon(), ringerMode: " + ringerMode); |
| 549 | // |
| 550 | // if (ringerMode == AudioManager.RINGER_MODE_SILENT) { |
| 551 | // icon = com.android.internal.R.drawable.ic_volume_off; |
| 552 | // } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 553 | // icon = com.android.internal.R.drawable.ic_vibrate; |
| 554 | // } else { |
| 555 | // icon = com.android.internal.R.drawable.ic_volume; |
| 556 | // } |
| 557 | // mLargeStreamIcon.setImageResource(icon); |
| 558 | // } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | |
| 560 | /** |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 561 | * Switch between icons because Bluetooth music is same as music volume, but with |
| 562 | * different icons. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | */ |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 564 | private void setMusicIcon(int resId, int resMuteId) { |
| 565 | StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC); |
| 566 | if (sc != null) { |
| 567 | sc.iconRes = resId; |
| 568 | sc.iconMuteRes = resMuteId; |
| 569 | 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] | 570 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | protected void onFreeResources() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | synchronized (this) { |
| 575 | for (int i = mToneGenerators.length - 1; i >= 0; i--) { |
| 576 | if (mToneGenerators[i] != null) { |
| 577 | mToneGenerators[i].release(); |
| 578 | } |
| 579 | mToneGenerators[i] = null; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | public void handleMessage(Message msg) { |
| 586 | switch (msg.what) { |
| 587 | |
| 588 | case MSG_VOLUME_CHANGED: { |
| 589 | onVolumeChanged(msg.arg1, msg.arg2); |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | case MSG_FREE_RESOURCES: { |
| 594 | onFreeResources(); |
| 595 | break; |
| 596 | } |
| 597 | |
| 598 | case MSG_STOP_SOUNDS: { |
| 599 | onStopSounds(); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | case MSG_PLAY_SOUND: { |
| 604 | onPlaySound(msg.arg1, msg.arg2); |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | case MSG_VIBRATE: { |
| 609 | onVibrate(); |
| 610 | break; |
| 611 | } |
| 612 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 613 | case MSG_TIMEOUT: { |
| 614 | if (mDialog.isShowing()) { |
| 615 | mDialog.dismiss(); |
| 616 | mActiveStreamType = -1; |
| 617 | } |
| 618 | break; |
| 619 | } |
| 620 | case MSG_RINGER_MODE_CHANGED: { |
| 621 | if (mDialog.isShowing()) { |
| 622 | updateStates(); |
| 623 | } |
| 624 | break; |
| 625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 629 | private void resetTimeout() { |
| 630 | removeMessages(MSG_TIMEOUT); |
| 631 | sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY); |
| 632 | } |
| 633 | |
| 634 | public void onProgressChanged(SeekBar seekBar, int progress, |
| 635 | boolean fromUser) { |
| 636 | final Object tag = seekBar.getTag(); |
| 637 | if (fromUser && tag instanceof StreamControl) { |
| 638 | StreamControl sc = (StreamControl) tag; |
| 639 | if (mAudioManager.getStreamVolume(sc.streamType) != progress) { |
| 640 | mAudioManager.setStreamVolume(sc.streamType, progress, 0); |
| 641 | } |
| 642 | } |
| 643 | resetTimeout(); |
| 644 | } |
| 645 | |
| 646 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 647 | } |
| 648 | |
| 649 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 650 | } |
| 651 | |
| 652 | public void onClick(View v) { |
| 653 | if (v == mMoreButton) { |
| 654 | expand(); |
| 655 | } else if (v.getTag() instanceof StreamControl) { |
| 656 | StreamControl sc = (StreamControl) v.getTag(); |
| 657 | mAudioManager.setRingerMode(mAudioManager.isSilentMode() |
| 658 | ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT); |
| 659 | // Expand the dialog if it hasn't been expanded yet. |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 660 | if (mShowCombinedVolumes && !isExpanded()) expand(); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 661 | } |
| 662 | resetTimeout(); |
| 663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |