2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.preference;
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.content.res.TypedArray;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.view.accessibility.AccessibilityEvent;
28 import android.view.accessibility.AccessibilityManager;
29 import android.widget.Checkable;
30 import android.widget.TextView;
33 * A {@link Preference} that provides checkbox widget
36 * This preference will store a boolean into the SharedPreferences.
38 * @attr ref android.R.styleable#CheckBoxPreference_summaryOff
39 * @attr ref android.R.styleable#CheckBoxPreference_summaryOn
40 * @attr ref android.R.styleable#CheckBoxPreference_disableDependentsState
42 public class CheckBoxPreference extends Preference {
44 private CharSequence mSummaryOn;
45 private CharSequence mSummaryOff;
47 private boolean mChecked;
48 private boolean mSendAccessibilityEventViewClickedType;
50 private AccessibilityManager mAccessibilityManager;
52 private boolean mDisableDependentsState;
54 public CheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
55 super(context, attrs, defStyle);
57 TypedArray a = context.obtainStyledAttributes(attrs,
58 com.android.internal.R.styleable.CheckBoxPreference, defStyle, 0);
59 mSummaryOn = a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn);
60 mSummaryOff = a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff);
61 mDisableDependentsState = a.getBoolean(
62 com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState, false);
65 mAccessibilityManager =
66 (AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
69 public CheckBoxPreference(Context context, AttributeSet attrs) {
70 this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
73 public CheckBoxPreference(Context context) {
78 protected void onBindView(View view) {
79 super.onBindView(view);
81 View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
82 if (checkboxView != null && checkboxView instanceof Checkable) {
83 ((Checkable) checkboxView).setChecked(mChecked);
85 // send an event to announce the value change of the CheckBox and is done here
86 // because clicking a preference does not immediately change the checked state
87 // for example when enabling the WiFi
88 if (mSendAccessibilityEventViewClickedType &&
89 mAccessibilityManager.isEnabled() &&
90 checkboxView.isEnabled()) {
91 mSendAccessibilityEventViewClickedType = false;
93 int eventType = AccessibilityEvent.TYPE_VIEW_CLICKED;
94 checkboxView.sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
98 // Sync the summary view
99 TextView summaryView = (TextView) view.findViewById(com.android.internal.R.id.summary);
100 if (summaryView != null) {
101 boolean useDefaultSummary = true;
102 if (mChecked && mSummaryOn != null) {
103 summaryView.setText(mSummaryOn);
104 useDefaultSummary = false;
105 } else if (!mChecked && mSummaryOff != null) {
106 summaryView.setText(mSummaryOff);
107 useDefaultSummary = false;
110 if (useDefaultSummary) {
111 final CharSequence summary = getSummary();
112 if (summary != null) {
113 summaryView.setText(summary);
114 useDefaultSummary = false;
118 int newVisibility = View.GONE;
119 if (!useDefaultSummary) {
120 // Someone has written to it
121 newVisibility = View.VISIBLE;
123 if (newVisibility != summaryView.getVisibility()) {
124 summaryView.setVisibility(newVisibility);
130 protected void onClick() {
133 boolean newValue = !isChecked();
135 // in onBindView() an AccessibilityEventViewClickedType is sent to announce the change
137 mSendAccessibilityEventViewClickedType = true;
139 if (!callChangeListener(newValue)) {
143 setChecked(newValue);
147 * Sets the checked state and saves it to the {@link SharedPreferences}.
149 * @param checked The checked state.
151 public void setChecked(boolean checked) {
152 if (mChecked != checked) {
154 persistBoolean(checked);
155 notifyDependencyChange(shouldDisableDependents());
161 * Returns the checked state.
163 * @return The checked state.
165 public boolean isChecked() {
170 public boolean shouldDisableDependents() {
171 boolean shouldDisable = mDisableDependentsState ? mChecked : !mChecked;
172 return shouldDisable || super.shouldDisableDependents();
176 * Sets the summary to be shown when checked.
178 * @param summary The summary to be shown when checked.
180 public void setSummaryOn(CharSequence summary) {
181 mSummaryOn = summary;
188 * @see #setSummaryOn(CharSequence)
189 * @param summaryResId The summary as a resource.
191 public void setSummaryOn(int summaryResId) {
192 setSummaryOn(getContext().getString(summaryResId));
196 * Returns the summary to be shown when checked.
197 * @return The summary.
199 public CharSequence getSummaryOn() {
204 * Sets the summary to be shown when unchecked.
206 * @param summary The summary to be shown when unchecked.
208 public void setSummaryOff(CharSequence summary) {
209 mSummaryOff = summary;
216 * @see #setSummaryOff(CharSequence)
217 * @param summaryResId The summary as a resource.
219 public void setSummaryOff(int summaryResId) {
220 setSummaryOff(getContext().getString(summaryResId));
224 * Returns the summary to be shown when unchecked.
225 * @return The summary.
227 public CharSequence getSummaryOff() {
232 * Returns whether dependents are disabled when this preference is on ({@code true})
233 * or when this preference is off ({@code false}).
235 * @return Whether dependents are disabled when this preference is on ({@code true})
236 * or when this preference is off ({@code false}).
238 public boolean getDisableDependentsState() {
239 return mDisableDependentsState;
243 * Sets whether dependents are disabled when this preference is on ({@code true})
244 * or when this preference is off ({@code false}).
246 * @param disableDependentsState The preference state that should disable dependents.
248 public void setDisableDependentsState(boolean disableDependentsState) {
249 mDisableDependentsState = disableDependentsState;
253 protected Object onGetDefaultValue(TypedArray a, int index) {
254 return a.getBoolean(index, false);
258 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
259 setChecked(restoreValue ? getPersistedBoolean(mChecked)
260 : (Boolean) defaultValue);
264 protected Parcelable onSaveInstanceState() {
265 final Parcelable superState = super.onSaveInstanceState();
266 if (isPersistent()) {
267 // No need to save instance state since it's persistent
271 final SavedState myState = new SavedState(superState);
272 myState.checked = isChecked();
277 protected void onRestoreInstanceState(Parcelable state) {
278 if (state == null || !state.getClass().equals(SavedState.class)) {
279 // Didn't save state for us in onSaveInstanceState
280 super.onRestoreInstanceState(state);
284 SavedState myState = (SavedState) state;
285 super.onRestoreInstanceState(myState.getSuperState());
286 setChecked(myState.checked);
289 private static class SavedState extends BaseSavedState {
292 public SavedState(Parcel source) {
294 checked = source.readInt() == 1;
298 public void writeToParcel(Parcel dest, int flags) {
299 super.writeToParcel(dest, flags);
300 dest.writeInt(checked ? 1 : 0);
303 public SavedState(Parcelable superState) {
307 public static final Parcelable.Creator<SavedState> CREATOR =
308 new Parcelable.Creator<SavedState>() {
309 public SavedState createFromParcel(Parcel in) {
310 return new SavedState(in);
313 public SavedState[] newArray(int size) {
314 return new SavedState[size];