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.widget.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 PrimaryCheckBoxPreference extends TwoTargetPreference { 35 36 private CheckBox mCheckBox; 37 private boolean mChecked; 38 private boolean mEnableCheckBox = true; 39 PrimaryCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40 public PrimaryCheckBoxPreference(Context context, AttributeSet attrs, 41 int defStyleAttr, int defStyleRes) { 42 super(context, attrs, defStyleAttr, defStyleRes); 43 } 44 PrimaryCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr)45 public PrimaryCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) { 46 super(context, attrs, defStyleAttr); 47 } 48 PrimaryCheckBoxPreference(Context context, AttributeSet attrs)49 public PrimaryCheckBoxPreference(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 } 52 PrimaryCheckBoxPreference(Context context)53 public PrimaryCheckBoxPreference(Context context) { 54 super(context); 55 } 56 57 @Override getSecondTargetResId()58 protected int getSecondTargetResId() { 59 return R.layout.preference_widget_primary_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 101 /** 102 * Set the check status of checkbox 103 * @param checked 104 */ setChecked(boolean checked)105 public void setChecked(boolean checked) { 106 mChecked = checked; 107 if (mCheckBox != null) { 108 mCheckBox.setChecked(checked); 109 } 110 } 111 112 /** 113 * Set the enabled status of CheckBox 114 * @param enabled 115 */ setCheckBoxEnabled(boolean enabled)116 public void setCheckBoxEnabled(boolean enabled) { 117 mEnableCheckBox = enabled; 118 if (mCheckBox != null) { 119 mCheckBox.setEnabled(enabled); 120 } 121 } 122 getCheckBox()123 public CheckBox getCheckBox() { 124 return mCheckBox; 125 } 126 } 127