From 19758a38a4f839f7120e414c836161bf0bceb164 Mon Sep 17 00:00:00 2001 From: Lang Yu Date: Wed, 26 Jan 2022 15:27:59 +0800 Subject: [PATCH] Fix BatteryStats counts foreground activity time. When an application crashes or ANRs in the foreground, or when the application actively commits suicide in the foreground. It is often impossible to switch to the PAUSED state of ActivityRecord. At this time, the Timer in BatteryStats cannot be terminated, and it will keep timing, which will eventually lead to errors in foreground activity time statistics. So we'll settle in the handleAppDied function to fix that. Bug: 208355716 Change-Id: I23baf545105e0bb9322dc4a1702e38e0ef825c53 Signed-off-by: unknown <80264454@adc.com> --- services/core/java/com/android/server/wm/ActivityRecord.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java index b52e527b0556..6149bcf05050 100644 --- a/services/core/java/com/android/server/wm/ActivityRecord.java +++ b/services/core/java/com/android/server/wm/ActivityRecord.java @@ -4932,6 +4932,10 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A mAtmService.updateActivityUsageStats(this, Event.ACTIVITY_STOPPED); break; case DESTROYED: + if (app != null && (mVisible || mVisibleRequested)) { + // The app may be died while visible (no PAUSED state). + mAtmService.updateBatteryStats(this, false); + } mAtmService.updateActivityUsageStats(this, Event.ACTIVITY_DESTROYED); // Fall through. case DESTROYING: