1 /*
2  * Copyright (C) 2023 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.car.userpicker;
18 
19 import static android.view.Display.INVALID_DISPLAY;
20 
21 import android.content.pm.UserInfo;
22 import android.graphics.drawable.Drawable;
23 import android.util.Log;
24 import android.view.View;
25 
26 import androidx.annotation.NonNull;
27 
28 /**
29  * Object wrapper class for {@link UserInfo}.  Use it to distinguish if a profile is a
30  * guest profile, add user profile, or the foreground user, and user state (logged-in, display)
31  */
32 final class UserRecord {
33     private static final String TAG = UserRecord.class.getSimpleName();
34     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
35 
36     public final UserInfo mInfo;
37     public final String mName;
38     public final boolean mIsStartGuestSession;
39     public final boolean mIsAddUser;
40     public final boolean mIsForeground;
41     public final Drawable mIcon;
42     public final boolean mIsLoggedIn;
43     public final int mLoggedInDisplay;
44     public final String mSeatLocationName;
45     public final boolean mIsStopping;
46 
47     // This is set from {@link UserPickerController}
48     public View.OnClickListener mOnClickListener;
49 
UserRecord(UserInfo info, String name, boolean isStartGuestSession, boolean isAddUser, boolean isForeground, Drawable icon, boolean isLoggedIn, int loggedInDisplay, String seatLocationName, boolean isStopping)50     private UserRecord(UserInfo info, String name, boolean isStartGuestSession, boolean isAddUser,
51             boolean isForeground, Drawable icon, boolean isLoggedIn, int loggedInDisplay,
52             String seatLocationName, boolean isStopping) {
53         mInfo = info;
54         mName = name;
55         mIsStartGuestSession = isStartGuestSession;
56         mIsAddUser = isAddUser;
57         mIsForeground = isForeground;
58         mIcon = icon;
59         mIsLoggedIn = isLoggedIn;
60         mLoggedInDisplay = loggedInDisplay;
61         mSeatLocationName = seatLocationName;
62         mIsStopping = isStopping;
63     }
64 
create(UserInfo info, String name, boolean isStartGuestSession, boolean isAddUser, boolean isForeground, Drawable icon, OnClickListenerCreatorBase listenerMaker)65     static UserRecord create(UserInfo info, String name, boolean isStartGuestSession,
66             boolean isAddUser, boolean isForeground, Drawable icon,
67             OnClickListenerCreatorBase listenerMaker) {
68         return create(info, name, isStartGuestSession, isAddUser, isForeground, icon, listenerMaker,
69                 false, INVALID_DISPLAY, null, false);
70     }
71 
create(UserInfo info, String name, boolean isStartGuestSession, boolean isAddUser, boolean isForeground, Drawable icon, OnClickListenerCreatorBase listenerMaker, boolean isLoggedIn, int loggedInDisplay, String seatLocationName, boolean isStopping)72     static UserRecord create(UserInfo info, String name, boolean isStartGuestSession,
73             boolean isAddUser, boolean isForeground, Drawable icon,
74             OnClickListenerCreatorBase listenerMaker, boolean isLoggedIn, int loggedInDisplay,
75             String seatLocationName, boolean isStopping) {
76         UserRecord userRecord = new UserRecord(info, name, isStartGuestSession, isAddUser,
77                 isForeground, icon, isLoggedIn, loggedInDisplay, seatLocationName, isStopping);
78         listenerMaker.setUserRecord(userRecord);
79         userRecord.mOnClickListener = listenerMaker.createOnClickListenerWithUserRecord();
80         return userRecord;
81     }
82 
83     abstract static class OnClickListenerCreatorBase {
84         protected UserRecord mUserRecord;
85 
setUserRecord(UserRecord userRecord)86         void setUserRecord(UserRecord userRecord) {
87             mUserRecord = userRecord;
88         }
89 
createOnClickListenerWithUserRecord()90         abstract View.OnClickListener createOnClickListenerWithUserRecord();
91     }
92 
93     @NonNull
94     @Override
toString()95     public String toString() {
96         if (DEBUG) {
97             StringBuilder sb = new StringBuilder();
98             sb.append("UserRecord@").append(Integer.toHexString(hashCode())).append(" [");
99             if (mInfo != null) {
100                 sb.append("userId=").append(mInfo.id).append(" ");
101             }
102             sb.append("name='").append(mName).append("' ");
103             sb.append("isStartGuestSession=").append(mIsStartGuestSession).append(" ");
104             sb.append("isAddUser=").append(mIsAddUser).append(" ");
105             sb.append("isForeground=").append(mIsForeground).append(" ");
106             sb.append("isLoggedIn=").append(mIsLoggedIn).append(" ");
107             sb.append("loggedInDisplay=").append(mLoggedInDisplay).append(" ");
108             sb.append("seatLocationName='").append(mSeatLocationName).append("' ");
109             sb.append("isStopping=").append(mIsStopping).append("]");
110             return sb.toString();
111         } else {
112             return super.toString();
113         }
114     }
115 }
116