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