mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-31 14:15:14 +00:00
Add all missing MPRIS properties in the media session control
Also simplified some code.
This commit is contained in:
@@ -46,22 +46,11 @@ class MprisReceiverCallback extends MediaController.Callback {
|
|||||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||||
@Override
|
@Override
|
||||||
public void onPlaybackStateChanged(@NonNull PlaybackState state) {
|
public void onPlaybackStateChanged(@NonNull PlaybackState state) {
|
||||||
switch (state.getState()) {
|
plugin.sendMetadata(player);
|
||||||
case PlaybackState.STATE_PLAYING:
|
|
||||||
player.setPlaying(true);
|
|
||||||
plugin.sendPlaying(player);
|
|
||||||
break;
|
|
||||||
case PlaybackState.STATE_PAUSED:
|
|
||||||
player.setPaused(true);
|
|
||||||
plugin.sendPlaying(player);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMetadataChanged(@Nullable MediaMetadata metadata) {
|
public void onMetadataChanged(@Nullable MediaMetadata metadata) {
|
||||||
if (metadata == null)
|
|
||||||
return;
|
|
||||||
plugin.sendMetadata(player);
|
plugin.sendMetadata(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -34,36 +34,59 @@ class MprisReceiverPlayer {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private boolean isPlaying;
|
|
||||||
|
|
||||||
MprisReceiverPlayer(MediaController controller, String name) {
|
MprisReceiverPlayer(MediaController controller, String name) {
|
||||||
|
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
||||||
if (controller.getPlaybackState() != null) {
|
|
||||||
isPlaying = controller.getPlaybackState().getState() == PlaybackState.STATE_PLAYING;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isPlaying() {
|
boolean isPlaying() {
|
||||||
return isPlaying;
|
PlaybackState state = controller.getPlaybackState();
|
||||||
|
if (state == null) return false;
|
||||||
|
|
||||||
|
return state.getState() == PlaybackState.STATE_PLAYING;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPlaying(boolean playing) {
|
boolean canPlay() {
|
||||||
isPlaying = playing;
|
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() {
|
boolean canPause() {
|
||||||
return !isPlaying;
|
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) {
|
boolean canGoPrevious() {
|
||||||
isPlaying = !paused;
|
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() {
|
void playPause() {
|
||||||
if (isPlaying) {
|
if (isPlaying()) {
|
||||||
controller.getTransportControls().pause();
|
controller.getTransportControls().pause();
|
||||||
} else {
|
} else {
|
||||||
controller.getTransportControls().play();
|
controller.getTransportControls().play();
|
||||||
@@ -75,24 +98,31 @@ class MprisReceiverPlayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String getAlbum() {
|
String getAlbum() {
|
||||||
if (controller.getMetadata() == null)
|
MediaMetadata metadata = controller.getMetadata();
|
||||||
return "";
|
if (metadata == null) return "";
|
||||||
String album = controller.getMetadata().getString(MediaMetadata.METADATA_KEY_ALBUM);
|
|
||||||
|
String album = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM);
|
||||||
return album != null ? album : "";
|
return album != null ? album : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
String getArtist() {
|
String getArtist() {
|
||||||
if (controller.getMetadata() == null)
|
MediaMetadata metadata = controller.getMetadata();
|
||||||
return "";
|
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 : "";
|
return artist != null ? artist : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
String getTitle() {
|
String getTitle() {
|
||||||
if (controller.getMetadata() == null)
|
MediaMetadata metadata = controller.getMetadata();
|
||||||
return "";
|
if (metadata == null) return "";
|
||||||
String title = controller.getMetadata().getString(MediaMetadata.METADATA_KEY_TITLE);
|
|
||||||
|
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 : "";
|
return title != null ? title : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +134,18 @@ class MprisReceiverPlayer {
|
|||||||
controller.getTransportControls().skipToNext();
|
controller.getTransportControls().skipToNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void play() {
|
||||||
|
controller.getTransportControls().play();
|
||||||
|
}
|
||||||
|
|
||||||
|
void pause() {
|
||||||
|
controller.getTransportControls().pause();
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
controller.getTransportControls().stop();
|
||||||
|
}
|
||||||
|
|
||||||
int getVolume() {
|
int getVolume() {
|
||||||
if (controller.getPlaybackInfo() == null)
|
if (controller.getPlaybackInfo() == null)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -115,4 +157,15 @@ class MprisReceiverPlayer {
|
|||||||
return 0;
|
return 0;
|
||||||
return controller.getPlaybackState().getPosition();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -117,10 +117,21 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (np.has("SetPosition")) {
|
||||||
|
long position = np.getLong("SetPosition", 0);
|
||||||
|
player.setPosition(position);
|
||||||
|
}
|
||||||
|
|
||||||
if (np.has("action")) {
|
if (np.has("action")) {
|
||||||
String action = np.getString("action");
|
String action = np.getString("action");
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
case "Play":
|
||||||
|
player.play();
|
||||||
|
break;
|
||||||
|
case "Pause":
|
||||||
|
player.pause();
|
||||||
|
break;
|
||||||
case "PlayPause":
|
case "PlayPause":
|
||||||
player.playPause();
|
player.playPause();
|
||||||
break;
|
break;
|
||||||
@@ -129,6 +140,10 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
|
|||||||
break;
|
break;
|
||||||
case "Previous":
|
case "Previous":
|
||||||
player.previous();
|
player.previous();
|
||||||
|
break;
|
||||||
|
case "Stop":
|
||||||
|
player.stop();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,20 +189,12 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
|
|||||||
device.sendPacket(np);
|
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
|
@Override
|
||||||
public int getMinSdk() {
|
public int getMinSdk() {
|
||||||
return Build.VERSION_CODES.LOLLIPOP_MR1;
|
return Build.VERSION_CODES.LOLLIPOP_MR1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMetadata(MprisReceiverPlayer player) {
|
void sendMetadata(MprisReceiverPlayer player) {
|
||||||
NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS);
|
NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS);
|
||||||
np.set("player", player.getName());
|
np.set("player", player.getName());
|
||||||
if (player.getArtist().isEmpty()) {
|
if (player.getArtist().isEmpty()) {
|
||||||
@@ -200,12 +207,12 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
|
|||||||
np.set("album", player.getAlbum());
|
np.set("album", player.getAlbum());
|
||||||
np.set("isPlaying", player.isPlaying());
|
np.set("isPlaying", player.isPlaying());
|
||||||
np.set("pos", player.getPosition());
|
np.set("pos", player.getPosition());
|
||||||
device.sendPacket(np);
|
np.set("length", player.getLength());
|
||||||
}
|
np.set("canPlay", player.canPlay());
|
||||||
|
np.set("canPause", player.canPause());
|
||||||
public void sendVolume(MprisReceiverPlayer player) {
|
np.set("canGoPrevious", player.canGoPrevious());
|
||||||
NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS);
|
np.set("canGoNext", player.canGoNext());
|
||||||
np.set("player", player.getName());
|
np.set("canSeek", player.canSeek());
|
||||||
np.set("volume", player.getVolume());
|
np.set("volume", player.getVolume());
|
||||||
device.sendPacket(np);
|
device.sendPacket(np);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user