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