Add system app check for anomaly detection.

In this CL, don't blame system app in anomaly detection and also
add log for it.

Following CL will update it to also check whether this system app
has launch entry.

Bug: 77477987
Test: RunSettingsRoboTests
Change-Id: I97490b32bc42ec2f8e03ec2d82f7c8bf89f9c66f
This commit is contained in:
Lei Yu
2018-04-10 10:54:33 -07:00
parent eaccf907e7
commit 6c4c7ba0fe
4 changed files with 113 additions and 20 deletions

View File

@@ -25,7 +25,9 @@ import android.os.BatteryManager;
import android.os.BatteryStats; import android.os.BatteryStats;
import android.os.Bundle; import android.os.Bundle;
import android.os.Build; import android.os.Build;
import android.os.Process;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@@ -43,6 +45,7 @@ import com.android.settings.R;
import com.android.settings.fuelgauge.anomaly.Anomaly; import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
import com.android.settingslib.utils.PowerUtil; import com.android.settingslib.utils.PowerUtil;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@@ -531,5 +534,40 @@ public class BatteryUtils {
return false; return false;
} }
/**
* Return {@code true} if we should hide anomaly app represented by {@code uid}
*/
public boolean shouldHideAnomaly(PowerWhitelistBackend powerWhitelistBackend, int uid) {
final String[] packageNames = mPackageManager.getPackagesForUid(uid);
if (ArrayUtils.isEmpty(packageNames)) {
// Don't show it if app has been uninstalled
return true;
}
return isSystemUid(uid) || isSystemApp(mPackageManager, packageNames)
|| powerWhitelistBackend.isSysWhitelistedExceptIdle(packageNames);
}
private boolean isSystemUid(int uid) {
final int appUid = UserHandle.getAppId(uid);
return appUid >= Process.ROOT_UID && appUid < Process.FIRST_APPLICATION_UID;
}
private boolean isSystemApp(PackageManager packageManager, String[] packageNames) {
for (String packageName : packageNames) {
try {
final ApplicationInfo info = packageManager.getApplicationInfo(packageName,
0 /* flags */);
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Package not found: " + packageName, e);
}
}
return false;
}
} }

View File

