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