2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 21:27:40 +00:00

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

The changes allow loading the plugin on older Android versions
This commit is contained in:
Matthijs Tijink 2019-06-10 15:59:37 +00:00
parent 51e957d822
commit 0e9dd25172

View File

@ -34,6 +34,7 @@ import org.kde.kdeconnect.Helpers.AppsHelper;
import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.NetworkPacket;
import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver; import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver;
import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.Plugins.Plugin;
import org.kde.kdeconnect.Plugins.PluginFactory;
import org.kde.kdeconnect.UserInterface.AlertDialogFragment; import org.kde.kdeconnect.UserInterface.AlertDialogFragment;
import org.kde.kdeconnect.UserInterface.StartActivityAlertDialogFragment; import org.kde.kdeconnect.UserInterface.StartActivityAlertDialogFragment;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
@ -44,17 +45,16 @@ import java.util.List;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @PluginFactory.LoadablePlugin
//FIXME: Breaks on Android 4 because it extends OnActiveSessionsChangedListener @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
//@PluginFactory.LoadablePlugin public class MprisReceiverPlugin extends Plugin {
public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.OnActiveSessionsChangedListener {
private final static String PACKET_TYPE_MPRIS = "kdeconnect.mpris"; private final static String PACKET_TYPE_MPRIS = "kdeconnect.mpris";
private final static String PACKET_TYPE_MPRIS_REQUEST = "kdeconnect.mpris.request"; private final static String PACKET_TYPE_MPRIS_REQUEST = "kdeconnect.mpris.request";
private static final String TAG = "MprisReceiver"; private static final String TAG = "MprisReceiver";
private HashMap<String, MprisReceiverPlayer> players; private HashMap<String, MprisReceiverPlayer> players;
private MediaSessionChangeListener mediaSessionChangeListener;
@Override @Override
public boolean onCreate() { public boolean onCreate() {
@ -68,7 +68,9 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
if (null == manager) if (null == manager)
return false; 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))); createPlayers(manager.getActiveSessions(new ComponentName(context, NotificationReceiver.class)));
sendPlayerList(); sendPlayerList();
@ -83,8 +85,9 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
MediaSessionManager manager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); MediaSessionManager manager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
if (manager != null) { if (manager != null && mediaSessionChangeListener != null) {
manager.removeOnActiveSessionsChangedListener(MprisReceiverPlugin.this); manager.removeOnActiveSessionsChangedListener(mediaSessionChangeListener);
mediaSessionChangeListener = null;
} }
} }
@ -169,18 +172,20 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
return new String[]{PACKET_TYPE_MPRIS}; return new String[]{PACKET_TYPE_MPRIS};
} }
@Override private final class MediaSessionChangeListener implements MediaSessionManager.OnActiveSessionsChangedListener {
public void onActiveSessionsChanged(@Nullable List<MediaController> controllers) { @Override
public void onActiveSessionsChanged(@Nullable List<MediaController> controllers) {
if (null == controllers) {
return;
}
players.clear();
createPlayers(controllers);
sendPlayerList();
if (null == controllers) {
return;
} }
players.clear();
createPlayers(controllers);
sendPlayerList();
} }
private void createPlayer(MediaController controller) { private void createPlayer(MediaController controller) {