Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java
Show First 20 Lines • Show All 86 Lines • ▼ Show 20 Line(s) | 45 | public class MprisPlayer { | |||
---|---|---|---|---|---|
87 | private boolean isSpotify() { | 87 | private boolean isSpotify() { | ||
88 | return getPlayer().toLowerCase().equals("spotify"); | 88 | return getPlayer().toLowerCase().equals("spotify"); | ||
89 | } | 89 | } | ||
90 | 90 | | |||
91 | public int getVolume() { | 91 | public int getVolume() { | ||
92 | return volume; | 92 | return volume; | ||
93 | } | 93 | } | ||
94 | 94 | | |||
95 | public long getLength(){ return length; } | 95 | public long getLength() { | ||
96 | return length; | ||||
97 | } | ||||
96 | 98 | | |||
97 | public boolean isPlaying() { | 99 | public boolean isPlaying() { | ||
98 | return playing; | 100 | return playing; | ||
99 | } | 101 | } | ||
100 | 102 | | |||
101 | public boolean isPlayAllowed() { | 103 | public boolean isPlayAllowed() { | ||
102 | return playAllowed; | 104 | return playAllowed; | ||
103 | } | 105 | } | ||
Show All 15 Lines | |||||
119 | } | 121 | } | ||
120 | 122 | | |||
121 | public boolean hasAlbumArt() { | 123 | public boolean hasAlbumArt() { | ||
122 | return !albumArtUrl.isEmpty(); | 124 | return !albumArtUrl.isEmpty(); | ||
123 | } | 125 | } | ||
124 | 126 | | |||
125 | /** | 127 | /** | ||
126 | * Returns the album art (if available). Note that this can return null even if hasAlbumArt() returns true. | 128 | * Returns the album art (if available). Note that this can return null even if hasAlbumArt() returns true. | ||
129 | * | ||||
127 | * @return The album art, or null if not available | 130 | * @return The album art, or null if not available | ||
128 | */ | 131 | */ | ||
129 | public Bitmap getAlbumArt() { | 132 | public Bitmap getAlbumArt() { | ||
130 | return AlbumArtCache.getAlbumArt(albumArtUrl); | 133 | return AlbumArtCache.getAlbumArt(albumArtUrl); | ||
131 | } | 134 | } | ||
132 | 135 | | |||
133 | public boolean isSetVolumeAllowed() { | 136 | public boolean isSetVolumeAllowed() { | ||
134 | return !isSpotify(); | 137 | return !isSpotify(); | ||
135 | } | 138 | } | ||
136 | 139 | | |||
137 | public long getPosition(){ | 140 | public long getPosition() { | ||
138 | if(playing) { | 141 | if (playing) { | ||
139 | return lastPosition + (System.currentTimeMillis() - lastPositionTime); | 142 | return lastPosition + (System.currentTimeMillis() - lastPositionTime); | ||
140 | } else { | 143 | } else { | ||
141 | return lastPosition; | 144 | return lastPosition; | ||
142 | } | 145 | } | ||
143 | } | 146 | } | ||
144 | 147 | | |||
145 | public void playPause() { | 148 | public void playPause() { | ||
146 | if (isPauseAllowed() || isPlayAllowed()) { | 149 | if (isPauseAllowed() || isPlayAllowed()) { | ||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | 197 | public void seek(int offset) { | |||
197 | } | 200 | } | ||
198 | } | 201 | } | ||
199 | } | 202 | } | ||
200 | 203 | | |||
201 | public final static String PACKAGE_TYPE_MPRIS = "kdeconnect.mpris"; | 204 | public final static String PACKAGE_TYPE_MPRIS = "kdeconnect.mpris"; | ||
202 | public final static String PACKAGE_TYPE_MPRIS_REQUEST = "kdeconnect.mpris.request"; | 205 | public final static String PACKAGE_TYPE_MPRIS_REQUEST = "kdeconnect.mpris.request"; | ||
203 | 206 | | |||
204 | private HashMap<String, MprisPlayer> players = new HashMap<>(); | 207 | private HashMap<String, MprisPlayer> players = new HashMap<>(); | ||
205 | private HashMap<String,Handler> playerStatusUpdated = new HashMap<>(); | 208 | private HashMap<String, Handler> playerStatusUpdated = new HashMap<>(); | ||
206 | 209 | | |||
207 | private HashMap<String,Handler> playerListUpdated = new HashMap<>(); | 210 | private HashMap<String, Handler> playerListUpdated = new HashMap<>(); | ||
208 | 211 | | |||
209 | @Override | 212 | @Override | ||
210 | public String getDisplayName() { | 213 | public String getDisplayName() { | ||
211 | return context.getResources().getString(R.string.pref_plugin_mpris); | 214 | return context.getResources().getString(R.string.pref_plugin_mpris); | ||
212 | } | 215 | } | ||
213 | 216 | | |||
214 | @Override | 217 | @Override | ||
215 | public String getDescription() { | 218 | public String getDescription() { | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 269 | if (np.has("player")) { | |||
268 | if (playerStatus != null) { | 271 | if (playerStatus != null) { | ||
269 | playerStatus.currentSong = np.getString("nowPlaying", playerStatus.currentSong); | 272 | playerStatus.currentSong = np.getString("nowPlaying", playerStatus.currentSong); | ||
270 | //Note: title, artist and album will not be available for all desktop clients | 273 | //Note: title, artist and album will not be available for all desktop clients | ||
271 | playerStatus.title = np.getString("title", playerStatus.title); | 274 | playerStatus.title = np.getString("title", playerStatus.title); | ||
272 | playerStatus.artist = np.getString("artist", playerStatus.artist); | 275 | playerStatus.artist = np.getString("artist", playerStatus.artist); | ||
273 | playerStatus.album = np.getString("album", playerStatus.album); | 276 | playerStatus.album = np.getString("album", playerStatus.album); | ||
274 | playerStatus.volume = np.getInt("volume", playerStatus.volume); | 277 | playerStatus.volume = np.getInt("volume", playerStatus.volume); | ||
275 | playerStatus.length = np.getLong("length", playerStatus.length); | 278 | playerStatus.length = np.getLong("length", playerStatus.length); | ||
276 | if(np.has("pos")){ | 279 | if (np.has("pos")) { | ||
277 | playerStatus.lastPosition = np.getLong("pos", playerStatus.lastPosition); | 280 | playerStatus.lastPosition = np.getLong("pos", playerStatus.lastPosition); | ||
278 | playerStatus.lastPositionTime = System.currentTimeMillis(); | 281 | playerStatus.lastPositionTime = System.currentTimeMillis(); | ||
279 | } | 282 | } | ||
280 | playerStatus.playing = np.getBoolean("isPlaying", playerStatus.playing); | 283 | playerStatus.playing = np.getBoolean("isPlaying", playerStatus.playing); | ||
281 | playerStatus.playAllowed = np.getBoolean("canPlay", playerStatus.playAllowed); | 284 | playerStatus.playAllowed = np.getBoolean("canPlay", playerStatus.playAllowed); | ||
282 | playerStatus.pauseAllowed = np.getBoolean("canPause", playerStatus.pauseAllowed); | 285 | playerStatus.pauseAllowed = np.getBoolean("canPause", playerStatus.pauseAllowed); | ||
283 | playerStatus.goNextAllowed = np.getBoolean("canGoNext", playerStatus.goNextAllowed); | 286 | playerStatus.goNextAllowed = np.getBoolean("canGoNext", playerStatus.goNextAllowed); | ||
284 | playerStatus.goPreviousAllowed = np.getBoolean("canGoPrevious", playerStatus.goPreviousAllowed); | 287 | playerStatus.goPreviousAllowed = np.getBoolean("canGoPrevious", playerStatus.goPreviousAllowed); | ||
285 | playerStatus.seekAllowed = np.getBoolean("canSeek", playerStatus.seekAllowed); | 288 | playerStatus.seekAllowed = np.getBoolean("canSeek", playerStatus.seekAllowed); | ||
286 | String newAlbumArtUrlstring = np.getString("albumArtUrl", playerStatus.albumArtUrl); | 289 | String newAlbumArtUrlstring = np.getString("albumArtUrl", playerStatus.albumArtUrl); | ||
287 | try { | 290 | try { | ||
288 | //Turn the url into canonical form (and check its validity) | 291 | //Turn the url into canonical form (and check its validity) | ||
289 | URL newAlbumArtUrl = new URL(newAlbumArtUrlstring); | 292 | URL newAlbumArtUrl = new URL(newAlbumArtUrlstring); | ||
290 | playerStatus.albumArtUrl = newAlbumArtUrl.toString(); | 293 | playerStatus.albumArtUrl = newAlbumArtUrl.toString(); | ||
291 | } catch (MalformedURLException ignored) {} | 294 | } catch (MalformedURLException ignored) { | ||
295 | } | ||||
296 | | ||||
292 | for (String key : playerStatusUpdated.keySet()) { | 297 | for (String key : playerStatusUpdated.keySet()) { | ||
293 | try { | 298 | try { | ||
294 | playerStatusUpdated.get(key).dispatchMessage(new Message()); | 299 | playerStatusUpdated.get(key).dispatchMessage(new Message()); | ||
295 | } catch(Exception e) { | 300 | } catch (Exception e) { | ||
296 | e.printStackTrace(); | 301 | e.printStackTrace(); | ||
297 | Log.e("MprisControl","Exception"); | 302 | Log.e("MprisControl", "Exception"); | ||
298 | playerStatusUpdated.remove(key); | 303 | playerStatusUpdated.remove(key); | ||
299 | } | 304 | } | ||
300 | } | 305 | } | ||
301 | } | 306 | } | ||
302 | } | 307 | } | ||
303 | 308 | | |||
304 | List<String> newPlayerList = np.getStringList("playerList"); | 309 | List<String> newPlayerList = np.getStringList("playerList"); | ||
305 | if (newPlayerList != null) { | 310 | if (newPlayerList != null) { | ||
Show All 26 Lines | 336 | if (!found) { | |||
332 | iter.remove(); | 337 | iter.remove(); | ||
333 | equals = false; | 338 | equals = false; | ||
334 | } | 339 | } | ||
335 | } | 340 | } | ||
336 | if (!equals) { | 341 | if (!equals) { | ||
337 | for (String key : playerListUpdated.keySet()) { | 342 | for (String key : playerListUpdated.keySet()) { | ||
338 | try { | 343 | try { | ||
339 | playerListUpdated.get(key).dispatchMessage(new Message()); | 344 | playerListUpdated.get(key).dispatchMessage(new Message()); | ||
340 | } catch(Exception e) { | 345 | } catch (Exception e) { | ||
341 | e.printStackTrace(); | 346 | e.printStackTrace(); | ||
342 | Log.e("MprisControl","Exception"); | 347 | Log.e("MprisControl", "Exception"); | ||
343 | playerListUpdated.remove(key); | 348 | playerListUpdated.remove(key); | ||
344 | } | 349 | } | ||
345 | } | 350 | } | ||
346 | } | 351 | } | ||
347 | } | 352 | } | ||
348 | 353 | | |||
349 | return true; | 354 | return true; | ||
350 | } | 355 | } | ||
351 | 356 | | |||
352 | @Override | 357 | @Override | ||
353 | public String[] getSupportedPackageTypes() { | 358 | public String[] getSupportedPackageTypes() { | ||
354 | return new String[] {PACKAGE_TYPE_MPRIS}; | 359 | return new String[]{PACKAGE_TYPE_MPRIS}; | ||
355 | } | 360 | } | ||
356 | 361 | | |||
357 | @Override | 362 | @Override | ||
358 | public String[] getOutgoingPackageTypes() { | 363 | public String[] getOutgoingPackageTypes() { | ||
359 | return new String[] {PACKAGE_TYPE_MPRIS_REQUEST}; | 364 | return new String[]{PACKAGE_TYPE_MPRIS_REQUEST}; | ||
360 | } | 365 | } | ||
361 | 366 | | |||
362 | public void setPlayerStatusUpdatedHandler(String id, Handler h) { | 367 | public void setPlayerStatusUpdatedHandler(String id, Handler h) { | ||
363 | playerStatusUpdated.put(id, h); | 368 | playerStatusUpdated.put(id, h); | ||
364 | 369 | | |||
365 | h.dispatchMessage(new Message()); | 370 | h.dispatchMessage(new Message()); | ||
366 | } | 371 | } | ||
367 | 372 | | |||
368 | public void removePlayerStatusUpdatedHandler(String id) { | 373 | public void removePlayerStatusUpdatedHandler(String id) { | ||
369 | playerStatusUpdated.remove(id); | 374 | playerStatusUpdated.remove(id); | ||
370 | } | 375 | } | ||
371 | 376 | | |||
372 | public void setPlayerListUpdatedHandler(String id, Handler h) { | 377 | public void setPlayerListUpdatedHandler(String id, Handler h) { | ||
373 | playerListUpdated.put(id,h); | 378 | playerListUpdated.put(id, h); | ||
374 | 379 | | |||
375 | h.dispatchMessage(new Message()); | 380 | h.dispatchMessage(new Message()); | ||
376 | } | 381 | } | ||
377 | 382 | | |||
378 | public void removePlayerListUpdatedHandler(String id) { | 383 | public void removePlayerListUpdatedHandler(String id) { | ||
379 | playerListUpdated.remove(id); | 384 | playerListUpdated.remove(id); | ||
380 | } | 385 | } | ||
381 | 386 | | |||
382 | public List<String> getPlayerList() { | 387 | public List<String> getPlayerList() { | ||
383 | List<String> playerlist = new ArrayList<>(players.keySet()); | 388 | List<String> playerlist = new ArrayList<>(players.keySet()); | ||
384 | Collections.sort(playerlist); | 389 | Collections.sort(playerlist); | ||
385 | return playerlist; | 390 | return playerlist; | ||
386 | } | 391 | } | ||
387 | 392 | | |||
388 | public MprisPlayer getPlayerStatus(String player) { | 393 | public MprisPlayer getPlayerStatus(String player) { | ||
389 | return players.get(player); | 394 | return players.get(player); | ||
390 | } | 395 | } | ||
391 | 396 | | |||
392 | public MprisPlayer getEmptyPlayer() { | 397 | public MprisPlayer getEmptyPlayer() { | ||
393 | return new MprisPlayer(); | 398 | return new MprisPlayer(); | ||
394 | } | 399 | } | ||
395 | 400 | | |||
396 | /** | 401 | /** | ||
397 | * Returns a playing mpris player, if any exist | 402 | * Returns a playing mpris player, if any exist | ||
403 | * | ||||
398 | * @return null if no players are playing, a playing player otherwise | 404 | * @return null if no players are playing, a playing player otherwise | ||
399 | */ | 405 | */ | ||
400 | public MprisPlayer getPlayingPlayer() { | 406 | public MprisPlayer getPlayingPlayer() { | ||
401 | for (MprisPlayer player : players.values()) { | 407 | for (MprisPlayer player : players.values()) { | ||
402 | if (player.isPlaying()) { | 408 | if (player.isPlaying()) { | ||
403 | return player; | 409 | return player; | ||
404 | } | 410 | } | ||
405 | } | 411 | } | ||
406 | return null; | 412 | return null; | ||
407 | } | 413 | } | ||
408 | 414 | | |||
409 | private void requestPlayerList() { | 415 | private void requestPlayerList() { | ||
410 | NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MPRIS_REQUEST); | 416 | NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MPRIS_REQUEST); | ||
411 | np.set("requestPlayerList",true); | 417 | np.set("requestPlayerList", true); | ||
412 | device.sendPackage(np); | 418 | device.sendPackage(np); | ||
413 | } | 419 | } | ||
414 | 420 | | |||
415 | private void requestPlayerStatus(String player) { | 421 | private void requestPlayerStatus(String player) { | ||
416 | NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MPRIS_REQUEST); | 422 | NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MPRIS_REQUEST); | ||
417 | np.set("player", player); | 423 | np.set("player", player); | ||
418 | np.set("requestNowPlaying",true); | 424 | np.set("requestNowPlaying", true); | ||
419 | np.set("requestVolume",true); | 425 | np.set("requestVolume", true); | ||
420 | device.sendPackage(np); | 426 | device.sendPackage(np); | ||
421 | } | 427 | } | ||
422 | 428 | | |||
423 | @Override | 429 | @Override | ||
424 | public boolean hasMainActivity() { | 430 | public boolean hasMainActivity() { | ||
425 | return true; | 431 | return true; | ||
426 | } | 432 | } | ||
427 | 433 | | |||
Show All 16 Lines | 449 | if (url.equals(player.albumArtUrl)) { | |||
444 | doEmitUpdate = true; | 450 | doEmitUpdate = true; | ||
445 | } | 451 | } | ||
446 | } | 452 | } | ||
447 | 453 | | |||
448 | if (doEmitUpdate) { | 454 | if (doEmitUpdate) { | ||
449 | for (String key : playerStatusUpdated.keySet()) { | 455 | for (String key : playerStatusUpdated.keySet()) { | ||
450 | try { | 456 | try { | ||
451 | playerStatusUpdated.get(key).dispatchMessage(new Message()); | 457 | playerStatusUpdated.get(key).dispatchMessage(new Message()); | ||
452 | } catch(Exception e) { | 458 | } catch (Exception e) { | ||
453 | e.printStackTrace(); | 459 | e.printStackTrace(); | ||
454 | Log.e("MprisControl","Exception"); | 460 | Log.e("MprisControl", "Exception"); | ||
455 | playerStatusUpdated.remove(key); | 461 | playerStatusUpdated.remove(key); | ||
456 | } | 462 | } | ||
457 | } | 463 | } | ||
458 | } | 464 | } | ||
459 | } | 465 | } | ||
460 | } | 466 | } |