AOSP Framework & Internals
3 min read

UserManagerService

Learn about UserManagerService.

UserManagerService (UMS) governs identity, user lifecycles, and device profiles in Android. It enables the system to safely isolate data and runtime states for multiple humans or personas sharing a single physical device.

Multi-user Support Internals

Android's multi-user architecture extends the Linux user model. In standard Linux, a UID maps to an account. In Android, the traditional Linux UID is mapped to an application (app sandbox). To support multiple Android users, the system multiplexes the Linux UID space.

An Android UID is calculated as: uid = (userId * PER_USER_RANGE) + appId

Where PER_USER_RANGE is typically 100,000. If an app has an appId of 10050, for the primary user (User 0), the UID is 10050. For a secondary user (User 10), the UID becomes 1010050. UserManagerService coordinates with PackageManagerService to maintain this mapping.

User Types

Android defines several distinct user types, defined natively in UserManager.java:

  • Primary (System) User (User 0): The default user created on first boot. It owns device level configurations like network settings and hardware states.
  • Secondary User: A fully separated space with its own apps, accounts, and settings.
  • Guest User: A transient secondary user. Data is wiped when the guest session ends.
  • Profile (e.g., Work Profile): A linked user space. Profiles share the UI with their parent user (e.g., apps from both appear in the same launcher, denoted by a briefcase badge) but maintain separate data and accounts.

UserInfo and UserData Structures

Internally, UMS tracks state using UserData and exposes it via UserInfo. The system persists user metadata in /data/system/users/.

The main file, /data/system/users/userlist.xml, defines the active users. Each user has a dedicated XML file (e.g., 0.xml, 10.xml) storing flags, restrictions, and creation times.

<!-- Example structure of a user XML file -->
<user id="10" serialNumber="10" flags="16" type="android.os.usertype.profile.MANAGED" creationTime="1610000000000" lastLoggedInTime="1610005000000" profileGroupId="0">
    <name>Work</name>
</user>

User Switching Mechanism

Switching users is a heavy operation orchestrated by ActivityManagerService (AMS), heavily relying on UMS.

  1. Request: A user switch is requested via ActivityManager.switchUser(userId).
  2. Freezing: AMS freezes the current user's activities.
  3. Data Preparation: UMS ensures the target user's encrypted credential storage (CE directory) is unlocked if a PIN/Password is provided, communicating with vold.
  4. Process Management: AMS starts the target user's persistent services and system UI components. Apps belonging to the background user may be frozen or killed depending on memory constraints.
  5. Broadcasts: The system fires ACTION_USER_BACKGROUND for the outgoing user and ACTION_USER_FOREGROUND for the incoming user.
  6. Display: WindowManager switches the visible surface layers to the target user's Activity stack.

You can trigger a user switch via adb for debugging:

adb shell am switch-user 10