1be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell/* 2be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Copyright (C) 2010 The Android Open Source Project 3be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 4be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 5be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * you may not use this file except in compliance with the License. 6be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * You may obtain a copy of the License at 7be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 8be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 9be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 10be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Unless required by applicable law or agreed to in writing, software 11be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 12be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * See the License for the specific language governing permissions and 14be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * limitations under the License. 15be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 16be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 17be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellpackage android.preference; 18be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 197b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbyeimport android.annotation.StringRes; 20be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.content.Context; 21be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.content.res.TypedArray; 22be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.util.AttributeSet; 23be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.view.View; 24be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.widget.Checkable; 25be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.widget.CompoundButton; 26be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellimport android.widget.Switch; 27be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 28be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell/** 29be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * A {@link Preference} that provides a two-state toggleable option. 30be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * <p> 31be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This preference will store a boolean into the SharedPreferences. 32be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 33be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_summaryOff 34be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_summaryOn 35be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_switchTextOff 36be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_switchTextOn 37be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @attr ref android.R.styleable#SwitchPreference_disableDependentsState 38be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 39be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powellpublic class SwitchPreference extends TwoStatePreference { 404d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette private final Listener mListener = new Listener(); 414d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette 42be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell // Switch text for on and off states 43be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell private CharSequence mSwitchOn; 44be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell private CharSequence mSwitchOff; 45be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 4694f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell private class Listener implements CompoundButton.OnCheckedChangeListener { 47be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell @Override 48be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 4994f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell if (!callChangeListener(isChecked)) { 5094f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell // Listener didn't like it, change it back. 5194f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell // CompoundButton will make sure we don't recurse. 5294f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell buttonView.setChecked(!isChecked); 5394f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell return; 5494f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell } 5594f67e2af607df29a1c47e43ef6570fea46aa2b4Adam Powell 56be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell SwitchPreference.this.setChecked(isChecked); 57be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 58be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 59be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 60be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 61be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Construct a new SwitchPreference with the given style options. 62be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 63be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param context The Context that will style this preference 64be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param attrs Style attributes that differ from the default 65617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 66617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 67617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 68617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleRes A resource identifier of a style resource that 69617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * supplies default values for the view, used only if 70617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * defStyleAttr is 0 or can not be found in the theme. Can be 0 71617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * to not look for defaults. 72be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 73617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 74617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 75be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 76be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell TypedArray a = context.obtainStyledAttributes(attrs, 77617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette com.android.internal.R.styleable.SwitchPreference, defStyleAttr, defStyleRes); 78be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn)); 79be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff)); 80be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOn(a.getString( 81be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell com.android.internal.R.styleable.SwitchPreference_switchTextOn)); 82be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOff(a.getString( 83be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell com.android.internal.R.styleable.SwitchPreference_switchTextOff)); 84be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setDisableDependentsState(a.getBoolean( 85be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false)); 86be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell a.recycle(); 87be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 88be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 89be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 90be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Construct a new SwitchPreference with the given style options. 91be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 92be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param context The Context that will style this preference 93be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param attrs Style attributes that differ from the default 94617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param defStyleAttr An attribute in the current theme that contains a 95617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * reference to a style resource that supplies default values for 96617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * the view. Can be 0 to not look for defaults. 97617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette */ 98617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 99617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette this(context, attrs, defStyleAttr, 0); 100617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette } 101617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 102617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette /** 103617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * Construct a new SwitchPreference with the given style options. 104617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * 105617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param context The Context that will style this preference 106617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette * @param attrs Style attributes that differ from the default 107be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 108be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public SwitchPreference(Context context, AttributeSet attrs) { 109be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle); 110be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 111be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 112be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 113be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Construct a new SwitchPreference with default style options. 114be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 115be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param context The Context that will style this preference 116be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 117be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public SwitchPreference(Context context) { 118be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell this(context, null); 119be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 120be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 121be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell @Override 122be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell protected void onBindView(View view) { 123be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell super.onBindView(view); 124be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 125be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell View checkableView = view.findViewById(com.android.internal.R.id.switchWidget); 126be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell if (checkableView != null && checkableView instanceof Checkable) { 1274d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette if (checkableView instanceof Switch) { 1284d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette final Switch switchView = (Switch) checkableView; 1294d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette switchView.setOnCheckedChangeListener(null); 1304d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette } 1314d065a04ce42c07569cf20651c949c4da97aa4a9Alan Viverette 132be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell ((Checkable) checkableView).setChecked(mChecked); 1337650259a597dd24137420d32acc35efc44db381eSvetoslav Ganov 134be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell if (checkableView instanceof Switch) { 135be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell final Switch switchView = (Switch) checkableView; 136be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell switchView.setTextOn(mSwitchOn); 137be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell switchView.setTextOff(mSwitchOff); 138be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell switchView.setOnCheckedChangeListener(mListener); 139be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 140be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 141be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 142be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell syncSummaryView(view); 143be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 144be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 145be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 146be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the on state. 147be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 148be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 149be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param onText Text to display in the on state 150be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 151be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void setSwitchTextOn(CharSequence onText) { 152be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell mSwitchOn = onText; 153be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell notifyChanged(); 154be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 155be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 156be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 157be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the off state. 158be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 159be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 160be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param offText Text to display in the off state 161be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 162be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public void setSwitchTextOff(CharSequence offText) { 163be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell mSwitchOff = offText; 164be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell notifyChanged(); 165be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 166be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 167be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 168be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the on state. 169be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 170be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 171be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param resId The text as a string resource ID 172be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 1737b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye public void setSwitchTextOn(@StringRes int resId) { 174be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOn(getContext().getString(resId)); 175be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 176be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 177be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 178be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * Set the text displayed on the switch widget in the off state. 179be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * This should be a very short string; one word if possible. 180be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * 181be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @param resId The text as a string resource ID 182be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 1837b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye public void setSwitchTextOff(@StringRes int resId) { 184be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell setSwitchTextOff(getContext().getString(resId)); 185be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 186be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 187be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 188be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @return The text that will be displayed on the switch widget in the on state 189be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 190be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public CharSequence getSwitchTextOn() { 191be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell return mSwitchOn; 192be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 193be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell 194be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell /** 195be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell * @return The text that will be displayed on the switch widget in the off state 196be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell */ 197be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell public CharSequence getSwitchTextOff() { 198be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell return mSwitchOff; 199be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell } 200be0a4535053bbfdebd215e244b154ac810fd8edcAdam Powell} 201