From b9a8da4e3ee9afe1d231b8a5f35a03e14532720f Mon Sep 17 00:00:00 2001 From: ibaker Date: Fri, 29 Apr 2022 16:02:27 +0100 Subject: [PATCH] Allow stable API users to use DebugTextViewHelper This is used from the main demo app. PiperOrigin-RevId: 445420580 --- .../exoplayer2/util/DebugTextViewHelper.java | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/util/DebugTextViewHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/util/DebugTextViewHelper.java index 77fb5c048e..3461ee9e48 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/util/DebugTextViewHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/DebugTextViewHelper.java @@ -28,12 +28,13 @@ import java.util.Locale; * A helper class for periodically updating a {@link TextView} with debug information obtained from * an {@link ExoPlayer}. */ -public class DebugTextViewHelper implements Player.Listener, Runnable { +public class DebugTextViewHelper { private static final int REFRESH_INTERVAL_MS = 1000; private final ExoPlayer player; private final TextView textView; + private final Updater updater; private boolean started; @@ -47,6 +48,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { Assertions.checkArgument(player.getApplicationLooper() == Looper.getMainLooper()); this.player = player; this.textView = textView; + this.updater = new Updater(); } /** @@ -58,7 +60,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { return; } started = true; - player.addListener(this); + player.addListener(updater); updateAndPost(); } @@ -71,36 +73,8 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { return; } started = false; - player.removeListener(this); - textView.removeCallbacks(this); - } - - // 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(); + player.removeListener(updater); + textView.removeCallbacks(updater); } // Protected methods. @@ -108,8 +82,8 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { @SuppressLint("SetTextI18n") protected final void updateAndPost() { textView.setText(getDebugString()); - textView.removeCallbacks(this); - textView.postDelayed(this, REFRESH_INTERVAL_MS); + textView.removeCallbacks(updater); + textView.postDelayed(updater, REFRESH_INTERVAL_MS); } /** 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); } } + + 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(); + } + } }