Debian Codegraph @

core/java/com/android/internal/preference/YesNoPreference.java

raw symbols SCIP
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.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 * The {@link YesNoPreference} is a preference to show a dialog with Yes and No
29 * buttons.
30 * <p>
31 * This preference will store a boolean into the SharedPreferences.
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 * Sets the value of this preference, and saves it to the persistent store
64 * if required.
65 *
66 * @param value The value of the preference.
67 */
68 public void setValue(boolean value) {
69 mWasPositiveResult = value;
70
71 persistBoolean(value);
72
73 notifyDependencyChange(!value);
74 }
75
76 /**
77 * Gets the value of this preference.
78 *
79 * @return The value of the preference.
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 // No need to save instance state since it's persistent
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 // Didn't save state for us in onSaveInstanceState
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 }