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.RequiresApi 26 import androidx.annotation.StyleRes 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.TwoTargetPreference 31 32 /** 33 * A `TwoTargetPreference` with additional features: 34 * - Propagates the supplied `app:extraWidgetIcon` drawable to the second target 35 * - Allows defining a click listener on the second target (the icon on the right) 36 */ 37 @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) 38 class PermissionTwoTargetPreference : TwoTargetPreference { 39 constructor(context: Context) : super(context) { 40 init(context, null) 41 } 42 43 constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { 44 init(context, attrs) 45 } 46 47 constructor( 48 context: Context, 49 attrs: AttributeSet?, 50 @AttrRes defStyleAttr: Int, 51 ) : super(context, attrs, defStyleAttr) { 52 init(context, attrs) 53 } 54 55 constructor( 56 context: Context, 57 attrs: AttributeSet?, 58 @AttrRes defStyleAttr: Int, 59 @StyleRes defStyleRes: Int, 60 ) : super(context, attrs, defStyleAttr, defStyleRes) { 61 init(context, attrs) 62 } 63 initnull64 private fun init(context: Context, attrs: AttributeSet?) { 65 layoutResource = R.layout.permission_preference_two_target 66 extraWidgetIconRes = 67 ResourceUtils.getResourceIdByAttr(context, attrs, R.attr.extraWidgetIcon) 68 } 69 70 @DrawableRes private var extraWidgetIconRes = 0 71 private var extraWidgetContentDescription: String? = null 72 private var secondTargetClickListener: OnSecondTargetClickListener? = null 73 onBindViewHoldernull74 override fun onBindViewHolder(holder: PreferenceViewHolder) { 75 super.onBindViewHolder(holder) 76 val settingsButton = holder.findViewById(R.id.settings_button) as ImageView 77 if (extraWidgetIconRes != 0) { 78 settingsButton.setImageResource(extraWidgetIconRes) 79 } 80 if (extraWidgetContentDescription != null) { 81 settingsButton.contentDescription = extraWidgetContentDescription 82 } 83 if (secondTargetClickListener != null) { 84 settingsButton.setOnClickListener { 85 secondTargetClickListener!!.onSecondTargetClick(this) 86 } 87 } else { 88 settingsButton.setOnClickListener(null) 89 } 90 } 91 getSecondTargetResIdnull92 override fun getSecondTargetResId() = R.layout.settings_button_preference_widget 93 94 override fun shouldHideSecondTarget() = secondTargetClickListener == null 95 96 fun setOnSecondTargetClickListener(listener: OnSecondTargetClickListener?) { 97 secondTargetClickListener = listener 98 notifyChanged() 99 } 100 setExtraWidgetIconnull101 fun setExtraWidgetIcon(@DrawableRes iconRes: Int, contentDescription: String) { 102 this.extraWidgetIconRes = iconRes 103 this.extraWidgetContentDescription = contentDescription 104 notifyChanged() 105 } 106 107 interface OnSecondTargetClickListener { onSecondTargetClicknull108 fun onSecondTargetClick(preference: TwoTargetPreference) 109 } 110 } 111