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

Commit 256abd41 authored by Victor Hsieh's avatar Victor Hsieh
Browse files

Return early if no need to record measurement

recordMeasurementsForAllPackages is guarded by the `record` boolean
variable deep in the control flow. Now that the method only writes
atoms, it can return early for simplicity.

Bug: 265244016
Test: Manual

Change-Id: Ic877c85a844d299cf71fa1d8d73d595ec1dad68d
parent a562ab96
Loading
Loading
Loading
Loading
+51 −52
Original line number Original line Diff line number Diff line
@@ -253,7 +253,9 @@ public class BinaryTransparencyService extends SystemService {
        /**
        /**
         * Measures and records digests for *all* covered binaries/packages.
         * Measures and records digests for *all* covered binaries/packages.
         *
         *
         * This method will be called in a Job scheduled to take measurements periodically.
         * This method will be called in a Job scheduled to take measurements periodically. If the
         * last measurement was performaned recently (less than RECORD_MEASUREMENT_COOLDOWN_MS
         * ago), the measurement and recording will be skipped.
         *
         *
         * Packages that are covered so far are:
         * Packages that are covered so far are:
         * - all APEXs (introduced in Android T)
         * - all APEXs (introduced in Android T)
@@ -262,18 +264,19 @@ public class BinaryTransparencyService extends SystemService {
         * - dynamically installed mobile bundled apps (MBAs) (new in Android U)
         * - dynamically installed mobile bundled apps (MBAs) (new in Android U)
         */
         */
        public void recordMeasurementsForAllPackages() {
        public void recordMeasurementsForAllPackages() {
            PackageManager pm = mContext.getPackageManager();
            Set<String> packagesMeasured = new HashSet<>();

            // check if we should record the resulting measurements
            // check if we should record the resulting measurements
            long currentTimeMs = System.currentTimeMillis();
            long currentTimeMs = System.currentTimeMillis();
            boolean record = false;
            if ((currentTimeMs - mMeasurementsLastRecordedMs) < RECORD_MEASUREMENTS_COOLDOWN_MS) {
            if ((currentTimeMs - mMeasurementsLastRecordedMs) >= RECORD_MEASUREMENTS_COOLDOWN_MS) {
                Slog.d(TAG, "Skip measurement since the last measurement was only taken at "
                        + mMeasurementsLastRecordedMs + " within the cooldown period");
                return;
            }
            Slog.d(TAG, "Measurement was last taken at " + mMeasurementsLastRecordedMs
            Slog.d(TAG, "Measurement was last taken at " + mMeasurementsLastRecordedMs
                    + " and is now updated to: " + currentTimeMs);
                    + " and is now updated to: " + currentTimeMs);
            mMeasurementsLastRecordedMs = currentTimeMs;
            mMeasurementsLastRecordedMs = currentTimeMs;
                record = true;

            }
            PackageManager pm = mContext.getPackageManager();
            Set<String> packagesMeasured = new HashSet<>();


            // measure all APEXs first
            // measure all APEXs first
            if (DEBUG) {
            if (DEBUG) {
@@ -284,7 +287,6 @@ public class BinaryTransparencyService extends SystemService {


                Bundle apexMeasurement = measurePackage(packageInfo);
                Bundle apexMeasurement = measurePackage(packageInfo);


                if (record) {
                var apexInfo = new IBinaryTransparencyService.ApexInfo();
                var apexInfo = new IBinaryTransparencyService.ApexInfo();
                apexInfo.packageName = packageInfo.packageName;
                apexInfo.packageName = packageInfo.packageName;
                apexInfo.longVersion = packageInfo.getLongVersionCode();
                apexInfo.longVersion = packageInfo.getLongVersionCode();
@@ -296,7 +298,6 @@ public class BinaryTransparencyService extends SystemService {


                recordApexInfo(apexInfo);
                recordApexInfo(apexInfo);
            }
            }
            }
            if (DEBUG) {
            if (DEBUG) {
                Slog.d(TAG, "Measured " + packagesMeasured.size()
                Slog.d(TAG, "Measured " + packagesMeasured.size()
                        + " packages after considering APEXs.");
                        + " packages after considering APEXs.");
@@ -330,7 +331,7 @@ public class BinaryTransparencyService extends SystemService {
                    }
                    }
                }
                }


                if (record && (mbaStatus == MBA_STATUS_UPDATED_PRELOAD)) {
                if (mbaStatus == MBA_STATUS_UPDATED_PRELOAD) {
                    Bundle packageMeasurement = measurePackage(packageInfo);
                    Bundle packageMeasurement = measurePackage(packageInfo);


                    var appInfo = new IBinaryTransparencyService.AppInfo();
                    var appInfo = new IBinaryTransparencyService.AppInfo();
@@ -361,7 +362,6 @@ public class BinaryTransparencyService extends SystemService {


                    Bundle packageMeasurement = measurePackage(packageInfo);
                    Bundle packageMeasurement = measurePackage(packageInfo);


                    if (record) {
                    if (DEBUG) {
                    if (DEBUG) {
                        Slog.d(TAG,
                        Slog.d(TAG,
                                "Extracting InstallSourceInfo for " + packageInfo.packageName);
                                "Extracting InstallSourceInfo for " + packageInfo.packageName);
@@ -394,7 +394,6 @@ public class BinaryTransparencyService extends SystemService {
                    writeAppInfoToLog(appInfo);
                    writeAppInfoToLog(appInfo);
                }
                }
            }
            }
            }
            if (DEBUG) {
            if (DEBUG) {
                long timeSpentMeasuring = System.currentTimeMillis() - currentTimeMs;
                long timeSpentMeasuring = System.currentTimeMillis() - currentTimeMs;
                Slog.d(TAG, "Measured " + packagesMeasured.size()
                Slog.d(TAG, "Measured " + packagesMeasured.size()