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