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

Commit 8e1e91f2 authored by Kunal's avatar Kunal Committed by Kunal Malhotra
Browse files

Bug fix for properly clearing the last open call pointer

Bug: 294585409
Test: atest FgsLoggerTest
Change-Id: I62becec98e2c5278127500b9cd411e496e6d6b45
parent 558e2ef7
Loading
Loading
Loading
Loading
+25 −18
Original line number Diff line number Diff line
@@ -220,6 +220,24 @@ public class ForegroundServiceTypeLoggerModule {
        final ArrayList<Long> timestampsFound = new ArrayList<>();
        for (int i = 0, size = apiTypes.size(); i < size; i++) {
            final int apiType = apiTypes.get(i);

            // remove the FGS record from the stack
            final ArrayMap<ComponentName, ServiceRecord> runningFgsOfType =
                    uidState.mRunningFgs.get(apiType);
            if (runningFgsOfType == null) {
                Slog.w(TAG, "Could not find appropriate running FGS for FGS stop for UID " + uid
                        + " in package " + record.packageName);
                continue;
            }

            runningFgsOfType.remove(record.getComponentName());
            if (runningFgsOfType.size() == 0) {
                // there's no more FGS running for this type, just get rid of it
                uidState.mRunningFgs.remove(apiType);
                // but we need to keep track of the timestamp in case an API stops
                uidState.mLastFgsTimeStamp.put(apiType, System.currentTimeMillis());
            }

            final int apiTypeIndex = uidState.mOpenWithFgsCount.indexOfKey(apiType);
            if (apiTypeIndex < 0) {
                Slog.w(TAG, "Logger should be tracking FGS types correctly for UID " + uid
@@ -238,22 +256,6 @@ public class ForegroundServiceTypeLoggerModule {
                // remove the last API close call
                uidState.mApiClosedCalls.remove(apiType);
            }
            // remove the FGS record from the stack
            final ArrayMap<ComponentName, ServiceRecord> runningFgsOfType =
                    uidState.mRunningFgs.get(apiType);
            if (runningFgsOfType == null) {
                Slog.w(TAG, "Could not find appropriate running FGS for FGS stop for UID " + uid
                        + " in package " + record.packageName);
                continue;
            }

            runningFgsOfType.remove(record.getComponentName());
            if (runningFgsOfType.size() == 0) {
                // there's no more FGS running for this type, just get rid of it
                uidState.mRunningFgs.remove(apiType);
                // but we need to keep track of the timestamp in case an API stops
                uidState.mLastFgsTimeStamp.put(apiType, System.currentTimeMillis());
            }
        }
        if (!apisFound.isEmpty()) {
            // time to log the call
@@ -383,9 +385,14 @@ public class ForegroundServiceTypeLoggerModule {
            // initialize if we don't contain
            uidState.mOpenedWithoutFgsCount.put(apiType, 0);
        }
        if (uidState.mOpenedWithoutFgsCount.get(apiType) != 0) {
        int apiOpenWithoutFgsCount = uidState.mOpenedWithoutFgsCount.get(apiType);
        if (apiOpenWithoutFgsCount != 0) {
            apiOpenWithoutFgsCount -= 1;
            if (apiOpenWithoutFgsCount == 0) {
                uidState.mApiOpenCalls.remove(apiType);
            }
            uidState.mOpenedWithoutFgsCount
                    .put(apiType, uidState.mOpenedWithoutFgsCount.get(apiType) - 1);
                    .put(apiType, apiOpenWithoutFgsCount);
            return System.currentTimeMillis();
        }
        // This is a part of a valid active FGS
+44 −0
Original line number Diff line number Diff line
@@ -151,6 +151,50 @@ public class FgsLoggerTest {
        resetAndVerifyZeroInteractions();
    }

    @Test
    public void testApiStartStopFgs() throws InterruptedException {
        ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null);
        record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA;

        mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA,
                1, 1, "aPackageHasNoName");
        Thread.sleep(2000);

        resetAndVerifyZeroInteractions();

        mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1);

        resetAndVerifyZeroInteractions();

        mFgsLogger.logForegroundServiceStart(1, 1, record);

        resetAndVerifyZeroInteractions();
    }

    @Test
    public void testFgsStartStopApiStartStop() throws InterruptedException {
        ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null);
        record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA;

        mFgsLogger.logForegroundServiceStart(1, 1, record);

        resetAndVerifyZeroInteractions();

        mFgsLogger.logForegroundServiceStop(1, record);

        resetAndVerifyZeroInteractions();

        mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA,
                1, 1, "aPackageHasNoName");
        Thread.sleep(2000);

        resetAndVerifyZeroInteractions();

        mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1);

        resetAndVerifyZeroInteractions();
    }

    @Test
    public void testMultipleStartStopApis() throws InterruptedException {
        ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null);