mirror of
https://github.com/samsonjs/media.git
synced 2026-04-04 11:05:47 +00:00
Prevent the creation of CommandButtons without commands.
The setPlayerCommand and setSessionCommand builder methods contain assertions to ensure that at most one of these fields is set, but this left it possible to create a command button with no command by calling build on an empty builder. PiperOrigin-RevId: 547488248
This commit is contained in:
parent
2bfc6c7740
commit
ab904bde2d
2 changed files with 17 additions and 3 deletions
|
|
@ -17,6 +17,7 @@ package androidx.media3.session;
|
|||
|
||||
import static androidx.media3.common.util.Assertions.checkArgument;
|
||||
import static androidx.media3.common.util.Assertions.checkNotNull;
|
||||
import static androidx.media3.common.util.Assertions.checkState;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
|
|
@ -147,17 +148,20 @@ public final class CommandButton implements Bundleable {
|
|||
|
||||
/** Builds a {@link CommandButton}. */
|
||||
public CommandButton build() {
|
||||
checkState(
|
||||
(sessionCommand == null) != (playerCommand == Player.COMMAND_INVALID),
|
||||
"Exactly one of sessionCommand and playerCommand should be set");
|
||||
return new CommandButton(
|
||||
sessionCommand, playerCommand, iconResId, displayName, extras, enabled);
|
||||
}
|
||||
}
|
||||
|
||||
/** The session command of the button. Can be {@code null} if the button is a placeholder. */
|
||||
/** The session command of the button. Will be {@code null} if {@link #playerCommand} is set. */
|
||||
@Nullable public final SessionCommand sessionCommand;
|
||||
|
||||
/**
|
||||
* The {@link Player.Command} command of the button. Can be {@link Player#COMMAND_INVALID} if the
|
||||
* button is a placeholder.
|
||||
* The {@link Player.Command} command of the button. Will be {@link Player#COMMAND_INVALID} if
|
||||
* {@link #sessionCommand} is set.
|
||||
*/
|
||||
public final @Player.Command int playerCommand;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ package androidx.media3.session;
|
|||
|
||||
import static androidx.media3.session.CommandButton.CREATOR;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import android.os.Bundle;
|
||||
import androidx.media3.common.Player;
|
||||
|
|
@ -199,4 +200,13 @@ public class CommandButtonTest {
|
|||
.build()
|
||||
.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_withoutSessionOrPlayerCommandSet_throwsIllegalStateException() {
|
||||
CommandButton.Builder builder =
|
||||
new CommandButton.Builder()
|
||||
.setDisplayName("button")
|
||||
.setIconResId(R.drawable.media3_notification_small_icon);
|
||||
assertThrows(IllegalStateException.class, builder::build);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue