2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-09-03 07:35:08 +00:00

Close the MPRIS media notification when the player disappears

The code now checks if the player still exists.
This commit is contained in:
Matthijs Tijink
2019-06-14 23:18:55 +02:00
parent 906c04ac1a
commit f688aad3e1
2 changed files with 26 additions and 18 deletions

View File

@@ -191,46 +191,44 @@ public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceCh
} }
private Pair<Device, MprisPlugin.MprisPlayer> findPlayer(BackgroundService service) { private Pair<Device, MprisPlugin.MprisPlayer> findPlayer(BackgroundService service) {
//First try the previously displayed player (if still playing) //First try the previously displayed player (if still playing) or the previous displayed device (otherwise)
if (notificationDevice != null && mprisDevices.contains(notificationDevice)) { if (notificationDevice != null && mprisDevices.contains(notificationDevice)) {
Device device = service.getDevice(notificationDevice); Device device = service.getDevice(notificationDevice);
if (device != null && device.isPluginEnabled("MprisPlugin")) { MprisPlugin.MprisPlayer player;
if (shouldShowPlayer(notificationPlayer) && notificationPlayer.isPlaying()) { if (notificationPlayer.isPlaying()) {
return new Pair<>(device, notificationPlayer); player = getPlayerFromDevice(device, notificationPlayer);
} } else {
player = getPlayerFromDevice(device, null);
// Try a different player for the same device }
MprisPlugin.MprisPlayer player = getPlayerFromDevice(device); if (player != null) {
if (player != null) { return new Pair<>(device, player);
return new Pair<>(device, player);
}
} }
} }
// Try a different player from another device // Try a different player from another device
for (Device otherDevice : service.getDevices().values()) { for (Device otherDevice : service.getDevices().values()) {
MprisPlugin.MprisPlayer player = getPlayerFromDevice(otherDevice); MprisPlugin.MprisPlayer player = getPlayerFromDevice(otherDevice, null);
if (player != null) { if (player != null) {
return new Pair<>(otherDevice, player); return new Pair<>(otherDevice, player);
} }
} }
//So no player is playing. Try the previously displayed player again //So no player is playing. Try the previously displayed player again
// This will succeed if it's paused:
// that allows pausing and subsequently resuming via the notification
if (notificationDevice != null && mprisDevices.contains(notificationDevice)) { if (notificationDevice != null && mprisDevices.contains(notificationDevice)) {
Device device = service.getDevice(notificationDevice); Device device = service.getDevice(notificationDevice);
if (device != null && device.isPluginEnabled("MprisPlugin")) { MprisPlugin.MprisPlayer player = getPlayerFromDevice(device, notificationPlayer);
if (shouldShowPlayer(notificationPlayer)) { if (player != null) {
return new Pair<>(device, notificationPlayer); return new Pair<>(device, player);
}
} }
} }
return new Pair<>(null, null); return new Pair<>(null, null);
} }
private MprisPlugin.MprisPlayer getPlayerFromDevice(Device device) { private MprisPlugin.MprisPlayer getPlayerFromDevice(Device device, MprisPlugin.MprisPlayer preferredPlayer) {
if (!mprisDevices.contains(device.getDeviceId())) if (!mprisDevices.contains(device.getDeviceId()))
return null; return null;
@@ -240,6 +238,12 @@ public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceCh
return null; return null;
} }
//First try the preferred player, if supplied
if (plugin.hasPlayer(preferredPlayer) && shouldShowPlayer(preferredPlayer)) {
return preferredPlayer;
}
//Otherwise, accept any playing player
MprisPlugin.MprisPlayer player = plugin.getPlayingPlayer(); MprisPlugin.MprisPlayer player = plugin.getPlayingPlayer();
if (shouldShowPlayer(player)) { if (shouldShowPlayer(player)) {
return player; return player;

View File

@@ -422,6 +422,10 @@ public class MprisPlugin extends Plugin {
return null; return null;
} }
boolean hasPlayer(MprisPlayer player) {
return players.containsValue(player);
}
private void requestPlayerList() { private void requestPlayerList() {
NetworkPacket np = new NetworkPacket(PACKET_TYPE_MPRIS_REQUEST); NetworkPacket np = new NetworkPacket(PACKET_TYPE_MPRIS_REQUEST);
np.set("requestPlayerList", true); np.set("requestPlayerList", true);