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