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