diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java new file mode 100644 --- /dev/null +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverCallback.java @@ -0,0 +1,77 @@ +/* + * Copyright 2018 Nicolas Fella + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kde.kdeconnect.Plugins.MprisReceiverPlugin; + +import android.media.MediaMetadata; +import android.media.session.MediaController; +import android.media.session.PlaybackState; +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; +import android.util.Log; + + +@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) +class MprisReceiverCallback extends MediaController.Callback { + + private static final String TAG = "MprisReceiver"; + + private final MprisReceiverPlayer player; + private MprisReceiverPlugin plugin; + + MprisReceiverCallback(MprisReceiverPlugin plugin, MprisReceiverPlayer player) { + this.player = player; + this.plugin = plugin; + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + @Override + public void onPlaybackStateChanged(@NonNull PlaybackState state) { + switch (state.getState()) { + case PlaybackState.STATE_PLAYING: + Log.d(TAG, "Playing"); + player.setPlaying(true); + plugin.sendPlaying(player); + break; + case PlaybackState.STATE_PAUSED: + player.setPaused(true); + plugin.sendPlaying(player); + Log.d(TAG, "Paused"); + break; + } + } + + @Override + public void onMetadataChanged(@Nullable MediaMetadata metadata) { + if (metadata == null) + return; + Log.d(TAG, "TITLE " + metadata.getString(MediaMetadata.METADATA_KEY_TITLE)); + Log.d(TAG, "ALBUM " + metadata.getString(MediaMetadata.METADATA_KEY_ALBUM)); + Log.d(TAG, "ALBUM ARTIST " + metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST)); + Log.d(TAG, "ART URI " + metadata.getString(MediaMetadata.METADATA_KEY_ART_URI)); + Log.d(TAG, "DISPLAY TITLE " + metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE)); + Log.d(TAG, "MEDIA ID " + metadata.getString(MediaMetadata.METADATA_KEY_MEDIA_ID)); + Log.d(TAG, "WRITER " + metadata.getString(MediaMetadata.METADATA_KEY_WRITER)); + plugin.sendMetadata(player); + } + +} diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java new file mode 100644 --- /dev/null +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlayer.java @@ -0,0 +1,97 @@ +/* + * Copyright 2018 Nicolas Fella + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kde.kdeconnect.Plugins.MprisReceiverPlugin; + +import android.media.MediaMetadata; +import android.media.session.MediaController; +import android.media.session.PlaybackState; +import android.os.Build; +import android.support.annotation.RequiresApi; + +@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) +class MprisReceiverPlayer { + + private MediaController controller; + + private String id; + + private boolean isPlaying; + + MprisReceiverPlayer(MediaController controller) { + + this.controller = controller; + this.id = controller.getPackageName(); + + isPlaying = controller.getPlaybackState().getState() == PlaybackState.STATE_PLAYING; + } + + boolean isPlaying() { + return isPlaying; + } + + public void setPlaying(boolean playing) { + isPlaying = playing; + } + + public boolean isPaused() { + return !isPlaying; + } + + public void setPaused(boolean paused) { + isPlaying = !paused; + } + + void playPause() { + if (isPlaying) { + controller.getTransportControls().pause(); + } else { + controller.getTransportControls().play(); + } + } + + public String getId() { + return id; + } + + public String getAlbum() { + return controller.getMetadata().getString(MediaMetadata.METADATA_KEY_ALBUM); + } + + public String getArtist() { + return controller.getMetadata().getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST); + } + + public String getTitle() { + return controller.getMetadata().getString(MediaMetadata.METADATA_KEY_TITLE); + } + + public void previous() { + controller.getTransportControls().skipToPrevious(); + } + + public void next() { + controller.getTransportControls().skipToNext(); + } + + int getVolume() { + return (int) (((float) controller.getPlaybackInfo().getCurrentVolume()) / ((float) controller.getPlaybackInfo().getMaxVolume())) * 100; + } +} diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java new file mode 100644 --- /dev/null +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java @@ -0,0 +1,202 @@ +/* + * Copyright 2018 Nicolas Fella + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kde.kdeconnect.Plugins.MprisReceiverPlugin; + +import android.content.ComponentName; +import android.content.Context; +import android.media.session.MediaController; +import android.media.session.MediaSessionManager; +import android.media.session.PlaybackState; +import android.nfc.Tag; +import android.os.Build; +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; +import android.util.Log; + +import org.kde.kdeconnect.NetworkPacket; +import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver; +import org.kde.kdeconnect.Plugins.Plugin; +import org.kde.kdeconnect_tp.R; + +import java.util.HashMap; +import java.util.List; + +@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) +public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.OnActiveSessionsChangedListener { + + private final static String PACKET_TYPE_MPRIS = "kdeconnect.mpris"; + private final static String PACKET_TYPE_MPRIS_REQUEST = "kdeconnect.mpris.request"; + + private static final String TAG = "MprisReceiver"; + + private HashMap players; + + @Override + public boolean onCreate() { + players = new HashMap<>(); + try { + MediaSessionManager manager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); + if (null == manager) + return false; + + manager.addOnActiveSessionsChangedListener(MprisReceiverPlugin.this, new ComponentName(context, NotificationReceiver.class), new Handler(Looper.getMainLooper())); + + createPlayers(manager.getActiveSessions(new ComponentName(context, NotificationReceiver.class))); + sendPlayerList(); + } catch (Exception e) { + Log.e(TAG, "Exception", e); + } + + return true; + } + + private void createPlayers(List sessions) { + for (MediaController controller : sessions) { + if (!players.containsKey(controller.getPackageName())) { + createPlayer(controller); + } + } + } + + @Override + public String getDisplayName() { + return context.getResources().getString(R.string.pref_plugin_mousepad); + } + + @Override + public String getDescription() { + return context.getResources().getString(R.string.pref_plugin_mousepad); + } + + @Override + public boolean onPacketReceived(NetworkPacket np) { + + if (np.getBoolean("requestPlayerList")) { + sendPlayerList(); + return true; + } + + if (!np.has("player")) { + return false; + } + Log.d(TAG, np.getString("player")); + MprisReceiverPlayer player = players.get(np.getString("player")); + + if (null == player) { + return false; + } + + + if (np.getBoolean("requestNowPlaying", false)) { + Log.d(TAG, "Sending status for player " + player.getId()); + sendMetadata(player); + return true; + } + + if (np.has("action")) { + String action = np.getString("action"); + + switch (action) { + case "PlayPause": + player.playPause(); + break; + case "Next": + player.next(); + break; + case "Previous": + player.previous(); + } + } + + return true; + } + + @Override + public String[] getSupportedPacketTypes() { + return new String[]{PACKET_TYPE_MPRIS_REQUEST}; + } + + @Override + public String[] getOutgoingPacketTypes() { + return new String[]{PACKET_TYPE_MPRIS}; + } + + @Override + public void onActiveSessionsChanged(@Nullable List controllers) { + + if (null == controllers) { + Log.d(TAG, "Empty"); + return; + } + createPlayers(controllers); + sendPlayerList(); + + } + + private void createPlayer(MediaController controller) { + Log.d(TAG, "Found new Mediacontroller " + controller.getPackageName()); + MprisReceiverPlayer player = new MprisReceiverPlayer(controller); + controller.registerCallback(new MprisReceiverCallback(this, player), new Handler(Looper.getMainLooper())); + players.put(controller.getPackageName(), player); + } + + private void sendPlayerList() { + Log.d(TAG, "Sending player list"); + NetworkPacket np = new NetworkPacket(PACKET_TYPE_MPRIS); + np.set("playerList", players.keySet()); + device.sendPacket(np); + } + + void sendPlaying(MprisReceiverPlayer player) { + + NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS); + np.set("player", player.getId()); + np.set("isPlaying", player.isPlaying()); + device.sendPacket(np); + } + + @Override + public int getMinSdk() { + return Build.VERSION_CODES.LOLLIPOP_MR1; + } + + public void sendMetadata(MprisReceiverPlayer player) { + Log.d(TAG, "Sending metadata"); + NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS); + np.set("player", player.getId()); + np.set("nowPlaying", player.getTitle()); + np.set("title", player.getTitle()); + np.set("artist", player.getArtist()); + np.set("album", player.getAlbum()); + np.set("isPlaying", player.isPlaying()); + device.sendPacket(np); + } + + public void sendVolume(MprisReceiverPlayer player) { + NetworkPacket np = new NetworkPacket(MprisReceiverPlugin.PACKET_TYPE_MPRIS); + np.set("player", player.getId()); + np.set("volume", player.getVolume()); + device.sendPacket(np); + } +} diff --git a/src/org/kde/kdeconnect/Plugins/PluginFactory.java b/src/org/kde/kdeconnect/Plugins/PluginFactory.java --- a/src/org/kde/kdeconnect/Plugins/PluginFactory.java +++ b/src/org/kde/kdeconnect/Plugins/PluginFactory.java @@ -30,6 +30,7 @@ import org.kde.kdeconnect.Plugins.FindMyPhonePlugin.FindMyPhonePlugin; import org.kde.kdeconnect.Plugins.MousePadPlugin.MousePadPlugin; import org.kde.kdeconnect.Plugins.MprisPlugin.MprisPlugin; +import org.kde.kdeconnect.Plugins.MprisReceiverPlugin.MprisReceiverPlugin; import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationsPlugin; import org.kde.kdeconnect.Plugins.PingPlugin.PingPlugin; import org.kde.kdeconnect.Plugins.ReceiveNotificationsPlugin.ReceiveNotificationsPlugin; @@ -128,6 +129,7 @@ PluginFactory.registerPlugin(FindMyPhonePlugin.class); PluginFactory.registerPlugin(RunCommandPlugin.class); PluginFactory.registerPlugin(RemoteKeyboardPlugin.class); + PluginFactory.registerPlugin(MprisReceiverPlugin.class); } public static PluginInfo getPluginInfo(Context context, String pluginKey) {