From e08a9009787b35ac433602d2cc4b392e19a7caf5 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Wed, 1 Feb 2017 22:55:40 +0100 Subject: [PATCH] Refactor the MusicPlaybackReceiver and related files Add actions to the filter (this should help with #536) Add "copy" constructors to MusicSpec and MusicStateSpec, and use those when receiving an updated intent, this way partial updates do not disrupt the local information. Iterate over incoming extra keys, explicitly check the incoming type and use only known type. This could help with #533 Possible problem: this code iterates over every key of the incoming bundle. --- .../externalevents/MusicPlaybackReceiver.java | 39 ++++++++++++------- .../gadgetbridge/model/MusicSpec.java | 13 +++++++ .../gadgetbridge/model/MusicStateSpec.java | 12 ++++++ .../service/DeviceCommunicationService.java | 3 +- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/MusicPlaybackReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/MusicPlaybackReceiver.java index a47c8f4d3..9d6ee214a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/MusicPlaybackReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/MusicPlaybackReceiver.java @@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.os.Bundle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,17 +27,32 @@ public class MusicPlaybackReceiver extends BroadcastReceiver { value != null ? value.toString() : "null", value != null ? value.getClass().getName() : "no class")); } */ - MusicSpec musicSpec = new MusicSpec(); - musicSpec.artist = intent.getStringExtra("artist"); - musicSpec.album = intent.getStringExtra("album"); - if (intent.hasExtra("track")) { - musicSpec.track = intent.getStringExtra("track"); - } - else if (intent.hasExtra("title")) { - musicSpec.track = intent.getStringExtra("title"); - } + MusicSpec musicSpec = new MusicSpec(lastMusicSpec); + MusicStateSpec stateSpec = new MusicStateSpec(lastStatecSpec); - musicSpec.duration = intent.getIntExtra("duration", 0) / 1000; + Bundle incomingBundle = intent.getExtras(); + for (String key : incomingBundle.keySet()) { + Object incoming = incomingBundle.get(key); + if (incoming instanceof String && "artist".equals(key)) { + musicSpec.artist = (String) incoming; + } else if (incoming instanceof String && "album".equals(key)) { + musicSpec.album = (String) incoming; + } else if (incoming instanceof String && "track".equals(key)) { + musicSpec.track = (String) incoming; + } else if (incoming instanceof String && "title".equals(key) && musicSpec.track == null) { + musicSpec.track = (String) incoming; + } else if (incoming instanceof Integer && "duration".equals(key)) { + musicSpec.duration = (Integer) incoming / 1000; + } else if (incoming instanceof Long && "duration".equals(key)) { + musicSpec.duration = ((Long) incoming).intValue() / 1000; + } else if (incoming instanceof Integer && "position".equals(key)) { + stateSpec.position = (Integer) incoming / 1000; + } else if (incoming instanceof Long && "position".equals(key)) { + stateSpec.position = ((Long) incoming).intValue() / 1000; + } else if (incoming instanceof Boolean && "playing".equals(key)) { + stateSpec.state = (byte) (((Boolean) incoming) ? MusicStateSpec.STATE_PLAYING : MusicStateSpec.STATE_PAUSED); + } + } if (!lastMusicSpec.equals(musicSpec)) { lastMusicSpec = musicSpec; @@ -47,9 +63,6 @@ public class MusicPlaybackReceiver extends BroadcastReceiver { } if (intent.hasExtra("position") && intent.hasExtra("playing")) { - MusicStateSpec stateSpec = new MusicStateSpec(); - stateSpec.position = intent.getIntExtra("position", 0) / 1000; - stateSpec.state = (byte) (intent.getBooleanExtra("playing", true) ? MusicStateSpec.STATE_PLAYING : MusicStateSpec.STATE_PAUSED); if (!lastStatecSpec.equals(stateSpec)) { LOG.info("Update Music State: state=" + stateSpec.state + ", position= " + stateSpec.position); GBApplication.deviceService().onSetMusicState(stateSpec); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicSpec.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicSpec.java index aa9f67ca9..9866a935d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicSpec.java @@ -17,6 +17,19 @@ public class MusicSpec { public int trackCount; public int trackNr; + public MusicSpec() { + + } + + public MusicSpec(MusicSpec old) { + this.duration = old.duration; + this.trackCount = old.trackCount; + this.trackNr = old.trackNr; + this.track = old.track; + this.album = old.album; + this.artist = old.artist; + } + @Override public boolean equals(Object obj) { if (obj == this) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicStateSpec.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicStateSpec.java index fb15948d7..f9d070eb8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicStateSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicStateSpec.java @@ -15,6 +15,18 @@ public class MusicStateSpec { public byte shuffle; public byte repeat; + public MusicStateSpec() { + + } + + public MusicStateSpec(MusicStateSpec old) { + this.state = old.state; + this.position = old.position; + this.playRate = old.playRate; + this.shuffle = old.shuffle; + this.repeat = old.repeat; + } + @Override public boolean equals(Object obj) { if (obj == this) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 1dd3934c3..277e2223a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -599,8 +599,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere mMusicPlaybackReceiver = new MusicPlaybackReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("com.android.music.metachanged"); + filter.addAction("com.android.music.playstatechanged"); + filter.addAction("com.android.music.playbackcomplete"); filter.addAction("net.sourceforge.subsonic.androidapp.EVENT_META_CHANGED"); - //filter.addAction("com.android.music.playstatechanged"); registerReceiver(mMusicPlaybackReceiver, filter); } if (mTimeChangeReceiver == null) {