| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.preference; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.TypedArray; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
import android.preference.DialogPreference; |
| 25 |
import android.util.AttributeSet; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class YesNoPreference extends DialogPreference { |
| 34 |
private boolean mWasPositiveResult; |
| 35 |
|
| 36 |
public YesNoPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 37 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 38 |
} |
| 39 |
|
| 40 |
public YesNoPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 41 |
this(context, attrs, defStyleAttr, 0); |
| 42 |
} |
| 43 |
|
| 44 |
@UnsupportedAppUsage |
| 45 |
public YesNoPreference(Context context, AttributeSet attrs) { |
| 46 |
this(context, attrs, com.android.internal.R.attr.yesNoPreferenceStyle); |
| 47 |
} |
| 48 |
|
| 49 |
public YesNoPreference(Context context) { |
| 50 |
this(context, null); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
protected void onDialogClosed(boolean positiveResult) { |
| 55 |
super.onDialogClosed(positiveResult); |
| 56 |
|
| 57 |
if (callChangeListener(positiveResult)) { |
| 58 |
setValue(positiveResult); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public void setValue(boolean value) { |
| 69 |
mWasPositiveResult = value; |
| 70 |
|
| 71 |
persistBoolean(value); |
| 72 |
|
| 73 |
notifyDependencyChange(!value); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public boolean getValue() { |
| 82 |
return mWasPositiveResult; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected Object onGetDefaultValue(TypedArray a, int index) { |
| 87 |
return a.getBoolean(index, false); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { |
| 92 |
setValue(restorePersistedValue ? getPersistedBoolean(mWasPositiveResult) : |
| 93 |
(Boolean) defaultValue); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public boolean shouldDisableDependents() { |
| 98 |
return !mWasPositiveResult || super.shouldDisableDependents(); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
protected Parcelable onSaveInstanceState() { |
| 103 |
final Parcelable superState = super.onSaveInstanceState(); |
| 104 |
if (isPersistent()) { |
| 105 |
|
| 106 |
return superState; |
| 107 |
} |
| 108 |
|
| 109 |
final SavedState myState = new SavedState(superState); |
| 110 |
myState.wasPositiveResult = getValue(); |
| 111 |
return myState; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
protected void onRestoreInstanceState(Parcelable state) { |
| 116 |
if (!state.getClass().equals(SavedState.class)) { |
| 117 |
|
| 118 |
super.onRestoreInstanceState(state); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
SavedState myState = (SavedState) state; |
| 123 |
super.onRestoreInstanceState(myState.getSuperState()); |
| 124 |
setValue(myState.wasPositiveResult); |
| 125 |
} |
| 126 |
|
| 127 |
private static class SavedState extends BaseSavedState { |
| 128 |
boolean wasPositiveResult; |
| 129 |
|
| 130 |
public SavedState(Parcel source) { |
| 131 |
super(source); |
| 132 |
wasPositiveResult = source.readInt() == 1; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void writeToParcel(Parcel dest, int flags) { |
| 137 |
super.writeToParcel(dest, flags); |
| 138 |
dest.writeInt(wasPositiveResult ? 1 : 0); |
| 139 |
} |
| 140 |
|
| 141 |
public SavedState(Parcelable superState) { |
| 142 |
super(superState); |
| 143 |
} |
| 144 |
|
| 145 |
public static final Parcelable.Creator<SavedState> CREATOR = |
| 146 |
new Parcelable.Creator<SavedState>() { |
| 147 |
public SavedState createFromParcel(Parcel in) { |
| 148 |
return new SavedState(in); |
| 149 |
} |
| 150 |
|
| 151 |
public SavedState[] newArray(int size) { |
| 152 |
return new SavedState[size]; |
| 153 |
} |
| 154 |
}; |
| 155 |
} |
| 156 |
|
| 157 |
} |