1 /* 2 * Copyright (C) 2024 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 package com.android.healthconnect.controller.selectabledeletion 17 18 import android.content.Context 19 import android.util.AttributeSet 20 import android.view.View.OnClickListener 21 import android.view.ViewGroup 22 import android.widget.CheckBox 23 import androidx.preference.Preference.OnPreferenceClickListener 24 import androidx.preference.PreferenceViewHolder 25 import com.android.healthconnect.controller.R 26 import com.android.healthconnect.controller.shared.preference.HealthPreference 27 28 /** Custom preference that displays a checkbox and allows the user to select all items */ 29 class SelectAllCheckboxPreference 30 @JvmOverloads 31 constructor(context: Context, attrs: AttributeSet? = null) : HealthPreference(context, attrs) { 32 33 private var widgetFrame: ViewGroup? = null 34 private var checkBox: CheckBox? = null 35 private var checkboxButtonListener: OnClickListener? = null 36 private var onPreferenceClickListener: OnPreferenceClickListener? = null 37 private var isChecked: Boolean = false 38 39 init { 40 widgetLayoutResource = R.layout.widget_checkbox 41 isSelectable = true 42 } 43 onBindViewHoldernull44 override fun onBindViewHolder(holder: PreferenceViewHolder) { 45 super.onBindViewHolder(holder) 46 widgetFrame = holder.findViewById(android.R.id.widget_frame) as ViewGroup? 47 widgetFrame?.tag = "checkbox" 48 widgetFrame?.contentDescription = getUpdatedContentDescription(isChecked) 49 50 checkBox = holder.findViewById(R.id.checkbox_button) as CheckBox 51 52 checkBox?.isChecked = isChecked 53 54 checkBox?.setOnClickListener(checkboxButtonListener) 55 56 val widgetFrameParent: ViewGroup? = widgetFrame?.parent as ViewGroup? 57 widgetFrameParent?.setPaddingRelative( 58 widgetFrameParent.paddingStart, 59 widgetFrameParent.paddingTop, 60 widgetFrameParent.paddingEnd, 61 widgetFrameParent.paddingBottom, 62 ) 63 } 64 setOnPreferenceClickListenerWithCheckboxnull65 fun setOnPreferenceClickListenerWithCheckbox(method: () -> Unit) { 66 val clickListener = OnPreferenceClickListener { 67 checkBox?.toggle() 68 setIsChecked(checkBox?.isChecked ?: false) 69 widgetFrame?.contentDescription = getUpdatedContentDescription(isChecked) 70 method() 71 true 72 } 73 74 checkboxButtonListener = OnClickListener { 75 setIsChecked(checkBox?.isChecked ?: false) 76 widgetFrame?.contentDescription = getUpdatedContentDescription(isChecked) 77 method() 78 } 79 80 if (onPreferenceClickListener == null) { 81 onPreferenceClickListener = clickListener 82 } 83 84 super.setOnPreferenceClickListener(clickListener) 85 notifyChanged() 86 } 87 removeOnPreferenceClickListenernull88 fun removeOnPreferenceClickListener() { 89 if (checkboxButtonListener != null) { 90 checkboxButtonListener = null 91 } 92 93 if (onPreferenceClickListener != null) { 94 onPreferenceClickListener = null 95 } 96 } 97 setIsCheckednull98 fun setIsChecked(checked: Boolean) { 99 isChecked = checked 100 } 101 getIsCheckednull102 fun getIsChecked(): Boolean { 103 return isChecked 104 } 105 getUpdatedContentDescriptionnull106 private fun getUpdatedContentDescription(isChecked: Boolean): String { 107 return if (isChecked) { 108 context.getString(R.string.a11y_checked) 109 } else { 110 context.getString(R.string.a11y_unchecked) 111 } 112 } 113 } 114