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