From 740116356c652480c4b5750cf690fa6830edc1d1 Mon Sep 17 00:00:00 2001 From: John Zhen Mo Date: Mon, 28 May 2018 20:02:02 -0700 Subject: [PATCH] -Fixed media session activation. -Removed redundant setShuffle call in media session callback and its user. -Removed unused dummy playback preparer. --- .../org/schabi/newpipe/player/BasePlayer.java | 1 + .../player/helper/MediaSessionManager.java | 12 ++++- .../mediasession/DummyPlaybackPreparer.java | 45 ------------------- .../mediasession/MediaSessionCallback.java | 1 - .../PlayQueuePlaybackController.java | 8 ---- .../playback/BasePlayerMediaSession.java | 5 --- 6 files changed, 11 insertions(+), 61 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/player/mediasession/DummyPlaybackPreparer.java diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index 94e0654e2..4c3d70421 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -271,6 +271,7 @@ public abstract class BasePlayer implements if (audioReactor != null) audioReactor.dispose(); if (playbackManager != null) playbackManager.dispose(); if (databaseUpdateReactor != null) databaseUpdateReactor.dispose(); + if (mediaSessionManager != null) mediaSessionManager.dispose(); if (playQueueAdapter != null) { playQueueAdapter.unsetSelectedListener(); diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java index 2611705a8..b174ed3ed 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java @@ -11,7 +11,6 @@ import android.view.KeyEvent; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; -import org.schabi.newpipe.player.mediasession.DummyPlaybackPreparer; import org.schabi.newpipe.player.mediasession.MediaSessionCallback; import org.schabi.newpipe.player.mediasession.PlayQueueNavigator; import org.schabi.newpipe.player.mediasession.PlayQueuePlaybackController; @@ -26,10 +25,12 @@ public class MediaSessionManager { @NonNull final Player player, @NonNull final MediaSessionCallback callback) { this.mediaSession = new MediaSessionCompat(context, TAG); + this.mediaSession.setActive(true); + this.sessionConnector = new MediaSessionConnector(mediaSession, new PlayQueuePlaybackController(callback)); this.sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback)); - this.sessionConnector.setPlayer(player, new DummyPlaybackPreparer()); + this.sessionConnector.setPlayer(player, null); } @Nullable @@ -37,4 +38,11 @@ public class MediaSessionManager { public KeyEvent handleMediaButtonIntent(final Intent intent) { return MediaButtonReceiver.handleIntent(mediaSession, intent); } + + public void dispose() { + this.sessionConnector.setPlayer(null, null); + this.sessionConnector.setQueueNavigator(null); + this.mediaSession.setActive(false); + this.mediaSession.release(); + } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/DummyPlaybackPreparer.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/DummyPlaybackPreparer.java deleted file mode 100644 index 431a90d8a..000000000 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/DummyPlaybackPreparer.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.schabi.newpipe.player.mediasession; - -import android.net.Uri; -import android.os.Bundle; -import android.os.ResultReceiver; - -import com.google.android.exoplayer2.Player; -import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; - -public class DummyPlaybackPreparer implements MediaSessionConnector.PlaybackPreparer { - @Override - public long getSupportedPrepareActions() { - return 0; - } - - @Override - public void onPrepare() { - - } - - @Override - public void onPrepareFromMediaId(String mediaId, Bundle extras) { - - } - - @Override - public void onPrepareFromSearch(String query, Bundle extras) { - - } - - @Override - public void onPrepareFromUri(Uri uri, Bundle extras) { - - } - - @Override - public String[] getCommands() { - return new String[0]; - } - - @Override - public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) { - - } -} diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionCallback.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionCallback.java index a1a57a87d..498fb4a88 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionCallback.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionCallback.java @@ -13,5 +13,4 @@ public interface MediaSessionCallback { void onPlay(); void onPause(); - void onSetShuffle(final boolean isShuffled); } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java index 2aa41bd63..a460a1653 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java @@ -1,7 +1,5 @@ package org.schabi.newpipe.player.mediasession; -import android.support.v4.media.session.PlaybackStateCompat; - import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.ext.mediasession.DefaultPlaybackController; @@ -22,10 +20,4 @@ public class PlayQueuePlaybackController extends DefaultPlaybackController { public void onPause(Player player) { callback.onPause(); } - - @Override - public void onSetShuffleMode(Player player, int shuffleMode) { - callback.onSetShuffle(shuffleMode == PlaybackStateCompat.SHUFFLE_MODE_ALL - || shuffleMode == PlaybackStateCompat.SHUFFLE_MODE_GROUP); - } } diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java b/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java index 6cce4a764..3365828d1 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java @@ -69,9 +69,4 @@ public class BasePlayerMediaSession implements MediaSessionCallback { public void onPause() { player.onPause(); } - - @Override - public void onSetShuffle(boolean isShuffled) { - player.onShuffleModeEnabledChanged(isShuffled); - } }