From aec3e458d8847678b67569aefb819288dfd4052c Mon Sep 17 00:00:00 2001 From: bachinger Date: Mon, 23 Nov 2020 16:02:48 +0000 Subject: [PATCH] Use onEvents in PlayerNotificationManager This saves a few lines of code and is nicer. We already did make sure to update the notification only once by posting because of b/145521438. #exofixit PiperOrigin-RevId: 343852256 --- .../ui/PlayerNotificationManager.java | 60 +++++++------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java index b183fddbb6..939980e231 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java @@ -15,6 +15,15 @@ */ package com.google.android.exoplayer2.ui; +import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED; +import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED; +import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED; +import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED; +import static com.google.android.exoplayer2.Player.EVENT_POSITION_DISCONTINUITY; +import static com.google.android.exoplayer2.Player.EVENT_REPEAT_MODE_CHANGED; +import static com.google.android.exoplayer2.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED; +import static com.google.android.exoplayer2.Player.EVENT_TIMELINE_CHANGED; + import android.app.Notification; import android.app.NotificationChannel; import android.app.PendingIntent; @@ -38,7 +47,6 @@ import androidx.media.app.NotificationCompat.MediaStyle; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.DefaultControlDispatcher; -import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackPreparer; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Timeline; @@ -1331,44 +1339,18 @@ public class PlayerNotificationManager { private class PlayerListener implements Player.EventListener { @Override - public void onPlaybackStateChanged(@Player.State int playbackState) { - postStartOrUpdateNotification(); - } - - @Override - public void onPlayWhenReadyChanged( - boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) { - postStartOrUpdateNotification(); - } - - @Override - public void onIsPlayingChanged(boolean isPlaying) { - postStartOrUpdateNotification(); - } - - @Override - public void onTimelineChanged(Timeline timeline, int reason) { - postStartOrUpdateNotification(); - } - - @Override - public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { - postStartOrUpdateNotification(); - } - - @Override - public void onPositionDiscontinuity(int reason) { - postStartOrUpdateNotification(); - } - - @Override - public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) { - postStartOrUpdateNotification(); - } - - @Override - public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) { - postStartOrUpdateNotification(); + public void onEvents(Player player, Player.Events events) { + if (events.containsAny( + EVENT_PLAYBACK_STATE_CHANGED, + EVENT_PLAY_WHEN_READY_CHANGED, + EVENT_IS_PLAYING_CHANGED, + EVENT_TIMELINE_CHANGED, + EVENT_PLAYBACK_PARAMETERS_CHANGED, + EVENT_POSITION_DISCONTINUITY, + EVENT_REPEAT_MODE_CHANGED, + EVENT_SHUFFLE_MODE_ENABLED_CHANGED)) { + postStartOrUpdateNotification(); + } } }