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 | |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 35 | |
| 36 | public YesNoPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 37 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Alan Viverette | 599d2a4 | 2013-09-16 13:48:29 -0700 | [diff] [blame] | 40 | public YesNoPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 41 | this(context, attrs, defStyleAttr, 0); |
| 42 | } |
| 43 | |
Artur Satayev | fa05ca2 | 2019-07-25 16:08:46 +0100 | [diff] [blame] | 44 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | public YesNoPreference(Context context, AttributeSet attrs) { |
| 46 | this(context, attrs, com.android.internal.R.attr.yesNoPreferenceStyle); |
| 47 | } |
Alan Viverette | 599d2a4 | 2013-09-16 13:48:29 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |