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