blob: 8a66c243b17cf5ec035e4c1eb45f4428b29da615 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Amith Yamasani9b4742c2009-08-24 17:28:17 -070019import android.app.Dialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.res.TypedArray;
Amith Yamasani9b4742c2009-08-24 17:28:17 -070022import android.os.Parcel;
23import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.AttributeSet;
Marco Nelissen69f593c2009-07-28 09:55:04 -070025import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.View;
27import android.widget.SeekBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Alan Viverette5c3188e2015-06-08 15:59:32 -070029import com.android.internal.R;
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
32 * @hide
33 */
John Reck014fea22011-06-15 16:46:36 -070034public class VolumePreference extends SeekBarDialogPreference implements
John Spurlock74a2e062014-05-16 21:03:29 -040035 PreferenceManager.OnActivityStopListener, View.OnKeyListener, SeekBarVolumizer.Callback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 private int mStreamType;
Amith Yamasani9b4742c2009-08-24 17:28:17 -070037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 /** May be null if the dialog isn't visible. */
39 private SeekBarVolumizer mSeekBarVolumizer;
John Reck014fea22011-06-15 16:46:36 -070040
Alan Viverette617feb92013-09-09 18:09:13 -070041 public VolumePreference(
42 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
43 super(context, attrs, defStyleAttr, defStyleRes);
John Reck014fea22011-06-15 16:46:36 -070044
Alan Viverette617feb92013-09-09 18:09:13 -070045 final TypedArray a = context.obtainStyledAttributes(attrs,
Alan Viverette5c3188e2015-06-08 15:59:32 -070046 R.styleable.VolumePreference, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 mStreamType = a.getInt(android.R.styleable.VolumePreference_streamType, 0);
Amith Yamasani998127c2011-01-31 20:28:03 -080048 a.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -070050
Alan Viverette617feb92013-09-09 18:09:13 -070051 public VolumePreference(Context context, AttributeSet attrs, int defStyleAttr) {
52 this(context, attrs, defStyleAttr, 0);
53 }
54
55 public VolumePreference(Context context, AttributeSet attrs) {
Alan Viverette5c3188e2015-06-08 15:59:32 -070056 this(context, attrs, R.attr.seekBarDialogPreferenceStyle);
57 }
58
59 public VolumePreference(Context context) {
60 this(context, null);
Alan Viverette617feb92013-09-09 18:09:13 -070061 }
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 public void setStreamType(int streamType) {
64 mStreamType = streamType;
65 }
66
67 @Override
68 protected void onBindDialogView(View view) {
69 super.onBindDialogView(view);
John Reck014fea22011-06-15 16:46:36 -070070
Alan Viverette5c3188e2015-06-08 15:59:32 -070071 final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
John Spurlock95caba12014-05-27 13:10:38 -040072 mSeekBarVolumizer = new SeekBarVolumizer(getContext(), mStreamType, null, this);
John Spurlock0e588ea2014-10-08 12:33:22 -040073 mSeekBarVolumizer.start();
John Spurlock95caba12014-05-27 13:10:38 -040074 mSeekBarVolumizer.setSeekBar(seekBar);
Amith Yamasani9b4742c2009-08-24 17:28:17 -070075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 getPreferenceManager().registerOnActivityStopListener(this);
Marco Nelissen69f593c2009-07-28 09:55:04 -070077
78 // grab focus and key events so that pressing the volume buttons in the
79 // dialog doesn't also show the normal volume adjust toast.
80 view.setOnKeyListener(this);
81 view.setFocusableInTouchMode(true);
82 view.requestFocus();
83 }
84
85 public boolean onKey(View v, int keyCode, KeyEvent event) {
Amith Yamasani6ff59062009-08-25 15:40:14 -070086 // If key arrives immediately after the activity has been cleaned up.
87 if (mSeekBarVolumizer == null) return true;
Marco Nelissen69f593c2009-07-28 09:55:04 -070088 boolean isdown = (event.getAction() == KeyEvent.ACTION_DOWN);
89 switch (keyCode) {
90 case KeyEvent.KEYCODE_VOLUME_DOWN:
91 if (isdown) {
92 mSeekBarVolumizer.changeVolumeBy(-1);
93 }
94 return true;
95 case KeyEvent.KEYCODE_VOLUME_UP:
96 if (isdown) {
97 mSeekBarVolumizer.changeVolumeBy(1);
98 }
99 return true;
Jeff Brownb0418da2010-11-01 15:24:01 -0700100 case KeyEvent.KEYCODE_VOLUME_MUTE:
101 if (isdown) {
102 mSeekBarVolumizer.muteVolume();
103 }
104 return true;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700105 default:
106 return false;
107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 }
109
110 @Override
111 protected void onDialogClosed(boolean positiveResult) {
112 super.onDialogClosed(positiveResult);
John Reck014fea22011-06-15 16:46:36 -0700113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 if (!positiveResult && mSeekBarVolumizer != null) {
115 mSeekBarVolumizer.revertVolume();
116 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 cleanup();
119 }
120
121 public void onActivityStop() {
Samuel Asteberga4588682011-02-03 14:30:58 +0100122 if (mSeekBarVolumizer != null) {
John Spurlock735f9eb2014-10-21 14:23:24 -0400123 mSeekBarVolumizer.stopSample();
Samuel Asteberga4588682011-02-03 14:30:58 +0100124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
126
127 /**
128 * Do clean up. This can be called multiple times!
129 */
130 private void cleanup() {
131 getPreferenceManager().unregisterOnActivityStopListener(this);
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 if (mSeekBarVolumizer != null) {
Alan Viverette5c3188e2015-06-08 15:59:32 -0700134 final Dialog dialog = getDialog();
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700135 if (dialog != null && dialog.isShowing()) {
Alan Viverette5c3188e2015-06-08 15:59:32 -0700136 final View view = dialog.getWindow().getDecorView().findViewById(R.id.seekbar);
137 if (view != null) {
138 view.setOnKeyListener(null);
139 }
140
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700141 // Stopped while dialog was showing, revert changes
142 mSeekBarVolumizer.revertVolume();
143 }
Alan Viverette5c3188e2015-06-08 15:59:32 -0700144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 mSeekBarVolumizer.stop();
146 mSeekBarVolumizer = null;
147 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700150
John Spurlock74a2e062014-05-16 21:03:29 -0400151 @Override
152 public void onSampleStarting(SeekBarVolumizer volumizer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) {
154 mSeekBarVolumizer.stopSample();
155 }
156 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700157
158 @Override
John Spurlockbcc10872014-11-28 15:29:21 -0500159 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
160 // noop
161 }
162
163 @Override
John Spurlock96d4a9e2015-06-10 17:49:35 -0400164 public void onMuted(boolean muted, boolean zenMuted) {
John Spurlockbcc10872014-11-28 15:29:21 -0500165 // noop
166 }
167
168 @Override
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700169 protected Parcelable onSaveInstanceState() {
170 final Parcelable superState = super.onSaveInstanceState();
171 if (isPersistent()) {
172 // No need to save instance state since it's persistent
173 return superState;
174 }
175
176 final SavedState myState = new SavedState(superState);
177 if (mSeekBarVolumizer != null) {
178 mSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore());
179 }
180 return myState;
181 }
182
183 @Override
184 protected void onRestoreInstanceState(Parcelable state) {
185 if (state == null || !state.getClass().equals(SavedState.class)) {
186 // Didn't save state for us in onSaveInstanceState
187 super.onRestoreInstanceState(state);
188 return;
189 }
190
191 SavedState myState = (SavedState) state;
192 super.onRestoreInstanceState(myState.getSuperState());
193 if (mSeekBarVolumizer != null) {
194 mSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore());
195 }
196 }
197
198 public static class VolumeStore {
199 public int volume = -1;
200 public int originalVolume = -1;
201 }
202
203 private static class SavedState extends BaseSavedState {
204 VolumeStore mVolumeStore = new VolumeStore();
205
206 public SavedState(Parcel source) {
207 super(source);
208 mVolumeStore.volume = source.readInt();
209 mVolumeStore.originalVolume = source.readInt();
210 }
211
212 @Override
213 public void writeToParcel(Parcel dest, int flags) {
214 super.writeToParcel(dest, flags);
215 dest.writeInt(mVolumeStore.volume);
216 dest.writeInt(mVolumeStore.originalVolume);
217 }
218
219 VolumeStore getVolumeStore() {
220 return mVolumeStore;
221 }
222
223 public SavedState(Parcelable superState) {
224 super(superState);
225 }
226
227 public static final Parcelable.Creator<SavedState> CREATOR =
228 new Parcelable.Creator<SavedState>() {
229 public SavedState createFromParcel(Parcel in) {
230 return new SavedState(in);
231 }
232
233 public SavedState[] newArray(int size) {
234 return new SavedState[size];
235 }
236 };
237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238}