package android.preference;
import android.app.Dialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.SeekBar;
import com.android.internal.R;
public class VolumePreference extends SeekBarDialogPreference implements
PreferenceManager.OnActivityStopListener, View.OnKeyListener, SeekBarVolumizer.Callback {
private int mStreamType;
private SeekBarVolumizer mSeekBarVolumizer;
public VolumePreference(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.VolumePreference, defStyleAttr, defStyleRes);
mStreamType = a.getInt(android.R.styleable.VolumePreference_streamType, 0);
a.recycle();
}
public VolumePreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public VolumePreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.seekBarDialogPreferenceStyle);
}
public VolumePreference(Context context) {
this(context, null);
}
public void setStreamType(int streamType) {
mStreamType = streamType;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
mSeekBarVolumizer = new SeekBarVolumizer(getContext(), mStreamType, null, this);
mSeekBarVolumizer.start();
mSeekBarVolumizer.setSeekBar(seekBar);
getPreferenceManager().registerOnActivityStopListener(this);
view.setOnKeyListener(this);
view.setFocusableInTouchMode(true);
view.requestFocus();
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (mSeekBarVolumizer == null) return true;
boolean isdown = (event.getAction() == KeyEvent.ACTION_DOWN);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (isdown) {
mSeekBarVolumizer.changeVolumeBy(-1);
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (isdown) {
mSeekBarVolumizer.changeVolumeBy(1);
}
return true;
case KeyEvent.KEYCODE_VOLUME_MUTE:
if (isdown) {
mSeekBarVolumizer.muteVolume();
}
return true;
default:
return false;
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult && mSeekBarVolumizer != null) {
mSeekBarVolumizer.revertVolume();
}
cleanup();
}
public void onActivityStop() {
if (mSeekBarVolumizer != null) {
mSeekBarVolumizer.stopSample();
}
}
private void cleanup() {
getPreferenceManager().unregisterOnActivityStopListener(this);
if (mSeekBarVolumizer != null) {
final Dialog dialog = getDialog();
if (dialog != null && dialog.isShowing()) {
final View view = dialog.getWindow().getDecorView().findViewById(R.id.seekbar);
if (view != null) {
view.setOnKeyListener(null);
}
mSeekBarVolumizer.revertVolume();
}
mSeekBarVolumizer.stop();
mSeekBarVolumizer = null;
}
}
@Override
public void onSampleStarting(SeekBarVolumizer volumizer) {
if (mSeekBarVolumizer != null && volumizer != mSeekBarVolumizer) {
mSeekBarVolumizer.stopSample();
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
}
@Override
public void onMuted(boolean muted, boolean zenMuted) {
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
return superState;
}
final SavedState myState = new SavedState(superState);
if (mSeekBarVolumizer != null) {
mSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore());
}
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
if (mSeekBarVolumizer != null) {
mSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore());
}
}
public static class VolumeStore {
public int volume = -1;
public int originalVolume = -1;
}
private static class SavedState extends BaseSavedState {
VolumeStore mVolumeStore = new VolumeStore();
public SavedState(Parcel source) {
super(source);
mVolumeStore.volume = source.readInt();
mVolumeStore.originalVolume = source.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(mVolumeStore.volume);
dest.writeInt(mVolumeStore.originalVolume);
}
VolumeStore getVolumeStore() {
return mVolumeStore;
}
public SavedState(Parcelable superState) {
super(superState);
}
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}