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 com.android.internal.preference; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.preference.DialogPreference; |
| 24 | import android.util.AttributeSet; |
| 25 | |
| 26 | /** |
| 27 | * The {@link YesNoPreference} is a preference to show a dialog with Yes and No |
| 28 | * buttons. |
| 29 | * <p> |
| 30 | * This preference will store a boolean into the SharedPreferences. |
| 31 | */ |
| 32 | public class YesNoPreference extends DialogPreference { |
| 33 | private boolean mWasPositiveResult; |
| 34 | |
| 35 | public YesNoPreference(Context context, AttributeSet attrs, int defStyle) { |
| 36 | super(context, attrs, defStyle); |
| 37 | } |
| 38 | |
| 39 | public YesNoPreference(Context context, AttributeSet attrs) { |
| 40 | this(context, attrs, com.android.internal.R.attr.yesNoPreferenceStyle); |
| 41 | } |
| 42 | |
| 43 | public YesNoPreference(Context context) { |
| 44 | this(context, null); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected void onDialogClosed(boolean positiveResult) { |
| 49 | super.onDialogClosed(positiveResult); |
| 50 | |
| 51 | if (callChangeListener(positiveResult)) { |
| 52 | setValue(positiveResult); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the value of this preference, and saves it to the persistent store |
| 58 | * if required. |
| 59 | * |
| 60 | * @param value The value of the preference. |
| 61 | */ |
| 62 | public void setValue(boolean value) { |
| 63 | mWasPositiveResult = value; |
| 64 | |
| 65 | persistBoolean(value); |
| 66 | |
| 67 | notifyDependencyChange(!value); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the value of this preference. |
| 72 | * |
| 73 | * @return The value of the preference. |
| 74 | */ |
| 75 | public boolean getValue() { |
| 76 | return mWasPositiveResult; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected Object onGetDefaultValue(TypedArray a, int index) { |
| 81 | return a.getBoolean(index, false); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { |
| 86 | setValue(restorePersistedValue ? getPersistedBoolean(mWasPositiveResult) : |
| 87 | (Boolean) defaultValue); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean shouldDisableDependents() { |
| 92 | return !mWasPositiveResult || super.shouldDisableDependents(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected Parcelable onSaveInstanceState() { |
| 97 | final Parcelable superState = super.onSaveInstanceState(); |
| 98 | if (isPersistent()) { |
| 99 | // No need to save instance state since it's persistent |
| 100 | return superState; |
| 101 | } |
| 102 | |
| 103 | final SavedState myState = new SavedState(superState); |
| 104 | myState.wasPositiveResult = getValue(); |
| 105 | return myState; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onRestoreInstanceState(Parcelable state) { |
| 110 | if (!state.getClass().equals(SavedState.class)) { |
| 111 | // Didn't save state for us in onSaveInstanceState |
| 112 | super.onRestoreInstanceState(state); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | SavedState myState = (SavedState) state; |
| 117 | super.onRestoreInstanceState(myState.getSuperState()); |
| 118 | setValue(myState.wasPositiveResult); |
| 119 | } |
| 120 | |
| 121 | private static class SavedState extends BaseSavedState { |
| 122 | boolean wasPositiveResult; |
| 123 | |
| 124 | public SavedState(Parcel source) { |
| 125 | super(source); |
| 126 | wasPositiveResult = source.readInt() == 1; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void writeToParcel(Parcel dest, int flags) { |
| 131 | super.writeToParcel(dest, flags); |
| 132 | dest.writeInt(wasPositiveResult ? 1 : 0); |
| 133 | } |
| 134 | |
| 135 | public SavedState(Parcelable superState) { |
| 136 | super(superState); |
| 137 | } |
| 138 | |
| 139 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 140 | new Parcelable.Creator<SavedState>() { |
| 141 | public SavedState createFromParcel(Parcel in) { |
| 142 | return new SavedState(in); |
| 143 | } |
| 144 | |
| 145 | public SavedState[] newArray(int size) { |
| 146 | return new SavedState[size]; |
| 147 | } |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | } |