Add iconUri to CommandButton

PiperOrigin-RevId: 578916804
This commit is contained in:
Googler 2023-11-02 11:10:22 -07:00 committed by Copybara-Service
parent 901f89c456
commit 21a9bfe440
2 changed files with 68 additions and 3 deletions

View File

@ -19,6 +19,7 @@ import static androidx.media3.common.util.Assertions.checkArgument;
import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState; import static androidx.media3.common.util.Assertions.checkState;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
@ -48,6 +49,7 @@ public final class CommandButton implements Bundleable {
@Nullable private SessionCommand sessionCommand; @Nullable private SessionCommand sessionCommand;
private @Player.Command int playerCommand; private @Player.Command int playerCommand;
@DrawableRes private int iconResId; @DrawableRes private int iconResId;
@Nullable private Uri iconUri;
private CharSequence displayName; private CharSequence displayName;
private Bundle extras; private Bundle extras;
private boolean enabled; private boolean enabled;
@ -111,6 +113,19 @@ public final class CommandButton implements Bundleable {
return this; return this;
} }
/**
* Sets a {@link Uri} for the icon of this button.
*
* @param uri The uri to an icon.
* @return This builder for chaining.
*/
@UnstableApi
@CanIgnoreReturnValue
public Builder setIconUri(Uri uri) {
this.iconUri = uri;
return this;
}
/** /**
* Sets a display name of this button. * Sets a display name of this button.
* *
@ -153,7 +168,7 @@ public final class CommandButton implements Bundleable {
(sessionCommand == null) != (playerCommand == Player.COMMAND_INVALID), (sessionCommand == null) != (playerCommand == Player.COMMAND_INVALID),
"Exactly one of sessionCommand and playerCommand should be set"); "Exactly one of sessionCommand and playerCommand should be set");
return new CommandButton( return new CommandButton(
sessionCommand, playerCommand, iconResId, displayName, extras, enabled); sessionCommand, playerCommand, iconResId, iconUri, displayName, extras, enabled);
} }
} }
@ -172,6 +187,9 @@ public final class CommandButton implements Bundleable {
*/ */
@DrawableRes public final int iconResId; @DrawableRes public final int iconResId;
/** The {@link Uri} for the icon of the button. Can be {@code null}. */
@UnstableApi @Nullable public final Uri iconUri;
/** /**
* The display name of the button. Can be empty if the command is predefined and a custom name * The display name of the button. Can be empty if the command is predefined and a custom name
* isn't needed. * isn't needed.
@ -191,12 +209,14 @@ public final class CommandButton implements Bundleable {
@Nullable SessionCommand sessionCommand, @Nullable SessionCommand sessionCommand,
@Player.Command int playerCommand, @Player.Command int playerCommand,
@DrawableRes int iconResId, @DrawableRes int iconResId,
@Nullable Uri iconUri,
CharSequence displayName, CharSequence displayName,
Bundle extras, Bundle extras,
boolean enabled) { boolean enabled) {
this.sessionCommand = sessionCommand; this.sessionCommand = sessionCommand;
this.playerCommand = playerCommand; this.playerCommand = playerCommand;
this.iconResId = iconResId; this.iconResId = iconResId;
this.iconUri = iconUri;
this.displayName = displayName; this.displayName = displayName;
this.extras = new Bundle(extras); this.extras = new Bundle(extras);
this.isEnabled = enabled; this.isEnabled = enabled;
@ -212,7 +232,13 @@ public final class CommandButton implements Bundleable {
return this; return this;
} }
return new CommandButton( return new CommandButton(
sessionCommand, playerCommand, iconResId, displayName, new Bundle(extras), isEnabled); sessionCommand,
playerCommand,
iconResId,
iconUri,
displayName,
new Bundle(extras),
isEnabled);
} }
/** Checks the given command button for equality while ignoring {@link #extras}. */ /** Checks the given command button for equality while ignoring {@link #extras}. */
@ -228,13 +254,15 @@ public final class CommandButton implements Bundleable {
return Objects.equal(sessionCommand, button.sessionCommand) return Objects.equal(sessionCommand, button.sessionCommand)
&& playerCommand == button.playerCommand && playerCommand == button.playerCommand
&& iconResId == button.iconResId && iconResId == button.iconResId
&& Objects.equal(iconUri, button.iconUri)
&& TextUtils.equals(displayName, button.displayName) && TextUtils.equals(displayName, button.displayName)
&& isEnabled == button.isEnabled; && isEnabled == button.isEnabled;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(sessionCommand, playerCommand, iconResId, displayName, isEnabled); return Objects.hashCode(
sessionCommand, playerCommand, iconResId, displayName, isEnabled, iconUri);
} }
/** /**

View File

@ -19,6 +19,7 @@ import static androidx.media3.session.CommandButton.CREATOR;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertThrows;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.media3.common.Player; import androidx.media3.common.Player;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
@ -114,18 +115,46 @@ public class CommandButtonTest {
.containsExactly(button1.copyWithIsEnabled(true), button2.copyWithIsEnabled(true)); .containsExactly(button1.copyWithIsEnabled(true), button2.copyWithIsEnabled(true));
} }
@Test
public void getIconUri_returnsUri() {
Uri uri = Uri.parse("content://test");
CommandButton button =
new CommandButton.Builder()
.setDisplayName("button1")
.setIconResId(R.drawable.media3_notification_small_icon)
.setIconUri(uri)
.setPlayerCommand(Player.COMMAND_SEEK_TO_PREVIOUS)
.build();
assertThat(button.iconUri).isEqualTo(uri);
}
@Test
public void getIconUri_returnsNullIfUnset() {
CommandButton button =
new CommandButton.Builder()
.setDisplayName("button1")
.setIconResId(R.drawable.media3_notification_small_icon)
.setPlayerCommand(Player.COMMAND_SEEK_TO_PREVIOUS)
.build();
assertThat(button.iconUri).isNull();
}
@Test @Test
public void equals() { public void equals() {
assertThat( assertThat(
new CommandButton.Builder() new CommandButton.Builder()
.setDisplayName("button") .setDisplayName("button")
.setIconResId(R.drawable.media3_notification_small_icon) .setIconResId(R.drawable.media3_notification_small_icon)
.setIconUri(Uri.parse("content://test"))
.setPlayerCommand(Player.COMMAND_SEEK_TO_NEXT) .setPlayerCommand(Player.COMMAND_SEEK_TO_NEXT)
.build()) .build())
.isEqualTo( .isEqualTo(
new CommandButton.Builder() new CommandButton.Builder()
.setDisplayName("button") .setDisplayName("button")
.setIconResId(R.drawable.media3_notification_small_icon) .setIconResId(R.drawable.media3_notification_small_icon)
.setIconUri(Uri.parse("content://test"))
.setPlayerCommand(Player.COMMAND_SEEK_TO_NEXT) .setPlayerCommand(Player.COMMAND_SEEK_TO_NEXT)
.build()); .build());
} }
@ -176,6 +205,14 @@ public class CommandButtonTest {
.setDisplayName("button") .setDisplayName("button")
.setIconResId(R.drawable.media3_notification_small_icon) .setIconResId(R.drawable.media3_notification_small_icon)
.build()); .build());
assertThat(button)
.isNotEqualTo(
new CommandButton.Builder()
.setDisplayName("button")
.setIconResId(R.drawable.media3_notification_small_icon)
.setIconUri(Uri.parse("content://test"))
.setPlayerCommand(Player.COMMAND_SEEK_TO_NEXT)
.build());
} }
@Test @Test