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