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