Merge "Hide app if it shares uid with whitelisted app" into pi-dev
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
index 451b402..2b04173 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
@@ -30,6 +30,7 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.Process;
 import android.os.StatsDimensionsValue;
@@ -131,6 +132,7 @@
                 StatsManager.EXTRA_STATS_BROADCAST_SUBSCRIBER_COOKIES);
         final AnomalyInfo anomalyInfo = new AnomalyInfo(
                 !ArrayUtils.isEmpty(cookies) ? cookies.get(0) : "");
+        final PackageManager packageManager = context.getPackageManager();
         Log.i(TAG, "Extra stats value: " + intentDimsValue.toString());
 
         try {
@@ -141,8 +143,8 @@
                     : Settings.Global.getInt(contentResolver,
                             Settings.Global.APP_AUTO_RESTRICTION_ENABLED, ON) == ON;
             final String packageName = batteryUtils.getPackageName(uid);
-            if (!powerWhitelistBackend.isSysWhitelistedExceptIdle(packageName)
-                    && !isSystemUid(uid)) {
+            if (!isSystemUid(uid) && !powerWhitelistBackend.isSysWhitelistedExceptIdle(
+                    packageManager.getPackagesForUid(uid))) {
                 boolean anomalyDetected = true;
                 if (anomalyInfo.anomalyType
                         == StatsManagerConfig.AnomalyType.EXCESSIVE_BACKGROUND_SERVICE) {
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java
index 0894b66..dbca602 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java
@@ -64,7 +64,7 @@
 
 @RunWith(SettingsRobolectricTestRunner.class)
 public class AnomalyDetectionJobServiceTest {
-    private static final int UID = 123;
+    private static final int UID = 12345;
     private static final String SYSTEM_PACKAGE = "com.android.system";
     private static final String SUBSCRIBER_COOKIES_AUTO_RESTRICTION =
             "anomaly_type=6,auto_restriction=true";
@@ -120,8 +120,8 @@
 
     @Test
     public void testSaveAnomalyToDatabase_systemWhitelisted_doNotSave() {
-        doReturn(SYSTEM_PACKAGE).when(mBatteryUtils).getPackageName(anyInt());
-        doReturn(true).when(mPowerWhitelistBackend).isSysWhitelisted(SYSTEM_PACKAGE);
+        doReturn(UID).when(mAnomalyDetectionJobService).extractUidFromStatsDimensionsValue(any());
+        doReturn(true).when(mPowerWhitelistBackend).isSysWhitelistedExceptIdle(any(String[].class));
 
         mAnomalyDetectionJobService.saveAnomalyToDatabase(mContext, mBatteryStatsHelper,
                 mUserManager, mBatteryDatabaseManager, mBatteryUtils, mPolicy,