From 0e9dd25172e182b1b589889ebe7f67ecbf1b3d7f Mon Sep 17 00:00:00 2001 From: Matthijs Tijink Date: Mon, 10 Jun 2019 15:59:37 +0000 Subject: [PATCH] Enable the MPRIS server plugin - allows control of android media players The changes allow loading the plugin on older Android versions --- .../MprisReceiverPlugin.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java index 2dc1c8a9..92107971 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java @@ -34,6 +34,7 @@ import org.kde.kdeconnect.Helpers.AppsHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver; import org.kde.kdeconnect.Plugins.Plugin; +import org.kde.kdeconnect.Plugins.PluginFactory; import org.kde.kdeconnect.UserInterface.AlertDialogFragment; import org.kde.kdeconnect.UserInterface.StartActivityAlertDialogFragment; import org.kde.kdeconnect_tp.R; @@ -44,17 +45,16 @@ import java.util.List; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) -//FIXME: Breaks on Android 4 because it extends OnActiveSessionsChangedListener -//@PluginFactory.LoadablePlugin -public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.OnActiveSessionsChangedListener { - +@PluginFactory.LoadablePlugin +@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) +public class MprisReceiverPlugin extends Plugin { 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; + private MediaSessionChangeListener mediaSessionChangeListener; @Override public boolean onCreate() { @@ -68,7 +68,9 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O if (null == manager) return false; - manager.addOnActiveSessionsChangedListener(MprisReceiverPlugin.this, new ComponentName(context, NotificationReceiver.class), new Handler(Looper.getMainLooper())); + assert(mediaSessionChangeListener == null); + mediaSessionChangeListener = new MediaSessionChangeListener(); + manager.addOnActiveSessionsChangedListener(mediaSessionChangeListener, new ComponentName(context, NotificationReceiver.class), new Handler(Looper.getMainLooper())); createPlayers(manager.getActiveSessions(new ComponentName(context, NotificationReceiver.class))); sendPlayerList(); @@ -83,8 +85,9 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O public void onDestroy() { super.onDestroy(); MediaSessionManager manager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); - if (manager != null) { - manager.removeOnActiveSessionsChangedListener(MprisReceiverPlugin.this); + if (manager != null && mediaSessionChangeListener != null) { + manager.removeOnActiveSessionsChangedListener(mediaSessionChangeListener); + mediaSessionChangeListener = null; } } @@ -169,18 +172,20 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O return new String[]{PACKET_TYPE_MPRIS}; } - @Override - public void onActiveSessionsChanged(@Nullable List controllers) { + private final class MediaSessionChangeListener implements MediaSessionManager.OnActiveSessionsChangedListener { + @Override + public void onActiveSessionsChanged(@Nullable List controllers) { + + if (null == controllers) { + return; + } + + players.clear(); + + createPlayers(controllers); + sendPlayerList(); - if (null == controllers) { - return; } - - players.clear(); - - createPlayers(controllers); - sendPlayerList(); - } private void createPlayer(MediaController controller) {