diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java index 111b279391c..c8a5d47cfa8 100644 --- a/src/com/android/settings/fuelgauge/BatteryUtils.java +++ b/src/com/android/settings/fuelgauge/BatteryUtils.java @@ -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; @@ -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; + } } diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java index 661cb53fa56..518b633a480 100644 --- a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java +++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java @@ -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)) { @@ -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 @@ -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) { 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 16d7c3f6b37..d6fad34e5dd 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java @@ -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 @@ -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()); } @@ -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 @@ -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)); } @@ -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