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.util.AttributeSet; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.widget.CheckBox; 24 25 import androidx.preference.PreferenceViewHolder; 26 27 import com.android.settings.R; 28 import com.android.settingslib.TwoTargetPreference; 29 30 /** 31 * A custom preference that provides inline checkbox. It has a mandatory field for title, and 32 * optional fields for icon and sub-text. 33 */ 34 public class MasterCheckBoxPreference extends TwoTargetPreference { 35 36 private CheckBox mCheckBox; 37 private boolean mChecked; 38 private boolean mEnableCheckBox = true; 39 MasterCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40 public MasterCheckBoxPreference(Context context, AttributeSet attrs, 41 int defStyleAttr, int defStyleRes) { 42 super(context, attrs, defStyleAttr, defStyleRes); 43 } 44 MasterCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr)45 public MasterCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) { 46 super(context, attrs, defStyleAttr); 47 } 48 MasterCheckBoxPreference(Context context, AttributeSet attrs)49 public MasterCheckBoxPreference(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 } 52 MasterCheckBoxPreference(Context context)53 public MasterCheckBoxPreference(Context context) { 54 super(context); 55 } 56 57 @Override getSecondTargetResId()58 protected int getSecondTargetResId() { 59 return R.layout.preference_widget_master_checkbox; 60 } 61 62 @Override onBindViewHolder(PreferenceViewHolder holder)63 public void onBindViewHolder(PreferenceViewHolder holder) { 64 super.onBindViewHolder(holder); 65 final View widgetView = holder.findViewById(android.R.id.widget_frame); 66 if (widgetView != null) { 67 widgetView.setOnClickListener(new OnClickListener() { 68 @Override 69 public void onClick(View v) { 70 if (mCheckBox != null && !mCheckBox.isEnabled()) { 71 return; 72 } 73 setChecked(!mChecked); 74 if (!callChangeListener(mChecked)) { 75 setChecked(!mChecked); 76 } else { 77 persistBoolean(mChecked); 78 } 79 } 80 }); 81 } 82 83 mCheckBox = (CheckBox) holder.findViewById(R.id.checkboxWidget); 84 if (mCheckBox != null) { 85 mCheckBox.setContentDescription(getTitle()); 86 mCheckBox.setChecked(mChecked); 87 mCheckBox.setEnabled(mEnableCheckBox); 88 } 89 } 90 91 @Override setEnabled(boolean enabled)92 public void setEnabled(boolean enabled) { 93 super.setEnabled(enabled); 94 setCheckBoxEnabled(enabled); 95 } 96 isChecked()97 public boolean isChecked() { 98 return mCheckBox != null && mChecked; 99 } 100 setChecked(boolean checked)101 public void setChecked(boolean checked) { 102 mChecked = checked; 103 if (mCheckBox != null) { 104 mCheckBox.setChecked(checked); 105 } 106 } 107 setCheckBoxEnabled(boolean enabled)108 public void setCheckBoxEnabled(boolean enabled) { 109 mEnableCheckBox = enabled; 110 if (mCheckBox != null) { 111 mCheckBox.setEnabled(enabled); 112 } 113 } 114 getCheckBox()115 public CheckBox getCheckBox() { 116 return mCheckBox; 117 } 118 } 119