VolumePreference.java revision b0418da0e7594a8c2164a46985c5f1993632e010
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            case KeyEvent.KEYCODE_VOLUME_MUTE:
96                if (isdown) {
97                    mSeekBarVolumizer.muteVolume();
98                }
99                return true;
100            default:
101                return false;
102        }
103    }
104
105    @Override
106    protected void onDialogClosed(boolean positiveResult) {
107        super.onDialogClosed(positiveResult);
108
109        if (!positiveResult && mSeekBarVolumizer != null) {
110            mSeekBarVolumizer.revertVolume();
111        }
112
113        cleanup();
114    }
115
116    public void onActivityStop() {
117        cleanup();
118    }
119
120    /**
121     * Do clean up.  This can be called multiple times!
122     */
123    private void cleanup() {
124       getPreferenceManager().unregisterOnActivityStopListener(this);
125
126       if (mSeekBarVolumizer != null) {
127           Dialog dialog = getDialog();
128           if (dialog != null && dialog.isShowing()) {
129               View view = dialog.getWindow().getDecorView()
130                       .findViewById(com.android.internal.R.id.seekbar);
131               if (view != null) view.setOnKeyListener(null);
132               // Stopped while dialog was showing, revert changes
133               mSeekBarVolumizer.revertVolume();
134           }
135           mSeekBarVolumizer.stop();
136           mSeekBarVolumizer = null;
137       }
138
139    }
140
141    protected void onSampleStarting(SeekBarVolumizer volumizer) {
142        if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) {
143            mSeekBarVolumizer.stopSample();
144        }
145    }
146
147    @Override
148    protected Parcelable onSaveInstanceState() {
149        final Parcelable superState = super.onSaveInstanceState();
150        if (isPersistent()) {
151            // No need to save instance state since it's persistent
152            return superState;
153        }
154
155        final SavedState myState = new SavedState(superState);
156        if (mSeekBarVolumizer != null) {
157            mSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore());
158        }
159        return myState;
160    }
161
162    @Override
163    protected void onRestoreInstanceState(Parcelable state) {
164        if (state == null || !state.getClass().equals(SavedState.class)) {
165            // Didn't save state for us in onSaveInstanceState
166            super.onRestoreInstanceState(state);
167            return;
168        }
169
170        SavedState myState = (SavedState) state;
171        super.onRestoreInstanceState(myState.getSuperState());
172        if (mSeekBarVolumizer != null) {
173            mSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore());
174        }
175    }
176
177    public static class VolumeStore {
178        public int volume = -1;
179        public int originalVolume = -1;
180    }
181
182    private static class SavedState extends BaseSavedState {
183        VolumeStore mVolumeStore = new VolumeStore();
184
185        public SavedState(Parcel source) {
186            super(source);
187            mVolumeStore.volume = source.readInt();
188            mVolumeStore.originalVolume = source.readInt();
189        }
190
191        @Override
192        public void writeToParcel(Parcel dest, int flags) {
193            super.writeToParcel(dest, flags);
194            dest.writeInt(mVolumeStore.volume);
195            dest.writeInt(mVolumeStore.originalVolume);
196        }
197
198        VolumeStore getVolumeStore() {
199            return mVolumeStore;
200        }
201
202        public SavedState(Parcelable superState) {
203            super(superState);
204        }
205
206        public static final Parcelable.Creator<SavedState> CREATOR =
207                new Parcelable.Creator<SavedState>() {
208            public SavedState createFromParcel(Parcel in) {
209                return new SavedState(in);
210            }
211
212            public SavedState[] newArray(int size) {
213                return new SavedState[size];
214            }
215        };
216    }
217
218    /**
219     * Turns a {@link SeekBar} into a volume control.
220     */
221    public class SeekBarVolumizer implements OnSeekBarChangeListener, Runnable {
222
223        private Context mContext;
224        private Handler mHandler = new Handler();
225
226        private AudioManager mAudioManager;
227        private int mStreamType;
228        private int mOriginalStreamVolume;
229        private Ringtone mRingtone;
230
231        private int mLastProgress = -1;
232        private SeekBar mSeekBar;
233        private int mVolumeBeforeMute = -1;
234
235        private ContentObserver mVolumeObserver = new ContentObserver(mHandler) {
236            @Override
237            public void onChange(boolean selfChange) {
238                super.onChange(selfChange);
239                if (mSeekBar != null) {
240                    int volume = System.getInt(mContext.getContentResolver(),
241                            System.VOLUME_SETTINGS[mStreamType], -1);
242                    // Works around an atomicity problem with volume updates
243                    // TODO: Fix the actual issue, probably in AudioService
244                    if (volume >= 0) {
245                        mSeekBar.setProgress(volume);
246                    }
247                }
248            }
249        };
250
251        public SeekBarVolumizer(Context context, SeekBar seekBar, int streamType) {
252            mContext = context;
253            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
254            mStreamType = streamType;
255            mSeekBar = seekBar;
256
257            initSeekBar(seekBar);
258        }
259
260        private void initSeekBar(SeekBar seekBar) {
261            seekBar.setMax(mAudioManager.getStreamMaxVolume(mStreamType));
262            mOriginalStreamVolume = mAudioManager.getStreamVolume(mStreamType);
263            seekBar.setProgress(mOriginalStreamVolume);
264            seekBar.setOnSeekBarChangeListener(this);
265
266            mContext.getContentResolver().registerContentObserver(
267                    System.getUriFor(System.VOLUME_SETTINGS[mStreamType]),
268                    false, mVolumeObserver);
269
270            Uri defaultUri = null;
271            if (mStreamType == AudioManager.STREAM_RING) {
272                defaultUri = Settings.System.DEFAULT_RINGTONE_URI;
273            } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) {
274                defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI;
275            } else {
276                defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
277            }
278
279            mRingtone = RingtoneManager.getRingtone(mContext, defaultUri);
280            if (mRingtone != null) {
281                mRingtone.setStreamType(mStreamType);
282            }
283        }
284
285        public void stop() {
286            stopSample();
287            mContext.getContentResolver().unregisterContentObserver(mVolumeObserver);
288            mSeekBar.setOnSeekBarChangeListener(null);
289        }
290
291        public void revertVolume() {
292            mAudioManager.setStreamVolume(mStreamType, mOriginalStreamVolume, 0);
293        }
294
295        public void onProgressChanged(SeekBar seekBar, int progress,
296                boolean fromTouch) {
297            if (!fromTouch) {
298                return;
299            }
300
301            postSetVolume(progress);
302        }
303
304        void postSetVolume(int progress) {
305            // Do the volume changing separately to give responsive UI
306            mLastProgress = progress;
307            mHandler.removeCallbacks(this);
308            mHandler.post(this);
309        }
310
311        public void onStartTrackingTouch(SeekBar seekBar) {
312        }
313
314        public void onStopTrackingTouch(SeekBar seekBar) {
315            if (mRingtone != null && !mRingtone.isPlaying()) {
316                sample();
317            }
318        }
319
320        public void run() {
321            mAudioManager.setStreamVolume(mStreamType, mLastProgress, 0);
322        }
323
324        private void sample() {
325            onSampleStarting(this);
326            mRingtone.play();
327        }
328
329        public void stopSample() {
330            if (mRingtone != null) {
331                mRingtone.stop();
332            }
333        }
334
335        public SeekBar getSeekBar() {
336            return mSeekBar;
337        }
338
339        public void changeVolumeBy(int amount) {
340            mSeekBar.incrementProgressBy(amount);
341            if (mRingtone != null && !mRingtone.isPlaying()) {
342                sample();
343            }
344            postSetVolume(mSeekBar.getProgress());
345            mVolumeBeforeMute = -1;
346        }
347
348        public void muteVolume() {
349            if (mVolumeBeforeMute != -1) {
350                mSeekBar.setProgress(mVolumeBeforeMute);
351                sample();
352                postSetVolume(mVolumeBeforeMute);
353                mVolumeBeforeMute = -1;
354            } else {
355                mVolumeBeforeMute = mSeekBar.getProgress();
356                mSeekBar.setProgress(0);
357                stopSample();
358                postSetVolume(0);
359            }
360        }
361
362        public void onSaveInstanceState(VolumeStore volumeStore) {
363            if (mLastProgress >= 0) {
364                volumeStore.volume = mLastProgress;
365                volumeStore.originalVolume = mOriginalStreamVolume;
366            }
367        }
368
369        public void onRestoreInstanceState(VolumeStore volumeStore) {
370            if (volumeStore.volume != -1) {
371                mOriginalStreamVolume = volumeStore.originalVolume;
372                mLastProgress = volumeStore.volume;
373                postSetVolume(mLastProgress);
374            }
375        }
376    }
377}
378