Merge "Allow rolling back Turn Off Work 2.0" into udc-dev

This commit is contained in:
Pavel Grafov
2023-05-11 11:58:08 +00:00
committed by Android (Google) Code Review
5 changed files with 78 additions and 23 deletions

View File

@@ -697,8 +697,6 @@ public final class SuspendPackageHelper {
Computer snapshot, int userId, boolean suspend) { Computer snapshot, int userId, boolean suspend) {
final Set<String> toSuspend = packagesToSuspendInQuietMode(snapshot, userId); final Set<String> toSuspend = packagesToSuspendInQuietMode(snapshot, userId);
if (!suspend) { if (!suspend) {
// Note: this method is called from DPMS constructor to suspend apps on upgrade, but
// it won't enter here because 'suspend' will equal 'true'.
final DevicePolicyManagerInternal dpm = final DevicePolicyManagerInternal dpm =
LocalServices.getService(DevicePolicyManagerInternal.class); LocalServices.getService(DevicePolicyManagerInternal.class);
if (dpm != null) { if (dpm != null) {

View File

@@ -16,6 +16,8 @@
package com.android.server.devicepolicy; package com.android.server.devicepolicy;
import static com.android.server.devicepolicy.DevicePolicyManagerService.DEFAULT_KEEP_PROFILES_RUNNING_FLAG;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.UserIdInt; import android.annotation.UserIdInt;
@@ -70,6 +72,7 @@ class DevicePolicyData {
private static final String TAG_PASSWORD_TOKEN_HANDLE = "password-token"; private static final String TAG_PASSWORD_TOKEN_HANDLE = "password-token";
private static final String TAG_PROTECTED_PACKAGES = "protected-packages"; private static final String TAG_PROTECTED_PACKAGES = "protected-packages";
private static final String TAG_BYPASS_ROLE_QUALIFICATIONS = "bypass-role-qualifications"; private static final String TAG_BYPASS_ROLE_QUALIFICATIONS = "bypass-role-qualifications";
private static final String TAG_KEEP_PROFILES_RUNNING = "keep-profiles-running";
private static final String ATTR_VALUE = "value"; private static final String ATTR_VALUE = "value";
private static final String ATTR_ALIAS = "alias"; private static final String ATTR_ALIAS = "alias";
private static final String ATTR_ID = "id"; private static final String ATTR_ID = "id";
@@ -193,6 +196,12 @@ class DevicePolicyData {
// starts. // starts.
String mNewUserDisclaimer = NEW_USER_DISCLAIMER_NOT_NEEDED; String mNewUserDisclaimer = NEW_USER_DISCLAIMER_NOT_NEEDED;
/**
* Effective state of the feature flag. It is updated to the current configuration value
* during boot and doesn't change value after than unless overridden by test code.
*/
boolean mEffectiveKeepProfilesRunning = DEFAULT_KEEP_PROFILES_RUNNING_FLAG;
DevicePolicyData(@UserIdInt int userId) { DevicePolicyData(@UserIdInt int userId) {
mUserId = userId; mUserId = userId;
} }
@@ -392,6 +401,12 @@ class DevicePolicyData {
out.endTag(null, TAG_BYPASS_ROLE_QUALIFICATIONS); out.endTag(null, TAG_BYPASS_ROLE_QUALIFICATIONS);
} }
if (policyData.mEffectiveKeepProfilesRunning != DEFAULT_KEEP_PROFILES_RUNNING_FLAG) {
out.startTag(null, TAG_KEEP_PROFILES_RUNNING);
out.attributeBoolean(null, ATTR_VALUE, policyData.mEffectiveKeepProfilesRunning);
out.endTag(null, TAG_KEEP_PROFILES_RUNNING);
}
out.endTag(null, "policies"); out.endTag(null, "policies");
out.endDocument(); out.endDocument();
@@ -574,7 +589,10 @@ class DevicePolicyData {
parser.getAttributeBoolean(null, ATTR_VALUE, false); parser.getAttributeBoolean(null, ATTR_VALUE, false);
} else if (TAG_BYPASS_ROLE_QUALIFICATIONS.equals(tag)) { } else if (TAG_BYPASS_ROLE_QUALIFICATIONS.equals(tag)) {
policy.mBypassDevicePolicyManagementRoleQualifications = true; policy.mBypassDevicePolicyManagementRoleQualifications = true;
policy.mCurrentRoleHolder = parser.getAttributeValue(null, ATTR_VALUE); policy.mCurrentRoleHolder = parser.getAttributeValue(null, ATTR_VALUE);
} else if (TAG_KEEP_PROFILES_RUNNING.equals(tag)) {
policy.mEffectiveKeepProfilesRunning = parser.getAttributeBoolean(
null, ATTR_VALUE, DEFAULT_KEEP_PROFILES_RUNNING_FLAG);
// Deprecated tags below // Deprecated tags below
} else if (TAG_PROTECTED_PACKAGES.equals(tag)) { } else if (TAG_PROTECTED_PACKAGES.equals(tag)) {
if (policy.mUserControlDisabledPackages == null) { if (policy.mUserControlDisabledPackages == null) {

View File

@@ -677,7 +677,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
// to decide whether an existing policy in the {@link #DEVICE_POLICIES_XML} needs to // to decide whether an existing policy in the {@link #DEVICE_POLICIES_XML} needs to
// be upgraded. See {@link PolicyVersionUpgrader} on instructions how to add an upgrade // be upgraded. See {@link PolicyVersionUpgrader} on instructions how to add an upgrade
// step. // step.
static final int DPMS_VERSION = 4; static final int DPMS_VERSION = 5;
static { static {
SECURE_SETTINGS_ALLOWLIST = new ArraySet<>(); SECURE_SETTINGS_ALLOWLIST = new ArraySet<>();
@@ -874,7 +874,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
// TODO(b/265683382) remove the flag after rollout. // TODO(b/265683382) remove the flag after rollout.
private static final String KEEP_PROFILES_RUNNING_FLAG = "enable_keep_profiles_running"; private static final String KEEP_PROFILES_RUNNING_FLAG = "enable_keep_profiles_running";
private static final boolean DEFAULT_KEEP_PROFILES_RUNNING_FLAG = true; public static final boolean DEFAULT_KEEP_PROFILES_RUNNING_FLAG = true;
private static final String ENABLE_WORK_PROFILE_TELEPHONY_FLAG = private static final String ENABLE_WORK_PROFILE_TELEPHONY_FLAG =
"enable_work_profile_telephony"; "enable_work_profile_telephony";
@@ -888,12 +888,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
private static final String APPLICATION_EXEMPTIONS_FLAG = "application_exemptions"; private static final String APPLICATION_EXEMPTIONS_FLAG = "application_exemptions";
private static final boolean DEFAULT_APPLICATION_EXEMPTIONS_FLAG = true; private static final boolean DEFAULT_APPLICATION_EXEMPTIONS_FLAG = true;
/**
* This feature flag is checked once after boot and this value us used until the next reboot to
* avoid needing to handle the flag changing on the fly.
*/
private boolean mKeepProfilesRunning = isKeepProfilesRunningFlagEnabled();
/** /**
* For apps targeting U+ * For apps targeting U+
* Enable multiple admins to coexist on the same device. * Enable multiple admins to coexist on the same device.
@@ -2096,12 +2090,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
performPolicyVersionUpgrade(); performPolicyVersionUpgrade();
} }
// TODO(b/265683382) move it into an upgrade step when removing the flag, so that it is
// executed only once on upgrading devices, not every boot.
if (mKeepProfilesRunning) {
suspendAppsForQuietProfiles();
}
mUserData = new SparseArray<>(); mUserData = new SparseArray<>();
mOwners = makeOwners(injector, pathProvider); mOwners = makeOwners(injector, pathProvider);
@@ -2202,12 +2190,12 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
return packageNameAndSignature; return packageNameAndSignature;
} }
private void suspendAppsForQuietProfiles() { private void suspendAppsForQuietProfiles(boolean toSuspend) {
PackageManagerInternal pmi = mInjector.getPackageManagerInternal(); PackageManagerInternal pmi = mInjector.getPackageManagerInternal();
List<UserInfo> users = mUserManager.getUsers(); List<UserInfo> users = mUserManager.getUsers();
for (UserInfo user : users) { for (UserInfo user : users) {
if (user.isManagedProfile() && user.isQuietModeEnabled()) { if (user.isManagedProfile() && user.isQuietModeEnabled()) {
pmi.setPackagesSuspendedForQuietMode(user.id, true); pmi.setPackagesSuspendedForQuietMode(user.id, toSuspend);
} }
} }
} }
@@ -3482,6 +3470,10 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
revertTransferOwnershipIfNecessaryLocked(); revertTransferOwnershipIfNecessaryLocked();
} }
updateUsbDataSignal(); updateUsbDataSignal();
// In case flag value has changed, we apply it during boot to avoid doing it concurrently
// with user toggling quiet mode.
setKeepProfileRunningEnabledUnchecked(isKeepProfilesRunningFlagEnabled());
} }
// TODO(b/230841522) Make it static. // TODO(b/230841522) Make it static.
@@ -11324,7 +11316,8 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
(size == 1 ? "" : "s")); (size == 1 ? "" : "s"));
} }
pw.println(); pw.println();
pw.println("Keep profiles running: " + mKeepProfilesRunning); pw.println("Keep profiles running: "
+ getUserData(UserHandle.USER_SYSTEM).mEffectiveKeepProfilesRunning);
pw.println(); pw.println();
mPolicyCache.dump(pw); mPolicyCache.dump(pw);
@@ -16274,7 +16267,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
@Override @Override
public boolean isKeepProfilesRunningEnabled() { public boolean isKeepProfilesRunningEnabled() {
return mKeepProfilesRunning; return getUserDataUnchecked(UserHandle.USER_SYSTEM).mEffectiveKeepProfilesRunning;
} }
private @Mode int findInteractAcrossProfilesResetMode(String packageName) { private @Mode int findInteractAcrossProfilesResetMode(String packageName) {
@@ -23762,6 +23755,18 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
return false; return false;
} }
private void setKeepProfileRunningEnabledUnchecked(boolean keepProfileRunning) {
synchronized (getLockObject()) {
DevicePolicyData policyData = getUserDataUnchecked(UserHandle.USER_SYSTEM);
if (policyData.mEffectiveKeepProfilesRunning == keepProfileRunning) {
return;
}
policyData.mEffectiveKeepProfilesRunning = keepProfileRunning;
saveSettingsLocked(UserHandle.USER_SYSTEM);
}
suspendAppsForQuietProfiles(keepProfileRunning);
}
private boolean isWorkProfileTelephonyEnabled() { private boolean isWorkProfileTelephonyEnabled() {
return isWorkProfileTelephonyDevicePolicyManagerFlagEnabled() return isWorkProfileTelephonyDevicePolicyManagerFlagEnabled()
&& isWorkProfileTelephonySubscriptionManagerFlagEnabled(); && isWorkProfileTelephonySubscriptionManagerFlagEnabled();
@@ -23786,7 +23791,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
public void setOverrideKeepProfilesRunning(boolean enabled) { public void setOverrideKeepProfilesRunning(boolean enabled) {
Preconditions.checkCallAuthorization( Preconditions.checkCallAuthorization(
hasCallingOrSelfPermission(MANAGE_PROFILE_AND_DEVICE_OWNERS)); hasCallingOrSelfPermission(MANAGE_PROFILE_AND_DEVICE_OWNERS));
mKeepProfilesRunning = enabled; setKeepProfileRunningEnabledUnchecked(enabled);
Slog.i(LOG_TAG, "Keep profiles running overridden to: " + enabled); Slog.i(LOG_TAG, "Keep profiles running overridden to: " + enabled);
} }

View File

@@ -110,6 +110,12 @@ public class PolicyVersionUpgrader {
currentVersion = 4; currentVersion = 4;
} }
if (currentVersion == 4) {
Slog.i(LOG_TAG, String.format("Upgrading from version %d", currentVersion));
initializeEffectiveKeepProfilesRunning(allUsersData);
currentVersion = 5;
}
writePoliciesAndVersion(allUsers, allUsersData, ownersData, currentVersion); writePoliciesAndVersion(allUsers, allUsersData, ownersData, currentVersion);
} }
@@ -214,6 +220,16 @@ public class PolicyVersionUpgrader {
ownerAdmin.suspendedPackages.size(), ownerPackage, ownerUserId)); ownerAdmin.suspendedPackages.size(), ownerPackage, ownerUserId));
} }
private void initializeEffectiveKeepProfilesRunning(
SparseArray<DevicePolicyData> allUsersData) {
DevicePolicyData systemUserData = allUsersData.get(UserHandle.USER_SYSTEM);
if (systemUserData == null) {
return;
}
systemUserData.mEffectiveKeepProfilesRunning = false;
Slog.i(LOG_TAG, "Keep profile running effective state set to false");
}
private OwnersData loadOwners(int[] allUsers) { private OwnersData loadOwners(int[] allUsers) {
OwnersData ownersData = new OwnersData(mPathProvider); OwnersData ownersData = new OwnersData(mPathProvider);
ownersData.load(allUsers); ownersData.load(allUsers);

View File

@@ -76,7 +76,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
public class PolicyVersionUpgraderTest extends DpmTestBase { public class PolicyVersionUpgraderTest extends DpmTestBase {
// NOTE: Only change this value if the corresponding CL also adds a test to test the upgrade // NOTE: Only change this value if the corresponding CL also adds a test to test the upgrade
// to the new version. // to the new version.
private static final int LATEST_TESTED_VERSION = 4; private static final int LATEST_TESTED_VERSION = 5;
public static final String PERMISSIONS_TAG = "admin-can-grant-sensors-permissions"; public static final String PERMISSIONS_TAG = "admin-can-grant-sensors-permissions";
public static final String DEVICE_OWNER_XML = "device_owner_2.xml"; public static final String DEVICE_OWNER_XML = "device_owner_2.xml";
private ComponentName mFakeAdmin; private ComponentName mFakeAdmin;
@@ -312,6 +312,24 @@ public class PolicyVersionUpgraderTest extends DpmTestBase {
assertThat(storedSuspendedPkgs).isEqualTo(suspendedPkgs); assertThat(storedSuspendedPkgs).isEqualTo(suspendedPkgs);
} }
@Test
public void testEffectiveKeepProfilesRunningSet() throws Exception {
writeVersionToXml(4);
final int userId = UserHandle.USER_SYSTEM;
mProvider.mUsers = new int[]{userId};
preparePoliciesFile(userId, "device_policies.xml");
mUpgrader.upgradePolicy(5);
assertThat(readVersionFromXml()).isAtLeast(5);
Document policies = readPolicies(userId);
Element keepProfilesRunning = (Element) policies.getDocumentElement()
.getElementsByTagName("keep-profiles-running").item(0);
assertThat(keepProfilesRunning.getAttribute("value")).isEqualTo("false");
}
@Test @Test
public void isLatestVersionTested() { public void isLatestVersionTested() {
assertThat(DevicePolicyManagerService.DPMS_VERSION).isEqualTo(LATEST_TESTED_VERSION); assertThat(DevicePolicyManagerService.DPMS_VERSION).isEqualTo(LATEST_TESTED_VERSION);