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