Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 70bd0c6d authored by Lei Yu's avatar Lei Yu Committed by Android (Google) Code Review
Browse files

Merge "Log app versionCode in anomaly detection" into pi-dev

parents 660e8240 63795b2f
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -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;
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -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) {
+8 −3
Original line number Diff line number Diff line
@@ -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