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.settings.widget 18 19 import android.content.Context 20 import android.content.res.Resources 21 import android.util.AttributeSet 22 import androidx.compose.ui.graphics.Color 23 import androidx.compose.ui.graphics.vector.ImageVector 24 import androidx.compose.ui.res.vectorResource 25 import com.android.settings.spa.preference.ComposePreference 26 import com.android.settingslib.spa.widget.card.CardButton 27 import com.android.settingslib.spa.widget.card.CardModel 28 import com.android.settingslib.spa.widget.card.SettingsCard 29 30 /** A preference for settings banner tips card. */ 31 class TipCardPreference 32 @JvmOverloads 33 constructor( 34 context: Context, 35 attr: AttributeSet? = null, 36 ) : ComposePreference(context, attr) { 37 38 /** A icon resource id for displaying icon on tips card. */ 39 var iconResId: Int? = null 40 41 /** A color resource id for displaying icon and button text on tips card. */ 42 var tintColorResId: Int? = null 43 44 /** The primary button's text. */ 45 var primaryButtonText: String = "" 46 47 /** The accessibility content description of the primary button. */ 48 var primaryButtonContentDescription: String? = null 49 50 /** The action for click on primary button. */ <lambda>null51 var primaryButtonAction: () -> Unit = {} 52 53 /** The visibility of primary button on tips card. The default value is `false`. */ 54 var primaryButtonVisibility: Boolean = false 55 56 /** The text on the second button of this [SettingsCard]. */ 57 var secondaryButtonText: String = "" 58 59 /** The accessibility content description of the secondary button. */ 60 var secondaryButtonContentDescription: String? = null 61 62 /** The action for click on secondary button. */ <lambda>null63 var secondaryButtonAction: () -> Unit = {} 64 65 /** The visibility of secondary button on tips card. The default value is `false`. */ 66 var secondaryButtonVisibility: Boolean = false 67 68 var onClick: (() -> Unit)? = null 69 70 /** The callback for click on card preference itself. */ 71 private var onDismiss: (() -> Unit)? = null 72 73 /** Enable the dismiss button on tips card. */ enableDismissnull74 fun enableDismiss(enable: Boolean) = 75 if (enable) onDismiss = { isVisible = false } else onDismiss = null 76 77 /** Clear layout state if needed. */ resetLayoutStatenull78 fun resetLayoutState() { 79 primaryButtonVisibility = false 80 secondaryButtonVisibility = false 81 notifyChanged() 82 } 83 84 /** Build the tips card content to apply any changes of this card's property. */ buildContentnull85 fun buildContent() { 86 setContent { 87 SettingsCard( 88 CardModel( 89 title = title?.toString() ?: "", 90 text = summary?.toString() ?: "", 91 buttons = listOfNotNull(configPrimaryButton(), configSecondaryButton()), 92 tintColor = tintColorResId?.let { Color(context.getColor(it)) } 93 ?: Color.Unspecified, 94 onDismiss = onDismiss, 95 imageVector = 96 iconResId 97 ?.takeIf { it != Resources.ID_NULL } 98 ?.let { ImageVector.vectorResource(it) }, 99 onClick = onClick, 100 ) 101 ) 102 } 103 } 104 configPrimaryButtonnull105 private fun configPrimaryButton(): CardButton? { 106 return if (primaryButtonVisibility) 107 CardButton( 108 text = primaryButtonText, 109 contentDescription = primaryButtonContentDescription, 110 onClick = primaryButtonAction, 111 ) 112 else null 113 } 114 configSecondaryButtonnull115 private fun configSecondaryButton(): CardButton? { 116 return if (secondaryButtonVisibility) 117 CardButton( 118 text = secondaryButtonText, 119 contentDescription = secondaryButtonContentDescription, 120 onClick = secondaryButtonAction, 121 ) 122 else null 123 } 124 notifyChangednull125 override fun notifyChanged() { 126 buildContent() 127 super.notifyChanged() 128 } 129 } 130