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

Commit b48b9c73 authored by Pavel Grafov's avatar Pavel Grafov Committed by Android (Google) Code Review
Browse files

Merge changes from topic "power-exemption-bg-appop" into main

* changes:
  Make power exemption grant bg usage app op
  Cleanup flag and unused method
parents 3c4c6b8f cd75e6ae
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -316,11 +316,6 @@ public abstract class DevicePolicyManagerInternal {
     */
    public abstract boolean isUserOrganizationManaged(@UserIdInt int userId);

    /**
     * Returns whether the application exemptions feature flag is enabled.
     */
    public abstract boolean isApplicationExemptionsFlagEnabled();

    /**
     * Returns a map of admin to {@link Bundle} map of restrictions set by the admins for the
     * provided {@code packageName} in the provided {@code userId}
+9 −0
Original line number Diff line number Diff line
@@ -195,6 +195,15 @@ flag {
  }
}

flag {
  name: "power_exemption_bg_usage_fix"
  namespace: "enterprise"
  description: "Ensure aps with EXEMPT_FROM_POWER_RESTRICTIONS can execute in the background"
  bug: "333379020"
  metadata {
    purpose: PURPOSE_BUGFIX
  }
}

flag {
  name: "esim_management_ux_enabled"
+34 −51
Original line number Diff line number Diff line
@@ -107,6 +107,8 @@ import static android.app.AppOpsManager.OPSTR_SYSTEM_EXEMPT_FROM_DISMISSIBLE_NOT
import static android.app.AppOpsManager.OPSTR_SYSTEM_EXEMPT_FROM_HIBERNATION;
import static android.app.AppOpsManager.OPSTR_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS;
import static android.app.AppOpsManager.OPSTR_SYSTEM_EXEMPT_FROM_SUSPENSION;
import static android.app.AppOpsManager.OP_RUN_ANY_IN_BACKGROUND;
import static android.app.AppOpsManager.OP_RUN_IN_BACKGROUND;
import static android.app.admin.DeviceAdminInfo.HEADLESS_DEVICE_OWNER_MODE_AFFILIATED;
import static android.app.admin.DeviceAdminInfo.HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER;
import static android.app.admin.DeviceAdminInfo.HEADLESS_DEVICE_OWNER_MODE_UNSUPPORTED;
@@ -886,10 +888,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            "enable_permission_based_access";
    private static final boolean DEFAULT_VALUE_PERMISSION_BASED_ACCESS_FLAG = false;
    // TODO(b/266831522) remove the flag after rollout.
    private static final String APPLICATION_EXEMPTIONS_FLAG = "application_exemptions";
    private static final boolean DEFAULT_APPLICATION_EXEMPTIONS_FLAG = true;
    private static final int RETRY_COPY_ACCOUNT_ATTEMPTS = 3;
    /**
@@ -3689,26 +3687,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        mDevicePolicyEngine.handleStartUser(userId);
    }
    void pushUserControlDisabledPackagesLocked(int userId) {
        final int targetUserId;
        final ActiveAdmin owner;
        if (getDeviceOwnerUserIdUncheckedLocked() == userId) {
            owner = getDeviceOwnerAdminLocked();
            targetUserId = UserHandle.USER_ALL;
        } else {
            owner = getProfileOwnerAdminLocked(userId);
            targetUserId = userId;
        }
        List<String> protectedPackages = (owner == null || owner.protectedPackages == null)
                ? null : owner.protectedPackages;
        mInjector.binderWithCleanCallingIdentity(() ->
                mInjector.getPackageManagerInternal().setOwnerProtectedPackages(
                        targetUserId, protectedPackages));
        mUsageStatsManagerInternal.setAdminProtectedPackages(new ArraySet(protectedPackages),
                targetUserId);
    }
    void handleUnlockUser(int userId) {
        startOwnerService(userId, "unlock-user");
        mDevicePolicyEngine.handleUnlockUser(userId);
@@ -15912,14 +15890,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            return getDeviceStateCache().isUserOrganizationManaged(userHandle);
        }
        @Override
        public boolean isApplicationExemptionsFlagEnabled() {
            return DeviceConfig.getBoolean(
                    NAMESPACE_DEVICE_POLICY_MANAGER,
                    APPLICATION_EXEMPTIONS_FLAG,
                    DEFAULT_APPLICATION_EXEMPTIONS_FLAG);
        }
        @Override
        public List<Bundle> getApplicationRestrictionsPerAdminForUser(
                String packageName, @UserIdInt int userId) {
@@ -20378,25 +20348,31 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                hasCallingOrSelfPermission(permission.MANAGE_DEVICE_POLICY_APP_EXEMPTIONS));
        final CallerIdentity caller = getCallerIdentity(callerPackage);
        final ApplicationInfo packageInfo;
        packageInfo = getPackageInfoWithNullCheck(packageName, caller);
        final AppOpsManager appOpsMgr = mInjector.getAppOpsManager();
        final ApplicationInfo appInfo = getPackageInfoWithNullCheck(packageName, caller);
        final int uid = appInfo.uid;
        for (Map.Entry<Integer, String> entry :
                APPLICATION_EXEMPTION_CONSTANTS_TO_APP_OPS.entrySet()) {
            int currentMode = mInjector.getAppOpsManager().unsafeCheckOpNoThrow(
                    entry.getValue(), packageInfo.uid, packageInfo.packageName);
            int newMode = ArrayUtils.contains(exemptions, entry.getKey())
                    ? MODE_ALLOWED : MODE_DEFAULT;
        mInjector.binderWithCleanCallingIdentity(() -> {
            APPLICATION_EXEMPTION_CONSTANTS_TO_APP_OPS.forEach((exemption, appOp) -> {
                int currentMode = appOpsMgr.unsafeCheckOpNoThrow(appOp, uid, packageName);
                int newMode = ArrayUtils.contains(exemptions, exemption)
                        ? MODE_ALLOWED : MODE_DEFAULT;
                if (currentMode != newMode) {
                    mInjector.getAppOpsManager()
                            .setMode(entry.getValue(),
                                    packageInfo.uid,
                                    packageName,
                                    newMode);
                    appOpsMgr.setMode(appOp, uid, packageName, newMode);
                    // If the user has already disabled background usage for the package, it won't
                    // have OP_RUN_ANY_IN_BACKGROUND app op and won't execute in the background. The
                    // code below grants that app op, and once the exemption is in place, the user
                    // won't be able to disable background usage anymore.
                    if (Flags.powerExemptionBgUsageFix()
                            && exemption == EXEMPT_FROM_POWER_RESTRICTIONS
                            && newMode == MODE_ALLOWED) {
                        setBgUsageAppOp(appOpsMgr, appInfo);
                    }
            });
                }
            });
        });
        String[] appOpExemptions = new String[exemptions.length];
        for (int i = 0; i < exemptions.length; i++) {
            appOpExemptions[i] = APPLICATION_EXEMPTION_CONSTANTS_TO_APP_OPS.get(exemptions[i]);
@@ -20408,6 +20384,13 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                .write();
    }
    static void setBgUsageAppOp(AppOpsManager appOpsMgr, ApplicationInfo appInfo) {
        appOpsMgr.setMode(OP_RUN_ANY_IN_BACKGROUND, appInfo.uid, appInfo.packageName, MODE_ALLOWED);
        if (appInfo.targetSdkVersion < Build.VERSION_CODES.O) {
            appOpsMgr.setMode(OP_RUN_IN_BACKGROUND, appInfo.uid, appInfo.packageName, MODE_ALLOWED);
        }
    }
    @Override
    public int[] getApplicationExemptions(String packageName) {
        if (!mHasFeature) {