From 740b086d44b041cbfd5869a3945b8682ec3db8b1 Mon Sep 17 00:00:00 2001 From: Alex Johnston Date: Mon, 6 Feb 2023 17:29:34 +0000 Subject: Implement OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS exemption OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS details: * An app with this appop will be made exempt from all power restrictions, including app standby and doze. * In addition, the app will be able to start fgs from the bg, and the user will not be able to stop fgs run by the app. Changes: * Add DevicePolicyManager constant to exempt an app from power restrictions * Link the constant with the OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS appop * Implement the OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS Bug: 246330879 Test: atest FgsManagerControllerTest atest ActiveServicesTest atest BackgroundRestrictionTest atest ApplicationExemptionsTest Manual testing: - Give OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS appop to TestDPC app - Verify the app can start fg services from the bg - Verify fgs started by the app cannot be stopped - Verify the app cannot be put into background restricted via Settings Change-Id: I98289082dbe2a526ee66d157748c3e9e964a2a7e --- .../fuelgauge/PowerAllowlistBackend.java | 32 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'packages/SettingsLib/src') diff --git a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/PowerAllowlistBackend.java b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/PowerAllowlistBackend.java index 2e8f36834584..8fd4e912e04a 100644 --- a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/PowerAllowlistBackend.java +++ b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/PowerAllowlistBackend.java @@ -16,6 +16,9 @@ package com.android.settingslib.fuelgauge; +import static android.provider.DeviceConfig.NAMESPACE_ACTIVITY_MANAGER; + +import android.app.AppOpsManager; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; @@ -23,6 +26,7 @@ import android.content.pm.PackageManager; import android.os.IDeviceIdleController; import android.os.RemoteException; import android.os.ServiceManager; +import android.provider.DeviceConfig; import android.telecom.DefaultDialerManager; import android.text.TextUtils; import android.util.ArraySet; @@ -42,6 +46,10 @@ public class PowerAllowlistBackend { private static final String DEVICE_IDLE_SERVICE = "deviceidle"; + private static final String SYSTEM_EXEMPT_POWER_RESTRICTIONS_ENABLED = + "system_exempt_power_restrictions_enabled"; + private static final boolean DEFAULT_SYSTEM_EXEMPT_POWER_RESTRICTIONS_ENABLED = true; + private static PowerAllowlistBackend sInstance; private final Context mAppContext; @@ -76,12 +84,12 @@ public class PowerAllowlistBackend { /** * Check if target package is in allow list */ - public boolean isAllowlisted(String pkg) { + public boolean isAllowlisted(String pkg, int uid) { if (mAllowlistedApps.contains(pkg)) { return true; } - if (isDefaultActiveApp(pkg)) { + if (isDefaultActiveApp(pkg, uid)) { return true; } @@ -91,7 +99,7 @@ public class PowerAllowlistBackend { /** * Check if it is default active app in multiple area(i.e. SMS, Dialer, Device admin..) */ - public boolean isDefaultActiveApp(String pkg) { + public boolean isDefaultActiveApp(String pkg, int uid) { // Additionally, check if pkg is default dialer/sms. They are considered essential apps and // should be automatically allowlisted (otherwise user may be able to set restriction on // them, leading to bad device behavior.) @@ -106,9 +114,23 @@ public class PowerAllowlistBackend { return true; } + final AppOpsManager appOpsManager = mAppContext.getSystemService(AppOpsManager.class); + if (isSystemExemptFlagEnabled() && appOpsManager.checkOpNoThrow( + AppOpsManager.OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS, uid, pkg) + == AppOpsManager.MODE_ALLOWED) { + return true; + } + return false; } + private static boolean isSystemExemptFlagEnabled() { + return DeviceConfig.getBoolean( + NAMESPACE_ACTIVITY_MANAGER, + SYSTEM_EXEMPT_POWER_RESTRICTIONS_ENABLED, + DEFAULT_SYSTEM_EXEMPT_POWER_RESTRICTIONS_ENABLED); + } + /** * Check if target package is in allow list except idle app */ @@ -126,12 +148,12 @@ public class PowerAllowlistBackend { * @param pkgs a list of packageName * @return true when one of package is in allow list */ - public boolean isAllowlisted(String[] pkgs) { + public boolean isAllowlisted(String[] pkgs, int uid) { if (ArrayUtils.isEmpty(pkgs)) { return false; } for (String pkg : pkgs) { - if (isAllowlisted(pkg)) { + if (isAllowlisted(pkg, uid)) { return true; } } -- cgit v1.2.3-59-g8ed1b