Skip to content

Commit

Permalink
Add "setCustomErrorMessage" override that allows including an extras …
Browse files Browse the repository at this point in the history
…bundle with a custom error.

PiperOrigin-RevId: 243807109
  • Loading branch information
nic0lette authored and ojw28 committed Apr 16, 2019
1 parent 5856e75 commit 0be4bee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* MediaSession extension:
* Allow handling of custom commands via `registerCustomCommandReceiver`.
* Add ability to include an extras `Bundle` when reporting a custom error.

### 2.10.0 ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public interface MediaMetadataProvider {
@Nullable private Player player;
@Nullable private ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider;
@Nullable private Pair<Integer, CharSequence> customError;
@Nullable private Bundle customErrorExtras;
@Nullable private PlaybackPreparer playbackPreparer;
@Nullable private QueueNavigator queueNavigator;
@Nullable private QueueEditor queueEditor;
Expand Down Expand Up @@ -581,7 +582,20 @@ public void setCustomErrorMessage(@Nullable CharSequence message) {
* @param code The error code to report. Ignored when {@code message} is {@code null}.
*/
public void setCustomErrorMessage(@Nullable CharSequence message, int code) {
setCustomErrorMessage(message, code, /* extras= */ null);
}

/**
* Sets a custom error on the session.
*
* @param message The error string to report or {@code null} to clear the error.
* @param code The error code to report. Ignored when {@code message} is {@code null}.
* @param extras Extras to include in reported {@link PlaybackStateCompat}.
*/
public void setCustomErrorMessage(
@Nullable CharSequence message, int code, @Nullable Bundle extras) {
customError = (message == null) ? null : new Pair<>(code, message);
customErrorExtras = (message == null) ? null : extras;
invalidateMediaSessionPlaybackState();
}

Expand Down Expand Up @@ -654,6 +668,7 @@ public final void invalidateMediaSessionPlaybackState() {
customActionMap = Collections.unmodifiableMap(currentActions);

int playbackState = player.getPlaybackState();
Bundle extras = new Bundle();
ExoPlaybackException playbackError =
playbackState == Player.STATE_IDLE ? player.getPlaybackError() : null;
boolean reportError = playbackError != null || customError != null;
Expand All @@ -663,6 +678,9 @@ public final void invalidateMediaSessionPlaybackState() {
: mapPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
if (customError != null) {
builder.setErrorMessage(customError.first, customError.second);
if (customErrorExtras != null) {
extras.putAll(customErrorExtras);
}
} else if (playbackError != null && errorMessageProvider != null) {
Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackError);
builder.setErrorMessage(message.first, message.second);
Expand All @@ -671,7 +689,6 @@ public final void invalidateMediaSessionPlaybackState() {
queueNavigator != null
? queueNavigator.getActiveQueueItemId(player)
: MediaSessionCompat.QueueItem.UNKNOWN_ID;
Bundle extras = new Bundle();
extras.putFloat(EXTRAS_PITCH, player.getPlaybackParameters().pitch);
builder
.setActions(buildPrepareActions() | buildPlaybackActions(player))
Expand Down

0 comments on commit 0be4bee

Please sign in to comment.