From 9e9c3cbe5e1576e89ef1f5492b2c14865bc1ddbe Mon Sep 17 00:00:00 2001 From: christosts Date: Mon, 22 Jan 2024 05:16:46 -0800 Subject: [PATCH] SimpleBasePlayer: add protected method for thread verification Add protected method in SimpleBasePlayer for thread verification to help subclasses verify thread access for additional methods they define and still report the same message to the user. Also, remove the DAC link pointing to the ExoPlayer-specific documentation from the exception message. Users who extend SimpleBasePlayer have access to the class' javadoc. PiperOrigin-RevId: 600426692 --- .../media3/common/SimpleBasePlayer.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/libraries/common/src/main/java/androidx/media3/common/SimpleBasePlayer.java b/libraries/common/src/main/java/androidx/media3/common/SimpleBasePlayer.java index 9c75c30283..70a3f78541 100644 --- a/libraries/common/src/main/java/androidx/media3/common/SimpleBasePlayer.java +++ b/libraries/common/src/main/java/androidx/media3/common/SimpleBasePlayer.java @@ -3354,6 +3354,25 @@ public abstract class SimpleBasePlayer extends BasePlayer { throw new IllegalStateException("Missing implementation to handle one of the COMMAND_SEEK_*"); } + /** + * Throws an {@link IllegalStateException} if the the thread calling this method does not match + * the {@link Looper} thread that was specified upon construction of this instance. + * + *

Subclasses can use this method to verify that their own defined methods are also accessed by + * the correct thread. + */ + protected final void verifyApplicationThread() { + if (Thread.currentThread() != applicationLooper.getThread()) { + String message = + Util.formatInvariant( + "Player is accessed on the wrong thread.\n" + + "Current thread: '%s'\n" + + "Expected thread: '%s'\n", + Thread.currentThread().getName(), applicationLooper.getThread().getName()); + throw new IllegalStateException(message); + } + } + @RequiresNonNull("state") private boolean shouldHandleCommand(@Player.Command int commandCode) { return !released && state.availableCommands.contains(commandCode); @@ -3582,17 +3601,7 @@ public abstract class SimpleBasePlayer extends BasePlayer { @EnsuresNonNull("state") private void verifyApplicationThreadAndInitState() { - if (Thread.currentThread() != applicationLooper.getThread()) { - String message = - Util.formatInvariant( - "Player is accessed on the wrong thread.\n" - + "Current thread: '%s'\n" - + "Expected thread: '%s'\n" - + "See https://developer.android.com/guide/topics/media/issues/" - + "player-accessed-on-wrong-thread", - Thread.currentThread().getName(), applicationLooper.getThread().getName()); - throw new IllegalStateException(message); - } + verifyApplicationThread(); if (state == null) { // First time accessing state. state = getState();