VolumePreference.java revision 6ff5906981fac2aa67c8c1f0df151b536e80ccaf
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 17package android.preference; 18 19import android.app.Dialog; 20import android.content.Context; 21import android.content.res.TypedArray; 22import android.database.ContentObserver; 23import android.media.AudioManager; 24import android.media.Ringtone; 25import android.media.RingtoneManager; 26import android.net.Uri; 27import android.os.Handler; 28import android.os.Parcel; 29import android.os.Parcelable; 30import android.provider.Settings; 31import android.provider.Settings.System; 32import android.util.AttributeSet; 33import android.view.KeyEvent; 34import android.view.View; 35import android.widget.SeekBar; 36import android.widget.SeekBar.OnSeekBarChangeListener; 37 38/** 39 * @hide 40 */ 41public class VolumePreference extends SeekBarPreference implements 42 PreferenceManager.OnActivityStopListener, View.OnKeyListener { 43 44 private static final String TAG = "VolumePreference"; 45 46 private int mStreamType; 47 48 /** May be null if the dialog isn't visible. */ 49 private SeekBarVolumizer mSeekBarVolumizer; 50 51 public VolumePreference(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 54 TypedArray a = context.obtainStyledAttributes(attrs, 55 com.android.internal.R.styleable.VolumePreference, 0, 0); 56 mStreamType = a.getInt(android.R.styleable.VolumePreference_streamType, 0); 57 a.recycle(); 58 } 59 60 public void setStreamType(int streamType) { 61 mStreamType = streamType; 62 } 63 64 @Override 65 protected void onBindDialogView(View view) { 66 super.onBindDialogView(view); 67 68 final SeekBar seekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar); 69 mSeekBarVolumizer = new SeekBarVolumizer(getContext(), seekBar, mStreamType); 70 71 getPreferenceManager().registerOnActivityStopListener(this); 72 73 // grab focus and key events so that pressing the volume buttons in the 74 // dialog doesn't also show the normal volume adjust toast. 75 view.setOnKeyListener(this); 76 view.setFocusableInTouchMode(true); 77 view.requestFocus(); 78 } 79 80 public boolean onKey(View v, int keyCode, KeyEvent event) { 81 // If key arrives immediately after the activity has been cleaned up. 82 if (mSeekBarVolumizer == null) return true; 83 boolean isdown = (event.getAction() == KeyEvent.ACTION_DOWN); 84 switch (keyCode) { 85 case KeyEvent.KEYCODE_VOLUME_DOWN: 86 if (isdown) { 87 mSeekBarVolumizer.changeVolumeBy(-1); 88 } 89 return true; 90 case KeyEvent.KEYCODE_VOLUME_UP: 91 if (isdown) { 92 mSeekBarVolumizer.changeVolumeBy(1); 93 } 94 return true; 95 default: 96 return false; 97 } 98 } 99 100 @Override 101 protected void onDialogClosed(boolean positiveResult) { 102 super.onDialogClosed(positiveResult); 103 104 if (!positiveResult && mSeekBarVolumizer != null) { 105 mSeekBarVolumizer.revertVolume(); 106 } 107 108 cleanup(); 109 } 110 111 public void onActivityStop() { 112 cleanup(); 113 } 114 115 /** 116 * Do clean up. This can be called multiple times! 117 */ 118 private void cleanup() { 119 getPreferenceManager().unregisterOnActivityStopListener(this); 120 121 if (mSeekBarVolumizer != null) { 122 Dialog dialog = getDialog(); 123 if (dialog != null && dialog.isShowing()) { 124 // Stopped while dialog was showing, revert changes 125 mSeekBarVolumizer.revertVolume(); 126 } 127 mSeekBarVolumizer.stop(); 128 mSeekBarVolumizer = null; 129 } 130 131 } 132 133 protected void onSampleStarting(SeekBarVolumizer volumizer) { 134 if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) { 135 mSeekBarVolumizer.stopSample(); 136 } 137 } 138 139 @Override 140 protected Parcelable onSaveInstanceState() { 141 final Parcelable superState = super.onSaveInstanceState(); 142 if (isPersistent()) { 143 // No need to save instance state since it's persistent 144 return superState; 145 } 146 147 final SavedState myState = new SavedState(superState); 148 if (mSeekBarVolumizer != null) { 149 mSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore()); 150 } 151 return myState; 152 } 153 154 @Override 155 protected void onRestoreInstanceState(Parcelable state) { 156 if (state == null || !state.getClass().equals(SavedState.class)) { 157 // Didn't save state for us in onSaveInstanceState 158 super.onRestoreInstanceState(state); 159 return; 160 } 161 162 SavedState myState = (SavedState) state; 163 super.onRestoreInstanceState(myState.getSuperState()); 164 if (mSeekBarVolumizer != null) { 165 mSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore()); 166 } 167 } 168 169 public static class VolumeStore { 170 public int volume = -1; 171 public int originalVolume = -1; 172 } 173 174 private static class SavedState extends BaseSavedState { 175 VolumeStore mVolumeStore = new VolumeStore(); 176 177 public SavedState(Parcel source) { 178 super(source); 179 mVolumeStore.volume = source.readInt(); 180 mVolumeStore.originalVolume = source.readInt(); 181 } 182 183 @Override 184 public void writeToParcel(Parcel dest, int flags) { 185 super.writeToParcel(dest, flags); 186 dest.writeInt(mVolumeStore.volume); 187 dest.writeInt(mVolumeStore.originalVolume); 188 } 189 190 VolumeStore getVolumeStore() { 191 return mVolumeStore; 192 } 193 194 public SavedState(Parcelable superState) { 195 super(superState); 196 } 197 198 public static final Parcelable.Creator<SavedState> CREATOR = 199 new Parcelable.Creator<SavedState>() { 200 public SavedState createFromParcel(Parcel in) { 201 return new SavedState(in); 202 } 203 204 public SavedState[] newArray(int size) { 205 return new SavedState[size]; 206 } 207 }; 208 } 209 210 /** 211 * Turns a {@link SeekBar} into a volume control. 212 */ 213 public class SeekBarVolumizer implements OnSeekBarChangeListener, Runnable { 214 215 private Context mContext; 216 private Handler mHandler = new Handler(); 217 218 private AudioManager mAudioManager; 219 private int mStreamType; 220 private int mOriginalStreamVolume; 221 private Ringtone mRingtone; 222 223 private int mLastProgress = -1; 224 private SeekBar mSeekBar; 225 226 private ContentObserver mVolumeObserver = new ContentObserver(mHandler) { 227 @Override 228 public void onChange(boolean selfChange) { 229 super.onChange(selfChange); 230 231 if (mSeekBar != null) { 232 mSeekBar.setProgress(System.getInt(mContext.getContentResolver(), 233 System.VOLUME_SETTINGS[mStreamType], 0)); 234 } 235 } 236 }; 237 238 public SeekBarVolumizer(Context context, SeekBar seekBar, int streamType) { 239 mContext = context; 240 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 241 mStreamType = streamType; 242 mSeekBar = seekBar; 243 244 initSeekBar(seekBar); 245 } 246 247 private void initSeekBar(SeekBar seekBar) { 248 seekBar.setMax(mAudioManager.getStreamMaxVolume(mStreamType)); 249 mOriginalStreamVolume = mAudioManager.getStreamVolume(mStreamType); 250 seekBar.setProgress(mOriginalStreamVolume); 251 seekBar.setOnSeekBarChangeListener(this); 252 253 mContext.getContentResolver().registerContentObserver( 254 System.getUriFor(System.VOLUME_SETTINGS[mStreamType]), 255 false, mVolumeObserver); 256 257 Uri defaultUri = null; 258 if (mStreamType == AudioManager.STREAM_RING) { 259 defaultUri = Settings.System.DEFAULT_RINGTONE_URI; 260 } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) { 261 defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI; 262 } else { 263 defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI; 264 } 265 266 mRingtone = RingtoneManager.getRingtone(mContext, defaultUri); 267 if (mRingtone != null) { 268 mRingtone.setStreamType(mStreamType); 269 } 270 } 271 272 public void stop() { 273 stopSample(); 274 mContext.getContentResolver().unregisterContentObserver(mVolumeObserver); 275 mSeekBar.setOnSeekBarChangeListener(null); 276 } 277 278 public void revertVolume() { 279 mAudioManager.setStreamVolume(mStreamType, mOriginalStreamVolume, 0); 280 } 281 282 public void onProgressChanged(SeekBar seekBar, int progress, 283 boolean fromTouch) { 284 if (!fromTouch) { 285 return; 286 } 287 288 postSetVolume(progress); 289 } 290 291 void postSetVolume(int progress) { 292 // Do the volume changing separately to give responsive UI 293 mLastProgress = progress; 294 mHandler.removeCallbacks(this); 295 mHandler.post(this); 296 } 297 298 public void onStartTrackingTouch(SeekBar seekBar) { 299 } 300 301 public void onStopTrackingTouch(SeekBar seekBar) { 302 if (mRingtone != null && !mRingtone.isPlaying()) { 303 sample(); 304 } 305 } 306 307 public void run() { 308 mAudioManager.setStreamVolume(mStreamType, mLastProgress, 0); 309 } 310 311 private void sample() { 312 onSampleStarting(this); 313 mRingtone.play(); 314 } 315 316 public void stopSample() { 317 if (mRingtone != null) { 318 mRingtone.stop(); 319 } 320 } 321 322 public SeekBar getSeekBar() { 323 return mSeekBar; 324 } 325 326 public void changeVolumeBy(int amount) { 327 mSeekBar.incrementProgressBy(amount); 328 if (mRingtone != null && !mRingtone.isPlaying()) { 329 sample(); 330 } 331 postSetVolume(mSeekBar.getProgress()); 332 } 333 334 public void onSaveInstanceState(VolumeStore volumeStore) { 335 if (mLastProgress >= 0) { 336 volumeStore.volume = mLastProgress; 337 volumeStore.originalVolume = mOriginalStreamVolume; 338 } 339 } 340 341 public void onRestoreInstanceState(VolumeStore volumeStore) { 342 if (volumeStore.volume != -1) { 343 mOriginalStreamVolume = volumeStore.originalVolume; 344 mLastProgress = volumeStore.volume; 345 postSetVolume(mLastProgress); 346 } 347 } 348 } 349} 350