Loading src/com/android/settings/fuelgauge/BatteryUtils.java +17 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.BatteryStats; Loading Loading @@ -595,5 +596,21 @@ public class BatteryUtils { return false; } /** * Return version number of an app represented by {@code packageName}, and return -1 if not * found. */ public long getAppLongVersionCode(String packageName) { try { final PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, 0 /* flags */); return packageInfo.getLongVersionCode(); } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Cannot find package: " + packageName, e); } return -1L; } } src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java +7 −2 Original line number Diff line number Diff line Loading @@ -146,6 +146,7 @@ public class AnomalyDetectionJobService extends JobService { : Settings.Global.getInt(contentResolver, Settings.Global.APP_AUTO_RESTRICTION_ENABLED, ON) == ON; final String packageName = batteryUtils.getPackageName(uid); final long versionCode = batteryUtils.getAppLongVersionCode(packageName); final boolean anomalyDetected; if (isExcessiveBackgroundAnomaly(anomalyInfo)) { Loading @@ -162,7 +163,9 @@ public class AnomalyDetectionJobService extends JobService { MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED, packageName, Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, anomalyInfo.anomalyType)); anomalyInfo.anomalyType), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, versionCode)); } else { if (autoFeatureOn && anomalyInfo.autoRestriction) { // Auto restrict this app Loading @@ -180,7 +183,9 @@ public class AnomalyDetectionJobService extends JobService { MetricsProto.MetricsEvent.ACTION_ANOMALY_TRIGGERED, packageName, Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, anomalyInfo.anomalyType)); anomalyInfo.anomalyType), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, versionCode)); } } } catch (NullPointerException | IndexOutOfBoundsException e) { Loading tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java +8 −3 Original line number Diff line number Diff line Loading @@ -79,6 +79,7 @@ public class AnomalyDetectionJobServiceTest { private static final String SUBSCRIBER_COOKIES_NOT_AUTO_RESTRICTION = "anomaly_type=6,auto_restriction=false"; private static final int ANOMALY_TYPE = 6; private static final long VERSION_CODE = 15; @Mock private BatteryStatsHelper mBatteryStatsHelper; @Mock Loading Loading @@ -107,6 +108,7 @@ public class AnomalyDetectionJobServiceTest { mBundle = new Bundle(); mBundle.putParcelable(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, mStatsDimensionsValue); mFeatureFactory = FakeFeatureFactory.setupForTest(); when(mBatteryUtils.getAppLongVersionCode(any())).thenReturn(VERSION_CODE); mAnomalyDetectionJobService = spy(new AnomalyDetectionJobService()); } Loading Loading @@ -163,7 +165,8 @@ public class AnomalyDetectionJobServiceTest { verify(mFeatureFactory.metricsFeatureProvider).action(mContext, MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED, SYSTEM_PACKAGE, Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, ANOMALY_TYPE)); Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, ANOMALY_TYPE), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, VERSION_CODE)); } @Test Loading Loading @@ -217,7 +220,8 @@ public class AnomalyDetectionJobServiceTest { verify(mFeatureFactory.metricsFeatureProvider).action(mContext, MetricsProto.MetricsEvent.ACTION_ANOMALY_TRIGGERED, SYSTEM_PACKAGE, Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE)); Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, VERSION_CODE)); } Loading @@ -242,7 +246,8 @@ public class AnomalyDetectionJobServiceTest { verify(mFeatureFactory.metricsFeatureProvider).action(mContext, MetricsProto.MetricsEvent.ACTION_ANOMALY_TRIGGERED, SYSTEM_PACKAGE, Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE)); Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, VERSION_CODE)); } @Test Loading Loading
src/com/android/settings/fuelgauge/BatteryUtils.java +17 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.BatteryStats; Loading Loading @@ -595,5 +596,21 @@ public class BatteryUtils { return false; } /** * Return version number of an app represented by {@code packageName}, and return -1 if not * found. */ public long getAppLongVersionCode(String packageName) { try { final PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, 0 /* flags */); return packageInfo.getLongVersionCode(); } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Cannot find package: " + packageName, e); } return -1L; } }
src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java +7 −2 Original line number Diff line number Diff line Loading @@ -146,6 +146,7 @@ public class AnomalyDetectionJobService extends JobService { : Settings.Global.getInt(contentResolver, Settings.Global.APP_AUTO_RESTRICTION_ENABLED, ON) == ON; final String packageName = batteryUtils.getPackageName(uid); final long versionCode = batteryUtils.getAppLongVersionCode(packageName); final boolean anomalyDetected; if (isExcessiveBackgroundAnomaly(anomalyInfo)) { Loading @@ -162,7 +163,9 @@ public class AnomalyDetectionJobService extends JobService { MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED, packageName, Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, anomalyInfo.anomalyType)); anomalyInfo.anomalyType), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, versionCode)); } else { if (autoFeatureOn && anomalyInfo.autoRestriction) { // Auto restrict this app Loading @@ -180,7 +183,9 @@ public class AnomalyDetectionJobService extends JobService { MetricsProto.MetricsEvent.ACTION_ANOMALY_TRIGGERED, packageName, Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, anomalyInfo.anomalyType)); anomalyInfo.anomalyType), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, versionCode)); } } } catch (NullPointerException | IndexOutOfBoundsException e) { Loading
tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java +8 −3 Original line number Diff line number Diff line Loading @@ -79,6 +79,7 @@ public class AnomalyDetectionJobServiceTest { private static final String SUBSCRIBER_COOKIES_NOT_AUTO_RESTRICTION = "anomaly_type=6,auto_restriction=false"; private static final int ANOMALY_TYPE = 6; private static final long VERSION_CODE = 15; @Mock private BatteryStatsHelper mBatteryStatsHelper; @Mock Loading Loading @@ -107,6 +108,7 @@ public class AnomalyDetectionJobServiceTest { mBundle = new Bundle(); mBundle.putParcelable(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, mStatsDimensionsValue); mFeatureFactory = FakeFeatureFactory.setupForTest(); when(mBatteryUtils.getAppLongVersionCode(any())).thenReturn(VERSION_CODE); mAnomalyDetectionJobService = spy(new AnomalyDetectionJobService()); } Loading Loading @@ -163,7 +165,8 @@ public class AnomalyDetectionJobServiceTest { verify(mFeatureFactory.metricsFeatureProvider).action(mContext, MetricsProto.MetricsEvent.ACTION_ANOMALY_IGNORED, SYSTEM_PACKAGE, Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, ANOMALY_TYPE)); Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, ANOMALY_TYPE), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, VERSION_CODE)); } @Test Loading Loading @@ -217,7 +220,8 @@ public class AnomalyDetectionJobServiceTest { verify(mFeatureFactory.metricsFeatureProvider).action(mContext, MetricsProto.MetricsEvent.ACTION_ANOMALY_TRIGGERED, SYSTEM_PACKAGE, Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE)); Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, VERSION_CODE)); } Loading @@ -242,7 +246,8 @@ public class AnomalyDetectionJobServiceTest { verify(mFeatureFactory.metricsFeatureProvider).action(mContext, MetricsProto.MetricsEvent.ACTION_ANOMALY_TRIGGERED, SYSTEM_PACKAGE, Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE)); Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_TYPE), Pair.create(MetricsProto.MetricsEvent.FIELD_APP_VERSION_CODE, VERSION_CODE)); } @Test Loading