1 /* 2 * Copyright (C) 2021 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.applications.intentpicker; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.widget.CheckBox; 22 23 import androidx.preference.PreferenceViewHolder; 24 25 import com.android.settingslib.widget.TwoTargetPreference; 26 27 /** This preference has a check box in the left side. */ 28 public class LeftSideCheckBoxPreference extends TwoTargetPreference { 29 private boolean mChecked; 30 private CheckBox mCheckBox; 31 LeftSideCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)32 public LeftSideCheckBoxPreference(Context context, AttributeSet attrs, 33 int defStyleAttr, int defStyleRes) { 34 super(context, attrs, defStyleAttr, defStyleRes); 35 setLayoutResource(com.android.settingslib.R.layout.preference_checkable_two_target); 36 } 37 LeftSideCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr)38 public LeftSideCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) { 39 this(context, attrs, defStyleAttr, 0); 40 } 41 LeftSideCheckBoxPreference(Context context, AttributeSet attrs)42 public LeftSideCheckBoxPreference(Context context, AttributeSet attrs) { 43 this(context, attrs, 0); 44 } 45 LeftSideCheckBoxPreference(Context context)46 public LeftSideCheckBoxPreference(Context context) { 47 this(context, /* attrs= */ null); 48 } 49 LeftSideCheckBoxPreference(Context context, boolean isChecked)50 public LeftSideCheckBoxPreference(Context context, boolean isChecked) { 51 super(context); 52 mChecked = isChecked; 53 setLayoutResource(com.android.settingslib.R.layout.preference_checkable_two_target); 54 } 55 56 @Override onBindViewHolder(PreferenceViewHolder view)57 public void onBindViewHolder(PreferenceViewHolder view) { 58 super.onBindViewHolder(view); 59 mCheckBox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox); 60 if (mCheckBox != null) { 61 mCheckBox.setChecked(mChecked); 62 } 63 } 64 65 @Override onClick()66 protected void onClick() { 67 if (mCheckBox != null) { 68 mChecked = !mChecked; 69 mCheckBox.setChecked(mChecked); 70 callChangeListener(mChecked); 71 } 72 } 73 } 74