Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 0e9dd251 authored by Matthijs Tijink's avatar Matthijs Tijink

Enable the MPRIS server plugin - allows control of android media players

The changes allow loading the plugin on older Android versions
parent 51e957d8
......@@ -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<String, MprisReceiverPlayer> 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<MediaController> controllers) {
private final class MediaSessionChangeListener implements MediaSessionManager.OnActiveSessionsChangedListener {
@Override
public void onActiveSessionsChanged(@Nullable List<MediaController> controllers) {
if (null == controllers) {
return;
}
if (null == controllers) {
return;
}
players.clear();
players.clear();
createPlayers(controllers);
sendPlayerList();
createPlayers(controllers);
sendPlayerList();
}
}
private void createPlayer(MediaController controller) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment