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