1 /* 2 * Copyright (C) 2022 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.safetycenter.ui.view 18 19 import android.content.Context 20 import android.safetycenter.SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_UNSPECIFIED 21 import android.safetycenter.SafetyCenterEntry.SEVERITY_UNSPECIFIED_ICON_TYPE_NO_ICON 22 import android.view.View 23 import android.view.ViewGroup 24 import android.widget.ImageView 25 import android.widget.LinearLayout 26 import android.widget.TextView 27 import com.android.permissioncontroller.R 28 import com.android.permissioncontroller.safetycenter.ui.SeverityIconPicker 29 30 internal class SafetyEntryCommonViewsManager(rootEntryView: ViewGroup?) { 31 <lambda>null32 val titleView: TextView? by lazyView { rootEntryView?.findViewById(R.id.title) } <lambda>null33 val summaryView: TextView? by lazyView { rootEntryView?.findViewById(R.id.summary) } <lambda>null34 private val iconView: ImageView? by lazyView { rootEntryView?.findViewById(R.id.icon) } <lambda>null35 private val iconFrame: View? by lazyView { rootEntryView?.findViewById(R.id.icon_frame) } <lambda>null36 private val emptySpace: View? by lazyView { rootEntryView?.findViewById(R.id.empty_space) } 37 showDetailsnull38 fun showDetails( 39 id: String, 40 title: CharSequence, 41 summary: CharSequence?, 42 severityLevel: Int, 43 severityUnspecifiedIconType: Int 44 ) { 45 titleView?.text = title 46 summaryView?.showText(summary) 47 48 iconView?.setImageResource( 49 SeverityIconPicker.selectIconResId(id, severityLevel, severityUnspecifiedIconType) 50 ) 51 52 val hideIcon = 53 (severityLevel == ENTRY_SEVERITY_LEVEL_UNSPECIFIED && 54 severityUnspecifiedIconType == SEVERITY_UNSPECIFIED_ICON_TYPE_NO_ICON) 55 iconFrame?.visibility = if (hideIcon) LinearLayout.GONE else LinearLayout.VISIBLE 56 emptySpace?.visibility = if (hideIcon) LinearLayout.VISIBLE else LinearLayout.GONE 57 } 58 showTextnull59 private fun TextView.showText(text: CharSequence?) { 60 if (text != null && text.isNotEmpty()) { 61 visibility = View.VISIBLE 62 this.text = text 63 } else { 64 visibility = View.GONE 65 } 66 } 67 68 companion object { 69 70 private const val DEFAULT_DISABLED_ALPHA = 0.4f 71 72 /** 73 * Change opacity to make some entries look disabled but still be clickable 74 * 75 * @param isEntryEnabled whether the [android.safetycenter.SafetyCenterEntry] is enabled 76 * @param isPreferenceEnabled whether the corresponding preference is enabled 77 * @param titleView view displaying the title text of the entry 78 * @param summaryView view displaying the summary text of the entry 79 */ changeEnabledStatenull80 fun changeEnabledState( 81 context: Context, 82 isEntryEnabled: Boolean, 83 isPreferenceEnabled: Boolean, 84 titleView: TextView?, 85 summaryView: TextView? 86 ) { 87 val disabledAlpha = getDisabledAlpha(context) 88 if (isEntryEnabled) { 89 titleView?.alpha = 1f 90 summaryView?.alpha = 1f 91 } else if (isPreferenceEnabled) { 92 /* Check that preference is enabled before lowering because disabled preferences 93 * already have a low visibility */ 94 titleView?.alpha = disabledAlpha 95 summaryView?.alpha = disabledAlpha 96 } 97 } 98 getDisabledAlphanull99 private fun getDisabledAlpha(context: Context): Float { 100 val styledAttributes = 101 context.obtainStyledAttributes(intArrayOf(android.R.attr.disabledAlpha)) 102 try { 103 return styledAttributes.getFloat(0, DEFAULT_DISABLED_ALPHA) 104 } finally { 105 styledAttributes.recycle() 106 } 107 } 108 } 109 } 110