Merge "Log app versionCode in anomaly detection" into pi-dev
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index 111b279..c8a5d47 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -20,6 +20,7 @@
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 @@
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 661cb53..518b633 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
@@ -146,6 +146,7 @@
: 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 @@
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 @@
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 16d7c3f..d6fad34 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 @@
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 @@
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 @@
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 @@
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 @@
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