Debian Codegraph @

core/java/android/preference/SwitchPreference.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2010 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 android.preference;
18
19 import android.annotation.StringRes;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.content.res.TypedArray;
23 import android.os.Build;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.widget.Checkable;
27 import android.widget.CompoundButton;
28 import android.widget.Switch;
29
30 /**
31 * A {@link Preference} that provides a two-state toggleable option.
32 * <p>
33 * This preference will store a boolean into the SharedPreferences.
34 *
35 * @attr ref android.R.styleable#SwitchPreference_summaryOff
36 * @attr ref android.R.styleable#SwitchPreference_summaryOn
37 * @attr ref android.R.styleable#SwitchPreference_switchTextOff
38 * @attr ref android.R.styleable#SwitchPreference_switchTextOn
39 * @attr ref android.R.styleable#SwitchPreference_disableDependentsState
40 *
41 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
42 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
43 * Preference Library</a> for consistent behavior across all devices. For more information on
44 * using the AndroidX Preference Library see
45 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
46 */
47 @Deprecated
48 public class SwitchPreference extends TwoStatePreference {
49 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
50 private final Listener mListener = new Listener();
51
52 // Switch text for on and off states
53 private CharSequence mSwitchOn;
54 private CharSequence mSwitchOff;
55
56 private class Listener implements CompoundButton.OnCheckedChangeListener {
57 @Override
58 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
59 if (!callChangeListener(isChecked)) {
60 // Listener didn't like it, change it back.
61 // CompoundButton will make sure we don't recurse.
62 buttonView.setChecked(!isChecked);
63 return;
64 }
65
66 SwitchPreference.this.setChecked(isChecked);
67 }
68 }
69
70 /**
71 * Construct a new SwitchPreference with the given style options.
72 *
73 * @param context The Context that will style this preference
74 * @param attrs Style attributes that differ from the default
75 * @param defStyleAttr An attribute in the current theme that contains a
76 * reference to a style resource that supplies default values for
77 * the view. Can be 0 to not look for defaults.
78 * @param defStyleRes A resource identifier of a style resource that
79 * supplies default values for the view, used only if
80 * defStyleAttr is 0 or can not be found in the theme. Can be 0
81 * to not look for defaults.
82 */
83 public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
84 super(context, attrs, defStyleAttr, defStyleRes);
85
86 TypedArray a = context.obtainStyledAttributes(attrs,
87 com.android.internal.R.styleable.SwitchPreference, defStyleAttr, defStyleRes);
88 setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn));
89 setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff));
90 setSwitchTextOn(a.getString(
91 com.android.internal.R.styleable.SwitchPreference_switchTextOn));
92 setSwitchTextOff(a.getString(
93 com.android.internal.R.styleable.SwitchPreference_switchTextOff));
94 setDisableDependentsState(a.getBoolean(
95 com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false));
96 a.recycle();
97 }
98
99 /**
100 * Construct a new SwitchPreference with the given style options.
101 *
102 * @param context The Context that will style this preference
103 * @param attrs Style attributes that differ from the default
104 * @param defStyleAttr An attribute in the current theme that contains a
105 * reference to a style resource that supplies default values for
106 * the view. Can be 0 to not look for defaults.
107 */
108 public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
109 this(context, attrs, defStyleAttr, 0);
110 }
111
112 /**
113 * Construct a new SwitchPreference with the given style options.
114 *
115 * @param context The Context that will style this preference
116 * @param attrs Style attributes that differ from the default
117 */
118 public SwitchPreference(Context context, AttributeSet attrs) {
119 this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
120 }
121
122 /**
123 * Construct a new SwitchPreference with default style options.
124 *
125 * @param context The Context that will style this preference
126 */
127 public SwitchPreference(Context context) {
128 this(context, null);
129 }
130
131 @Override
132 protected void onBindView(View view) {
133 super.onBindView(view);
134
135 View checkableView = view.findViewById(com.android.internal.R.id.switch_widget);
136 if (checkableView != null && checkableView instanceof Checkable) {
137 if (checkableView instanceof Switch) {
138 final Switch switchView = (Switch) checkableView;
139 switchView.setOnCheckedChangeListener(null);
140 }
141
142 ((Checkable) checkableView).setChecked(mChecked);
143
144 if (checkableView instanceof Switch) {
145 final Switch switchView = (Switch) checkableView;
146 switchView.setTextOn(mSwitchOn);
147 switchView.setTextOff(mSwitchOff);
148 switchView.setOnCheckedChangeListener(mListener);
149 }
150 }
151
152 syncSummaryView(view);
153 }
154
155 /**
156 * Set the text displayed on the switch widget in the on state.
157 * This should be a very short string; one word if possible.
158 *
159 * @param onText Text to display in the on state
160 */
161 public void setSwitchTextOn(CharSequence onText) {
162 mSwitchOn = onText;
163 notifyChanged();
164 }
165
166 /**
167 * Set the text displayed on the switch widget in the off state.
168 * This should be a very short string; one word if possible.
169 *
170 * @param offText Text to display in the off state
171 */
172 public void setSwitchTextOff(CharSequence offText) {
173 mSwitchOff = offText;
174 notifyChanged();
175 }
176
177 /**
178 * Set the text displayed on the switch widget in the on state.
179 * This should be a very short string; one word if possible.
180 *
181 * @param resId The text as a string resource ID
182 */
183 public void setSwitchTextOn(@StringRes int resId) {
184 setSwitchTextOn(getContext().getString(resId));
185 }
186
187 /**
188 * Set the text displayed on the switch widget in the off state.
189 * This should be a very short string; one word if possible.
190 *
191 * @param resId The text as a string resource ID
192 */
193 public void setSwitchTextOff(@StringRes int resId) {
194 setSwitchTextOff(getContext().getString(resId));
195 }
196
197 /**
198 * @return The text that will be displayed on the switch widget in the on state
199 */
200 public CharSequence getSwitchTextOn() {
201 return mSwitchOn;
202 }
203
204 /**
205 * @return The text that will be displayed on the switch widget in the off state
206 */
207 public CharSequence getSwitchTextOff() {
208 return mSwitchOff;
209 }
210 }