• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.support.v7.preference;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.support.v4.content.res.TypedArrayUtils;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.accessibility.AccessibilityManager;
25 import android.widget.Checkable;
26 import android.widget.CompoundButton;
27 
28 /**
29  * A {@link Preference} that provides checkbox widget
30  * functionality.
31  * <p>
32  * This preference will store a boolean into the SharedPreferences.
33  *
34  * @attr name android:summaryOff
35  * @attr name android:summaryOn
36  * @attr name android:disableDependentsState
37  */
38 public class CheckBoxPreference extends TwoStatePreference {
39     private final Listener mListener = new Listener();
40 
41     private class Listener implements CompoundButton.OnCheckedChangeListener {
42         @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)43         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
44             if (!callChangeListener(isChecked)) {
45                 // Listener didn't like it, change it back.
46                 // CompoundButton will make sure we don't recurse.
47                 buttonView.setChecked(!isChecked);
48                 return;
49             }
50             CheckBoxPreference.this.setChecked(isChecked);
51         }
52     }
53 
CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr)54     public CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
55         this(context, attrs, defStyleAttr, 0);
56     }
57 
CheckBoxPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)58     public CheckBoxPreference(
59             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
60         super(context, attrs, defStyleAttr, defStyleRes);
61 
62         final TypedArray a = context.obtainStyledAttributes(attrs,
63                 R.styleable.CheckBoxPreference, defStyleAttr, defStyleRes);
64 
65         setSummaryOn(TypedArrayUtils.getString(a, R.styleable.CheckBoxPreference_summaryOn,
66                 R.styleable.CheckBoxPreference_android_summaryOn));
67 
68         setSummaryOff(TypedArrayUtils.getString(a, R.styleable.CheckBoxPreference_summaryOff,
69                 R.styleable.CheckBoxPreference_android_summaryOff));
70 
71         setDisableDependentsState(TypedArrayUtils.getBoolean(a,
72                 R.styleable.CheckBoxPreference_disableDependentsState,
73                 R.styleable.CheckBoxPreference_android_disableDependentsState, false));
74 
75         a.recycle();
76     }
77 
CheckBoxPreference(Context context, AttributeSet attrs)78     public CheckBoxPreference(Context context, AttributeSet attrs) {
79         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.checkBoxPreferenceStyle,
80                 android.R.attr.checkBoxPreferenceStyle));
81     }
82 
CheckBoxPreference(Context context)83     public CheckBoxPreference(Context context) {
84         this(context, null);
85     }
86 
87     @Override
onBindViewHolder(PreferenceViewHolder holder)88     public void onBindViewHolder(PreferenceViewHolder holder) {
89         super.onBindViewHolder(holder);
90 
91         syncCheckboxView(holder.findViewById(android.R.id.checkbox));
92 
93         syncSummaryView(holder);
94     }
95 
96     /**
97      * @hide
98      */
99     @Override
performClick(View view)100     protected void performClick(View view) {
101         super.performClick(view);
102         syncViewIfAccessibilityEnabled(view);
103     }
104 
syncViewIfAccessibilityEnabled(View view)105     private void syncViewIfAccessibilityEnabled(View view) {
106         AccessibilityManager accessibilityManager = (AccessibilityManager)
107                 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
108         if (!accessibilityManager.isEnabled()) {
109             return;
110         }
111 
112         View checkboxView = view.findViewById(android.R.id.checkbox);
113         syncCheckboxView(checkboxView);
114 
115         View summaryView = view.findViewById(android.R.id.summary);
116         syncSummaryView(summaryView);
117     }
118 
syncCheckboxView(View view)119     private void syncCheckboxView(View view) {
120         if (view instanceof CompoundButton) {
121             ((CompoundButton) view).setOnCheckedChangeListener(null);
122         }
123         if (view instanceof Checkable) {
124             ((Checkable) view).setChecked(mChecked);
125         }
126         if (view instanceof CompoundButton) {
127             ((CompoundButton) view).setOnCheckedChangeListener(mListener);
128         }
129     }
130 }
131