| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.preference; |
| 18 |
|
| 19 |
import android.app.Dialog; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.res.TypedArray; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.util.AttributeSet; |
| 26 |
import android.view.KeyEvent; |
| 27 |
import android.view.View; |
| 28 |
import android.widget.SeekBar; |
| 29 |
|
| 30 |
import com.android.internal.R; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Deprecated |
| 42 |
public class VolumePreference extends SeekBarDialogPreference implements |
| 43 |
PreferenceManager.OnActivityStopListener, View.OnKeyListener, SeekBarVolumizer.Callback { |
| 44 |
@UnsupportedAppUsage |
| 45 |
private int mStreamType; |
| 46 |
|
| 47 |
|
| 48 |
private SeekBarVolumizer mSeekBarVolumizer; |
| 49 |
|
| 50 |
public VolumePreference( |
| 51 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 52 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 53 |
|
| 54 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 55 |
R.styleable.VolumePreference, defStyleAttr, defStyleRes); |
| 56 |
mStreamType = a.getInt(android.R.styleable.VolumePreference_streamType, 0); |
| 57 |
a.recycle(); |
| 58 |
} |
| 59 |
|
| 60 |
public VolumePreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 61 |
this(context, attrs, defStyleAttr, 0); |
| 62 |
} |
| 63 |
|
| 64 |
@UnsupportedAppUsage |
| 65 |
public VolumePreference(Context context, AttributeSet attrs) { |
| 66 |
this(context, attrs, R.attr.seekBarDialogPreferenceStyle); |
| 67 |
} |
| 68 |
|
| 69 |
public VolumePreference(Context context) { |
| 70 |
this(context, null); |
| 71 |
} |
| 72 |
|
| 73 |
public void setStreamType(int streamType) { |
| 74 |
mStreamType = streamType; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
protected void onBindDialogView(View view) { |
| 79 |
super.onBindDialogView(view); |
| 80 |
|
| 81 |
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar); |
| 82 |
mSeekBarVolumizer = new SeekBarVolumizer(getContext(), mStreamType, null, this); |
| 83 |
mSeekBarVolumizer.start(); |
| 84 |
mSeekBarVolumizer.setSeekBar(seekBar); |
| 85 |
|
| 86 |
getPreferenceManager().registerOnActivityStopListener(this); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
view.setOnKeyListener(this); |
| 91 |
view.setFocusableInTouchMode(true); |
| 92 |
view.requestFocus(); |
| 93 |
} |
| 94 |
|
| 95 |
public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 96 |
|
| 97 |
if (mSeekBarVolumizer == null) return true; |
| 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; |
| 110 |
case KeyEvent.KEYCODE_VOLUME_MUTE: |
| 111 |
if (isdown) { |
| 112 |
mSeekBarVolumizer.muteVolume(); |
| 113 |
} |
| 114 |
return true; |
| 115 |
default: |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
protected void onDialogClosed(boolean positiveResult) { |
| 122 |
super.onDialogClosed(positiveResult); |
| 123 |
|
| 124 |
if (!positiveResult && mSeekBarVolumizer != null) { |
| 125 |
mSeekBarVolumizer.revertVolume(); |
| 126 |
} |
| 127 |
|
| 128 |
cleanup(); |
| 129 |
} |
| 130 |
|
| 131 |
public void onActivityStop() { |
| 132 |
if (mSeekBarVolumizer != null) { |
| 133 |
mSeekBarVolumizer.stopSample(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private void cleanup() { |
| 141 |
getPreferenceManager().unregisterOnActivityStopListener(this); |
| 142 |
|
| 143 |
if (mSeekBarVolumizer != null) { |
| 144 |
final Dialog dialog = getDialog(); |
| 145 |
if (dialog != null && dialog.isShowing()) { |
| 146 |
final View view = dialog.getWindow().getDecorView().findViewById(R.id.seekbar); |
| 147 |
if (view != null) { |
| 148 |
view.setOnKeyListener(null); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
mSeekBarVolumizer.revertVolume(); |
| 153 |
} |
| 154 |
|
| 155 |
mSeekBarVolumizer.stop(); |
| 156 |
mSeekBarVolumizer = null; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void onSampleStarting(SeekBarVolumizer volumizer) { |
| 163 |
if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) { |
| 164 |
mSeekBarVolumizer.stopSample(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void onMuted(boolean muted, boolean zenMuted) { |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onStartTrackingTouch(SeekBarVolumizer sbv) { |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
protected Parcelable onSaveInstanceState() { |
| 185 |
final Parcelable superState = super.onSaveInstanceState(); |
| 186 |
if (isPersistent()) { |
| 187 |
|
| 188 |
return superState; |
| 189 |
} |
| 190 |
|
| 191 |
final SavedState myState = new SavedState(superState); |
| 192 |
if (mSeekBarVolumizer != null) { |
| 193 |
mSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore()); |
| 194 |
} |
| 195 |
return myState; |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
protected void onRestoreInstanceState(Parcelable state) { |
| 200 |
if (state == null || !state.getClass().equals(SavedState.class)) { |
| 201 |
|
| 202 |
super.onRestoreInstanceState(state); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
SavedState myState = (SavedState) state; |
| 207 |
super.onRestoreInstanceState(myState.getSuperState()); |
| 208 |
if (mSeekBarVolumizer != null) { |
| 209 |
mSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore()); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
public static class VolumeStore { |
| 214 |
@UnsupportedAppUsage |
| 215 |
public int volume = -1; |
| 216 |
@UnsupportedAppUsage |
| 217 |
public int originalVolume = -1; |
| 218 |
} |
| 219 |
|
| 220 |
private static class SavedState extends BaseSavedState { |
| 221 |
VolumeStore mVolumeStore = new VolumeStore(); |
| 222 |
|
| 223 |
public SavedState(Parcel source) { |
| 224 |
super(source); |
| 225 |
mVolumeStore.volume = source.readInt(); |
| 226 |
mVolumeStore.originalVolume = source.readInt(); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public void writeToParcel(Parcel dest, int flags) { |
| 231 |
super.writeToParcel(dest, flags); |
| 232 |
dest.writeInt(mVolumeStore.volume); |
| 233 |
dest.writeInt(mVolumeStore.originalVolume); |
| 234 |
} |
| 235 |
|
| 236 |
VolumeStore getVolumeStore() { |
| 237 |
return mVolumeStore; |
| 238 |
} |
| 239 |
|
| 240 |
public SavedState(Parcelable superState) { |
| 241 |
super(superState); |
| 242 |
} |
| 243 |
|
| 244 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 245 |
new Parcelable.Creator<SavedState>() { |
| 246 |
public SavedState createFromParcel(Parcel in) { |
| 247 |
return new SavedState(in); |
| 248 |
} |
| 249 |
|
| 250 |
public SavedState[] newArray(int size) { |
| 251 |
return new SavedState[size]; |
| 252 |
} |
| 253 |
}; |
| 254 |
} |
| 255 |
} |