1 /* 2 * Copyright (C) 2019 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 18 19 import android.app.Application 20 import android.content.Context 21 import android.os.UserHandle 22 import android.text.TextUtils 23 import android.view.View 24 import android.widget.ImageView 25 import android.widget.TextView 26 import androidx.preference.AndroidResources 27 import androidx.preference.PreferenceViewHolder 28 import com.android.permissioncontroller.R 29 import com.android.permissioncontroller.permission.utils.KotlinUtils 30 31 /** 32 * A Preference representing a package for a user, which loads and displays its icon only upon being 33 * bound to a viewHolder. This lets us synchronously load package icons and labels, while still 34 * displaying the PermissionAppsFragment instantly. 35 * 36 * @param app The current application 37 * @param packageName The name of the package whose icon this preference will retrieve 38 * @param user The user whose package icon will be retrieved 39 * @param context The current context 40 */ 41 open class SmartIconLoadPackagePermissionPreference( 42 private val app: Application, 43 private val packageName: String, 44 private val user: UserHandle, 45 context: Context 46 ) : PermissionPreference(context) { 47 48 private var titleContentDescription: CharSequence? = null 49 50 /** 51 * Loads the package's badged icon upon being bound to a viewholder. This allows us to load 52 * icons synchronously, because we only load the icons that are visible on the screen. 53 */ onBindViewHoldernull54 override fun onBindViewHolder(holder: PreferenceViewHolder) { 55 super.onBindViewHolder(holder) 56 57 val title = holder.findViewById(android.R.id.title) as TextView 58 title.maxLines = 1 59 title.ellipsize = TextUtils.TruncateAt.END 60 61 val icon = holder.findViewById(android.R.id.icon) as ImageView 62 val iconSize = 63 context.resources.getDimensionPixelSize(R.dimen.permission_preference_app_icon_size) 64 icon.maxWidth = iconSize 65 icon.maxHeight = iconSize 66 67 icon.setImageDrawable(KotlinUtils.getBadgedPackageIcon(app, packageName, user)) 68 icon.visibility = View.VISIBLE 69 70 var imageFrame: View? = holder.findViewById(R.id.icon_frame) 71 if (imageFrame == null) { 72 imageFrame = holder.findViewById(AndroidResources.ANDROID_R_ICON_FRAME) 73 } 74 if (imageFrame != null) { 75 imageFrame.visibility = View.VISIBLE 76 } 77 holder.findViewById(android.R.id.title)?.let { 78 it.contentDescription = titleContentDescription 79 } 80 } 81 setTitleContentDescriptionnull82 fun setTitleContentDescription(contentDescription: CharSequence) { 83 titleContentDescription = contentDescription 84 } 85 } 86