1 /*
2  * Copyright (C) 2019 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 package com.android.tradefed.device;
17 
18 /**
19  * Similar to UserInfo class from platform.
20  *
21  * <p>This is intended to be similar to android.content.pm.UserInfo.
22  *
23  * <p>Stores data and basic logic around the information for one user.
24  */
25 public final class UserInfo {
26     // From android.content.pm.UserInfo
27     public static final int FLAG_PRIMARY = 0x00000001;
28     public static final int FLAG_GUEST = 0x00000004;
29     public static final int FLAG_RESTRICTED = 0x00000008;
30     public static final int FLAG_EPHEMERAL = 0x00000100;
31     public static final int FLAG_MANAGED_PROFILE = 0x00000020;
32     public static final int FLAG_PROFILE = 0x00001000;
33     public static final int USER_SYSTEM = 0;
34     public static final int FLAG_MAIN = 0x00004000;
35     public static final int FLAG_FOR_TESTING = 0x00008000;
36 
37     public static final int FLAGS_NOT_SECONDARY =
38             FLAG_PRIMARY | FLAG_MANAGED_PROFILE | FLAG_GUEST | FLAG_RESTRICTED;
39     public static final String CLONE_PROFILE_TYPE = "profile.CLONE";
40 
41     public static final String COMMUNAL_PROFILE_TYPE = "profile.COMMUNAL";
42 
43     public static final String PRIVATE_PROFILE_TYPE = "profile.PRIVATE";
44 
45     private final int mUserId;
46     private final String mUserName;
47     private final int mFlag;
48     private final boolean mIsRunning;
49     private String mUserType;
50 
51     /** Supported variants of a user's type in external APIs. */
52     public enum UserType {
53         /** current foreground user of the device */
54         CURRENT,
55         /**
56          * guest user. Only one can exist at a time, may be ephemeral and have more restrictions.
57          */
58         GUEST,
59         /** user flagged as primary on the device; most often primary = system user = user 0 */
60         PRIMARY,
61         /** system user = user 0 */
62         SYSTEM,
63         /**
64          * user flagged as main user on the device; on non-hsum main user = system user = user 0 on
65          * hsum main user = first human user.
66          */
67         MAIN,
68         /** secondary user, i.e. non-primary and non-system. */
69         SECONDARY,
70         /** managed profile user, e.g. work profile. */
71         MANAGED_PROFILE,
72         /** clone profile user */
73         CLONE_PROFILE,
74         /** communal profile user */
75         COMMUNAL_PROFILE,
76         /** private profile user */
77         PRIVATE_PROFILE;
78 
isCurrent()79         public boolean isCurrent() {
80             return this == CURRENT;
81         }
82 
isGuest()83         public boolean isGuest() {
84             return this == GUEST;
85         }
86 
isPrimary()87         public boolean isPrimary() {
88             return this == PRIMARY;
89         }
90 
isSystem()91         public boolean isSystem() {
92             return this == SYSTEM;
93         }
94 
isMain()95         public boolean isMain() {
96             return this == MAIN;
97         }
98 
isSecondary()99         public boolean isSecondary() {
100             return this == SECONDARY;
101         }
102 
isManagedProfile()103         public boolean isManagedProfile() {
104             return this == MANAGED_PROFILE;
105         }
106 
isCloneProfile()107         public boolean isCloneProfile() {
108             return this == CLONE_PROFILE;
109         }
110 
isPrivateProfile()111         public boolean isPrivateProfile() {
112             return this == PRIVATE_PROFILE;
113         }
114 
115         /** Return whether this instance is of profile type. */
isProfile()116         public boolean isProfile() {
117             // Other types are not supported
118             return isManagedProfile() || isCloneProfile() || isPrivateProfile();
119         }
120     }
121 
UserInfo(int userId, String userName, int flag, boolean isRunning)122     public UserInfo(int userId, String userName, int flag, boolean isRunning) {
123         mUserId = userId;
124         mUserName = userName;
125         mFlag = flag;
126         mIsRunning = isRunning;
127     }
128 
UserInfo(int userId, String userName, int flag, boolean isRunning, String userType)129     public UserInfo(int userId, String userName, int flag, boolean isRunning, String userType) {
130         this(userId, userName, flag, isRunning);
131         mUserType = userType;
132     }
133 
userId()134     public int userId() {
135         return mUserId;
136     }
137 
userName()138     public String userName() {
139         return mUserName;
140     }
141 
flag()142     public int flag() {
143         return mFlag;
144     }
145 
isRunning()146     public boolean isRunning() {
147         return mIsRunning;
148     }
149 
isGuest()150     public boolean isGuest() {
151         return (mFlag & FLAG_GUEST) == FLAG_GUEST;
152     }
153 
isPrimary()154     public boolean isPrimary() {
155         return (mFlag & FLAG_PRIMARY) == FLAG_PRIMARY;
156     }
157 
isSecondary()158     public boolean isSecondary() {
159         return !isSystem() && (mFlag & FLAGS_NOT_SECONDARY) == 0;
160     }
161 
isSystem()162     public boolean isSystem() {
163         return mUserId == USER_SYSTEM;
164     }
165 
isMain()166     public boolean isMain() {
167         return (mFlag & FLAG_MAIN) == FLAG_MAIN;
168     }
169 
isManagedProfile()170     public boolean isManagedProfile() {
171         return (mFlag & FLAG_MANAGED_PROFILE) == FLAG_MANAGED_PROFILE;
172     }
173 
isCloneProfile()174     public boolean isCloneProfile() {
175         return CLONE_PROFILE_TYPE.equals(mUserType);
176     }
177 
isPrivateProfile()178     public boolean isPrivateProfile() {
179         return PRIVATE_PROFILE_TYPE.equals(mUserType);
180     }
181 
isCommunalProfile()182     public boolean isCommunalProfile() {
183         return COMMUNAL_PROFILE_TYPE.equals(mUserType);
184     }
185 
isEphemeral()186     public boolean isEphemeral() {
187         return (mFlag & FLAG_EPHEMERAL) == FLAG_EPHEMERAL;
188     }
189 
isFlagForTesting()190     public boolean isFlagForTesting() {
191         return (mFlag & FLAG_FOR_TESTING) == FLAG_FOR_TESTING;
192     }
193 
194     /** Return whether this instance is of the specified type. */
isUserType(UserType userType, int currentUserId)195     public boolean isUserType(UserType userType, int currentUserId) {
196         switch (userType) {
197             case CURRENT:
198                 return mUserId == currentUserId;
199             case GUEST:
200                 return isGuest();
201             case PRIMARY:
202                 return isPrimary();
203             case SYSTEM:
204                 return isSystem();
205             case MAIN:
206                 return isMain();
207             case SECONDARY:
208                 return isSecondary();
209             case MANAGED_PROFILE:
210                 return isManagedProfile();
211             case CLONE_PROFILE:
212                 return isCloneProfile();
213             case COMMUNAL_PROFILE:
214                 return isCommunalProfile();
215             case PRIVATE_PROFILE:
216                 return isPrivateProfile();
217             default:
218                 throw new RuntimeException("Variant not covered: " + userType);
219         }
220     }
221 }
222