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

Commit 1cd3a8e8 authored by Pavel Grafov's avatar Pavel Grafov
Browse files

Count quiet mode as "off" for max time off policy

Fixes: 280349467
Fixes: 280059883
Test: atest android.devicepolicy.cts.MaximumTimeOffTest
Change-Id: I6bac45ceeedf3ed804cb75bababb9b06aa11bf3a
parent 6a9342d7
Loading
Loading
Loading
Loading
+23 −18
Original line number Diff line number Diff line
@@ -1214,7 +1214,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_STOPPED, userHandle);
                if (isManagedProfile(userHandle)) {
                    Slogf.d(LOG_TAG, "Managed profile was stopped");
                    updatePersonalAppsSuspension(userHandle, false /* unlocked */);
                    updatePersonalAppsSuspension(userHandle);
                }
            } else if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_SWITCHED, userHandle);
@@ -1224,8 +1224,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                }
                if (isManagedProfile(userHandle)) {
                    Slogf.d(LOG_TAG, "Managed profile became unlocked");
                    final boolean suspended =
                            updatePersonalAppsSuspension(userHandle, true /* unlocked */);
                    final boolean suspended = updatePersonalAppsSuspension(userHandle);
                    triggerPolicyComplianceCheckIfNeeded(userHandle, suspended);
                }
            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
@@ -1252,13 +1251,13 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                updateSystemUpdateFreezePeriodsRecord(/* saveIfChanged */ true);
                final int userId = getManagedUserId(getMainUserId());
                if (userId >= 0) {
                    updatePersonalAppsSuspension(userId, mUserManager.isUserUnlocked(userId));
                    updatePersonalAppsSuspension(userId);
                }
            } else if (ACTION_PROFILE_OFF_DEADLINE.equals(action)) {
                Slogf.i(LOG_TAG, "Profile off deadline alarm was triggered");
                final int userId = getManagedUserId(getMainUserId());
                if (userId >= 0) {
                    updatePersonalAppsSuspension(userId, mUserManager.isUserUnlocked(userId));
                    updatePersonalAppsSuspension(userId);
                } else {
                    Slogf.wtf(LOG_TAG, "Got deadline alarm for nonexistent profile");
                }
@@ -1268,9 +1267,12 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            } else if (ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
                notifyIfManagedSubscriptionsAreUnavailable(
                        UserHandle.of(userHandle), /* managedProfileAvailable= */ false);
                updatePersonalAppsSuspension(userHandle);
            } else if (ACTION_MANAGED_PROFILE_AVAILABLE.equals(action)) {
                notifyIfManagedSubscriptionsAreUnavailable(
                        UserHandle.of(userHandle), /* managedProfileAvailable= */ true);
                final boolean suspended = updatePersonalAppsSuspension(userHandle);
                triggerPolicyComplianceCheckIfNeeded(userHandle, suspended);
            } else if (LOGIN_ACCOUNTS_CHANGED_ACTION.equals(action)) {
                calculateHasIncompatibleAccounts();
            }
@@ -3433,7 +3435,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        final int profileUserHandle = getManagedUserId(userHandle);
        if (profileUserHandle >= 0) {
            // Given that the parent user has just started, profile should be locked.
            updatePersonalAppsSuspension(profileUserHandle, false /* unlocked */);
            updatePersonalAppsSuspension(profileUserHandle);
        } else {
            suspendPersonalAppsInternal(userHandle, false);
        }
