Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java
Show All 36 Lines | |||||
37 | import org.kde.kdeconnect.BackgroundService; | 37 | import org.kde.kdeconnect.BackgroundService; | ||
38 | import org.kde.kdeconnect.Device; | 38 | import org.kde.kdeconnect.Device; | ||
39 | import org.kde.kdeconnect_tp.R; | 39 | import org.kde.kdeconnect_tp.R; | ||
40 | 40 | | |||
41 | import java.util.HashSet; | 41 | import java.util.HashSet; | ||
42 | 42 | | |||
43 | /** | 43 | /** | ||
44 | * Controls the mpris media control notification | 44 | * Controls the mpris media control notification | ||
45 | * | 45 | * <p> | ||
46 | * There are two parts to this: | 46 | * There are two parts to this: | ||
47 | * - The notification (with buttons etc.) | 47 | * - The notification (with buttons etc.) | ||
48 | * - The media session (via MediaSessionCompat; for lock screen control on | 48 | * - The media session (via MediaSessionCompat; for lock screen control on | ||
49 | * older Android version. And in the future for lock screen album covers) | 49 | * older Android version. And in the future for lock screen album covers) | ||
50 | */ | 50 | */ | ||
51 | public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceChangeListener { | 51 | public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceChangeListener { | ||
52 | public final static int MPRIS_MEDIA_NOTIFICATION_ID = 0x91b70463; // echo MprisNotification | md5sum | head -c 8 | 52 | public final static int MPRIS_MEDIA_NOTIFICATION_ID = 0x91b70463; // echo MprisNotification | md5sum | head -c 8 | ||
53 | public final static String MPRIS_MEDIA_SESSION_TAG = "org.kde.kdeconnect_tp.media_session"; | 53 | public final static String MPRIS_MEDIA_SESSION_TAG = "org.kde.kdeconnect_tp.media_session"; | ||
54 | 54 | | |||
55 | private static MprisMediaSession instance = new MprisMediaSession(); | 55 | private static MprisMediaSession instance = new MprisMediaSession(); | ||
56 | | ||||
56 | public static MprisMediaSession getInstance() { | 57 | public static MprisMediaSession getInstance() { | ||
57 | return instance; | 58 | return instance; | ||
58 | } | 59 | } | ||
60 | | ||||
59 | public static MediaSessionCompat getMediaSession() { | 61 | public static MediaSessionCompat getMediaSession() { | ||
60 | return instance.mediaSession; | 62 | return instance.mediaSession; | ||
61 | } | 63 | } | ||
62 | 64 | | |||
63 | //Holds the device and player displayed in the notification | 65 | //Holds the device and player displayed in the notification | ||
64 | private String notificationDevice = null; | 66 | private String notificationDevice = null; | ||
65 | private MprisPlugin.MprisPlayer notificationPlayer = null; | 67 | private MprisPlugin.MprisPlayer notificationPlayer = null; | ||
66 | //Holds the device ids for which we can display a notification | 68 | //Holds the device ids for which we can display a notification | ||
Show All 34 Lines | 82 | private MediaSessionCompat.Callback mediaSessionCallback = new MediaSessionCompat.Callback() { | |||
101 | @Override | 103 | @Override | ||
102 | public void onStop() { | 104 | public void onStop() { | ||
103 | notificationPlayer.stop(); | 105 | notificationPlayer.stop(); | ||
104 | } | 106 | } | ||
105 | }; | 107 | }; | ||
106 | 108 | | |||
107 | /** | 109 | /** | ||
108 | * Called by the mpris plugin when it wants media control notifications for its device | 110 | * Called by the mpris plugin when it wants media control notifications for its device | ||
109 | * | 111 | * <p> | ||
110 | * Can be called multiple times, once for each device | 112 | * Can be called multiple times, once for each device | ||
113 | * | ||||
111 | * @param _context The context | 114 | * @param _context The context | ||
112 | * @param mpris The mpris plugin | 115 | * @param mpris The mpris plugin | ||
113 | * @param device The device id | 116 | * @param device The device id | ||
114 | */ | 117 | */ | ||
115 | public void onCreate(Context _context, MprisPlugin mpris, String device) { | 118 | public void onCreate(Context _context, MprisPlugin mpris, String device) { | ||
116 | if (mprisDevices.isEmpty()) { | 119 | if (mprisDevices.isEmpty()) { | ||
117 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_context); | 120 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_context); | ||
118 | prefs.registerOnSharedPreferenceChangeListener(this); | 121 | prefs.registerOnSharedPreferenceChangeListener(this); | ||
119 | } | 122 | } | ||
120 | context = _context; | 123 | context = _context; | ||
121 | mprisDevices.add(device); | 124 | mprisDevices.add(device); | ||
122 | 125 | | |||
123 | mpris.setPlayerListUpdatedHandler("media_notification", mediaNotificationHandler); | 126 | mpris.setPlayerListUpdatedHandler("media_notification", mediaNotificationHandler); | ||
124 | mpris.setPlayerStatusUpdatedHandler("media_notification", mediaNotificationHandler); | 127 | mpris.setPlayerStatusUpdatedHandler("media_notification", mediaNotificationHandler); | ||
125 | 128 | | |||
126 | updateMediaNotification(); | 129 | updateMediaNotification(); | ||
127 | } | 130 | } | ||
128 | 131 | | |||
129 | /** | 132 | /** | ||
130 | * Called when a device disconnects/does not want notifications anymore | 133 | * Called when a device disconnects/does not want notifications anymore | ||
131 | * | 134 | * <p> | ||
132 | * Can be called multiple times, once for each device | 135 | * Can be called multiple times, once for each device | ||
136 | * | ||||
133 | * @param mpris The mpris plugin | 137 | * @param mpris The mpris plugin | ||
134 | * @param device The device id | 138 | * @param device The device id | ||
135 | */ | 139 | */ | ||
136 | public void onDestroy(MprisPlugin mpris, String device) { | 140 | public void onDestroy(MprisPlugin mpris, String device) { | ||
137 | mprisDevices.remove(device); | 141 | mprisDevices.remove(device); | ||
138 | mpris.removePlayerStatusUpdatedHandler("media_notification"); | 142 | mpris.removePlayerStatusUpdatedHandler("media_notification"); | ||
139 | mpris.removePlayerListUpdatedHandler("media_notification"); | 143 | mpris.removePlayerListUpdatedHandler("media_notification"); | ||
140 | updateMediaNotification(); | 144 | updateMediaNotification(); | ||
141 | 145 | | |||
142 | if (mprisDevices.isEmpty()) { | 146 | if (mprisDevices.isEmpty()) { | ||
143 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); | 147 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); | ||
144 | prefs.unregisterOnSharedPreferenceChangeListener(this); | 148 | prefs.unregisterOnSharedPreferenceChangeListener(this); | ||
145 | } | 149 | } | ||
146 | } | 150 | } | ||
147 | 151 | | |||
148 | /** | 152 | /** | ||
149 | * Updates which device+player we're going to use in the notification | 153 | * Updates which device+player we're going to use in the notification | ||
150 | * | 154 | * <p> | ||
151 | * Prefers playing devices/mpris players, but tries to keep displaying the same | 155 | * Prefers playing devices/mpris players, but tries to keep displaying the same | ||
152 | * player and device, while possible. | 156 | * player and device, while possible. | ||
157 | * | ||||
153 | * @param service The background service | 158 | * @param service The background service | ||
154 | */ | 159 | */ | ||
155 | private void updateCurrentPlayer(BackgroundService service) { | 160 | private void updateCurrentPlayer(BackgroundService service) { | ||
156 | Device device = null; | 161 | Device device = null; | ||
157 | MprisPlugin.MprisPlayer playing = null; | 162 | MprisPlugin.MprisPlayer playing = null; | ||
158 | 163 | | |||
159 | //First try the previously displayed player | 164 | //First try the previously displayed player | ||
160 | if (notificationDevice != null && mprisDevices.contains(notificationDevice) && notificationPlayer != null) { | 165 | if (notificationDevice != null && mprisDevices.contains(notificationDevice) && notificationPlayer != null) { | ||
▲ Show 20 Lines • Show All 242 Lines • Show Last 20 Lines |