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
This commit is contained in:
christosts 2024-01-22 05:16:46 -08:00 committed by Copybara-Service
parent 6029521898
commit 9e9c3cbe5e

View File

@ -3354,6 +3354,25 @@ public abstract class SimpleBasePlayer extends BasePlayer {
throw new IllegalStateException("Missing implementation to handle one of the COMMAND_SEEK_*"); 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.
*
* <p>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") @RequiresNonNull("state")
private boolean shouldHandleCommand(@Player.Command int commandCode) { private boolean shouldHandleCommand(@Player.Command int commandCode) {
return !released && state.availableCommands.contains(commandCode); return !released && state.availableCommands.contains(commandCode);
@ -3582,17 +3601,7 @@ public abstract class SimpleBasePlayer extends BasePlayer {
@EnsuresNonNull("state") @EnsuresNonNull("state")
private void verifyApplicationThreadAndInitState() { private void verifyApplicationThreadAndInitState() {
if (Thread.currentThread() != applicationLooper.getThread()) { verifyApplicationThread();
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);
}
if (state == null) { if (state == null) {
// First time accessing state. // First time accessing state.
state = getState(); state = getState();