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 18 package com.android.systemui.user.legacyhelper.ui 19 20 import android.content.Context 21 import androidx.annotation.DrawableRes 22 import androidx.annotation.StringRes 23 import com.android.systemui.R 24 import com.android.systemui.user.data.source.UserRecord 25 26 /** 27 * Defines utility functions for helping with legacy UI code for users. 28 * 29 * We need these to avoid code duplication between logic inside the UserSwitcherController and in 30 * modern architecture classes such as repositories, interactors, and view-models. If we ever 31 * simplify UserSwitcherController (or delete it), the code here could be moved into its call-sites. 32 */ 33 object LegacyUserUiHelper { 34 35 @JvmStatic 36 @DrawableRes getUserSwitcherActionIconResourceIdnull37 fun getUserSwitcherActionIconResourceId( 38 isAddUser: Boolean, 39 isGuest: Boolean, 40 isAddSupervisedUser: Boolean, 41 isTablet: Boolean = false, 42 isManageUsers: Boolean, 43 ): Int { 44 return if (isAddUser && isTablet) { 45 R.drawable.ic_account_circle_filled 46 } else if (isAddUser) { 47 R.drawable.ic_add 48 } else if (isGuest) { 49 R.drawable.ic_account_circle 50 } else if (isAddSupervisedUser) { 51 R.drawable.ic_add_supervised_user 52 } else if (isManageUsers) { 53 R.drawable.ic_manage_users 54 } else { 55 R.drawable.ic_avatar_user 56 } 57 } 58 59 @JvmStatic getUserRecordNamenull60 fun getUserRecordName( 61 context: Context, 62 record: UserRecord, 63 isGuestUserAutoCreated: Boolean, 64 isGuestUserResetting: Boolean, 65 isTablet: Boolean = false, 66 ): String { 67 val resourceId: Int? = getGuestUserRecordNameResourceId(record) 68 return when { 69 resourceId != null -> context.getString(resourceId) 70 record.info != null -> record.info.name 71 else -> 72 context.getString( 73 getUserSwitcherActionTextResourceId( 74 isGuest = record.isGuest, 75 isGuestUserAutoCreated = isGuestUserAutoCreated, 76 isGuestUserResetting = isGuestUserResetting, 77 isAddUser = record.isAddUser, 78 isAddSupervisedUser = record.isAddSupervisedUser, 79 isTablet = isTablet, 80 isManageUsers = record.isManageUsers, 81 ) 82 ) 83 } 84 } 85 86 /** 87 * Returns the resource ID for a string for the name of the guest user. 88 * 89 * If the given record is not the guest user, returns `null`. 90 */ 91 @StringRes getGuestUserRecordNameResourceIdnull92 fun getGuestUserRecordNameResourceId(record: UserRecord): Int? { 93 return when { 94 record.isGuest && record.isCurrent -> 95 com.android.settingslib.R.string.guest_exit_quick_settings_button 96 record.isGuest && record.info != null -> com.android.internal.R.string.guest_name 97 else -> null 98 } 99 } 100 101 @JvmStatic 102 @StringRes getUserSwitcherActionTextResourceIdnull103 fun getUserSwitcherActionTextResourceId( 104 isGuest: Boolean, 105 isGuestUserAutoCreated: Boolean, 106 isGuestUserResetting: Boolean, 107 isAddUser: Boolean, 108 isAddSupervisedUser: Boolean, 109 isTablet: Boolean = false, 110 isManageUsers: Boolean, 111 ): Int { 112 check(isGuest || isAddUser || isAddSupervisedUser || isManageUsers) 113 114 return when { 115 isGuest && isGuestUserAutoCreated && isGuestUserResetting -> 116 com.android.settingslib.R.string.guest_resetting 117 isGuest && isTablet -> com.android.settingslib.R.string.guest_new_guest 118 isGuest && isGuestUserAutoCreated -> com.android.internal.R.string.guest_name 119 isGuest -> com.android.internal.R.string.guest_name 120 isAddUser -> com.android.settingslib.R.string.user_add_user 121 isAddSupervisedUser -> R.string.add_user_supervised 122 isManageUsers -> R.string.manage_users 123 else -> error("This should never happen!") 124 } 125 } 126 127 /** Alpha value to apply to a user view in the user switcher when it's selectable. */ 128 const val USER_SWITCHER_USER_VIEW_SELECTABLE_ALPHA = 1.0f 129 130 /** Alpha value to apply to a user view in the user switcher when it's not selectable. */ 131 const val USER_SWITCHER_USER_VIEW_NOT_SELECTABLE_ALPHA = 0.38f 132 } 133