Refactor shouldEnablePlayPauseButton out of PlayerControlView into Util

PiperOrigin-RevId: 691502799
This commit is contained in:
jbibik 2024-10-30 11:56:27 -07:00 committed by Copybara-Service
parent 2b27e33784
commit 6cbf77b3f0
2 changed files with 16 additions and 9 deletions

View File

@ -21,6 +21,7 @@ import static androidx.media3.common.C.AUXILIARY_TRACK_TYPE_DEPTH_LINEAR;
import static androidx.media3.common.C.AUXILIARY_TRACK_TYPE_DEPTH_METADATA;
import static androidx.media3.common.C.AUXILIARY_TRACK_TYPE_ORIGINAL;
import static androidx.media3.common.C.AUXILIARY_TRACK_TYPE_UNDEFINED;
import static androidx.media3.common.Player.COMMAND_GET_TIMELINE;
import static androidx.media3.common.Player.COMMAND_PLAY_PAUSE;
import static androidx.media3.common.Player.COMMAND_PREPARE;
import static androidx.media3.common.Player.COMMAND_SEEK_BACK;
@ -3548,6 +3549,20 @@ public final class Util {
return Integer.toString(i, Character.MAX_RADIX);
}
/**
* Returns whether a play-pause button should be enabled or not.
*
* @param player The {@link Player}. May be {@code null}.
*/
@EnsuresNonNullIf(result = true, expression = "#1")
@UnstableApi
public static boolean shouldEnablePlayPauseButton(@Nullable Player player) {
return player != null
&& player.isCommandAvailable(COMMAND_PLAY_PAUSE)
&& (!player.isCommandAvailable(COMMAND_GET_TIMELINE)
|| !player.getCurrentTimeline().isEmpty());
}
/**
* Returns whether a play button should be presented on a UI element for playback control. If
* {@code false}, a pause button should be shown instead.

View File

@ -18,7 +18,6 @@ package androidx.media3.ui;
import static androidx.media3.common.Player.COMMAND_GET_CURRENT_MEDIA_ITEM;
import static androidx.media3.common.Player.COMMAND_GET_TIMELINE;
import static androidx.media3.common.Player.COMMAND_GET_TRACKS;
import static androidx.media3.common.Player.COMMAND_PLAY_PAUSE;
import static androidx.media3.common.Player.COMMAND_SEEK_BACK;
import static androidx.media3.common.Player.COMMAND_SEEK_FORWARD;
import static androidx.media3.common.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM;
@ -1178,7 +1177,7 @@ public class PlayerControlView extends FrameLayout {
playPauseButton.setImageDrawable(drawable);
playPauseButton.setContentDescription(resources.getString(stringRes));
boolean enablePlayPause = shouldEnablePlayPauseButton();
boolean enablePlayPause = Util.shouldEnablePlayPauseButton(player);
updateButton(enablePlayPause, playPauseButton);
}
}
@ -1732,13 +1731,6 @@ public class PlayerControlView extends FrameLayout {
}
}
private boolean shouldEnablePlayPauseButton() {
return player != null
&& player.isCommandAvailable(COMMAND_PLAY_PAUSE)
&& (!player.isCommandAvailable(COMMAND_GET_TIMELINE)
|| !player.getCurrentTimeline().isEmpty());
}
@SuppressLint("InlinedApi")
private static boolean isHandledMediaKey(int keyCode) {
return keyCode == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD