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

Commit 99ed4259 authored by Kholoud Mohamed's avatar Kholoud Mohamed
Browse files

Fix bad state for clouddpc users

This fix will be removed before the SDK is finalised.

Bug: 332477138
Test: manually got the device into the bad state then applied the fix
Change-Id: I3b970a17833b0bdd5939b271d8c3027e9d7ddc33
parent 17205499
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -233,3 +233,13 @@ flag {
    purpose: PURPOSE_BUGFIX
  }
}

flag {
    name: "headless_single_user_bad_device_admin_state_fix"
    namespace: "enterprise"
    description: "Fix the bad state in DPMS caused by an earlier bug related to the headless single user change"
    bug: "332477138"
    metadata {
      purpose: PURPOSE_BUGFIX
    }
}
+30 −0
Original line number Diff line number Diff line
@@ -2251,11 +2251,41 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                if (userHandle == UserHandle.USER_SYSTEM) {
                    mStateCache.setDeviceProvisioned(policy.mUserSetupComplete);
                }
                if (Flags.headlessSingleUserBadDeviceAdminStateFix()) {
                    fixBadDeviceAdminStateForInternalUsers(userHandle, policy);
                }
            }
            return policy;
        }
    }
    private void fixBadDeviceAdminStateForInternalUsers(int userId, DevicePolicyData policy) {
        ComponentName component = mOwners.getDeviceOwnerComponent();
        int doUserId = mOwners.getDeviceOwnerUserId();
        ComponentName cloudDpc = new ComponentName(
                "com.google.android.apps.work.clouddpc",
                "com.google.android.apps.work.clouddpc.receivers.CloudDeviceAdminReceiver");
        if (component == null || doUserId != userId || !component.equals(cloudDpc)) {
            return;
        }
        Slogf.i(LOG_TAG, "Attempting to apply a temp fix for cloudpc internal users' bad state.");
        final int n = policy.mAdminList.size();
        for (int i = 0; i < n; i++) {
            ActiveAdmin admin = policy.mAdminList.get(i);
            if (component.equals(admin.info.getComponent())) {
                Slogf.i(LOG_TAG, "An ActiveAdmin already exists, fix not required.");
                return;
            }
        }
        DeviceAdminInfo dai = findAdmin(component, userId, /* throwForMissingPermission= */ false);
        if (dai != null) {
            ActiveAdmin ap = new ActiveAdmin(dai, /* parent */ false);
            policy.mAdminMap.put(ap.info.getComponent(), ap);
            policy.mAdminList.add(ap);
            Slogf.i(LOG_TAG, "Fix applied, an ActiveAdmin has been added.");
        }
    }
    /**
     * Creates and loads the policy data from xml for data that is shared between
     * various profiles of a user. In contrast to {@link #getUserData(int)}