Music data handling improvements (#550)

* Fixed extracting the track length.

* Added current track and total track count.

* Few small changes to make sure everything gets updated properly.

* Remove unnecessary includes.
This commit is contained in:
Avamander 2017-02-11 10:49:01 +02:00 committed by Carsten Pfeiffer
parent 5dfd40062f
commit fea31924ba
2 changed files with 29 additions and 15 deletions

View File

@ -15,7 +15,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
public class MusicPlaybackReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(MusicPlaybackReceiver.class);
private static MusicSpec lastMusicSpec = new MusicSpec();
private static MusicStateSpec lastStatecSpec = new MusicStateSpec();
private static MusicStateSpec lastStateSpec = new MusicStateSpec();
@Override
public void onReceive(Context context, Intent intent) {
@ -28,7 +28,7 @@ public class MusicPlaybackReceiver extends BroadcastReceiver {
}
*/
MusicSpec musicSpec = new MusicSpec(lastMusicSpec);
MusicStateSpec stateSpec = new MusicStateSpec(lastStatecSpec);
MusicStateSpec stateSpec = new MusicStateSpec(lastStateSpec);
Bundle incomingBundle = intent.getExtras();
for (String key : incomingBundle.keySet()) {
@ -51,6 +51,14 @@ public class MusicPlaybackReceiver extends BroadcastReceiver {
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);
} else if (incoming instanceof String && "duration".equals(key)) {
musicSpec.duration = Integer.valueOf((String) incoming) / 1000;
} else if (incoming instanceof String && "trackno".equals(key)) {
musicSpec.trackNr = Integer.valueOf((String) incoming);
} else if (incoming instanceof String && "totaltrack".equals(key)) {
musicSpec.trackCount = Integer.valueOf((String) incoming);
} else if (incoming instanceof Integer && "pos".equals(key)) {
stateSpec.position = (Integer) incoming;
}
}
@ -59,17 +67,15 @@ public class MusicPlaybackReceiver extends BroadcastReceiver {
LOG.info("Update Music Info: " + musicSpec.artist + " / " + musicSpec.album + " / " + musicSpec.track);
GBApplication.deviceService().onSetMusicInfo(musicSpec);
} else {
LOG.info("got metadata changed intent, but nothing changed, ignoring.");
LOG.info("Got metadata changed intent, but nothing changed, ignoring.");
}
if (intent.hasExtra("position") && intent.hasExtra("playing")) {
if (!lastStatecSpec.equals(stateSpec)) {
LOG.info("Update Music State: state=" + stateSpec.state + ", position= " + stateSpec.position);
GBApplication.deviceService().onSetMusicState(stateSpec);
} else {
LOG.info("got state changed intent, but not enough has changed, ignoring.");
}
lastStatecSpec = stateSpec;
if (!lastStateSpec.equals(stateSpec)) {
lastStateSpec = stateSpec;
LOG.info("Update Music State: state=" + stateSpec.state + ", position= " + stateSpec.position);
GBApplication.deviceService().onSetMusicState(stateSpec);
} else {
LOG.info("Got state changed intent, but not enough has changed, ignoring.");
}
}
}

View File

@ -156,6 +156,15 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private AlarmReceiver mAlarmReceiver = null;
private Random mRandom = new Random();
private final String[] mMusicActions = {
"com.android.music.metachanged",
"com.android.music.playstatechanged",
"com.android.music.queuechanged",
"com.android.music.playbackcomplete",
"net.sourceforge.subsonic.androidapp.EVENT_META_CHANGED",
"com.maxmpz.audioplayer.TPOS_SYNC",
"com.maxmpz.audioplayer.STATUS_CHANGED",};
/**
* For testing!
*
@ -591,10 +600,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
if (mMusicPlaybackReceiver == null) {
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");
for (String action : mMusicActions){
filter.addAction(action);
}
registerReceiver(mMusicPlaybackReceiver, filter);
}
if (mTimeChangeReceiver == null) {