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

Commit 775b26d8 authored by Pavel Grafov's avatar Pavel Grafov
Browse files

Re-activate backup service after cleaning a profile owner

Currently backup service is re-activated unconditionally when clearing
a device owner but not profile owner. With this CL it should be
re-activate in both cases.

NB: there are two bits of state related to backup service:
1. activated or deactivated: This is out of user control, but can be
   changed by the admin via DPM.setBackupServiceEnabled (this name is
   a bit misleading here).
2. enabled or disabled: this is controlled by the user via Settings
   and only available when backup service is activated (see 1.)

Bug: 143274029
Test: atest CtsAdminTestCases && adb shell bmgr enabled
Test: atest com.android.server.devicepolicy.DevicePolicyManagerTest
Change-Id: I6f11642abe544c7df265ed7e2ad466d47796e7f9
parent 07e10e3a
Loading
Loading
Loading
Loading
+4 −11
Original line number Diff line number Diff line
@@ -8105,15 +8105,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        mSecurityLogMonitor.stop();
        setNetworkLoggingActiveInternal(false);
        deleteTransferOwnershipBundleLocked(userId);
        try {
            if (mInjector.getIBackupManager() != null) {
                // Reactivate backup service.
                mInjector.getIBackupManager().setBackupServiceActive(UserHandle.USER_SYSTEM, true);
            }
        } catch (RemoteException e) {
            throw new IllegalStateException("Failed reactivating backup service.", e);
        }
        toggleBackupServiceActive(UserHandle.USER_SYSTEM, true);
    }
    @Override
@@ -8173,7 +8165,6 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        }
    }
    private void toggleBackupServiceActive(int userId, boolean makeActive) {
        long ident = mInjector.binderClearCallingIdentity();
        try {
@@ -8182,7 +8173,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
                        .setBackupServiceActive(userId, makeActive);
            }
        } catch (RemoteException e) {
            throw new IllegalStateException("Failed deactivating backup service.", e);
            throw new IllegalStateException(String.format("Failed %s backup service.",
                    makeActive ? "activating" : "deactivating"), e);
        } finally {
            mInjector.binderRestoreCallingIdentity(ident);
        }
@@ -8233,6 +8225,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        mOwners.removeProfileOwner(userId);
        mOwners.writeProfileOwner(userId);
        deleteTransferOwnershipBundleLocked(userId);
        toggleBackupServiceActive(userId, true);
    }
    @Override
+31 −4
Original line number Diff line number Diff line
@@ -943,10 +943,6 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        verify(getServices().iactivityManager, times(1)).updateDeviceOwner(
                eq(admin1.getPackageName()));

        // TODO We should check if the caller has called clearCallerIdentity().
        verify(getServices().ibackupManager, times(1)).setBackupServiceActive(
                eq(UserHandle.USER_SYSTEM), eq(false));

        verify(mContext.spiedContext, times(1)).sendBroadcastAsUser(
                MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED),
                MockUtils.checkUserHandle(UserHandle.USER_SYSTEM));
@@ -1175,6 +1171,37 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        // TODO Check other calls.
    }

    public void testDeviceOwnerBackupActivateDeactivate() throws Exception {
        mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
        mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);

        // Set admin1 as a DA to the secondary user.
        mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
        setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
        dpm.setActiveAdmin(admin1, /* replace =*/ false);
        assertTrue(dpm.setDeviceOwner(admin1, "owner-name"));

        verify(getServices().ibackupManager, times(1)).setBackupServiceActive(
                eq(UserHandle.USER_SYSTEM), eq(false));

        dpm.clearDeviceOwnerApp(admin1.getPackageName());

        verify(getServices().ibackupManager, times(1)).setBackupServiceActive(
                eq(UserHandle.USER_SYSTEM), eq(true));
    }

    public void testProfileOwnerBackupActivateDeactivate() throws Exception {
        setAsProfileOwner(admin1);

        verify(getServices().ibackupManager, times(1)).setBackupServiceActive(
                eq(DpmMockContext.CALLER_USER_HANDLE), eq(false));

        dpm.clearProfileOwner(admin1);

        verify(getServices().ibackupManager, times(1)).setBackupServiceActive(
                eq(DpmMockContext.CALLER_USER_HANDLE), eq(true));
    }

    public void testClearDeviceOwner_fromDifferentUser() throws Exception {
        mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
        mContext.callerPermissions.add(permission.MANAGE_USERS);