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.isStreamMute(mStreamType) ?
243                            mAudioManager.getLastAudibleStreamVolume(mStreamType)
244                            : mAudioManager.getStreamVolume(mStreamType);
245                    mSeekBar.setProgress(volume);
246                }
247            }
248        };
249
250        public SeekBarVolumizer(Context context, SeekBar seekBar, int streamType) {
251            this(context, seekBar, streamType, null);
252        }
253
254        public SeekBarVolumizer(Context context, SeekBar seekBar, int streamType, Uri defaultUri) {
255            mContext = context;
256            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
257            mStreamType = streamType;
258            mSeekBar = seekBar;
259
260            initSeekBar(seekBar, defaultUri);
261        }
262
263        private void initSeekBar(SeekBar seekBar, Uri defaultUri) {
264            seekBar.setMax(mAudioManager.getStreamMaxVolume(mStreamType));
265            mOriginalStreamVolume = mAudioManager.getStreamVolume(mStreamType);
266            seekBar.setProgress(mOriginalStreamVolume);
267            seekBar.setOnSeekBarChangeListener(this);
268
269            mContext.getContentResolver().registerContentObserver(
270                    System.getUriFor(System.VOLUME_SETTINGS[mStreamType]),
271                    false, mVolumeObserver);
272
273            if (defaultUri == null) {
274                if (mStreamType == AudioManager.STREAM_RING) {
275                    defaultUri = Settings.System.DEFAULT_RINGTONE_URI;
276                } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) {
277                    defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI;
278                } else {
279                    defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
280                }
281            }
282
283            mRingtone = RingtoneManager.getRingtone(mContext, defaultUri);
284
285            if (mRingtone != null) {
286                mRingtone.setStreamType(mStreamType);
287            }
288        }
289
290        public void stop() {
291            stopSample();
292            mContext.getContentResolver().unregisterContentObserver(mVolumeObserver);
293            mSeekBar.setOnSeekBarChangeListener(null);
294        }
295
296        public void revertVolume() {
297            mAudioManager.setStreamVolume(mStreamType, mOriginalStreamVolume, 0);
298        }
299
300        public void onProgressChanged(SeekBar seekBar, int progress,
301                boolean fromTouch) {
302            if (!fromTouch) {
303                return;
304            }
305
306            postSetVolume(progress);
307        }
308
309        void postSetVolume(int progress) {
310            // Do the volume changing separately to give responsive UI
311            mLastProgress = progress;
312            mHandler.removeCallbacks(this);
313            mHandler.post(this);
314        }
315
316        public void onStartTrackingTouch(SeekBar seekBar) {
317        }
318
319        public void onStopTrackingTouch(SeekBar seekBar) {
320            if (!isSamplePlaying()) {
321                startSample();
322            }
323        }
324
325        public void run() {
326            mAudioManager.setStreamVolume(mStreamType, mLastProgress, 0);
327        }
328
329        public boolean isSamplePlaying() {
330            return mRingtone != null && mRingtone.isPlaying();
331        }
332
333        public void startSample() {
334            onSampleStarting(this);
335            if (mRingtone != null) {
336                mRingtone.play();
337            }
338        }
339
340        public void stopSample() {
341            if (mRingtone != null) {
342                mRingtone.stop();
343            }
344        }
345
346        public SeekBar getSeekBar() {
347            return mSeekBar;
348        }
349
350        public void changeVolumeBy(int amount) {
351            mSeekBar.incrementProgressBy(amount);
352            if (!isSamplePlaying()) {
353                startSample();
354            }
355            postSetVolume(mSeekBar.getProgress());
356            mVolumeBeforeMute = -1;
357        }
358
359        public void muteVolume() {
360            if (mVolumeBeforeMute != -1) {
361                mSeekBar.setProgress(mVolumeBeforeMute);
362                startSample();
363                postSetVolume(mVolumeBeforeMute);
364                mVolumeBeforeMute = -1;
365            } else {
366                mVolumeBeforeMute = mSeekBar.getProgress();
367                mSeekBar.setProgress(0);
368                stopSample();
369                postSetVolume(0);
370            }
371        }
372
373        public void onSaveInstanceState(VolumeStore volumeStore) {
374            if (mLastProgress >= 0) {
375                volumeStore.volume = mLastProgress;
376                volumeStore.originalVolume = mOriginalStreamVolume;
377            }
378        }
379
380        public void onRestoreInstanceState(VolumeStore volumeStore) {
381            if (volumeStore.volume != -1) {
382                mOriginalStreamVolume = volumeStore.originalVolume;
383                mLastProgress = volumeStore.volume;
384                postSetVolume(mLastProgress);
385            }
386        }
387    }
388}
389