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