Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 816fd236 authored by Annie Meng's avatar Annie Meng
Browse files

[Multi-user] Consolidate system and non-system user unlock callbacks

In Trampoline, initializing the BMS service and starting the service for
the system user are now handled separately. System and non-system users
now both use the same unlock callback to bring up their UserBMS instance.

Bug: 120212806
Test: 1) atest RunBackupFrameworksServicesRoboTests
2) atest TrampolineTest
3) Unlock system user -> starts service for user
4) Multi-user enabled + unlock non-system user -> starts service for
user

Change-Id: Id49bb4a3834eb299be69d924b94a36794a4eb2e4
parent 69be3f19
Loading
Loading
Loading
Loading
+13 −19
Original line number Diff line number Diff line
@@ -124,20 +124,6 @@ public class BackupManagerService {
        }
    }

    /**
     * Called through Trampoline from {@link Lifecycle#onUnlockUser(int)}. We run the heavy work on
     * a background thread to keep the unlock time down.
     */
    public void unlockSystemUser() {
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup enable");
        try {
            sInstance.setBackupEnabled(readBackupEnableState(UserHandle.USER_SYSTEM));
        } catch (RemoteException e) {
            // can't happen; it's a local object
        }
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
    }

    /**
     * Starts the backup service for user {@code userId} by creating a new instance of {@link
     * UserBackupManagerService} and registering it with this service.
@@ -152,10 +138,19 @@ public class BackupManagerService {

    /**
     * Starts the backup service for user {@code userId} by registering its instance of {@link
     * UserBackupManagerService} with this service.
     * UserBackupManagerService} with this service and setting enabled state.
     */
    void startServiceForUser(int userId, UserBackupManagerService userBackupManagerService) {
        mServiceUsers.put(userId, userBackupManagerService);

        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup enable");
        try {
            // TODO(b/121198604): Make enable file per-user and clean up indirection.
            mTrampoline.setBackupEnabledForUser(userId, readBackupEnableState(userId));
        } catch (RemoteException e) {
            // Can't happen, it's a local object.
        }
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
    }

    SparseArray<UserBackupManagerService> getServiceUsers() {
@@ -821,10 +816,9 @@ public class BackupManagerService {
        @Override
        public void onUnlockUser(int userId) {
            if (userId == UserHandle.USER_SYSTEM) {
                sInstance.initializeServiceAndUnlockSystemUser();
            } else {
                sInstance.unlockUser(userId);
                sInstance.initializeService();
            }
            sInstance.unlockUser(userId);
        }
    }
}
+19 −42
Original line number Diff line number Diff line
@@ -150,20 +150,19 @@ public class Trampoline extends IBackupManager.Stub {
    }

    /**
     * Initialize {@link BackupManagerService} if the backup service is not disabled. Only the
     * system user can initialize the service.
     * Called from {@link BackupManagerService.Lifecycle} when the system user is unlocked. Attempts
     * to initialize {@link BackupManagerService}. Offloads work onto the handler thread {@link
     * #mHandlerThread} to keep unlock time low.
     */
    /* package */ void initializeService(int userId) {
    void initializeService() {
        postToHandler(
                () -> {
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup init");
                    if (mGlobalDisable) {
                        Slog.i(TAG, "Backup service not supported");
                        return;
                    }

        if (userId != UserHandle.USER_SYSTEM) {
            Slog.i(TAG, "Cannot initialize backup service for non-system user: " + userId);
            return;
        }

                    synchronized (mStateLock) {
                        if (!mSuppressFile.exists()) {
                            mService = createBackupManagerService();
@@ -171,40 +170,18 @@ public class Trampoline extends IBackupManager.Stub {
                            Slog.i(TAG, "Backup service inactive");
                        }
                    }
    }

    /**
     * Called from {@link BackupManagerService.Lifecycle} when the system user is unlocked. Attempts
     * to initialize {@link BackupManagerService} and set backup state for the system user.
     */
    void initializeServiceAndUnlockSystemUser() {
        postToHandler(
                () -> {
                    // Initialize the backup service.
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup init");
                    initializeService(UserHandle.USER_SYSTEM);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

                    // Start the service for the system user.
                    BackupManagerService service = mService;
                    if (service != null) {
                        Slog.i(TAG, "Starting service for system user");
                        service.startServiceForUser(UserHandle.USER_SYSTEM);
                        Slog.i(TAG, "Unlocking system user");
                        service.unlockSystemUser();
                    }
                });
    }

    /**
     * Called from {@link BackupManagerService.Lifecycle} when a non-system user {@code userId} is
     * unlocked. Starts the backup service for this user if the service supports multi-user.
     * Offloads work onto the handler thread {@link #mHandlerThread} to keep unlock time low.
     * Called from {@link BackupManagerService.Lifecycle} when a user {@code userId} is unlocked.
     * Starts the backup service for this user if it's the system user or if the service supports
     * multi-user. Offloads work onto the handler thread {@link #mHandlerThread} to keep unlock time
     * low.
     */
    // TODO(b/120212806): Consolidate service start for system and non-system users when system
    // user-only logic is removed.
    void unlockUser(int userId) {
        if (!isMultiUserEnabled()) {
        if (userId != UserHandle.USER_SYSTEM && !isMultiUserEnabled()) {
            Slog.i(TAG, "Multi-user disabled, cannot start service for user: " + userId);
            return;
        }
+84 −77

File changed.

Preview size limit exceeded, changes collapsed.