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.systemui.statusbar.policy 18 19 import android.content.Context 20 import android.graphics.ColorFilter 21 import android.graphics.ColorMatrix 22 import android.graphics.ColorMatrixColorFilter 23 import android.graphics.drawable.Drawable 24 import android.widget.BaseAdapter 25 import com.android.systemui.qs.user.UserSwitchDialogController.DialogShower 26 import com.android.systemui.user.data.source.UserRecord 27 import com.android.systemui.user.legacyhelper.ui.LegacyUserUiHelper.getUserRecordName 28 import com.android.systemui.user.legacyhelper.ui.LegacyUserUiHelper.getUserSwitcherActionIconResourceId 29 import java.lang.ref.WeakReference 30 31 /** Provides views for user switcher experiences. */ 32 abstract class BaseUserSwitcherAdapter 33 protected constructor( 34 protected val controller: UserSwitcherController, 35 ) : BaseAdapter() { 36 37 protected open val users: List<UserRecord> <lambda>null38 get() = controller.users.filter { !controller.isKeyguardShowing || !it.isRestricted } 39 40 init { 41 controller.addAdapter(WeakReference(this)) 42 } 43 getCountnull44 override fun getCount(): Int { 45 return users.size 46 } 47 getItemnull48 override fun getItem(position: Int): UserRecord { 49 return users[position] 50 } 51 getItemIdnull52 override fun getItemId(position: Int): Long { 53 return position.toLong() 54 } 55 56 /** 57 * Notifies that a user item in the UI has been clicked. 58 * 59 * If the user switcher is hosted in a dialog, passing a non-null [dialogShower] will allow 60 * animation to and from the parent dialog. 61 */ 62 @JvmOverloads onUserListItemClickednull63 fun onUserListItemClicked( 64 record: UserRecord, 65 dialogShower: DialogShower? = null, 66 ) { 67 controller.onUserListItemClicked(record, dialogShower) 68 } 69 getNamenull70 open fun getName(context: Context, item: UserRecord): String { 71 return getName(context, item, false) 72 } 73 74 /** Returns the name for the given {@link UserRecord}. */ getNamenull75 open fun getName(context: Context, item: UserRecord, isTablet: Boolean): String { 76 return getUserRecordName( 77 context = context, 78 record = item, 79 isGuestUserAutoCreated = controller.isGuestUserAutoCreated, 80 isGuestUserResetting = controller.isGuestUserResetting, 81 isTablet = isTablet, 82 ) 83 } 84 refreshnull85 fun refresh() { 86 controller.refreshUsers() 87 } 88 89 companion object { 90 @JvmStatic <lambda>null91 protected val disabledUserAvatarColorFilter: ColorFilter by lazy { 92 val matrix = ColorMatrix() 93 matrix.setSaturation(0f) // 0 - grayscale 94 ColorMatrixColorFilter(matrix) 95 } 96 97 @JvmStatic 98 @JvmOverloads getIconDrawablenull99 protected fun getIconDrawable( 100 context: Context, 101 item: UserRecord, 102 isTablet: Boolean = false, 103 ): Drawable { 104 val iconRes = 105 getUserSwitcherActionIconResourceId( 106 item.isAddUser, 107 item.isGuest, 108 item.isAddSupervisedUser, 109 isTablet, 110 item.isManageUsers, 111 ) 112 return checkNotNull(context.getDrawable(iconRes)) 113 } 114 } 115 } 116