Allow stable API users to use DebugTextViewHelper

This is used from the main demo app.

PiperOrigin-RevId: 445420580
This commit is contained in:
ibaker 2022-04-29 16:02:27 +01:00 committed by Ian Baker
parent b0df946f67
commit b9a8da4e3e

View File

@ -28,12 +28,13 @@ import java.util.Locale;
* A helper class for periodically updating a {@link TextView} with debug information obtained from * A helper class for periodically updating a {@link TextView} with debug information obtained from
* an {@link ExoPlayer}. * an {@link ExoPlayer}.
*/ */
public class DebugTextViewHelper implements Player.Listener, Runnable { public class DebugTextViewHelper {
private static final int REFRESH_INTERVAL_MS = 1000; private static final int REFRESH_INTERVAL_MS = 1000;
private final ExoPlayer player; private final ExoPlayer player;
private final TextView textView; private final TextView textView;
private final Updater updater;
private boolean started; private boolean started;
@ -47,6 +48,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
Assertions.checkArgument(player.getApplicationLooper() == Looper.getMainLooper()); Assertions.checkArgument(player.getApplicationLooper() == Looper.getMainLooper());
this.player = player; this.player = player;
this.textView = textView; this.textView = textView;
this.updater = new Updater();
} }
/** /**
@ -58,7 +60,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
return; return;
} }
started = true; started = true;
player.addListener(this); player.addListener(updater);
updateAndPost(); updateAndPost();
} }
@ -71,36 +73,8 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
return; return;
} }
started = false; started = false;
player.removeListener(this); player.removeListener(updater);
textView.removeCallbacks(this); textView.removeCallbacks(updater);
}
// Player.Listener implementation.
@Override
public final void onPlaybackStateChanged(@Player.State int playbackState) {
updateAndPost();
}
@Override
public final void onPlayWhenReadyChanged(
boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
updateAndPost();
}
@Override
public final void onPositionDiscontinuity(
Player.PositionInfo oldPosition,
Player.PositionInfo newPosition,
@Player.DiscontinuityReason int reason) {
updateAndPost();
}
// Runnable implementation.
@Override
public final void run() {
updateAndPost();
} }
// Protected methods. // Protected methods.
@ -108,8 +82,8 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
protected final void updateAndPost() { protected final void updateAndPost() {
textView.setText(getDebugString()); textView.setText(getDebugString());
textView.removeCallbacks(this); textView.removeCallbacks(updater);
textView.postDelayed(this, REFRESH_INTERVAL_MS); textView.postDelayed(updater, REFRESH_INTERVAL_MS);
} }
/** Returns the debugging information string to be shown by the target {@link TextView}. */ /** Returns the debugging information string to be shown by the target {@link TextView}. */
@ -219,4 +193,35 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
return String.valueOf(averageUs); return String.valueOf(averageUs);
} }
} }
private final class Updater implements Player.Listener, Runnable {
// Player.Listener implementation.
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
updateAndPost();
}
@Override
public void onPlayWhenReadyChanged(
boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
updateAndPost();
}
@Override
public void onPositionDiscontinuity(
Player.PositionInfo oldPosition,
Player.PositionInfo newPosition,
@Player.DiscontinuityReason int reason) {
updateAndPost();
}
// Runnable implementation.
@Override
public void run() {
updateAndPost();
}
}
} }