mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Make foreground service timeout configurable
This allows apps to set a shorter timeout if so desired. Issue: androidx/media#2206 #cherrypick PiperOrigin-RevId: 735360459 (cherry picked from commit 222950cfd1bdc942b90e0991e6b96a3f2c86518a)
This commit is contained in:
parent
67ec5b76ad
commit
b215670445
@ -57,7 +57,6 @@ import java.util.concurrent.TimeoutException;
|
|||||||
|
|
||||||
private static final String TAG = "MediaNtfMng";
|
private static final String TAG = "MediaNtfMng";
|
||||||
private static final int MSG_USER_ENGAGED_TIMEOUT = 1;
|
private static final int MSG_USER_ENGAGED_TIMEOUT = 1;
|
||||||
private static final long USER_ENGAGED_TIMEOUT_MS = 600_000;
|
|
||||||
|
|
||||||
private final MediaSessionService mediaSessionService;
|
private final MediaSessionService mediaSessionService;
|
||||||
private final MediaNotification.Provider mediaNotificationProvider;
|
private final MediaNotification.Provider mediaNotificationProvider;
|
||||||
@ -73,6 +72,7 @@ import java.util.concurrent.TimeoutException;
|
|||||||
private boolean startedInForeground;
|
private boolean startedInForeground;
|
||||||
private boolean isUserEngaged;
|
private boolean isUserEngaged;
|
||||||
private boolean isUserEngagedTimeoutEnabled;
|
private boolean isUserEngagedTimeoutEnabled;
|
||||||
|
private long userEngagedTimeoutMs;
|
||||||
|
|
||||||
public MediaNotificationManager(
|
public MediaNotificationManager(
|
||||||
MediaSessionService mediaSessionService,
|
MediaSessionService mediaSessionService,
|
||||||
@ -88,6 +88,7 @@ import java.util.concurrent.TimeoutException;
|
|||||||
controllerMap = new HashMap<>();
|
controllerMap = new HashMap<>();
|
||||||
startedInForeground = false;
|
startedInForeground = false;
|
||||||
isUserEngagedTimeoutEnabled = true;
|
isUserEngagedTimeoutEnabled = true;
|
||||||
|
userEngagedTimeoutMs = MediaSessionService.DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSession(MediaSession session) {
|
public void addSession(MediaSession session) {
|
||||||
@ -191,6 +192,10 @@ import java.util.concurrent.TimeoutException;
|
|||||||
return startedInForeground;
|
return startedInForeground;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUserEngagedTimeoutMs(long userEngagedTimeoutMs) {
|
||||||
|
this.userEngagedTimeoutMs = userEngagedTimeoutMs;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handleMessage(Message msg) {
|
public boolean handleMessage(Message msg) {
|
||||||
if (msg.what == MSG_USER_ENGAGED_TIMEOUT) {
|
if (msg.what == MSG_USER_ENGAGED_TIMEOUT) {
|
||||||
@ -206,8 +211,9 @@ import java.util.concurrent.TimeoutException;
|
|||||||
|
|
||||||
/* package */ boolean shouldRunInForeground(boolean startInForegroundWhenPaused) {
|
/* package */ boolean shouldRunInForeground(boolean startInForegroundWhenPaused) {
|
||||||
boolean isUserEngaged = isAnySessionUserEngaged(startInForegroundWhenPaused);
|
boolean isUserEngaged = isAnySessionUserEngaged(startInForegroundWhenPaused);
|
||||||
if (this.isUserEngaged && !isUserEngaged && isUserEngagedTimeoutEnabled) {
|
boolean useTimeout = isUserEngagedTimeoutEnabled && userEngagedTimeoutMs > 0;
|
||||||
mainHandler.sendEmptyMessageDelayed(MSG_USER_ENGAGED_TIMEOUT, USER_ENGAGED_TIMEOUT_MS);
|
if (this.isUserEngaged && !isUserEngaged && useTimeout) {
|
||||||
|
mainHandler.sendEmptyMessageDelayed(MSG_USER_ENGAGED_TIMEOUT, userEngagedTimeoutMs);
|
||||||
} else if (isUserEngaged) {
|
} else if (isUserEngaged) {
|
||||||
mainHandler.removeMessages(MSG_USER_ENGAGED_TIMEOUT);
|
mainHandler.removeMessages(MSG_USER_ENGAGED_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
@ -159,6 +159,12 @@ public abstract class MediaSessionService extends Service {
|
|||||||
/** The action for {@link Intent} filter that must be declared by the service. */
|
/** The action for {@link Intent} filter that must be declared by the service. */
|
||||||
public static final String SERVICE_INTERFACE = "androidx.media3.session.MediaSessionService";
|
public static final String SERVICE_INTERFACE = "androidx.media3.session.MediaSessionService";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default timeout for a session to stay in a foreground service state after it paused,
|
||||||
|
* stopped, failed or ended.
|
||||||
|
*/
|
||||||
|
@UnstableApi public static final long DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS = 600_000;
|
||||||
|
|
||||||
private static final String TAG = "MSessionService";
|
private static final String TAG = "MSessionService";
|
||||||
|
|
||||||
private final Object lock;
|
private final Object lock;
|
||||||
@ -471,6 +477,27 @@ public abstract class MediaSessionService extends Service {
|
|||||||
/* maxCommandsForMediaItems= */ 0);
|
/* maxCommandsForMediaItems= */ 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the timeout for a session to stay in a foreground service state after it paused, stopped,
|
||||||
|
* failed or ended.
|
||||||
|
*
|
||||||
|
* <p>Has no effect on already running timeouts.
|
||||||
|
*
|
||||||
|
* <p>The default and maximum value is {@link #DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS}. If a larger
|
||||||
|
* value is provided, it will be clamped down to {@link #DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS}.
|
||||||
|
*
|
||||||
|
* @param foregroundServiceTimeoutMs The timeout in milliseconds.
|
||||||
|
*/
|
||||||
|
@UnstableApi
|
||||||
|
public final void setForegroundServiceTimeoutMs(long foregroundServiceTimeoutMs) {
|
||||||
|
getMediaNotificationManager()
|
||||||
|
.setUserEngagedTimeoutMs(
|
||||||
|
Util.constrainValue(
|
||||||
|
foregroundServiceTimeoutMs,
|
||||||
|
/* min= */ 0,
|
||||||
|
/* max= */ DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether there is a session with ongoing user-engaged playback that is run in a
|
* Returns whether there is a session with ongoing user-engaged playback that is run in a
|
||||||
* foreground service.
|
* foreground service.
|
||||||
@ -478,9 +505,10 @@ public abstract class MediaSessionService extends Service {
|
|||||||
* <p>It is only possible to terminate the service with {@link #stopSelf()} if this method returns
|
* <p>It is only possible to terminate the service with {@link #stopSelf()} if this method returns
|
||||||
* {@code false}.
|
* {@code false}.
|
||||||
*
|
*
|
||||||
* <p>Note that sessions are kept in foreground and this method returns {@code true} for a period
|
* <p>Note that sessions are kept in foreground and this method returns {@code true} for the
|
||||||
* of 10 minutes after they paused, stopped or failed. Use {@link #pauseAllPlayersAndStopSelf()}
|
* {@linkplain #setForegroundServiceTimeoutMs foreground service timeout} after they paused,
|
||||||
* to pause all ongoing playbacks immediately and terminate the service.
|
* stopped, failed or ended. Use {@link #pauseAllPlayersAndStopSelf()} to pause all ongoing
|
||||||
|
* playbacks immediately and terminate the service.
|
||||||
*/
|
*/
|
||||||
@UnstableApi
|
@UnstableApi
|
||||||
public boolean isPlaybackOngoing() {
|
public boolean isPlaybackOngoing() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user