summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/safetycenter/ui/view/SafetyEntryCommonViewsManager.kt
blob: 34565421a06c3d781a7673bd8f314e2bd7fe65e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.safetycenter.ui.view

import android.content.Context
import android.safetycenter.SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_UNSPECIFIED
import android.safetycenter.SafetyCenterEntry.SEVERITY_UNSPECIFIED_ICON_TYPE_NO_ICON
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.android.permissioncontroller.R
import com.android.permissioncontroller.safetycenter.ui.SeverityIconPicker

internal class SafetyEntryCommonViewsManager(rootEntryView: ViewGroup?) {

    val titleView: TextView? by lazyView { rootEntryView?.findViewById(R.id.title) }
    val summaryView: TextView? by lazyView { rootEntryView?.findViewById(R.id.summary) }
    private val iconView: ImageView? by lazyView { rootEntryView?.findViewById(R.id.icon) }
    private val iconFrame: View? by lazyView { rootEntryView?.findViewById(R.id.icon_frame) }
    private val emptySpace: View? by lazyView { rootEntryView?.findViewById(R.id.empty_space) }

    fun showDetails(
        id: String,
        title: CharSequence,
        summary: CharSequence?,
        severityLevel: Int,
        severityUnspecifiedIconType: Int
    ) {
        titleView?.text = title
        summaryView?.showText(summary)

        iconView?.setImageResource(
            SeverityIconPicker.selectIconResId(id, severityLevel, severityUnspecifiedIconType)
        )

        val hideIcon =
            (severityLevel == ENTRY_SEVERITY_LEVEL_UNSPECIFIED &&
                severityUnspecifiedIconType == SEVERITY_UNSPECIFIED_ICON_TYPE_NO_ICON)
        iconFrame?.visibility = if (hideIcon) LinearLayout.GONE else LinearLayout.VISIBLE
        emptySpace?.visibility = if (hideIcon) LinearLayout.VISIBLE else LinearLayout.GONE
    }

    private fun TextView.showText(text: CharSequence?) {
        if (text != null && text.isNotEmpty()) {
            visibility = View.VISIBLE
            this.text = text
        } else {
            visibility = View.GONE
        }
    }

    companion object {

        private const val DEFAULT_DISABLED_ALPHA = 0.4f

        /**
         * Change opacity to make some entries look disabled but still be clickable
         *
         * @param isEntryEnabled whether the [android.safetycenter.SafetyCenterEntry] is enabled
         * @param isPreferenceEnabled whether the corresponding preference is enabled
         * @param titleView view displaying the title text of the entry
         * @param summaryView view displaying the summary text of the entry
         */
        fun changeEnabledState(
            context: Context,
            isEntryEnabled: Boolean,
            isPreferenceEnabled: Boolean,
            titleView: TextView?,
            summaryView: TextView?
        ) {
            val disabledAlpha = getDisabledAlpha(context)
            if (isEntryEnabled) {
                titleView?.alpha = 1f
                summaryView?.alpha = 1f
            } else if (isPreferenceEnabled) {
                /* Check that preference is enabled before lowering because disabled preferences
                 * already have a low visibility */
                titleView?.alpha = disabledAlpha
                summaryView?.alpha = disabledAlpha
            }
        }

        private fun getDisabledAlpha(context: Context): Float {
            val styledAttributes =
                context.obtainStyledAttributes(intArrayOf(android.R.attr.disabledAlpha))
            try {
                return styledAttributes.getFloat(0, DEFAULT_DISABLED_ALPHA)
            } finally {
                styledAttributes.recycle()
            }
        }
    }
}