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

Commit a8b8a80b authored by Annie Meng's avatar Annie Meng Committed by Android (Google) Code Review
Browse files

Merge "[Multi-user] Decouple UserBMS and BMS creation"

parents 66daf214 6a93c233
Loading
Loading
Loading
Loading
+311 −78

File changed.

Preview size limit exceeded, changes collapsed.

+21 −11
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ public class Trampoline extends IBackupManager.Stub {
        return SystemProperties.getBoolean(BACKUP_DISABLE_PROPERTY, false);
    }

    protected boolean isMultiUserEnabled() {
    private boolean isMultiUserEnabled() {
        return Settings.Global.getInt(
                mContext.getContentResolver(),
                Settings.Global.BACKUP_MULTI_USER_ENABLED,
@@ -145,6 +145,10 @@ public class Trampoline extends IBackupManager.Stub {
        return new BackupManagerService(mContext, this, mHandlerThread);
    }

    protected void postToHandler(Runnable runnable) {
        mHandler.post(runnable);
    }

    /**
     * Initialize {@link BackupManagerService} if the backup service is not disabled. Only the
     * system user can initialize the service.
@@ -174,14 +178,18 @@ public class Trampoline extends IBackupManager.Stub {
     * to initialize {@link BackupManagerService} and set backup state for the system user.
     */
    void initializeServiceAndUnlockSystemUser() {
        mHandler.post(
        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();
                    }
@@ -195,20 +203,21 @@ public class Trampoline extends IBackupManager.Stub {
     */
    // TODO(b/120212806): Consolidate service start for system and non-system users when system
    // user-only logic is removed.
    void startServiceForUser(int userId) {
    void unlockUser(int userId) {
        if (!isMultiUserEnabled()) {
            Slog.i(TAG, "Multi-user disabled, cannot start service for user: " + userId);
            return;
        }

        mHandler.post(
                () -> {
        postToHandler(() -> startServiceForUser(userId));
    }

    private void startServiceForUser(int userId) {
        BackupManagerService service = mService;
        if (service != null) {
            Slog.i(TAG, "Starting service for user: " + userId);
            service.startServiceForUser(userId);
        }
                });
    }

    /**
@@ -242,6 +251,7 @@ public class Trampoline extends IBackupManager.Stub {
            if (makeActive) {
                mService = createBackupManagerService();
                mSuppressFile.delete();
                startServiceForUser(userId);
            } else {
                mService = null;
                try {
+958 −222

File changed.

Preview size limit exceeded, changes collapsed.

+11 −4
Original line number Diff line number Diff line
@@ -132,19 +132,21 @@ public class TrampolineTest {
    }

    @Test
    public void startServiceForUser_whenMultiUserSettingDisabled_isIgnored() {
    public void unlockUser_whenMultiUserSettingDisabled_isIgnoredForNonSystemUser() {
        Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 0);
        mTrampoline.initializeService(UserHandle.USER_SYSTEM);

        mTrampoline.startServiceForUser(10);
        mTrampoline.unlockUser(10);

        verify(mBackupManagerServiceMock, never()).startServiceForUser(10);
    }

    @Test
    public void startServiceForUser_whenMultiUserSettingEnabled_callsBackupManagerService() {
    public void unlockUser_whenMultiUserSettingEnabled_callsBackupManagerServiceForNonSystemUser() {
        Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 1);
        mTrampoline.initializeService(UserHandle.USER_SYSTEM);

        mTrampoline.startServiceForUser(10);
        mTrampoline.unlockUser(10);

        verify(mBackupManagerServiceMock).startServiceForUser(10);
    }
@@ -989,6 +991,11 @@ public class TrampolineTest {
            return sBackupManagerServiceMock;
        }

        @Override
        protected void postToHandler(Runnable runnable) {
            runnable.run();
        }

        int getCreateServiceCallsCount() {
            return mCreateServiceCallsCount;
        }