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

Commit cd75e6ae authored by Pavel Grafov's avatar Pavel Grafov
Browse files

Make power exemption grant bg usage app op

Bug: 333379020
Test: atest CtsDevicePolicyTestCases:ApplicationExemptionsTest#setApplicationExemptions_powerRestrictionExemption_allowsBgUsage
Change-Id: Ifb85fffa26ecf451f0259bda440dab65608f13e9
parent 705840a8
Loading
Loading
Loading
Loading
+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 −19
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;
@@ -20345,25 +20347,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]);
@@ -20375,6 +20383,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) {