Merge "Log app versionCode in anomaly detection" into pi-dev am: 70bd0c6d00

am: 471b71fb1c

Change-Id: I1fd2e48ed8f5eff66e2e1c8c97255a1894059097
This commit is contained in:
Lei Yu
2018-04-12 18:57:37 -07:00
committed by android-build-merger
3 changed files with 32 additions and 5 deletions

View File

@@ -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;
}
}

View File

@@ -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) {