blob: a2d5a2312047b91d6588773afc8ebdfceca2112b [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
Mathew Inwoodf2217132018-08-17 13:41:55 +010019import android.annotation.UnsupportedAppUsage;
Amith Yamasani9b4742c2009-08-24 17:28:17 -070020import android.app.Dialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.res.TypedArray;
Amith Yamasani9b4742c2009-08-24 17:28:17 -070023import android.os.Parcel;
24import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.util.AttributeSet;
Marco Nelissen69f593c2009-07-28 09:55:04 -070026import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.view.View;
28import android.widget.SeekBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Alan Viverette5c3188e2015-06-08 15:59:32 -070030import com.android.internal.R;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
33 * @hide
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000034 *
35 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
36 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
37 * Preference Library</a> for consistent behavior across all devices. For more information on
38 * using the AndroidX Preference Library see
39 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000041@Deprecated
John Reck014fea22011-06-15 16:46:36 -070042public class VolumePreference extends SeekBarDialogPreference implements
John Spurlock74a2e062014-05-16 21:03:29 -040043 PreferenceManager.OnActivityStopListener, View.OnKeyListener, SeekBarVolumizer.Callback {
Mathew Inwoodf2217132018-08-17 13:41:55 +010044 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 private int mStreamType;
Amith Yamasani9b4742c2009-08-24 17:28:17 -070046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 /** May be null if the dialog isn't visible. */
48 private SeekBarVolumizer mSeekBarVolumizer;
John Reck014fea22011-06-15 16:46:36 -070049
Alan Viverette617feb92013-09-09 18:09:13 -070050 public VolumePreference(
51 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
52 super(context, attrs, defStyleAttr, defStyleRes);
John Reck014fea22011-06-15 16:46:36 -070053
Alan Viverette617feb92013-09-09 18:09:13 -070054 final TypedArray a = context.obtainStyledAttributes(attrs,
Alan Viverette5c3188e2015-06-08 15:59:32 -070055 R.styleable.VolumePreference, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 mStreamType = a.getInt(android.R.styleable.VolumePreference_streamType, 0);
Amith Yamasani998127c2011-01-31 20:28:03 -080057 a.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -070059
Alan Viverette617feb92013-09-09 18:09:13 -070060 public VolumePreference(Context context, AttributeSet attrs, int defStyleAttr) {
61 this(context, attrs, defStyleAttr, 0);
62 }
63
Mathew Inwoodf2217132018-08-17 13:41:55 +010064 @UnsupportedAppUsage
Alan Viverette617feb92013-09-09 18:09:13 -070065 public VolumePreference(Context context, AttributeSet attrs) {
Alan Viverette5c3188e2015-06-08 15:59:32 -070066 this(context, attrs, R.attr.seekBarDialogPreferenceStyle);
67 }
68
69 public VolumePreference(Context context) {
70 this(context, null);
Alan Viverette617feb92013-09-09 18:09:13 -070071 }
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 public void setStreamType(int streamType) {
74 mStreamType = streamType;
75 }
76
77 @Override
78 protected void onBindDialogView(View view) {
79 super.onBindDialogView(view);
John Reck014fea22011-06-15 16:46:36 -070080
Alan Viverette5c3188e2015-06-08 15:59:32 -070081 final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
John Spurlock95caba12014-05-27 13:10:38 -040082 mSeekBarVolumizer = new SeekBarVolumizer(getContext(), mStreamType, null, this);
John Spurlock0e588ea2014-10-08 12:33:22 -040083 mSeekBarVolumizer.start();
John Spurlock95caba12014-05-27 13:10:38 -040084 mSeekBarVolumizer.setSeekBar(seekBar);
Amith Yamasani9b4742c2009-08-24 17:28:17 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 getPreferenceManager().registerOnActivityStopListener(this);
Marco Nelissen69f593c2009-07-28 09:55:04 -070087
88 // grab focus and key events so that pressing the volume buttons in the
89 // dialog doesn't also show the normal volume adjust toast.
90 view.setOnKeyListener(this);
91 view.setFocusableInTouchMode(true);
92 view.requestFocus();
93 }
94
95 public boolean onKey(View v, int keyCode, KeyEvent event) {
Amith Yamasani6ff59062009-08-25 15:40:14 -070096 // If key arrives immediately after the activity has been cleaned up.
97 if (mSeekBarVolumizer == null) return true;
Marco Nelissen69f593c2009-07-28 09:55:04 -070098 boolean isdown = (event.getAction() == KeyEvent.ACTION_DOWN);
99 switch (keyCode) {
100 case KeyEvent.KEYCODE_VOLUME_DOWN:
101 if (isdown) {
102 mSeekBarVolumizer.changeVolumeBy(-1);
103 }
104 return true;
105 case KeyEvent.KEYCODE_VOLUME_UP:
106 if (isdown) {
107 mSeekBarVolumizer.changeVolumeBy(1);
108 }
109 return true;
Jeff Brownb0418da2010-11-01 15:24:01 -0700110 case KeyEvent.KEYCODE_VOLUME_MUTE:
111 if (isdown) {
112 mSeekBarVolumizer.muteVolume();
113 }
114 return true;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700115 default:
116 return false;
117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
119
120 @Override
121 protected void onDialogClosed(boolean positiveResult) {
122 super.onDialogClosed(positiveResult);
John Reck014fea22011-06-15 16:46:36 -0700123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 if (!positiveResult && mSeekBarVolumizer != null) {
125 mSeekBarVolumizer.revertVolume();
126 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 cleanup();
129 }
130
131 public void onActivityStop() {
Samuel Asteberga4588682011-02-03 14:30:58 +0100132 if (mSeekBarVolumizer != null) {
John Spurlock735f9eb2014-10-21 14:23:24 -0400133 mSeekBarVolumizer.stopSample();
Samuel Asteberga4588682011-02-03 14:30:58 +0100134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
137 /**
138 * Do clean up. This can be called multiple times!
139 */
140 private void cleanup() {
141 getPreferenceManager().unregisterOnActivityStopListener(this);
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 if (mSeekBarVolumizer != null) {
Alan Viverette5c3188e2015-06-08 15:59:32 -0700144 final Dialog dialog = getDialog();
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700145 if (dialog != null && dialog.isShowing()) {
Alan Viverette5c3188e2015-06-08 15:59:32 -0700146 final View view = dialog.getWindow().getDecorView().findViewById(R.id.seekbar);
147 if (view != null) {
148 view.setOnKeyListener(null);
149 }
150
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700151 // Stopped while dialog was showing, revert changes
152 mSeekBarVolumizer.revertVolume();
153 }
Alan Viverette5c3188e2015-06-08 15:59:32 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 mSeekBarVolumizer.stop();
156 mSeekBarVolumizer = null;
157 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700160
John Spurlock74a2e062014-05-16 21:03:29 -0400161 @Override
162 public void onSampleStarting(SeekBarVolumizer volumizer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) {
164 mSeekBarVolumizer.stopSample();
165 }
166 }
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700167
168 @Override
John Spurlockbcc10872014-11-28 15:29:21 -0500169 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
170 // noop
171 }
172
173 @Override
John Spurlock96d4a9e2015-06-10 17:49:35 -0400174 public void onMuted(boolean muted, boolean zenMuted) {
John Spurlockbcc10872014-11-28 15:29:21 -0500175 // noop
176 }
177
178 @Override
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700179 protected Parcelable onSaveInstanceState() {
180 final Parcelable superState = super.onSaveInstanceState();
181 if (isPersistent()) {
182 // No need to save instance state since it's persistent
183 return superState;
184 }
185
186 final SavedState myState = new SavedState(superState);
187 if (mSeekBarVolumizer != null) {
188 mSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore());
189 }
190 return myState;
191 }
192
193 @Override
194 protected void onRestoreInstanceState(Parcelable state) {
195 if (state == null || !state.getClass().equals(SavedState.class)) {
196 // Didn't save state for us in onSaveInstanceState
197 super.onRestoreInstanceState(state);
198 return;
199 }
200
201 SavedState myState = (SavedState) state;
202 super.onRestoreInstanceState(myState.getSuperState());
203 if (mSeekBarVolumizer != null) {
204 mSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore());
205 }
206 }
207
208 public static class VolumeStore {
Mathew Inwoodf2217132018-08-17 13:41:55 +0100209 @UnsupportedAppUsage
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700210 public int volume = -1;
Mathew Inwoodf2217132018-08-17 13:41:55 +0100211 @UnsupportedAppUsage
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700212 public int originalVolume = -1;
213 }
214
215 private static class SavedState extends BaseSavedState {
216 VolumeStore mVolumeStore = new VolumeStore();
217
218 public SavedState(Parcel source) {
219 super(source);
220 mVolumeStore.volume = source.readInt();
221 mVolumeStore.originalVolume = source.readInt();
222 }
223
224 @Override
225 public void writeToParcel(Parcel dest, int flags) {
226 super.writeToParcel(dest, flags);
227 dest.writeInt(mVolumeStore.volume);
228 dest.writeInt(mVolumeStore.originalVolume);
229 }
230
231 VolumeStore getVolumeStore() {
232 return mVolumeStore;
233 }
234
235 public SavedState(Parcelable superState) {
236 super(superState);
237 }
238
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700239 public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
Amith Yamasani9b4742c2009-08-24 17:28:17 -0700240 new Parcelable.Creator<SavedState>() {
241 public SavedState createFromParcel(Parcel in) {
242 return new SavedState(in);
243 }
244
245 public SavedState[] newArray(int size) {
246 return new SavedState[size];
247 }
248 };
249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250}