Unrestricting app when put on power whitelist
Power whitelisting and background restrictions are opposing rules, so to
keep apps out of confusing states, unrestricting an app whenever it is
put on the power whitelist.
Test: atest RunSettingsRoboTests:HighPowerDetailTest
Bug: 77924141
Change-Id: I4952ac6c893bc7e98aa1b6ad3a86906fc299dea5
diff --git a/res/values/strings.xml b/res/values/strings.xml
index c8dad37..5f45a3f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8698,7 +8698,7 @@
<string name="ignore_optimizations_off">Optimize</string>
<!-- Ignore battery optimizations on description [CHAR LIMIT=NONE] -->
- <string name="ignore_optimizations_on_desc">May drain your battery more quickly</string>
+ <string name="ignore_optimizations_on_desc">May drain your battery more quickly. App will no longer be restricted from using background battery.</string>
<!-- Ignore battery optimizations off description [CHAR LIMIT=NONE] -->
<string name="ignore_optimizations_off_desc">Recommended for better battery life</string>
diff --git a/src/com/android/settings/applications/manageapplications/ManageApplications.java b/src/com/android/settings/applications/manageapplications/ManageApplications.java
index 301e71b..328d4a2 100644
--- a/src/com/android/settings/applications/manageapplications/ManageApplications.java
+++ b/src/com/android/settings/applications/manageapplications/ManageApplications.java
@@ -545,7 +545,7 @@
startAppInfoFragment(AppStorageSettings.class, R.string.storage_settings);
break;
case LIST_TYPE_HIGH_POWER:
- HighPowerDetail.show(this, mCurrentPkgName, INSTALLED_APP_DETAILS);
+ HighPowerDetail.show(this, mCurrentUid, mCurrentPkgName, INSTALLED_APP_DETAILS);
break;
case LIST_TYPE_OVERLAY:
startAppInfoFragment(DrawOverlayDetails.class, R.string.overlay_settings);
diff --git a/src/com/android/settings/fuelgauge/HighPowerDetail.java b/src/com/android/settings/fuelgauge/HighPowerDetail.java
index 9cafbcb..4f70700 100644
--- a/src/com/android/settings/fuelgauge/HighPowerDetail.java
+++ b/src/com/android/settings/fuelgauge/HighPowerDetail.java
@@ -17,6 +17,7 @@
package com.android.settings.fuelgauge;
import android.app.AlertDialog;
+import android.app.AppOpsManager;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
@@ -43,12 +44,18 @@
private static final String ARG_DEFAULT_ON = "default_on";
- private final PowerWhitelistBackend mBackend = PowerWhitelistBackend.getInstance();
-
- private String mPackageName;
+ @VisibleForTesting
+ PowerWhitelistBackend mBackend;
+ @VisibleForTesting
+ BatteryUtils mBatteryUtils;
+ @VisibleForTesting
+ String mPackageName;
+ @VisibleForTesting
+ int mPackageUid;
private CharSequence mLabel;
private boolean mDefaultOn;
- private boolean mIsEnabled;
+ @VisibleForTesting
+ boolean mIsEnabled;
private Checkable mOptionOn;
private Checkable mOptionOff;
@@ -60,8 +67,11 @@
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ mBatteryUtils = BatteryUtils.getInstance(getContext());
+ mBackend = PowerWhitelistBackend.getInstance();
mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
+ mPackageUid = getArguments().getInt(AppInfoBase.ARG_PACKAGE_UID);
PackageManager pm = getContext().getPackageManager();
try {
mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
@@ -129,6 +139,8 @@
if (newValue != oldValue) {
logSpecialPermissionChange(newValue, mPackageName, getContext());
if (newValue) {
+ mBatteryUtils.setForceAppStandby(mPackageUid, mPackageName,
+ AppOpsManager.MODE_ALLOWED);
mBackend.addApp(mPackageName);
} else {
mBackend.removeApp(mPackageName);
@@ -165,10 +177,11 @@
: R.string.high_power_off);
}
- public static void show(Fragment caller, String packageName, int requestCode) {
+ public static void show(Fragment caller, int uid, String packageName, int requestCode) {
HighPowerDetail fragment = new HighPowerDetail();
Bundle args = new Bundle();
args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
+ args.putInt(AppInfoBase.ARG_PACKAGE_UID, uid);
fragment.setArguments(args);
fragment.setTargetFragment(caller, requestCode);
fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName());
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/HighPowerDetailTest.java b/tests/robotests/src/com/android/settings/fuelgauge/HighPowerDetailTest.java
index 8029c55..1817325 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/HighPowerDetailTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/HighPowerDetailTest.java
@@ -18,27 +18,49 @@
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import android.app.AppOpsManager;
import android.content.Context;
+import android.content.DialogInterface;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class HighPowerDetailTest {
+ private static final int TEST_UID = 12000;
+ private static final String TEST_PACKAGE = "com.test.package";
private FakeFeatureFactory mFeatureFactory;
+ private HighPowerDetail mFragment;
+
+ @Mock
+ private PowerWhitelistBackend mPowerWhitelistBackend;
+ @Mock
+ private BatteryUtils mBatteryUtils;
@Before
public void setUp() {
mFeatureFactory = FakeFeatureFactory.setupForTest();
+
+ MockitoAnnotations.initMocks(this);
+ mFragment = spy(new HighPowerDetail());
+ mFragment.mBackend = mPowerWhitelistBackend;
+ mFragment.mBatteryUtils = mBatteryUtils;
+ mFragment.mPackageUid = TEST_UID;
+ mFragment.mPackageName = TEST_PACKAGE;
}
@Test
@@ -53,4 +75,13 @@
verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_ALLOW), eq("app"));
}
+
+ @Test
+ public void onClick_appAddedToDozeWhitelist_getsUnrestricted() {
+ mFragment.mIsEnabled = true;
+ when(mPowerWhitelistBackend.isWhitelisted(TEST_PACKAGE)).thenReturn(false);
+ mFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
+ verify(mBatteryUtils).setForceAppStandby(TEST_UID, TEST_PACKAGE,
+ AppOpsManager.MODE_ALLOWED);
+ }
}