@@ -30,6 +30,7 @@ import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process; import android.os.Process;
@@ -145,21 +146,24 @@ public class AnomalyDetectionJobService extends JobService {
: Settings.Global.getInt(contentResolver, : Settings.Global.getInt(contentResolver,
Settings.Global.APP_AUTO_RESTRICTION_ENABLED, ON) == ON; Settings.Global.APP_AUTO_RESTRICTION_ENABLED, ON) == ON;
final String packageName = batteryUtils.getPackageName(uid); final String packageName = batteryUtils.getPackageName(uid);
if (uid != UID_NULL && !isSystemUid(uid)
&& !powerWhitelistBackend.isSysWhitelistedExceptIdle(
packageManager.getPackagesForUid(uid))) {
boolean anomalyDetected = true;
if (anomalyInfo.anomalyType
== StatsManagerConfig.AnomalyType.EXCESSIVE_BACKGROUND_SERVICE) {
if (!batteryUtils.isPreOApp(packageName)
|| !batteryUtils.isAppHeavilyUsed(batteryStatsHelper, userManager, uid,
policy.excessiveBgDrainPercentage)) {
// Don't report if it is not legacy app or haven't used much battery
anomalyDetected = false;
}
}
if (anomalyDetected) { final boolean anomalyDetected;
if (isExcessiveBackgroundAnomaly(anomalyInfo)) {
anomalyDetected = batteryUtils.isPreOApp(packageName)
&& batteryUtils.isAppHeavilyUsed(batteryStatsHelper, userManager, uid,
policy.excessiveBgDrainPercentage);
} else {
anomalyDetected = true;
}
if (anomalyDetected) {
if (batteryUtils.shouldHideAnomaly(powerWhitelistBackend, uid)) {
metricsFeatureProvider.action(context,
MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED,
packageName,
Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT,
anomalyInfo.anomalyType));
} else {
if (autoFeatureOn && anomalyInfo.autoRestriction) { if (autoFeatureOn && anomalyInfo.autoRestriction) {
// Auto restrict this app // Auto restrict this app
batteryUtils.setForceAppStandby(uid, packageName, batteryUtils.setForceAppStandby(uid, packageName,
@@ -215,8 +219,8 @@ public class AnomalyDetectionJobService extends JobService {
return UID_NULL; return UID_NULL;
} }
private boolean isSystemUid(int uid) { private boolean isExcessiveBackgroundAnomaly(AnomalyInfo anomalyInfo) {
final int appUid = UserHandle.getAppId(uid); return anomalyInfo.anomalyType
return appUid >= Process.ROOT_UID && appUid < Process.FIRST_APPLICATION_UID; == StatsManagerConfig.AnomalyType.EXCESSIVE_BACKGROUND_SERVICE;
} }
} }

View File

@@ -53,6 +53,7 @@ import com.android.settings.R;
import com.android.settings.fuelgauge.anomaly.Anomaly; import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -81,7 +82,7 @@ public class BatteryUtilsTest {
private static final long TIME_SINCE_LAST_FULL_CHARGE_US = private static final long TIME_SINCE_LAST_FULL_CHARGE_US =
TIME_SINCE_LAST_FULL_CHARGE_MS * 1000; TIME_SINCE_LAST_FULL_CHARGE_MS * 1000;
private static final int UID = 123; private static final int UID = 12345;
private static final long TIME_EXPECTED_FOREGROUND = 1500; private static final long TIME_EXPECTED_FOREGROUND = 1500;
private static final long TIME_EXPECTED_BACKGROUND = 6000; private static final long TIME_EXPECTED_BACKGROUND = 6000;
private static final long TIME_EXPECTED_ALL = 7500; private static final long TIME_EXPECTED_ALL = 7500;
@@ -141,6 +142,8 @@ public class BatteryUtilsTest {
private ApplicationInfo mHighApplicationInfo; private ApplicationInfo mHighApplicationInfo;
@Mock @Mock
private ApplicationInfo mLowApplicationInfo; private ApplicationInfo mLowApplicationInfo;
@Mock
private PowerWhitelistBackend mPowerWhitelistBackend;
private BatteryUtils mBatteryUtils; private BatteryUtils mBatteryUtils;
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
private PowerUsageFeatureProvider mProvider; private PowerUsageFeatureProvider mProvider;
@@ -166,9 +169,9 @@ public class BatteryUtilsTest {
when(mBatteryStatsHelper.getStats().computeBatteryRealtime(anyLong(), anyInt())).thenReturn( when(mBatteryStatsHelper.getStats().computeBatteryRealtime(anyLong(), anyInt())).thenReturn(
TIME_SINCE_LAST_FULL_CHARGE_US); TIME_SINCE_LAST_FULL_CHARGE_US);
when(mPackageManager.getApplicationInfo(HIGH_SDK_PACKAGE, PackageManager.GET_META_DATA)) when(mPackageManager.getApplicationInfo(eq(HIGH_SDK_PACKAGE), anyInt()))
.thenReturn(mHighApplicationInfo); .thenReturn(mHighApplicationInfo);
when(mPackageManager.getApplicationInfo(LOW_SDK_PACKAGE, PackageManager.GET_META_DATA)) when(mPackageManager.getApplicationInfo(eq(LOW_SDK_PACKAGE), anyInt()))
.thenReturn(mLowApplicationInfo); .thenReturn(mLowApplicationInfo);
mHighApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O; mHighApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O;
mLowApplicationInfo.targetSdkVersion = Build.VERSION_CODES.L; mLowApplicationInfo.targetSdkVersion = Build.VERSION_CODES.L;
@@ -570,4 +573,27 @@ public class BatteryUtilsTest {
assertThat(mBatteryUtils.isAppHeavilyUsed(mBatteryStatsHelper, mUserManager, UID, assertThat(mBatteryUtils.isAppHeavilyUsed(mBatteryStatsHelper, mUserManager, UID,
DISCHARGE_AMOUNT /* threshold */ )).isFalse(); DISCHARGE_AMOUNT /* threshold */ )).isFalse();
} }
@Test
public void testShouldHideAnomaly_systemApp_returnTrue() {
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID);
mHighApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, UID)).isTrue();
}
@Test
public void testShouldHideAnomaly_systemUid_returnTrue() {
final int systemUid = Process.ROOT_UID;
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(systemUid);
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, systemUid)).isTrue();
}
@Test
public void testShouldHideAnomaly_normalApp_returnFalse() {
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID);
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, UID)).isFalse();
}
} }

View File

@@ -141,6 +141,31 @@ public class AnomalyDetectionJobServiceTest {
anyInt(), anyLong()); anyInt(), anyLong());
} }
@Test
public void testSaveAnomalyToDatabase_systemApp_doNotSaveButLog() {
final ArrayList<String> cookies = new ArrayList<>();
cookies.add(SUBSCRIBER_COOKIES_AUTO_RESTRICTION);
mBundle.putStringArrayList(StatsManager.EXTRA_STATS_BROADCAST_SUBSCRIBER_COOKIES, cookies);
doReturn(SYSTEM_PACKAGE).when(mBatteryUtils).getPackageName(anyInt());
doReturn(false).when(mPowerWhitelistBackend).isSysWhitelisted(SYSTEM_PACKAGE);
doReturn(Process.FIRST_APPLICATION_UID).when(
mAnomalyDetectionJobService).extractUidFromStatsDimensionsValue(any());
doReturn(true).when(mBatteryUtils).shouldHideAnomaly(any(), anyInt());
mAnomalyDetectionJobService.saveAnomalyToDatabase(mContext, mBatteryStatsHelper,
mUserManager, mBatteryDatabaseManager, mBatteryUtils, mPolicy,
mPowerWhitelistBackend, mContext.getContentResolver(),
mFeatureFactory.powerUsageFeatureProvider,
mFeatureFactory.metricsFeatureProvider, mBundle);
verify(mBatteryDatabaseManager, never()).insertAnomaly(anyInt(), anyString(), anyInt(),
anyInt(), anyLong());
verify(mFeatureFactory.metricsFeatureProvider).action(mContext,
MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED,
SYSTEM_PACKAGE,
Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, ANOMALY_TYPE));
}
@Test @Test
public void testSaveAnomalyToDatabase_systemUid_doNotSave() { public void testSaveAnomalyToDatabase_systemUid_doNotSave() {
doReturn(Process.SYSTEM_UID).when( doReturn(Process.SYSTEM_UID).when(