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 { 39 (!controller.isKeyguardShowing || !it.isRestricted) && 40 (controller.isUserSwitcherEnabled || it.isCurrent) 41 } 42 43 init { 44 controller.addAdapter(WeakReference(this)) 45 } 46 getCountnull47 override fun getCount(): Int { 48 return users.size 49 } 50 getItemnull51 override fun getItem(position: Int): UserRecord { 52 return users[position] 53 } 54 getItemIdnull55 override fun getItemId(position: Int): Long { 56 return position.toLong() 57 } 58 59 /** 60 * Notifies that a user item in the UI has been clicked. 61 * 62 * If the user switcher is hosted in a dialog, passing a non-null [dialogShower] will allow 63 * animation to and from the parent dialog. 64 */ 65 @JvmOverloads onUserListItemClickednull66 fun onUserListItemClicked( 67 record: UserRecord, 68 dialogShower: DialogShower? = null, 69 ) { 70 controller.onUserListItemClicked(record, dialogShower) 71 } 72 getNamenull73 open fun getName(context: Context, item: UserRecord): String { 74 return getName(context, item, false) 75 } 76 77 /** Returns the name for the given {@link UserRecord}. */ getNamenull78 open fun getName(context: Context, item: UserRecord, isTablet: Boolean): String { 79 return getUserRecordName( 80 context = context, 81 record = item, 82 isGuestUserAutoCreated = controller.isGuestUserAutoCreated, 83 isGuestUserResetting = controller.isGuestUserResetting, 84 isTablet = isTablet, 85 ) 86 } 87 refreshnull88 fun refresh() { 89 controller.refreshUsers() 90 } 91 92 companion object { 93 @JvmStatic <lambda>null94 protected val disabledUserAvatarColorFilter: ColorFilter by lazy { 95 val matrix = ColorMatrix() 96 matrix.setSaturation(0f) // 0 - grayscale 97 ColorMatrixColorFilter(matrix) 98 } 99 100 @JvmStatic 101 @JvmOverloads getIconDrawablenull102 protected fun getIconDrawable( 103 context: Context, 104 item: UserRecord, 105 isTablet: Boolean = false, 106 ): Drawable { 107 val iconRes = 108 getUserSwitcherActionIconResourceId( 109 item.isAddUser, 110 item.isGuest, 111 item.isAddSupervisedUser, 112 isTablet, 113 item.isManageUsers, 114 ) 115 return checkNotNull(context.getDrawable(iconRes)) 116 } 117 } 118 } 119