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