summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Santiago Seifert <aquilescanta@google.com> 2023-12-22 15:54:28 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2023-12-22 15:54:28 +0000
commita830950a471e67bd0daf3b5506d6c0ad78aa7e2c (patch)
treeba36772a4350667a0b5b95874b97ecd30f9e8f88
parent95b9d9374d28141e459d63e40e7091cca4079915 (diff)
parente7153d97f4fcc7798a713dcec31c8db7424a7631 (diff)
Merge "Dedup MediaSessionService foreground service handling code" into main
-rw-r--r--services/core/java/com/android/server/media/MediaSessionService.java61
1 files changed, 16 insertions, 45 deletions
diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java
index d4666bac89eb..db39b5e24bbb 100644
--- a/services/core/java/com/android/server/media/MediaSessionService.java
+++ b/services/core/java/com/android/server/media/MediaSessionService.java
@@ -290,31 +290,12 @@ public class MediaSessionService extends SystemService implements Monitor {
}
user.mPriorityStack.onSessionActiveStateChanged(record);
}
- notifyActivityManagerWithActiveStateChanges(record, record.isActive());
+ setForegroundServiceAllowance(
+ record, /* allowRunningInForeground= */ record.isActive());
mHandler.postSessionsChanged(record);
}
}
- private void notifyActivityManagerWithActiveStateChanges(
- MediaSessionRecordImpl record, boolean isActive) {
- if (!Flags.enableNotifyingActivityManagerWithMediaSessionStatusChange()) {
- return;
- }
- ForegroundServiceDelegationOptions foregroundServiceDelegationOptions =
- record.getForegroundServiceDelegationOptions();
- if (foregroundServiceDelegationOptions == null) {
- // This record doesn't support FGS delegation. In practice, this is MediaSession2.
- return;
- }
- if (isActive) {
- mActivityManagerInternal.startForegroundServiceDelegate(
- foregroundServiceDelegationOptions, /* connection= */ null);
- } else {
- mActivityManagerInternal.stopForegroundServiceDelegate(
- foregroundServiceDelegationOptions);
- }
- }
-
// Currently only media1 can become global priority session.
void setGlobalPrioritySession(MediaSessionRecord record) {
synchronized (mLock) {
@@ -407,27 +388,10 @@ public class MediaSessionService extends SystemService implements Monitor {
return;
}
user.mPriorityStack.onPlaybackStateChanged(record, shouldUpdatePriority);
- notifyActivityManagerWithPlaybackStateChanges(record, playbackState);
- }
- }
-
- private void notifyActivityManagerWithPlaybackStateChanges(
- MediaSessionRecordImpl record, PlaybackState playbackState) {
- if (!Flags.enableNotifyingActivityManagerWithMediaSessionStatusChange()) {
- return;
- }
- ForegroundServiceDelegationOptions foregroundServiceDelegationOptions =
- record.getForegroundServiceDelegationOptions();
- if (foregroundServiceDelegationOptions == null || playbackState == null) {
- // This record doesn't support FGS delegation. In practice, this is MediaSession2.
- return;
- }
- if (playbackState.shouldAllowServiceToRunInForeground()) {
- mActivityManagerInternal.startForegroundServiceDelegate(
- foregroundServiceDelegationOptions, /* connection= */ null);
- } else {
- mActivityManagerInternal.stopForegroundServiceDelegate(
- foregroundServiceDelegationOptions);
+ if (playbackState != null) {
+ setForegroundServiceAllowance(
+ record, playbackState.shouldAllowServiceToRunInForeground());
+ }
}
}
@@ -591,11 +555,12 @@ public class MediaSessionService extends SystemService implements Monitor {
}
session.close();
- notifyActivityManagerWithSessionDestroyed(session);
+ setForegroundServiceAllowance(session, /* allowRunningInForeground= */ false);
mHandler.postSessionsChanged(session);
}
- private void notifyActivityManagerWithSessionDestroyed(MediaSessionRecordImpl record) {
+ private void setForegroundServiceAllowance(
+ MediaSessionRecordImpl record, boolean allowRunningInForeground) {
if (!Flags.enableNotifyingActivityManagerWithMediaSessionStatusChange()) {
return;
}
@@ -605,7 +570,13 @@ public class MediaSessionService extends SystemService implements Monitor {
// This record doesn't support FGS delegation. In practice, this is MediaSession2.
return;
}
- mActivityManagerInternal.stopForegroundServiceDelegate(foregroundServiceDelegationOptions);
+ if (allowRunningInForeground) {
+ mActivityManagerInternal.startForegroundServiceDelegate(
+ foregroundServiceDelegationOptions, /* connection= */ null);
+ } else {
+ mActivityManagerInternal.stopForegroundServiceDelegate(
+ foregroundServiceDelegationOptions);
+ }
}
void tempAllowlistTargetPkgIfPossible(int targetUid, String targetPackage,