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()]; |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 266 | mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); |
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) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 299 | return mAudioManager.isMasterMute(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 300 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 301 | return mAudioManager.isStreamMute(streamType); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 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) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 307 | return mAudioManager.getMasterMaxVolume(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 308 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 309 | return mAudioManager.getStreamMaxVolume(streamType); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 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) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 315 | return mAudioManager.getMasterVolume(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 316 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 317 | return mAudioManager.getStreamVolume(streamType); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 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) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 323 | mAudioManager.setMasterVolume(index, flags); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 324 | } else { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 325 | mAudioManager.setStreamVolume(streamType, index, flags); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 326 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | private void createSliders() { |
| 330 | LayoutInflater inflater = (LayoutInflater) mContext |
| 331 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 332 | mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length); |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 333 | Resources res = mContext.getResources(); |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 334 | for (int i = 0; i < STREAMS.length; i++) { |
| 335 | StreamResources streamRes = STREAMS[i]; |
| 336 | int streamType = streamRes.streamType; |
| 337 | if (mVoiceCapable && streamRes == StreamResources.NotificationStream) { |
| 338 | streamRes = StreamResources.RingerStream; |
| 339 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 340 | StreamControl sc = new StreamControl(); |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 341 | sc.streamType = streamType; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 342 | sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null); |
| 343 | sc.group.setTag(sc); |
| 344 | sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 345 | sc.icon.setTag(sc); |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 346 | sc.icon.setContentDescription(res.getString(streamRes.descRes)); |
| 347 | sc.iconRes = streamRes.iconRes; |
| 348 | sc.iconMuteRes = streamRes.iconMuteRes; |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 349 | sc.icon.setImageResource(sc.iconRes); |
| 350 | sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar); |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 351 | int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO || |
| 352 | streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0; |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 353 | sc.seekbarView.setMax(getStreamMaxVolume(streamType) + plusOne); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 354 | sc.seekbarView.setOnSeekBarChangeListener(this); |
| 355 | sc.seekbarView.setTag(sc); |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 356 | mStreamControls.put(streamType, sc); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | private void reorderSliders(int activeStreamType) { |
| 361 | mSliderGroup.removeAllViews(); |
| 362 | |
| 363 | StreamControl active = mStreamControls.get(activeStreamType); |
| 364 | if (active == null) { |
| 365 | Log.e("VolumePanel", "Missing stream type! - " + activeStreamType); |
| 366 | mActiveStreamType = -1; |
| 367 | } else { |
| 368 | mSliderGroup.addView(active.group); |
| 369 | mActiveStreamType = activeStreamType; |
| 370 | active.group.setVisibility(View.VISIBLE); |
| 371 | updateSlider(active); |
| 372 | } |
| 373 | |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 374 | addOtherVolumes(); |
| 375 | } |
| 376 | |
| 377 | private void addOtherVolumes() { |
| 378 | if (!mShowCombinedVolumes) return; |
| 379 | |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 380 | for (int i = 0; i < STREAMS.length; i++) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 381 | // Skip the phone specific ones and the active one |
Amith Yamasani | 71def77 | 2011-10-12 12:25:24 -0700 | [diff] [blame] | 382 | final int streamType = STREAMS[i].streamType; |
| 383 | if (!STREAMS[i].show || streamType == mActiveStreamType) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | StreamControl sc = mStreamControls.get(streamType); |
| 387 | mSliderGroup.addView(sc.group); |
| 388 | updateSlider(sc); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** Update the mute and progress state of a slider */ |
| 393 | private void updateSlider(StreamControl sc) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 394 | sc.seekbarView.setProgress(getStreamVolume(sc.streamType)); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 395 | final boolean muted = isMuted(sc.streamType); |
| 396 | sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes); |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 397 | if (sc.streamType == AudioManager.STREAM_RING && |
| 398 | mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) { |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 399 | sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate); |
| 400 | } |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 401 | if (sc.streamType != mAudioManager.getMasterStreamType() && muted) { |
| 402 | sc.seekbarView.setEnabled(false); |
| 403 | } else { |
| 404 | sc.seekbarView.setEnabled(true); |
| 405 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | private boolean isExpanded() { |
| 409 | return mMoreButton.getVisibility() != View.VISIBLE; |
| 410 | } |
| 411 | |
| 412 | private void expand() { |
| 413 | final int count = mSliderGroup.getChildCount(); |
| 414 | for (int i = 0; i < count; i++) { |
| 415 | mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE); |
| 416 | } |
| 417 | mMoreButton.setVisibility(View.INVISIBLE); |
| 418 | mDivider.setVisibility(View.INVISIBLE); |
| 419 | } |
| 420 | |
| 421 | private void collapse() { |
| 422 | mMoreButton.setVisibility(View.VISIBLE); |
| 423 | mDivider.setVisibility(View.VISIBLE); |
| 424 | final int count = mSliderGroup.getChildCount(); |
| 425 | for (int i = 1; i < count; i++) { |
| 426 | mSliderGroup.getChildAt(i).setVisibility(View.GONE); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | private void updateStates() { |
| 431 | final int count = mSliderGroup.getChildCount(); |
| 432 | for (int i = 0; i < count; i++) { |
| 433 | StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag(); |
| 434 | updateSlider(sc); |
| 435 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | public void postVolumeChanged(int streamType, int flags) { |
| 439 | if (hasMessages(MSG_VOLUME_CHANGED)) return; |
Amith Yamasani | a654986 | 2012-05-30 17:29:28 -0700 | [diff] [blame^] | 440 | synchronized (this) { |
| 441 | if (mStreamControls == null) { |
| 442 | createSliders(); |
| 443 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 444 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | removeMessages(MSG_FREE_RESOURCES); |
| 446 | obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget(); |
| 447 | } |
| 448 | |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 449 | public void postMasterVolumeChanged(int flags) { |
| 450 | postVolumeChanged(STREAM_MASTER, flags); |
| 451 | } |
| 452 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 453 | public void postMuteChanged(int streamType, int flags) { |
| 454 | if (hasMessages(MSG_VOLUME_CHANGED)) return; |
Amith Yamasani | a654986 | 2012-05-30 17:29:28 -0700 | [diff] [blame^] | 455 | synchronized (this) { |
| 456 | if (mStreamControls == null) { |
| 457 | createSliders(); |
| 458 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 459 | } |
| 460 | removeMessages(MSG_FREE_RESOURCES); |
| 461 | obtainMessage(MSG_MUTE_CHANGED, streamType, flags).sendToTarget(); |
| 462 | } |
| 463 | |
| 464 | public void postMasterMuteChanged(int flags) { |
| 465 | postMuteChanged(STREAM_MASTER, flags); |
| 466 | } |
| 467 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | /** |
| 469 | * Override this if you have other work to do when the volume changes (for |
| 470 | * example, vibrating, playing a sound, etc.). Make sure to call through to |
| 471 | * the superclass implementation. |
| 472 | */ |
| 473 | protected void onVolumeChanged(int streamType, int flags) { |
| 474 | |
| 475 | if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")"); |
| 476 | |
| 477 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0) { |
Amith Yamasani | a654986 | 2012-05-30 17:29:28 -0700 | [diff] [blame^] | 478 | synchronized (this) { |
| 479 | if (mActiveStreamType != streamType) { |
| 480 | reorderSliders(streamType); |
| 481 | } |
| 482 | onShowVolumeChanged(streamType, flags); |
Amith Yamasani | e3361b8 | 2011-02-10 18:20:50 -0800 | [diff] [blame] | 483 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 486 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | removeMessages(MSG_PLAY_SOUND); |
| 488 | sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY); |
| 489 | } |
| 490 | |
| 491 | if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) { |
| 492 | removeMessages(MSG_PLAY_SOUND); |
| 493 | removeMessages(MSG_VIBRATE); |
| 494 | onStopSounds(); |
| 495 | } |
| 496 | |
| 497 | removeMessages(MSG_FREE_RESOURCES); |
| 498 | sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 499 | |
| 500 | resetTimeout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 503 | protected void onMuteChanged(int streamType, int flags) { |
| 504 | |
| 505 | if (LOGD) Log.d(TAG, "onMuteChanged(streamType: " + streamType + ", flags: " + flags + ")"); |
| 506 | |
| 507 | StreamControl sc = mStreamControls.get(streamType); |
| 508 | if (sc != null) { |
| 509 | sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes); |
| 510 | } |
| 511 | |
| 512 | onVolumeChanged(streamType, flags); |
| 513 | } |
| 514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | protected void onShowVolumeChanged(int streamType, int flags) { |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 516 | int index = getStreamVolume(streamType); |
Eric Laurent | d72d51c | 2011-02-03 18:47:47 -0800 | [diff] [blame] | 517 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 518 | mRingIsSilent = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | |
| 520 | if (LOGD) { |
| 521 | Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType |
| 522 | + ", flags: " + flags + "), index: " + index); |
| 523 | } |
| 524 | |
| 525 | // get max volume for progress bar |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 526 | |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 527 | int max = getStreamMaxVolume(streamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | |
| 529 | switch (streamType) { |
| 530 | |
| 531 | case AudioManager.STREAM_RING: { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 532 | // setRingerIcon(); |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 533 | Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri( |
| 534 | mContext, RingtoneManager.TYPE_RINGTONE); |
| 535 | if (ringuri == null) { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 536 | mRingIsSilent = true; |
| 537 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | |
| 541 | case AudioManager.STREAM_MUSIC: { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 542 | // Special case for when Bluetooth is active for music |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 543 | if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) & |
| 544 | (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP | |
| 545 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 546 | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 547 | 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] | 548 | } else { |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 549 | 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] | 550 | } |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | case AudioManager.STREAM_VOICE_CALL: { |
| 555 | /* |
| 556 | * For in-call voice call volume, there is no inaudible volume. |
| 557 | * Rescale the UI control so the progress bar doesn't go all |
| 558 | * the way to zero and don't show the mute icon. |
| 559 | */ |
| 560 | index++; |
| 561 | max++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | break; |
| 563 | } |
| 564 | |
| 565 | case AudioManager.STREAM_ALARM: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
| 569 | case AudioManager.STREAM_NOTIFICATION: { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 570 | Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri( |
| 571 | mContext, RingtoneManager.TYPE_NOTIFICATION); |
| 572 | if (ringuri == null) { |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 573 | mRingIsSilent = true; |
| 574 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | |
| 578 | case AudioManager.STREAM_BLUETOOTH_SCO: { |
| 579 | /* |
| 580 | * For in-call voice call volume, there is no inaudible volume. |
| 581 | * Rescale the UI control so the progress bar doesn't go all |
| 582 | * the way to zero and don't show the mute icon. |
| 583 | */ |
| 584 | index++; |
| 585 | max++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | break; |
| 587 | } |
| 588 | } |
| 589 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 590 | StreamControl sc = mStreamControls.get(streamType); |
| 591 | if (sc != null) { |
Amith Yamasani | d47a3aee | 2011-08-23 11:11:35 -0700 | [diff] [blame] | 592 | if (sc.seekbarView.getMax() != max) { |
| 593 | sc.seekbarView.setMax(max); |
| 594 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 595 | sc.seekbarView.setProgress(index); |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 596 | if (streamType != mAudioManager.getMasterStreamType() && isMuted(streamType)) { |
| 597 | sc.seekbarView.setEnabled(false); |
| 598 | } else { |
| 599 | sc.seekbarView.setEnabled(true); |
| 600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 603 | if (!mDialog.isShowing()) { |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 604 | mAudioManager.forceVolumeControlStream(streamType); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 605 | mDialog.setContentView(mView); |
| 606 | // Showing dialog - use collapsed state |
Amith Yamasani | 42722bf | 2011-07-22 10:34:27 -0700 | [diff] [blame] | 607 | if (mShowCombinedVolumes) { |
| 608 | collapse(); |
| 609 | } |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 610 | mDialog.show(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | // Do a little vibrate if applicable (only when going into vibrate mode) |
| 614 | if ((flags & AudioManager.FLAG_VIBRATE) != 0 && |
| 615 | mAudioService.isStreamAffectedByRingerMode(streamType) && |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 616 | mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY); |
| 618 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | protected void onPlaySound(int streamType, int flags) { |
| 622 | |
| 623 | if (hasMessages(MSG_STOP_SOUNDS)) { |
| 624 | removeMessages(MSG_STOP_SOUNDS); |
| 625 | // Force stop right now |
| 626 | onStopSounds(); |
| 627 | } |
| 628 | |
| 629 | synchronized (this) { |
| 630 | ToneGenerator toneGen = getOrCreateToneGenerator(streamType); |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 631 | if (toneGen != null) { |
| 632 | toneGen.startTone(ToneGenerator.TONE_PROP_BEEP); |
| 633 | sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION); |
| 634 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | protected void onStopSounds() { |
| 639 | |
| 640 | synchronized (this) { |
| 641 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 642 | for (int i = numStreamTypes - 1; i >= 0; i--) { |
| 643 | ToneGenerator toneGen = mToneGenerators[i]; |
| 644 | if (toneGen != null) { |
| 645 | toneGen.stopTone(); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | protected void onVibrate() { |
| 652 | |
| 653 | // Make sure we ended up in vibrate ringer mode |
Eric Laurent | 8c78752 | 2012-05-14 14:09:43 -0700 | [diff] [blame] | 654 | if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | |
| 658 | mVibrator.vibrate(VIBRATE_DURATION); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Lock on this VolumePanel instance as long as you use the returned ToneGenerator. |
| 663 | */ |
| 664 | private ToneGenerator getOrCreateToneGenerator(int streamType) { |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 665 | if (streamType == STREAM_MASTER) return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | synchronized (this) { |
| 667 | if (mToneGenerators[streamType] == null) { |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 668 | try { |
| 669 | mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME); |
| 670 | } catch (RuntimeException e) { |
| 671 | if (LOGD) { |
| 672 | Log.d(TAG, "ToneGenerator constructor failed with " |
| 673 | + "RuntimeException: " + e); |
| 674 | } |
| 675 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | } |
Eric Laurent | 733a42b | 2011-01-19 10:41:57 -0800 | [diff] [blame] | 677 | return mToneGenerators[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | |
| 682 | /** |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 683 | * Switch between icons because Bluetooth music is same as music volume, but with |
| 684 | * different icons. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | */ |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 686 | private void setMusicIcon(int resId, int resMuteId) { |
| 687 | StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC); |
| 688 | if (sc != null) { |
| 689 | sc.iconRes = resId; |
| 690 | sc.iconMuteRes = resMuteId; |
| 691 | 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] | 692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | protected void onFreeResources() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | synchronized (this) { |
| 697 | for (int i = mToneGenerators.length - 1; i >= 0; i--) { |
| 698 | if (mToneGenerators[i] != null) { |
| 699 | mToneGenerators[i].release(); |
| 700 | } |
| 701 | mToneGenerators[i] = null; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | @Override |
| 707 | public void handleMessage(Message msg) { |
| 708 | switch (msg.what) { |
| 709 | |
| 710 | case MSG_VOLUME_CHANGED: { |
| 711 | onVolumeChanged(msg.arg1, msg.arg2); |
| 712 | break; |
| 713 | } |
| 714 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 715 | case MSG_MUTE_CHANGED: { |
| 716 | onMuteChanged(msg.arg1, msg.arg2); |
| 717 | break; |
| 718 | } |
| 719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | case MSG_FREE_RESOURCES: { |
| 721 | onFreeResources(); |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | case MSG_STOP_SOUNDS: { |
| 726 | onStopSounds(); |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | case MSG_PLAY_SOUND: { |
| 731 | onPlaySound(msg.arg1, msg.arg2); |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | case MSG_VIBRATE: { |
| 736 | onVibrate(); |
| 737 | break; |
| 738 | } |
| 739 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 740 | case MSG_TIMEOUT: { |
| 741 | if (mDialog.isShowing()) { |
| 742 | mDialog.dismiss(); |
| 743 | mActiveStreamType = -1; |
| 744 | } |
| 745 | break; |
| 746 | } |
| 747 | case MSG_RINGER_MODE_CHANGED: { |
| 748 | if (mDialog.isShowing()) { |
| 749 | updateStates(); |
| 750 | } |
| 751 | break; |
| 752 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 756 | private void resetTimeout() { |
| 757 | removeMessages(MSG_TIMEOUT); |
| 758 | sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY); |
| 759 | } |
| 760 | |
Amith Yamasani | baf6dbf | 2011-08-18 17:40:29 -0700 | [diff] [blame] | 761 | private void forceTimeout() { |
| 762 | removeMessages(MSG_TIMEOUT); |
| 763 | sendMessage(obtainMessage(MSG_TIMEOUT)); |
| 764 | } |
| 765 | |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 766 | public void onProgressChanged(SeekBar seekBar, int progress, |
| 767 | boolean fromUser) { |
| 768 | final Object tag = seekBar.getTag(); |
| 769 | if (fromUser && tag instanceof StreamControl) { |
| 770 | StreamControl sc = (StreamControl) tag; |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 771 | if (getStreamVolume(sc.streamType) != progress) { |
| 772 | setStreamVolume(sc.streamType, progress, 0); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | resetTimeout(); |
| 776 | } |
| 777 | |
| 778 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 779 | } |
| 780 | |
| 781 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 782 | } |
| 783 | |
| 784 | public void onClick(View v) { |
| 785 | if (v == mMoreButton) { |
| 786 | expand(); |
Amith Yamasani | 2bbdd77 | 2011-02-02 18:54:13 -0800 | [diff] [blame] | 787 | } |
| 788 | resetTimeout(); |
| 789 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | } |