1/*
2 * Copyright (C) 2018 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 com.android.settings.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.support.v7.preference.CheckBoxPreference;
22import android.support.v7.preference.PreferenceViewHolder;
23import android.util.AttributeSet;
24import android.view.View;
25
26/**
27 * A CheckboxPreference that can disable its checkbox separate from its text.
28 * Differs from CheckboxPreference.setDisabled() in that the text is not dimmed.
29 */
30public class DisabledCheckBoxPreference extends CheckBoxPreference {
31    private PreferenceViewHolder mViewHolder;
32    private View mCheckBox;
33    private boolean mEnabledCheckBox;
34
35    public DisabledCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr,
36            int defStyleRes) {
37        super(context, attrs, defStyleAttr, defStyleRes);
38        setupDisabledCheckBoxPreference(context, attrs, defStyleAttr, defStyleRes);
39    }
40
41    public DisabledCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
42        super(context, attrs, defStyleAttr);
43        setupDisabledCheckBoxPreference(context, attrs, defStyleAttr, 0);
44    }
45
46    public DisabledCheckBoxPreference(Context context, AttributeSet attrs) {
47        super(context, attrs);
48        setupDisabledCheckBoxPreference(context, attrs, 0, 0);
49    }
50
51    public DisabledCheckBoxPreference(Context context) {
52        super(context);
53        setupDisabledCheckBoxPreference(context, null, 0, 0);
54    }
55
56    private void setupDisabledCheckBoxPreference(Context context, AttributeSet attrs,
57            int defStyleAttr, int defStyleRes) {
58        final TypedArray a = context.obtainStyledAttributes(
59                attrs, com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes);
60        for (int i = a.getIndexCount() - 1; i >= 0; i--) {
61            int attr = a.getIndex(i);
62            switch (attr) {
63                case com.android.internal.R.styleable.Preference_enabled:
64                    mEnabledCheckBox = a.getBoolean(attr, true);
65                    break;
66            }
67        }
68        a.recycle();
69
70        // Always tell super class this preference is enabled.
71        // We manually enable/disable checkbox using enableCheckBox.
72        super.setEnabled(true);
73        enableCheckbox(mEnabledCheckBox);
74    }
75
76    public void enableCheckbox(boolean enabled) {
77        mEnabledCheckBox = enabled;
78        if (mViewHolder != null && mCheckBox != null) {
79            mCheckBox.setEnabled(mEnabledCheckBox);
80            mViewHolder.itemView.setEnabled(mEnabledCheckBox);
81        }
82    }
83
84    @Override
85    public void onBindViewHolder(PreferenceViewHolder holder) {
86        super.onBindViewHolder(holder);
87        mViewHolder = holder;
88        mCheckBox = holder.findViewById(android.R.id.checkbox);
89
90        enableCheckbox(mEnabledCheckBox);
91    }
92
93    @Override
94    protected void performClick(View view) {
95        // only perform clicks if the checkbox is enabled
96        if (mEnabledCheckBox) {
97            super.performClick(view);
98        }
99    }
100
101}
102