From 3e85dd6160553d3d5b513da2895f716a1c4725e3 Mon Sep 17 00:00:00 2001 From: Matthijs Tijink Date: Sat, 9 Mar 2019 20:06:32 +0000 Subject: [PATCH] Add all missing MPRIS properties in the media session control Also simplified some code. --- .../MprisReceiverCallback.java | 13 +-- .../MprisReceiverPlayer.java | 101 +++++++++++++----- .../MprisReceiverPlugin.java | 37 ++++--- 3 files changed, 100 insertions(+), 51 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java index cf701cc3..97a4f615 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java @@ -46,22 +46,11 @@ class MprisReceiverCallback extends MediaController.Callback { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onPlaybackStateChanged(@NonNull PlaybackState state) { - switch (state.getState()) { - case PlaybackState.STATE_PLAYING: - player.setPlaying(true); - plugin.sendPlaying(player); - break; - case PlaybackState.STATE_PAUSED: - player.setPaused(true); - plugin.sendPlaying(player); - break; - } + plugin.sendMetadata(player); } @Override public void onMetadataChanged(@Nullable MediaMetadata metadata) { - if (metadata == null) - return; plugin.sendMetadata(player); } diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java index e0431601..a138e860 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java @@ -34,36 +34,59 @@ class MprisReceiverPlayer { private final String name; - private boolean isPlaying; - MprisReceiverPlayer(MediaController controller, String name) { - this.controller = controller; this.name = name; - - if (controller.getPlaybackState() != null) { - isPlaying = controller.getPlaybackState().getState() == PlaybackState.STATE_PLAYING; - } } boolean isPlaying() { - return isPlaying; + PlaybackState state = controller.getPlaybackState(); + if (state == null) return false; + + return state.getState() == PlaybackState.STATE_PLAYING; } - void setPlaying(boolean playing) { - isPlaying = playing; + boolean canPlay() { + PlaybackState state = controller.getPlaybackState(); + if (state == null) return false; + + if (state.getState() == PlaybackState.STATE_PLAYING) return true; + + return (state.getActions() & (PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE)) != 0; } - boolean isPaused() { - return !isPlaying; + boolean canPause() { + PlaybackState state = controller.getPlaybackState(); + if (state == null) return false; + + if (state.getState() == PlaybackState.STATE_PAUSED) return true; + + return (state.getActions() & (PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE)) != 0; } - void setPaused(boolean paused) { - isPlaying = !paused; + boolean canGoPrevious() { + PlaybackState state = controller.getPlaybackState(); + if (state == null) return false; + + return (state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0; + } + + boolean canGoNext() { + PlaybackState state = controller.getPlaybackState(); + if (state == null) return false; + + return (state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0; + } + + boolean canSeek() { + PlaybackState state = controller.getPlaybackState(); + if (state == null) return false; + + return (state.getActions() & PlaybackState.ACTION_SEEK_TO) != 0; } void playPause() { - if (isPlaying) { + if (isPlaying()) { controller.getTransportControls().pause(); } else { controller.getTransportControls().play(); @@ -75,24 +98,31 @@ class MprisReceiverPlayer { } String getAlbum() { - if (controller.getMetadata() == null) - return ""; - String album = controller.getMetadata().getString(MediaMetadata.METADATA_KEY_ALBUM); + MediaMetadata metadata = controller.getMetadata(); + if (metadata == null) return ""; + + String album = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM); return album != null ? album : ""; } String getArtist() { - if (controller.getMetadata() == null) - return ""; + MediaMetadata metadata = controller.getMetadata(); + if (metadata == null) return ""; + + String artist = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST); + if (artist == null || artist.isEmpty()) artist = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST); + if (artist == null || artist.isEmpty()) artist = metadata.getString(MediaMetadata.METADATA_KEY_AUTHOR); + if (artist == null || artist.isEmpty()) artist = metadata.getString(MediaMetadata.METADATA_KEY_WRITER); - String artist = controller.getMetadata().getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST); return artist != null ? artist : ""; } String getTitle() { - if (controller.getMetadata() == null) - return ""; - String title = controller.getMetadata().getString(MediaMetadata.METADATA_KEY_TITLE); + MediaMetadata metadata = controller.getMetadata(); + if (metadata == null) return ""; + + String title = metadata.getString(MediaMetadata.METADATA_KEY_TITLE); + if (title == null || title.isEmpty()) title = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE); return title != null ? title : ""; } @@ -104,6 +134,18 @@ class MprisReceiverPlayer { controller.getTransportControls().skipToNext(); } + void play() { + controller.getTransportControls().play(); + } + + void pause() { + controller.getTransportControls().pause(); + } + + void stop() { + controller.getTransportControls().stop(); + } + int getVolume() { if (controller.getPlaybackInfo() == null) return 0; @@ -115,4 +157,15 @@ class MprisReceiverPlayer { return 0; return controller.getPlaybackState().getPosition(); } + + void setPosition(long position) { + controller.getTransportControls().seekTo(position); + } + + long getLength() { + MediaMetadata metadata = controller.getMetadata(); + if (metadata == null) return 0; + + return metadata.getLong(MediaMetadata.METADATA_KEY_DURATION); + } } diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java index bddd150b..5f3ddff5 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java @@ -117,10 +117,21 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O return true; } + if (np.has("SetPosition")) { + long position = np.getLong("SetPosition", 0); + player.setPosition(position); + } + if (np.has("action")) { String action = np.getString("action"); switch (action) { + case "Play": + player.play(); + break; + case "Pause": + player.pause(); + break; case "PlayPause": player.playPause(); break; @@ -129,6 +140,10 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O break; case "Previous": player.previous(); + break; + case "Stop": + player.stop(); + break; } } @@ -174,20 +189,12 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O device.sendPacket(np); } - void sendPlaying(MprisReceiverPlayer player) { - - NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS); - np.set("player", player.getName()); - np.set("isPlaying", player.isPlaying()); - device.sendPacket(np); - } - @Override public int getMinSdk() { return Build.VERSION_CODES.LOLLIPOP_MR1; } - public void sendMetadata(MprisReceiverPlayer player) { + void sendMetadata(MprisReceiverPlayer player) { NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS); np.set("player", player.getName()); if (player.getArtist().isEmpty()) { @@ -200,12 +207,12 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O np.set("album", player.getAlbum()); np.set("isPlaying", player.isPlaying()); np.set("pos", player.getPosition()); - device.sendPacket(np); - } - - public void sendVolume(MprisReceiverPlayer player) { - NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS); - np.set("player", player.getName()); + np.set("length", player.getLength()); + np.set("canPlay", player.canPlay()); + np.set("canPause", player.canPause()); + np.set("canGoPrevious", player.canGoPrevious()); + np.set("canGoNext", player.canGoNext()); + np.set("canSeek", player.canSeek()); np.set("volume", player.getVolume()); device.sendPacket(np); }