• 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 
27 /**
28  * A {@link Preference} that provides checkbox widget
29  * functionality.
30  * <p>
31  * This preference will store a boolean into the SharedPreferences.
32  *
33  * @attr ref android.R.styleable#CheckBoxPreference_summaryOff
34  * @attr ref android.R.styleable#CheckBoxPreference_summaryOn
35  * @attr ref android.R.styleable#CheckBoxPreference_disableDependentsState
36  */
37 public class CheckBoxPreference extends TwoStatePreference {
38 
CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr)39     public CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
40         this(context, attrs, defStyleAttr, 0);
41     }
42 
CheckBoxPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)43     public CheckBoxPreference(
44             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
45         super(context, attrs, defStyleAttr, defStyleRes);
46 
47         final TypedArray a = context.obtainStyledAttributes(attrs,
48                 R.styleable.CheckBoxPreference, defStyleAttr, defStyleRes);
49 
50         setSummaryOn(TypedArrayUtils.getString(a, R.styleable.CheckBoxPreference_summaryOn,
51                 R.styleable.CheckBoxPreference_android_summaryOn));
52 
53         setSummaryOff(TypedArrayUtils.getString(a, R.styleable.CheckBoxPreference_summaryOff,
54                 R.styleable.CheckBoxPreference_android_summaryOff));
55 
56         setDisableDependentsState(TypedArrayUtils.getBoolean(a,
57                 R.styleable.CheckBoxPreference_disableDependentsState,
58                 R.styleable.CheckBoxPreference_android_disableDependentsState, false));
59 
60         a.recycle();
61     }
62 
CheckBoxPreference(Context context, AttributeSet attrs)63     public CheckBoxPreference(Context context, AttributeSet attrs) {
64         this(context, attrs, R.attr.checkBoxPreferenceStyle);
65     }
66 
CheckBoxPreference(Context context)67     public CheckBoxPreference(Context context) {
68         this(context, null);
69     }
70 
71     @Override
onBindViewHolder(PreferenceViewHolder holder)72     public void onBindViewHolder(PreferenceViewHolder holder) {
73         super.onBindViewHolder(holder);
74 
75         View checkboxView = holder.findViewById(R.id.checkbox);
76         if (checkboxView != null && checkboxView instanceof Checkable) {
77             ((Checkable) checkboxView).setChecked(mChecked);
78         }
79 
80         syncSummaryView(holder);
81     }
82 
83     /**
84      * @hide
85      */
86     @Override
performClick(View view)87     protected void performClick(View view) {
88         super.performClick(view);
89         syncViewIfAccessibilityEnabled(view);
90     }
91 
syncViewIfAccessibilityEnabled(View view)92     private void syncViewIfAccessibilityEnabled(View view) {
93         AccessibilityManager accessibilityManager = (AccessibilityManager)
94                 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
95         if (!accessibilityManager.isEnabled()) {
96             return;
97         }
98 
99         View checkboxView = view.findViewById(R.id.checkbox);
100         syncCheckboxView(checkboxView);
101 
102         View summaryView = view.findViewById(android.R.id.summary);
103         syncSummaryView(summaryView);
104     }
105 
syncCheckboxView(View view)106     private void syncCheckboxView(View view) {
107         if (view instanceof Checkable) {
108             ((Checkable) view).setChecked(mChecked);
109         }
110     }
111 }
112