summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/ui/handheld/SmartIconLoadPackagePermissionPreference.kt
blob: 27cbd8c15ac0504b6353312332e611b95cd7d710 (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
/*
 * Copyright (C) 2019 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.permission.ui.handheld

import android.app.Application
import android.content.Context
import android.os.UserHandle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.preference.AndroidResources
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.android.permissioncontroller.R
import com.android.permissioncontroller.permission.utils.KotlinUtils

/**
 * A Preference representing a package for a user, which loads and displays its icon only upon
 * being bound to a viewHolder. This lets us synchronously load package icons and labels, while
 * still displaying the PermissionAppsFragment instantly.
 *
 * @param app The current application
 * @param packageName The name of the package whose icon this preference will retrieve
 * @param user The user whose package icon will be retrieved
 * @param context The current context
 */
open class SmartIconLoadPackagePermissionPreference constructor(
    private val app: Application,
    private val packageName: String,
    private val user: UserHandle,
    context: Context
) : Preference(context) {

    private var titleContentDescription: CharSequence? = null

    /**
     * Loads the package's badged icon upon being bound to a viewholder. This allows us to load
     * icons synchronously, because we only load the icons that are visible on the screen.
     */
    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)

        val title = holder.findViewById(android.R.id.title) as TextView
        title.maxLines = 1
        title.ellipsize = TextUtils.TruncateAt.END

        val imageView = holder.findViewById(android.R.id.icon) as ImageView

        imageView.maxWidth =
            context.resources.getDimensionPixelSize(R.dimen.secondary_app_icon_size)
        imageView.maxHeight =
            context.resources.getDimensionPixelSize(R.dimen.secondary_app_icon_size)
        imageView.setImageDrawable(KotlinUtils.getBadgedPackageIcon(app, packageName, user))
        imageView.visibility = View.VISIBLE

        var imageFrame: View? = holder.findViewById(R.id.icon_frame)
        if (imageFrame == null) {
            imageFrame = holder.findViewById(AndroidResources.ANDROID_R_ICON_FRAME)
        }
        if (imageFrame != null) {
            imageFrame.visibility = View.VISIBLE
        }
        holder.findViewById(android.R.id.title)?.let {
            it.contentDescription = titleContentDescription
        }
    }

    fun setTitleContentDescription(contentDescription: CharSequence) {
        titleContentDescription = contentDescription
    }
}