blob: f8780542f2fe600c6788aabfa877eb643bc5f7b5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Artur Satayeved5a6ae2019-12-10 17:47:54 +000019import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.res.TypedArray;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.preference.DialogPreference;
25import 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 */
33public class YesNoPreference extends DialogPreference {
34 private boolean mWasPositiveResult;
Alan Viverette617feb92013-09-09 18:09:13 -070035
36 public YesNoPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
37 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 }
39
Alan Viverette599d2a42013-09-16 13:48:29 -070040 public YesNoPreference(Context context, AttributeSet attrs, int defStyleAttr) {
41 this(context, attrs, defStyleAttr, 0);
42 }
43
Artur Satayevfa05ca22019-07-25 16:08:46 +010044 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 public YesNoPreference(Context context, AttributeSet attrs) {
46 this(context, attrs, com.android.internal.R.attr.yesNoPreferenceStyle);
47 }
Alan Viverette599d2a42013-09-16 13:48:29 -070048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 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}