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         isTablet: Boolean = false,
45         isManageUsers: Boolean,
46     ): Int {
47         return if (isAddUser && isTablet) {
48             com.android.settingslib.R.drawable.ic_account_circle_filled
49         } else if (isAddUser) {
50             R.drawable.ic_add
51         } else if (isGuest) {
52             com.android.settingslib.R.drawable.ic_account_circle
53         } else if (isAddSupervisedUser) {
54             com.android.settingslib.R.drawable.ic_add_supervised_user
55         } else if (isManageUsers) {
56             R.drawable.ic_manage_users
57         } else {
58             R.drawable.ic_avatar_user
59         }
60     }
61 
62     @JvmStatic
getUserRecordNamenull63     fun getUserRecordName(
64         context: Context,
65         record: UserRecord,
66         isGuestUserAutoCreated: Boolean,
67         isGuestUserResetting: Boolean,
68         isTablet: Boolean = false,
69     ): String {
70         val resourceId: Int? = getGuestUserRecordNameResourceId(record)
71         return when {
72             resourceId != null -> context.getString(resourceId)
73             record.info != null ->
74                 record.info.name
75                     ?: "".also { Log.i(TAG, "Expected display name for: ${record.info}") }
76             else ->
77                 context.getString(
78                     getUserSwitcherActionTextResourceId(
79                         isGuest = record.isGuest,
80                         isGuestUserAutoCreated = isGuestUserAutoCreated,
81                         isGuestUserResetting = isGuestUserResetting,
82                         isAddUser = record.isAddUser,
83                         isAddSupervisedUser = record.isAddSupervisedUser,
84                         isTablet = isTablet,
85                         isManageUsers = record.isManageUsers,
86                     )
87                 )
88         }
89     }
90 
91     /**
92      * Returns the resource ID for a string for the name of the guest user.
93      *
94      * If the given record is not the guest user, returns `null`.
95      */
96     @StringRes
getGuestUserRecordNameResourceIdnull97     fun getGuestUserRecordNameResourceId(record: UserRecord): Int? {
98         return when {
99             record.isGuest && record.isCurrent ->
100                 com.android.settingslib.R.string.guest_exit_quick_settings_button
101             record.isGuest && record.info != null -> com.android.internal.R.string.guest_name
102             else -> null
103         }
104     }
105 
106     @JvmStatic
107     @StringRes
getUserSwitcherActionTextResourceIdnull108     fun getUserSwitcherActionTextResourceId(
109         isGuest: Boolean,
110         isGuestUserAutoCreated: Boolean,
111         isGuestUserResetting: Boolean,
112         isAddUser: Boolean,
113         isAddSupervisedUser: Boolean,
114         isTablet: Boolean = false,
115         isManageUsers: Boolean,
116     ): Int {
117         check(isGuest || isAddUser || isAddSupervisedUser || isManageUsers)
118 
119         return when {
120             isGuest && isGuestUserAutoCreated && isGuestUserResetting ->
121                 com.android.settingslib.R.string.guest_resetting
122             isGuest && isTablet -> com.android.settingslib.R.string.guest_new_guest
123             isGuest && isGuestUserAutoCreated -> com.android.internal.R.string.guest_name
124             isGuest -> com.android.internal.R.string.guest_name
125             isAddUser -> com.android.settingslib.R.string.user_add_user
126             isAddSupervisedUser -> R.string.add_user_supervised
127             isManageUsers -> R.string.manage_users
128             else -> error("This should never happen!")
129         }
130     }
131 
132     /** Alpha value to apply to a user view in the user switcher when it's selectable. */
133     const val USER_SWITCHER_USER_VIEW_SELECTABLE_ALPHA = 1.0f
134 
135     /** Alpha value to apply to a user view in the user switcher when it's not selectable. */
136     const val USER_SWITCHER_USER_VIEW_NOT_SELECTABLE_ALPHA = 0.38f
137 }
138