diff options
| -rw-r--r-- | services/core/java/com/android/server/am/ForegroundServiceTypeLoggerModule.java | 43 | ||||
| -rw-r--r-- | services/tests/servicestests/src/com/android/server/am/FgsLoggerTest.java | 44 |
2 files changed, 69 insertions, 18 deletions
diff --git a/services/core/java/com/android/server/am/ForegroundServiceTypeLoggerModule.java b/services/core/java/com/android/server/am/ForegroundServiceTypeLoggerModule.java index e0a71d46641a..caafb421a147 100644 --- a/services/core/java/com/android/server/am/ForegroundServiceTypeLoggerModule.java +++ b/services/core/java/com/android/server/am/ForegroundServiceTypeLoggerModule.java @@ -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 diff --git a/services/tests/servicestests/src/com/android/server/am/FgsLoggerTest.java b/services/tests/servicestests/src/com/android/server/am/FgsLoggerTest.java index f5005fdf1459..38bb3dea6ee3 100644 --- a/services/tests/servicestests/src/com/android/server/am/FgsLoggerTest.java +++ b/services/tests/servicestests/src/com/android/server/am/FgsLoggerTest.java @@ -152,6 +152,50 @@ public class FgsLoggerTest { } @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); record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; |