The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.preference; |
| 18 | |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 20 | import android.app.Dialog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.TypedArray; |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 26 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.widget.SeekBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 30 | import com.android.internal.R; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @hide |
Louis Pullen-Freilich | b9596fa | 2018-11-19 17:40:56 +0000 | [diff] [blame] | 34 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | */ |
Louis Pullen-Freilich | b9596fa | 2018-11-19 17:40:56 +0000 | [diff] [blame] | 41 | @Deprecated |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 42 | public class VolumePreference extends SeekBarDialogPreference implements |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 43 | PreferenceManager.OnActivityStopListener, View.OnKeyListener, SeekBarVolumizer.Callback { |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 44 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | private int mStreamType; |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | /** May be null if the dialog isn't visible. */ |
| 48 | private SeekBarVolumizer mSeekBarVolumizer; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 49 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 50 | public VolumePreference( |
| 51 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 52 | super(context, attrs, defStyleAttr, defStyleRes); |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 53 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 54 | final TypedArray a = context.obtainStyledAttributes(attrs, |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 55 | R.styleable.VolumePreference, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | mStreamType = a.getInt(android.R.styleable.VolumePreference_streamType, 0); |
Amith Yamasani | 998127c | 2011-01-31 20:28:03 -0800 | [diff] [blame] | 57 | a.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 59 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 60 | public VolumePreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 61 | this(context, attrs, defStyleAttr, 0); |
| 62 | } |
| 63 | |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 64 | @UnsupportedAppUsage |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 65 | public VolumePreference(Context context, AttributeSet attrs) { |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 66 | this(context, attrs, R.attr.seekBarDialogPreferenceStyle); |
| 67 | } |
| 68 | |
| 69 | public VolumePreference(Context context) { |
| 70 | this(context, null); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 71 | } |
| 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | public void setStreamType(int streamType) { |
| 74 | mStreamType = streamType; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onBindDialogView(View view) { |
| 79 | super.onBindDialogView(view); |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 80 | |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 81 | final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar); |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 82 | mSeekBarVolumizer = new SeekBarVolumizer(getContext(), mStreamType, null, this); |
John Spurlock | 0e588ea | 2014-10-08 12:33:22 -0400 | [diff] [blame] | 83 | mSeekBarVolumizer.start(); |
John Spurlock | 95caba1 | 2014-05-27 13:10:38 -0400 | [diff] [blame] | 84 | mSeekBarVolumizer.setSeekBar(seekBar); |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | getPreferenceManager().registerOnActivityStopListener(this); |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 87 | |
| 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 Yamasani | 6ff5906 | 2009-08-25 15:40:14 -0700 | [diff] [blame] | 96 | // If key arrives immediately after the activity has been cleaned up. |
| 97 | if (mSeekBarVolumizer == null) return true; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 98 | 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 Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 110 | case KeyEvent.KEYCODE_VOLUME_MUTE: |
| 111 | if (isdown) { |
| 112 | mSeekBarVolumizer.muteVolume(); |
| 113 | } |
| 114 | return true; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 115 | default: |
| 116 | return false; |
| 117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void onDialogClosed(boolean positiveResult) { |
| 122 | super.onDialogClosed(positiveResult); |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | if (!positiveResult && mSeekBarVolumizer != null) { |
| 125 | mSeekBarVolumizer.revertVolume(); |
| 126 | } |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | cleanup(); |
| 129 | } |
| 130 | |
| 131 | public void onActivityStop() { |
Samuel Asteberg | a458868 | 2011-02-03 14:30:58 +0100 | [diff] [blame] | 132 | if (mSeekBarVolumizer != null) { |
John Spurlock | 735f9eb | 2014-10-21 14:23:24 -0400 | [diff] [blame] | 133 | mSeekBarVolumizer.stopSample(); |
Samuel Asteberg | a458868 | 2011-02-03 14:30:58 +0100 | [diff] [blame] | 134 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Do clean up. This can be called multiple times! |
| 139 | */ |
| 140 | private void cleanup() { |
| 141 | getPreferenceManager().unregisterOnActivityStopListener(this); |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | if (mSeekBarVolumizer != null) { |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 144 | final Dialog dialog = getDialog(); |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 145 | if (dialog != null && dialog.isShowing()) { |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 146 | final View view = dialog.getWindow().getDecorView().findViewById(R.id.seekbar); |
| 147 | if (view != null) { |
| 148 | view.setOnKeyListener(null); |
| 149 | } |
| 150 | |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 151 | // Stopped while dialog was showing, revert changes |
| 152 | mSeekBarVolumizer.revertVolume(); |
| 153 | } |
Alan Viverette | 5c3188e | 2015-06-08 15:59:32 -0700 | [diff] [blame] | 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | mSeekBarVolumizer.stop(); |
| 156 | mSeekBarVolumizer = null; |
| 157 | } |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 160 | |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 161 | @Override |
| 162 | public void onSampleStarting(SeekBarVolumizer volumizer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) { |
| 164 | mSeekBarVolumizer.stopSample(); |
| 165 | } |
| 166 | } |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 167 | |
| 168 | @Override |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 169 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { |
| 170 | // noop |
| 171 | } |
| 172 | |
| 173 | @Override |
John Spurlock | 96d4a9e | 2015-06-10 17:49:35 -0400 | [diff] [blame] | 174 | public void onMuted(boolean muted, boolean zenMuted) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 175 | // noop |
| 176 | } |
| 177 | |
| 178 | @Override |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 179 | 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 Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 209 | @UnsupportedAppUsage |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 210 | public int volume = -1; |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 211 | @UnsupportedAppUsage |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 212 | 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 Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 239 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
Amith Yamasani | 9b4742c | 2009-08-24 17:28:17 -0700 | [diff] [blame] | 240 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | } |