Skip to content

Commit

Permalink
Add missing command checks in UI module
Browse files Browse the repository at this point in the history
The commands are partly checked already before enabling
features or calling player methods, but the checks were
still missing in many places.

#minor-release

PiperOrigin-RevId: 504589888
  • Loading branch information
tonihei authored and christosts committed Jan 25, 2023
1 parent 5ed72ef commit 2d7ddcc
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.android.exoplayer2.ui;

import static com.google.android.exoplayer2.Player.COMMAND_GET_MEDIA_ITEMS_METADATA;

import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand Down Expand Up @@ -46,6 +48,9 @@ public DefaultMediaDescriptionAdapter(@Nullable PendingIntent pendingIntent) {

@Override
public CharSequence getCurrentContentTitle(Player player) {
if (!player.isCommandAvailable(COMMAND_GET_MEDIA_ITEMS_METADATA)) {
return "";
}
@Nullable CharSequence displayTitle = player.getMediaMetadata().displayTitle;
if (!TextUtils.isEmpty(displayTitle)) {
return displayTitle;
Expand All @@ -64,6 +69,9 @@ public PendingIntent createCurrentContentIntent(Player player) {
@Nullable
@Override
public CharSequence getCurrentContentText(Player player) {
if (!player.isCommandAvailable(COMMAND_GET_MEDIA_ITEMS_METADATA)) {
return null;
}
@Nullable CharSequence artist = player.getMediaMetadata().artist;
if (!TextUtils.isEmpty(artist)) {
return artist;
Expand All @@ -75,6 +83,9 @@ public CharSequence getCurrentContentText(Player player) {
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, BitmapCallback callback) {
if (!player.isCommandAvailable(COMMAND_GET_MEDIA_ITEMS_METADATA)) {
return null;
}
@Nullable byte[] data = player.getMediaMetadata().artworkData;
if (data == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
*/
package com.google.android.exoplayer2.ui;

import static com.google.android.exoplayer2.Player.COMMAND_CHANGE_MEDIA_ITEMS;
import static com.google.android.exoplayer2.Player.COMMAND_GET_CURRENT_MEDIA_ITEM;
import static com.google.android.exoplayer2.Player.COMMAND_GET_TIMELINE;
import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE;
import static com.google.android.exoplayer2.Player.COMMAND_PREPARE;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_DEFAULT_POSITION;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS;
import static com.google.android.exoplayer2.Player.COMMAND_STOP;
import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_MEDIA_METADATA_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED;
Expand Down Expand Up @@ -1203,7 +1210,9 @@ protected NotificationCompat.Builder createNotification(
@Nullable NotificationCompat.Builder builder,
boolean ongoing,
@Nullable Bitmap largeIcon) {
if (player.getPlaybackState() == Player.STATE_IDLE && player.getCurrentTimeline().isEmpty()) {
if (player.getPlaybackState() == Player.STATE_IDLE
&& player.isCommandAvailable(COMMAND_GET_TIMELINE)
&& player.getCurrentTimeline().isEmpty()) {
builderActions = null;
return null;
}
Expand Down Expand Up @@ -1257,6 +1266,7 @@ protected NotificationCompat.Builder createNotification(
// Changing "showWhen" causes notification flicker if SDK_INT < 21.
if (Util.SDK_INT >= 21
&& useChronometer
&& player.isCommandAvailable(COMMAND_GET_CURRENT_MEDIA_ITEM)
&& player.isPlaying()
&& !player.isPlayingAd()
&& !player.isCurrentMediaItemDynamic()
Expand Down Expand Up @@ -1535,24 +1545,43 @@ public void onReceive(Context context, Intent intent) {
}
String action = intent.getAction();
if (ACTION_PLAY.equals(action)) {
if (player.getPlaybackState() == Player.STATE_IDLE) {
if (player.getPlaybackState() == Player.STATE_IDLE
&& player.isCommandAvailable(COMMAND_PREPARE)) {
player.prepare();
} else if (player.getPlaybackState() == Player.STATE_ENDED) {
player.seekToDefaultPosition(player.getCurrentMediaItemIndex());
} else if (player.getPlaybackState() == Player.STATE_ENDED
&& player.isCommandAvailable(COMMAND_SEEK_TO_DEFAULT_POSITION)) {
player.seekToDefaultPosition();
}
if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) {
player.play();
}
player.play();
} else if (ACTION_PAUSE.equals(action)) {
player.pause();
if (player.isCommandAvailable(COMMAND_PLAY_PAUSE)) {
player.pause();
}
} else if (ACTION_PREVIOUS.equals(action)) {
player.seekToPrevious();
if (player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS)) {
player.seekToPrevious();
}
} else if (ACTION_REWIND.equals(action)) {
player.seekBack();
if (player.isCommandAvailable(COMMAND_SEEK_BACK)) {
player.seekBack();
}
} else if (ACTION_FAST_FORWARD.equals(action)) {
player.seekForward();
if (player.isCommandAvailable(COMMAND_SEEK_FORWARD)) {
player.seekForward();
}
} else if (ACTION_NEXT.equals(action)) {
player.seekToNext();
if (player.isCommandAvailable(COMMAND_SEEK_TO_NEXT)) {
player.seekToNext();
}
} else if (ACTION_STOP.equals(action)) {
player.stop(/* reset= */ true);
if (player.isCommandAvailable(COMMAND_STOP)) {
player.stop();
}
if (player.isCommandAvailable(COMMAND_CHANGE_MEDIA_ITEMS)) {
player.clearMediaItems();
}
} else if (ACTION_DISMISS.equals(action)) {
stopNotification(/* dismissedByUser= */ true);
} else if (action != null
Expand Down
Loading

0 comments on commit 2d7ddcc

Please sign in to comment.