• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.server.pm;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SpecialUsers.CanBeNULL;
22 import android.annotation.UserIdInt;
23 import android.content.Context;
24 import android.content.pm.LauncherUserInfo;
25 import android.content.pm.UserInfo;
26 import android.content.pm.UserProperties;
27 import android.content.res.Resources;
28 import android.graphics.Bitmap;
29 import android.multiuser.Flags;
30 import android.os.Bundle;
31 import android.os.UserManager;
32 import android.util.DebugUtils;
33 
34 import com.android.internal.annotations.Keep;
35 
36 import java.lang.annotation.Retention;
37 import java.lang.annotation.RetentionPolicy;
38 import java.util.List;
39 
40 /**
41  * @hide Only for use within the system server.
42  */
43 public abstract class UserManagerInternal {
44 
45     public static final int OWNER_TYPE_DEVICE_OWNER = 0;
46     public static final int OWNER_TYPE_PROFILE_OWNER = 1;
47     public static final int OWNER_TYPE_PROFILE_OWNER_OF_ORGANIZATION_OWNED_DEVICE = 2;
48     public static final int OWNER_TYPE_NO_OWNER = 3;
49 
50     @Retention(RetentionPolicy.SOURCE)
51     @IntDef(value = {OWNER_TYPE_DEVICE_OWNER, OWNER_TYPE_PROFILE_OWNER,
52             OWNER_TYPE_PROFILE_OWNER_OF_ORGANIZATION_OWNED_DEVICE, OWNER_TYPE_NO_OWNER})
53     public @interface OwnerType {
54     }
55 
56     // TODO(b/248408342): Move keep annotation to the method referencing these fields reflectively.
57     @Keep public static final int USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE = 1;
58     @Keep public static final int USER_ASSIGNMENT_RESULT_SUCCESS_INVISIBLE = 2;
59     @Keep public static final int USER_ASSIGNMENT_RESULT_SUCCESS_ALREADY_VISIBLE = 3;
60     @Keep public static final int USER_ASSIGNMENT_RESULT_FAILURE = -1;
61 
62     private static final String PREFIX_USER_ASSIGNMENT_RESULT = "USER_ASSIGNMENT_RESULT_";
63     @IntDef(flag = false, prefix = {PREFIX_USER_ASSIGNMENT_RESULT}, value = {
64             USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE,
65             USER_ASSIGNMENT_RESULT_SUCCESS_INVISIBLE,
66             USER_ASSIGNMENT_RESULT_SUCCESS_ALREADY_VISIBLE,
67             USER_ASSIGNMENT_RESULT_FAILURE
68     })
69     public @interface UserAssignmentResult {}
70 
71     private static final String PREFIX_USER_START_MODE = "USER_START_MODE_";
72 
73     /**
74      * Type used to indicate how a user started.
75      */
76     @IntDef(flag = false, prefix = {PREFIX_USER_START_MODE}, value = {
77             USER_START_MODE_FOREGROUND,
78             USER_START_MODE_BACKGROUND,
79             USER_START_MODE_BACKGROUND_VISIBLE
80     })
81     public @interface UserStartMode {}
82 
83     // TODO(b/248408342): Move keep annotations below to the method referencing these fields
84     // reflectively.
85 
86     /** (Full) user started on foreground (a.k.a. "current user"). */
87     @Keep public static final int USER_START_MODE_FOREGROUND = 1;
88 
89     /**
90      * User (full or profile) started on background and is
91      * {@link UserManager#isUserVisible() invisible}.
92      *
93      * <p>This is the "traditional" way of starting a background user, and can be used to start
94      * profiles as well, although starting an invisible profile is not common from the System UI
95      * (it could be done through APIs or adb, though).
96      */
97     @Keep public static final int USER_START_MODE_BACKGROUND = 2;
98 
99     /**
100      * User (full or profile) started on background and is
101      * {@link UserManager#isUserVisible() visible}.
102      *
103      * <p>This is the "traditional" way of starting a profile (i.e., when the profile of the current
104      * user is the current foreground user), but it can also be used to start a full user associated
105      * with a display (which is the case on automotives with passenger displays).
106      */
107     @Keep public static final int USER_START_MODE_BACKGROUND_VISIBLE = 3;
108 
109     public interface UserRestrictionsListener {
110         /**
111          * Called when a user restriction changes.
112          *
113          * @param userId target user id
114          * @param newRestrictions new user restrictions
115          * @param prevRestrictions user restrictions that were previously set
116          */
onUserRestrictionsChanged(int userId, Bundle newRestrictions, Bundle prevRestrictions)117         void onUserRestrictionsChanged(int userId, Bundle newRestrictions, Bundle prevRestrictions);
118     }
119 
120     /**
121      * Listener for user lifecycle events.
122      *
123      * <p><b>NOTE: </b>implementations MUST not block the current thread.
124      */
125     public interface UserLifecycleListener {
126 
127         /**
128          * Called when a new user is created.
129          *
130          * @param user new user.
131          * @param token token passed to the method that created the user.
132          */
onUserCreated(UserInfo user, @Nullable Object token)133         default void onUserCreated(UserInfo user, @Nullable Object token) {}
134 
135         /** Called when an existing user is removed. */
onUserRemoved(UserInfo user)136         default void onUserRemoved(UserInfo user) {}
137     }
138 
139     /**
140      * Listener for {@link UserManager#isUserVisible() user visibility} changes.
141      */
142     public interface UserVisibilityListener {
143 
144         /**
145          * Called when the {@link UserManager#isUserVisible() user visibility} changed.
146          *
147          * <p><b>Note:</b> this method is called independently of
148          * {@link com.android.server.SystemService} callbacks; for example, the call with
149          * {@code visible} {@code true} might be called before the
150          * {@link com.android.server.SystemService#onUserStarting(com.android.server.SystemService.TargetUser)}
151          * call.
152          */
onUserVisibilityChanged(@serIdInt int userId, boolean visible)153         void onUserVisibilityChanged(@UserIdInt int userId, boolean visible);
154     }
155 
156     /**
157      * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to set
158      * restrictions enforced by the user.
159      *
160      * @param originatingUserId user id of the user where the restrictions originated.
161      * @param global            a bundle of global user restrictions. Global restrictions are
162      *                          restrictions that apply device-wide: to the managed profile,
163      *                          primary profile and secondary users and any profile created in
164      *                          any secondary user.
165      * @param local             a restriction set of local user restrictions. The key is the user
166      *                          id of the user whom the restrictions are targeting.
167      * @param isDeviceOwner     whether {@code originatingUserId} corresponds to device owner
168      *                          user id.
169      */
setDevicePolicyUserRestrictions(int originatingUserId, @Nullable Bundle global, @Nullable RestrictionsSet local, boolean isDeviceOwner)170     public abstract void setDevicePolicyUserRestrictions(int originatingUserId,
171             @Nullable Bundle global, @Nullable RestrictionsSet local, boolean isDeviceOwner);
172 
173     /**
174      * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to set a
175      * user restriction.
176      *
177      * @param userId user id to apply the restriction to. {@link com.android.os.UserHandle.USER_ALL}
178      *               will apply the restriction to all users globally.
179      * @param key    The key of the restriction.
180      * @param value  The value of the restriction.
181      */
setUserRestriction(@serIdInt int userId, @NonNull String key, boolean value)182     public abstract void setUserRestriction(@UserIdInt int userId, @NonNull String key,
183             boolean value);
184 
185     /** Return a user restriction. */
getUserRestriction(int userId, String key)186     public abstract boolean getUserRestriction(int userId, String key);
187 
188     /** Adds a listener to user restriction changes. */
addUserRestrictionsListener(UserRestrictionsListener listener)189     public abstract void addUserRestrictionsListener(UserRestrictionsListener listener);
190 
191     /** Remove a {@link UserRestrictionsListener}. */
removeUserRestrictionsListener(UserRestrictionsListener listener)192     public abstract void removeUserRestrictionsListener(UserRestrictionsListener listener);
193 
194     /** Adds a {@link UserLifecycleListener}. */
addUserLifecycleListener(UserLifecycleListener listener)195     public abstract void addUserLifecycleListener(UserLifecycleListener listener);
196 
197     /** Removes a {@link UserLifecycleListener}. */
removeUserLifecycleListener(UserLifecycleListener listener)198     public abstract void removeUserLifecycleListener(UserLifecycleListener listener);
199 
200     /**
201      * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to update
202      * whether the device is managed by device owner.
203      *
204      * @deprecated Use methods in {@link android.app.admin.DevicePolicyManagerInternal}.
205      */
206     @Deprecated
207     // TODO(b/258213147): Remove
setDeviceManaged(boolean isManaged)208     public abstract void setDeviceManaged(boolean isManaged);
209 
210     /**
211      * Returns whether the device is managed by device owner.
212      *
213      * @deprecated Use methods in {@link android.app.admin.DevicePolicyManagerInternal}.
214      */
215     @Deprecated
216     // TODO(b/258213147): Remove
isDeviceManaged()217     public abstract boolean isDeviceManaged();
218 
219     /**
220      * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to update
221      * whether the user is managed by profile owner.
222      *
223      * @deprecated Use methods in {@link android.app.admin.DevicePolicyManagerInternal}.
224      */
225     // TODO(b/258213147): Remove
226     @Deprecated
setUserManaged(int userId, boolean isManaged)227     public abstract void setUserManaged(int userId, boolean isManaged);
228 
229     /**
230      * Whether a profile owner manages this user.
231      *
232      * @deprecated Use methods in {@link android.app.admin.DevicePolicyManagerInternal}.
233      */
234     // TODO(b/258213147): Remove
235     @Deprecated
isUserManaged(int userId)236     public abstract boolean isUserManaged(int userId);
237 
238     /**
239      * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to omit
240      * restriction check, because DevicePolicyManager must always be able to set user icon
241      * regardless of any restriction.
242      * Also called by {@link com.android.server.pm.UserManagerService} because the logic of setting
243      * the icon is in this method.
244      */
setUserIcon(int userId, Bitmap bitmap)245     public abstract void setUserIcon(int userId, Bitmap bitmap);
246 
247     /**
248      * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to inform the
249      * user manager whether all users should be created ephemeral.
250      */
setForceEphemeralUsers(boolean forceEphemeralUsers)251     public abstract void setForceEphemeralUsers(boolean forceEphemeralUsers);
252 
253     /**
254      * Switches to the system user and deletes all other users.
255      *
256      * <p>Called by the {@link com.android.server.devicepolicy.DevicePolicyManagerService} when
257      * the force-ephemeral-users policy is toggled on to make sure there are no pre-existing
258      * non-ephemeral users left.
259      */
removeAllUsers()260     public abstract void removeAllUsers();
261 
262     /**
263      * Called by the activity manager when the ephemeral user goes to background and its removal
264      * starts as a result.
265      *
266      * <p>It marks the ephemeral user as disabled in order to prevent it from being re-entered
267      * before its removal finishes.
268      *
269      * @param userId the ID of the ephemeral user.
270      */
onEphemeralUserStop(int userId)271     public abstract void onEphemeralUserStop(int userId);
272 
273     /**
274      * Same as UserManager.createUser(), but bypasses the check for
275      * {@link UserManager#DISALLOW_ADD_USER} and {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE}
276      *
277      * <p>Called by the {@link com.android.server.devicepolicy.DevicePolicyManagerService} when
278      * createAndManageUser is called by the device owner; it uses {@code token} to block until
279      * the user is created (as it will be passed back to it through
280      * {@link UserLifecycleListener#onUserCreated(UserInfo, Object)});
281      */
createUserEvenWhenDisallowed( @ullable String name, @NonNull String userType, @UserInfo.UserInfoFlag int flags, @Nullable String[] disallowedPackages, @Nullable Object token)282     public abstract @NonNull UserInfo createUserEvenWhenDisallowed(
283             @Nullable String name, @NonNull String userType, @UserInfo.UserInfoFlag int flags,
284             @Nullable String[] disallowedPackages, @Nullable Object token)
285             throws UserManager.CheckedUserOperationException;
286 
287     /**
288      * Same as {@link UserManager#removeUser(int userId)}, but bypasses the check for
289      * {@link UserManager#DISALLOW_REMOVE_USER} and
290      * {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE} and does not require the
291      * {@link android.Manifest.permission#MANAGE_USERS} permission.
292      */
removeUserEvenWhenDisallowed(int userId)293     public abstract boolean removeUserEvenWhenDisallowed(int userId);
294 
295     /**
296      * Return whether the given user is running in an
297      * {@code UserState.STATE_RUNNING_UNLOCKING} or
298      * {@code UserState.STATE_RUNNING_UNLOCKED} state.
299      */
isUserUnlockingOrUnlocked(int userId)300     public abstract boolean isUserUnlockingOrUnlocked(int userId);
301 
302     /**
303      * Return whether the given user is running in an
304      * {@code UserState.STATE_RUNNING_UNLOCKED} state.
305      */
isUserUnlocked(int userId)306     public abstract boolean isUserUnlocked(int userId);
307 
308     /**
309      * Returns whether the given user is running
310      */
isUserRunning(int userId)311     public abstract boolean isUserRunning(int userId);
312 
313     /**
314      * Returns whether the given user is initialized
315      */
isUserInitialized(int userId)316     public abstract boolean isUserInitialized(int userId);
317 
318     /**
319      * Returns whether the given user exists
320      */
exists(int userId)321     public abstract boolean exists(int userId);
322 
323     /**
324      * Set user's running state
325      */
setUserState(int userId, int userState)326     public abstract void setUserState(int userId, int userState);
327 
328     /**
329      * Remove user's running state
330      */
removeUserState(int userId)331     public abstract void removeUserState(int userId);
332 
333     /**
334      * Returns an array of user ids. This array is cached in UserManagerService and passed as a
335      * reference, so do not modify the returned array.
336      *
337      * @return the array of user ids.
338      */
getUserIds()339     public abstract int[] getUserIds();
340 
341     /**
342      * Internal implementation of getUsers does not check permissions.
343      * This improves performance for calls from inside system server which already have permissions
344      * checked.
345      */
getUsers(boolean excludeDying)346     public abstract @NonNull List<UserInfo> getUsers(boolean excludeDying);
347 
348     /**
349      * Internal implementation of getUsers does not check permissions.
350      * This improves performance for calls from inside system server which already have permissions
351      * checked.
352      */
getUsers(boolean excludePartial, boolean excludeDying, boolean excludePreCreated)353     public abstract @NonNull List<UserInfo> getUsers(boolean excludePartial, boolean excludeDying,
354             boolean excludePreCreated);
355 
356     /**
357      * Returns a list of the users that are associated with the specified user, including the user
358      * itself. This includes the user, its profiles, its parent, and its parent's other profiles,
359      * as applicable.
360      *
361      * <p>Note that this includes all profile types (not including Restricted profiles).
362      *
363      * @param userId      id of the user to return profiles for
364      * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
365      * @return A non-empty array of ids of profiles associated with the specified user if the user
366      *         exists. Otherwise, an empty array.
367      */
getProfileIds(@serIdInt int userId, boolean enabledOnly)368     public abstract @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly);
369 
370     /**
371      * Returns a list of the users that are associated with the specified user, including the user
372      * itself. This includes the user, its profiles, its parent, and its parent's other profiles,
373      * as applicable.
374      *
375      * <p>Note that this includes only profile types that are not hidden.
376      *
377      * @param userId      id of the user to return profiles for
378      * @param enabledOnly whether return only {@link UserInfo#isEnabled() enabled} profiles
379      * @return A non-empty array of ids of profiles associated with the specified user if the user
380      *         exists. Otherwise, an empty array.
381      */
getProfileIdsExcludingHidden(@serIdInt int userId, boolean enabledOnly)382     public abstract @NonNull int[] getProfileIdsExcludingHidden(@UserIdInt int userId,
383             boolean enabledOnly);
384 
385     /**
386      * Checks if the {@code callingUserId} and {@code targetUserId} are same or in same group
387      * and that the {@code callingUserId} is not a profile and {@code targetUserId} is enabled.
388      *
389      * @return TRUE if the {@code callingUserId} can access {@code targetUserId}. FALSE
390      * otherwise
391      *
392      * @throws SecurityException if the calling user and {@code targetUser} are not in the same
393      * group and {@code throwSecurityException} is true, otherwise if will simply return false.
394      */
isProfileAccessible(int callingUserId, int targetUserId, String debugMsg, boolean throwSecurityException)395     public abstract boolean isProfileAccessible(int callingUserId, int targetUserId,
396             String debugMsg, boolean throwSecurityException);
397 
398     /**
399      * If {@code userId} is of a profile, return the parent user ID. Otherwise return itself.
400      */
getProfileParentId(int userId)401     public abstract int getProfileParentId(int userId);
402 
403     /**
404      * Checks whether changing a setting to a value is prohibited by the corresponding user
405      * restriction.
406      *
407      * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
408      * Context, int, String, boolean)}, which should be in sync with this method.
409      *
410      * @return {@code true} if the change is prohibited, {@code false} if the change is allowed.
411      *
412      * @hide
413      */
isSettingRestrictedForUser(String setting, int userId, String value, int callingUid)414     public abstract boolean isSettingRestrictedForUser(String setting, int userId, String value,
415             int callingUid);
416 
417     /** @return a specific user restriction that's in effect currently. */
hasUserRestriction(String restriction, int userId)418     public abstract boolean hasUserRestriction(String restriction, int userId);
419 
420     /**
421      * Gets a {@link UserInfo} for the given {@code userId}, or {@code null} if not found.
422      */
getUserInfo(@serIdInt int userId)423     public abstract @Nullable UserInfo getUserInfo(@UserIdInt int userId);
424 
425     /**
426      * Gets all {@link UserInfo UserInfos}.
427      */
getUserInfos()428     public abstract @NonNull UserInfo[] getUserInfos();
429 
430     /**
431      * Gets a {@link LauncherUserInfo} for the given {@code userId}, or {@code null} if not found.
432      */
getLauncherUserInfo(@serIdInt int userId)433     public abstract @Nullable LauncherUserInfo getLauncherUserInfo(@UserIdInt int userId);
434 
435     /**
436      * Sets all default cross profile intent filters between {@code parentUserId} and
437      * {@code profileUserId}.
438      */
setDefaultCrossProfileIntentFilters( @serIdInt int parentUserId, @UserIdInt int profileUserId)439     public abstract void setDefaultCrossProfileIntentFilters(
440             @UserIdInt int parentUserId, @UserIdInt int profileUserId);
441 
442     /**
443      * Returns {@code true} if the system should ignore errors when preparing
444      * the storage directories for the user with ID {@code userId}. This will
445      * return {@code false} for all new users; it will only return {@code true}
446      * for users that already existed on-disk from an older version of Android.
447      */
shouldIgnorePrepareStorageErrors(int userId)448     public abstract boolean shouldIgnorePrepareStorageErrors(int userId);
449 
450     /**
451      * Returns the {@link UserProperties} of the given user, or {@code null} if it is not found.
452      * NB: The actual object is returned. So do NOT modify it!
453      */
getUserProperties(@serIdInt int userId)454     public abstract @Nullable UserProperties getUserProperties(@UserIdInt int userId);
455 
456     /**
457      * Assigns a user to a display when it's starting, returning whether the assignment succeeded
458      * and the user is {@link UserManager#isUserVisible() visible}.
459      *
460      * <p><b>NOTE: </b>this method is meant to be used only by {@code UserController} (when a user
461      * is started); for extra unassignments, callers should call {@link
462      * #assignUserToExtraDisplay(int, int)} instead.
463      *
464      * <p><b>NOTE: </b>this method doesn't validate if the display exists, it's up to the caller to
465      * pass a valid display id.
466      */
assignUserToDisplayOnStart(@serIdInt int userId, @UserIdInt int profileGroupId, @UserStartMode int userStartMode, int displayId)467     public abstract @UserAssignmentResult int assignUserToDisplayOnStart(@UserIdInt int userId,
468             @UserIdInt int profileGroupId, @UserStartMode int userStartMode, int displayId);
469 
470     /**
471      * Assigns an extra display to the given user, so the user is visible on that display.
472      *
473      * <p>This method is meant to be used on automotive builds where a passenger zone has more than
474      * one display (for example, the "main" display and a smaller display used for input).
475      *
476      * <p><b>NOTE: </b>this call will be ignored on devices that do not
477      * {@link UserManager#isVisibleBackgroundUsersSupported() support visible background users}.
478      *
479      * @return whether the operation succeeded, in which case the user would be visible on the
480      * display.
481      */
assignUserToExtraDisplay(@serIdInt int userId, int displayId)482     public abstract boolean assignUserToExtraDisplay(@UserIdInt int userId, int displayId);
483 
484     /**
485      * Unassigns a user from its current display when it's stopping.
486      *
487      * <p><b>NOTE: </b>this method is meant to be used only by {@code UserController} (when a user
488      * is stopped); for extra unassignments, callers should call
489      * {@link #unassignUserFromExtraDisplay(int, int)} instead.
490      */
unassignUserFromDisplayOnStop(@serIdInt int userId)491     public abstract void unassignUserFromDisplayOnStop(@UserIdInt int userId);
492 
493     /**
494      * Unassigns the extra display from the given user.
495      *
496      * <p>This method is meant to be used on automotive builds where a passenger zone has more than
497      * one display (for example, the "main" display and a smaller display used for input).
498      *
499      * <p><b>NOTE: </b>this call will be ignored on devices that do not
500      * {@link UserManager#isVisibleBackgroundUsersSupported() support visible background users}.
501      *
502      * @return whether the operation succeeded, i.e., the user was previously
503      *         {@link #assignUserToExtraDisplay(int, int) assigned to an extra display}.
504      */
unassignUserFromExtraDisplay(@serIdInt int userId, int displayId)505     public abstract boolean unassignUserFromExtraDisplay(@UserIdInt int userId, int displayId);
506 
507     /**
508      * Returns {@code true} if the user is visible (as defined by
509      * {@link UserManager#isUserVisible()}.
510      */
isUserVisible(@serIdInt int userId)511     public abstract boolean isUserVisible(@UserIdInt int userId);
512 
513     /**
514      * Returns {@code true} if the user is visible (as defined by
515      * {@link UserManager#isUserVisible()} in the given display.
516      */
isUserVisible(@serIdInt int userId, int displayId)517     public abstract boolean isUserVisible(@UserIdInt int userId, int displayId);
518 
519     /**
520      * Checks if the given user is a visible background full user, which is a full background user
521      * assigned to secondary displays on the devices that have
522      * {@link UserManager#isVisibleBackgroundUsersEnabled()
523      * config_multiuserVisibleBackgroundUsers enabled} (for example, passenger users on
524      * automotive builds, using the display associated with their seats).
525      *
526      * @see UserManager#isUserVisible()
527      */
isVisibleBackgroundFullUser(@serIdInt int userId)528     public abstract boolean isVisibleBackgroundFullUser(@UserIdInt int userId);
529 
530     /**
531      * Returns the main display id assigned to the user, or {@code Display.INVALID_DISPLAY} if the
532      * user is not assigned to any main display.
533      *
534      * <p>In the context of multi-user multi-display, there can be multiple main displays, at most
535      * one per each zone. Main displays are where UI is launched which a user interacts with.
536      *
537      * <p>The current foreground user and its running profiles are associated with the
538      * {@link android.view.Display#DEFAULT_DISPLAY default display}, while other users would only be
539      * assigned to a display if a call to {@link #assignUserToDisplay(int, int)} is made for such
540      * user / display combination (for example, if the user was started with
541      * {@code ActivityManager.startUserInBackgroundOnSecondaryDisplay()}, {@code UserController}
542      * would make such call).
543      *
544      * <p>If the user is a profile and is running, it's assigned to its parent display.
545      */
getMainDisplayAssignedToUser(@serIdInt int userId)546     public abstract int getMainDisplayAssignedToUser(@UserIdInt int userId);
547 
548     /**
549      * Returns all display ids assigned to the user including {@link
550      * #assignUserToExtraDisplay(int, int) extra displays}, or {@code null} if there is no display
551      * assigned to the specified user.
552      *
553      * <p>Note that this method is different from {@link #getMainDisplayAssignedToUser(int)}, which
554      * returns a main display only.
555      */
getDisplaysAssignedToUser(@serIdInt int userId)556     public abstract @Nullable int[] getDisplaysAssignedToUser(@UserIdInt int userId);
557 
558     /**
559      * Returns the main user (i.e., not a profile) that is assigned to the display, or the
560      * {@link android.app.ActivityManager#getCurrentUser() current foreground user} if no user is
561      * associated with the display.
562      *
563      * <p>The {@link android.view.Display#DEFAULT_DISPLAY default display} is always assigned to
564      * the current foreground user, while other displays would only be associated with users through
565      * a explicit {@link #assignUserToDisplay(int, int)} call with that user / display combination
566      * (for example, if the user was started with
567      * {@code ActivityManager.startUserInBackgroundOnSecondaryDisplay()}, {@code UserController}
568      * would make such call).
569      */
getUserAssignedToDisplay(int displayId)570     public abstract @UserIdInt int getUserAssignedToDisplay(int displayId);
571 
572     /**
573      * Gets the user-friendly representation of the {@code result} of a
574      * {@link #assignUserToDisplayOnStart(int, int, boolean, int)} call.
575      */
userAssignmentResultToString(@serAssignmentResult int result)576     public static String userAssignmentResultToString(@UserAssignmentResult int result) {
577         return DebugUtils.constantToString(UserManagerInternal.class, PREFIX_USER_ASSIGNMENT_RESULT,
578                 result);
579     }
580 
581     /**
582      * Gets the user-friendly representation of a user start {@code mode}.
583      */
userStartModeToString(@serStartMode int mode)584     public static String userStartModeToString(@UserStartMode int mode) {
585         return DebugUtils.constantToString(UserManagerInternal.class, PREFIX_USER_START_MODE, mode);
586     }
587 
588     /** Adds a {@link UserVisibilityListener}. */
addUserVisibilityListener(UserVisibilityListener listener)589     public abstract void addUserVisibilityListener(UserVisibilityListener listener);
590 
591     /** Removes a {@link UserVisibilityListener}. */
removeUserVisibilityListener(UserVisibilityListener listener)592     public abstract void removeUserVisibilityListener(UserVisibilityListener listener);
593 
594     // TODO(b/242195409): remove this method if not needed anymore
595     /** Notify {@link UserVisibilityListener listeners} that the visibility of the
596      * {@link android.os.UserHandle#USER_SYSTEM} changed. */
onSystemUserVisibilityChanged(boolean visible)597     public abstract void onSystemUserVisibilityChanged(boolean visible);
598 
599     /** Return the integer types of the given user IDs. Only used for reporting metrics to statsd.
600      */
getUserTypesForStatsd(@serIdInt int[] userIds)601     public abstract int[] getUserTypesForStatsd(@UserIdInt int[] userIds);
602 
603     /**
604      * Returns the user id of the main user, or {@link android.os.UserHandle#USER_NULL} if there is
605      * no main user.
606      *
607      * <p>NB: Features should ideally not limit functionality to the main user. Ideally, they
608      * should either work for all users or for all admin users. If a feature should only work for
609      * select users, its determination of which user should be done intelligently or be
610      * customizable. Not all devices support a main user, and the idea of singling out one user as
611      * special is contrary to overall multiuser goals.
612      *
613      * @see UserManager#isMainUser()
614      */
getMainUserId()615     public abstract @UserIdInt int getMainUserId();
616 
617     /**
618      * Returns the id of the user which should be in the foreground after boot completes.
619      *
620      * <p>If a boot user has been provided by calling {@link UserManager#setBootUser}, the
621      * returned value will be whatever was specified, as long as that user exists and can be
622      * switched to.
623      *
624      * <p>Otherwise, in {@link UserManager#isHeadlessSystemUserMode() headless system user mode},
625      * this will be the user who was last in the foreground on this device.
626      *
627      * <p>In non-headless system user mode, the return value will be
628      * {@link android.os.UserHandle#USER_SYSTEM}.
629 
630      * @throws UserManager.CheckedUserOperationException if no switchable user can be found
631      */
getBootUser(boolean waitUntilSet)632     public abstract @UserIdInt int getBootUser(boolean waitUntilSet)
633             throws UserManager.CheckedUserOperationException;
634 
635     /**
636      * Returns the user id of the communal profile, or {@link android.os.UserHandle#USER_NULL}
637      * if there is no such user.
638      */
getCommunalProfileId()639     public abstract @CanBeNULL @UserIdInt int getCommunalProfileId();
640 
641     /**
642      * Returns the user id of the supervising profile, or {@link android.os.UserHandle#USER_NULL} if
643      * there is no such user.
644      */
getSupervisingProfileId()645     public abstract @CanBeNULL @UserIdInt int getSupervisingProfileId();
646 
647     /**
648      * Checks whether to show a notification for sounds (e.g., alarms, timers, etc.) from background
649      * users.
650      */
shouldShowNotificationForBackgroundUserSounds()651     public static boolean shouldShowNotificationForBackgroundUserSounds() {
652         return Flags.addUiForSoundsFromBackgroundUsers() && Resources.getSystem().getBoolean(
653                 com.android.internal.R.bool.config_showNotificationForBackgroundUserAlarms)
654                 && UserManager.supportsMultipleUsers();
655     }
656 }
657