• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.permissioncontroller.permission.ui.handheld.v36
18 
19 import android.content.Context
20 import android.os.Build
21 import android.util.AttributeSet
22 import android.widget.ImageView
23 import androidx.annotation.AttrRes
24 import androidx.annotation.DrawableRes
25 import androidx.annotation.IdRes
26 import androidx.annotation.RequiresApi
27 import androidx.preference.PreferenceViewHolder
28 import com.android.permissioncontroller.R
29 import com.android.permissioncontroller.permission.utils.ResourceUtils
30 import com.android.settingslib.widget.SelectorWithWidgetPreference
31 
32 /**
33  * A `SelectorWithWidgetPreference` with additional features:
34  * - Propagates the supplied `app:extraWidgetIcon` drawable to the extraWidget
35  * - Propagates the supplied `app:extraWidgetId` id to the extraWidget (the icon on the right)
36  * - Propagates the supplied `app:checkboxId` id to the checkbox (or radio button, on the left)
37  * - Allows defining a "disabled click listener" handler that handles clicks when disabled
38  */
39 @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
40 class PermissionSelectorWithWidgetPreference : SelectorWithWidgetPreference {
41     constructor(context: Context) : super(context) {
42         init(context, null)
43     }
44 
45     constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
46         init(context, attrs)
47     }
48 
49     constructor(
50         context: Context,
51         attrs: AttributeSet?,
52         @AttrRes defStyleAttr: Int,
53     ) : super(context, attrs, defStyleAttr) {
54         init(context, attrs)
55     }
56 
57     constructor(context: Context, isCheckbox: Boolean) : super(context, isCheckbox) {
58         init(context, null)
59     }
60 
initnull61     private fun init(context: Context, attrs: AttributeSet?) {
62         layoutResource = R.layout.permission_preference_selector_with_widget
63         widgetLayoutResource = R.layout.permission_preference_widget_radiobutton
64         extraWidgetIconRes =
65             ResourceUtils.getResourceIdByAttr(context, attrs, R.attr.extraWidgetIcon)
66         extraWidgetIdRes = ResourceUtils.getResourceIdByAttr(context, attrs, R.attr.extraWidgetId)
67         checkboxIdRes = ResourceUtils.getResourceIdByAttr(context, attrs, R.attr.checkboxId)
68     }
69 
70     @DrawableRes private var extraWidgetIconRes = 0
71     @IdRes private var extraWidgetIdRes = 0
72     @IdRes private var checkboxIdRes = 0
73     private var onDisabledClickListener: OnClickListener? = null
74 
onBindViewHoldernull75     override fun onBindViewHolder(holder: PreferenceViewHolder) {
76         super.onBindViewHolder(holder)
77         val extraWidget =
78             holder.findViewById(
79                 com.android.settingslib.widget.preference.selector.R.id.selector_extra_widget
80             ) as? ImageView
81         val checkbox = holder.findViewById(android.R.id.checkbox)
82         if (extraWidgetIconRes != 0) {
83             extraWidget?.setImageResource(extraWidgetIconRes)
84         }
85         if (extraWidgetIdRes != 0) {
86             extraWidget?.id = extraWidgetIdRes
87         }
88         if (checkboxIdRes != 0) {
89             checkbox?.id = checkboxIdRes
90         }
91         if (onDisabledClickListener != null) {
92             holder.itemView.isEnabled = true
93             holder.itemView.setOnClickListener {
94                 onDisabledClickListener?.onRadioButtonClicked(this)
95             }
96         }
97     }
98 
setOnDisabledClickListenernull99     fun setOnDisabledClickListener(onClickListener: OnClickListener?) {
100         onDisabledClickListener = onClickListener
101         notifyChanged()
102     }
103 }
104