From 7f289f5f0e6ffeebdee4006a580a7f9f0123c083 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Mon, 14 Jun 2021 16:14:46 -0700 Subject: [PATCH] Add a new field mAllowStartForegroundAtEntering. To save the mAllowStartForeground value when the service is entering FGS state. The logging of FOREGROUND_SERVICE_STATE_CHANGED__STATE__ENTER event could be deferred due to fgs notification deferred, mAllowStartForeground value could be changed for example due to stopService(). The FOREGROUND_SERVICE_STATE_CHANGED__STATE__ENTER event will read from mAllowStartForegroundAtEntering instead of mAllowStartForeground. For the same reason, also add a new field mAllowWhileInUsePermissionInFgsAtEntering to save mAllowWhileInUsePermissionInFgs value when the service is entering FGS state. Bug: 191054441 Test: build and run, monitor westworld event logs. Change-Id: Iad4541068ab27cb7686899998d833a7c6ada3172 --- .../com/android/server/am/ActiveServices.java | 23 +++++++++++++++---- .../com/android/server/am/ServiceRecord.java | 10 +++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 8aea4a948299b..9f831d2168c12 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -122,6 +122,7 @@ import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; +import android.os.PowerExemptionManager; import android.os.PowerExemptionManager.ReasonCode; import android.os.Process; import android.os.RemoteCallback; @@ -1850,7 +1851,6 @@ public final class ActiveServices { notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; r.foregroundNoti = notification; r.foregroundServiceType = foregroundServiceType; - boolean enterForeground = false; if (!r.isForeground) { final ServiceMap smap = getServiceMapLocked(r.userId); if (smap != null) { @@ -1877,7 +1877,12 @@ public final class ActiveServices { } r.isForeground = true; r.mLogEntering = true; - enterForeground = true; + // The logging of FOREGROUND_SERVICE_STATE_CHANGED__STATE__ENTER event could + // be deferred, make a copy of mAllowStartForeground and + // mAllowWhileInUsePermissionInFgs. + r.mAllowStartForegroundAtEntering = r.mAllowStartForeground; + r.mAllowWhileInUsePermissionInFgsAtEntering = + r.mAllowWhileInUsePermissionInFgs; r.mStartForegroundCount++; r.mFgsEnterTime = SystemClock.uptimeMillis(); if (!stopProcStatsOp) { @@ -6235,12 +6240,22 @@ public final class ActiveServices { r.packageName, mAm.mConstants.mFgsAtomSampleRate)) { return; } + boolean allowWhileInUsePermissionInFgs; + @PowerExemptionManager.ReasonCode int fgsStartReasonCode; + if (state == FrameworkStatsLog.FOREGROUND_SERVICE_STATE_CHANGED__STATE__ENTER + || state == FrameworkStatsLog.FOREGROUND_SERVICE_STATE_CHANGED__STATE__EXIT) { + allowWhileInUsePermissionInFgs = r.mAllowWhileInUsePermissionInFgsAtEntering; + fgsStartReasonCode = r.mAllowStartForegroundAtEntering; + } else { + allowWhileInUsePermissionInFgs = r.mAllowWhileInUsePermissionInFgs; + fgsStartReasonCode = r.mAllowStartForeground; + } FrameworkStatsLog.write(FrameworkStatsLog.FOREGROUND_SERVICE_STATE_CHANGED, r.appInfo.uid, r.shortInstanceName, state, - r.mAllowWhileInUsePermissionInFgs, - r.mAllowStartForeground, + allowWhileInUsePermissionInFgs, + fgsStartReasonCode, r.appInfo.targetSdkVersion, r.mRecentCallingUid, r.mRecentCallerApplicationInfo != null diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java index dd1ddd73a84f1..3ba07af710c24 100644 --- a/services/core/java/com/android/server/am/ServiceRecord.java +++ b/services/core/java/com/android/server/am/ServiceRecord.java @@ -18,7 +18,7 @@ package com.android.server.am; import static android.app.PendingIntent.FLAG_IMMUTABLE; import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; -import static android.os.PowerWhitelistManager.REASON_DENIED; +import static android.os.PowerExemptionManager.REASON_DENIED; import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; @@ -37,7 +37,7 @@ import android.net.Uri; import android.os.Binder; import android.os.Build; import android.os.IBinder; -import android.os.PowerWhitelistManager; +import android.os.PowerExemptionManager; import android.os.SystemClock; import android.os.UserHandle; import android.provider.Settings; @@ -153,6 +153,8 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN // allow while-in-use permissions in foreground service or not. // while-in-use permissions in FGS started from background might be restricted. boolean mAllowWhileInUsePermissionInFgs; + // A copy of mAllowWhileInUsePermissionInFgs's value when the service is entering FGS state. + boolean mAllowWhileInUsePermissionInFgsAtEntering; // the most recent package that start/bind this service. String mRecentCallingPackage; @@ -172,7 +174,9 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN // allow the service becomes foreground service? Service started from background may not be // allowed to become a foreground service. - @PowerWhitelistManager.ReasonCode int mAllowStartForeground = REASON_DENIED; + @PowerExemptionManager.ReasonCode int mAllowStartForeground = REASON_DENIED; + // A copy of mAllowStartForeground's value when the service is entering FGS state. + @PowerExemptionManager.ReasonCode int mAllowStartForegroundAtEntering = REASON_DENIED; // Debug info why mAllowStartForeground is allowed or denied. String mInfoAllowStartForeground; // Debug info if mAllowStartForeground is allowed because of a temp-allowlist.