@@ -20730,7 +20732,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
        mInjector.binderWithCleanCallingIdentity(() -> updatePersonalAppsSuspension(
                callingUserId, mUserManager.isUserUnlocked(callingUserId)));
                callingUserId));
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.SET_PERSONAL_APPS_SUSPENDED)
@@ -20764,16 +20766,19 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
    /**
     * Checks whether personal apps should be suspended according to the policy and applies the
     * change if needed.
     *
     * @param unlocked whether the profile is currently running unlocked.
     */
    private boolean updatePersonalAppsSuspension(int profileUserId, boolean unlocked) {
    private boolean updatePersonalAppsSuspension(int profileUserId) {
        final boolean shouldSuspend;
        synchronized (getLockObject()) {
            final ActiveAdmin profileOwner = getProfileOwnerAdminLocked(profileUserId);
            if (profileOwner != null) {
                final int notificationState =
                        updateProfileOffDeadlineLocked(profileUserId, profileOwner, unlocked);
                // Profile is considered "off" when it is either not running or is running locked
                // or is in quiet mode, i.e. when the admin cannot sync policies or show UI.
                boolean profileUserOff =
                        !mUserManagerInternal.isUserUnlockingOrUnlocked(profileUserId)
                        || mUserManager.isQuietModeEnabled(UserHandle.of(profileUserId));
                final int notificationState = updateProfileOffDeadlineLocked(
                        profileUserId, profileOwner, profileUserOff);
                final boolean suspendedExplicitly = profileOwner.mSuspendPersonalApps;
                final boolean suspendedByTimeout = profileOwner.mProfileOffDeadline == -1;
                Slogf.d(LOG_TAG,
@@ -20798,16 +20803,16 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
     * @return notification state
     */
    private int updateProfileOffDeadlineLocked(
            int profileUserId, ActiveAdmin profileOwner, boolean unlocked) {
            int profileUserId, ActiveAdmin profileOwner, boolean off) {
        final long now = mInjector.systemCurrentTimeMillis();
        if (profileOwner.mProfileOffDeadline != 0 && now > profileOwner.mProfileOffDeadline) {
            Slogf.i(LOG_TAG, "Profile off deadline has been reached, unlocked: " + unlocked);
            Slogf.i(LOG_TAG, "Profile off deadline has been reached, off: " + off);
            if (profileOwner.mProfileOffDeadline != -1) {
                // Move the deadline far to the past so that it cannot be rolled back by TZ change.
                profileOwner.mProfileOffDeadline = -1;
                saveSettingsLocked(profileUserId);
            }
            return unlocked ? PROFILE_OFF_NOTIFICATION_NONE : PROFILE_OFF_NOTIFICATION_SUSPENDED;
            return off ? PROFILE_OFF_NOTIFICATION_SUSPENDED : PROFILE_OFF_NOTIFICATION_NONE;
        }
        boolean shouldSaveSettings = false;
        if (profileOwner.mSuspendPersonalApps) {
@@ -20824,7 +20829,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            profileOwner.mProfileOffDeadline = 0;
            shouldSaveSettings = true;
        } else if (profileOwner.mProfileOffDeadline == 0
                && (profileOwner.mProfileMaximumTimeOffMillis != 0 && !unlocked)) {
                && (profileOwner.mProfileMaximumTimeOffMillis != 0 && off)) {
            // There profile is locked and there is a policy, but the deadline is not set -> set the
            // deadline.
            Slogf.i(LOG_TAG, "Profile off deadline is set.");
@@ -20838,7 +20843,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        final long alarmTime;
        final int notificationState;
        if (unlocked || profileOwner.mProfileOffDeadline == 0) {
        if (!off || profileOwner.mProfileOffDeadline == 0) {
            alarmTime = 0;
            notificationState = PROFILE_OFF_NOTIFICATION_NONE;
        } else if (profileOwner.mProfileOffDeadline - now < MANAGED_PROFILE_OFF_WARNING_PERIOD) {
@@ -21095,7 +21100,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
        mInjector.binderWithCleanCallingIdentity(
                () -> updatePersonalAppsSuspension(userId, mUserManager.isUserUnlocked()));
                () -> updatePersonalAppsSuspension(userId));
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.SET_MANAGED_PROFILE_MAXIMUM_TIME_OFF)
+2 −0
Original line number Diff line number Diff line
@@ -8586,6 +8586,8 @@ public class DevicePolicyManagerTest extends DpmTestBase {

    private void setUserUnlocked(int userHandle, boolean unlocked) {
        when(getServices().userManager.isUserUnlocked(eq(userHandle))).thenReturn(unlocked);
        when(getServices().userManagerInternal.isUserUnlockingOrUnlocked(eq(userHandle)))
                .thenReturn(unlocked);
    }

    private void prepareMocksForSetMaximumProfileTimeOff() throws Exception {