mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-25 18:15:49 +01:00
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.
This commit is contained in:
parent
d030ad9400
commit
e08a900978
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user