2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-02-08 23:53:40 +01:00
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-05-12 06:28:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-02-08 23:53:40 +01:00
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2016-06-11 23:37:03 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-02-08 23:53:40 +01:00
|
|
|
public class MusicPlaybackReceiver extends BroadcastReceiver {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MusicPlaybackReceiver.class);
|
2016-06-10 23:13:33 +02:00
|
|
|
private static MusicSpec lastMusicSpec = new MusicSpec();
|
2016-06-11 23:37:03 +02:00
|
|
|
private static MusicStateSpec lastStatecSpec = new MusicStateSpec();
|
2015-02-08 23:53:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2016-06-11 23:37:03 +02:00
|
|
|
/*
|
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
|
for (String key : bundle.keySet()) {
|
|
|
|
Object value = bundle.get(key);
|
|
|
|
LOG.info(String.format("%s %s (%s)", key,
|
|
|
|
value != null ? value.toString() : "null", value != null ? value.getClass().getName() : "no class"));
|
|
|
|
}
|
|
|
|
*/
|
2016-04-04 20:08:34 +02:00
|
|
|
MusicSpec musicSpec = new MusicSpec();
|
2016-06-10 23:13:33 +02:00
|
|
|
musicSpec.artist = intent.getStringExtra("artist");
|
|
|
|
musicSpec.album = intent.getStringExtra("album");
|
2016-12-26 23:53:24 +01:00
|
|
|
if (intent.hasExtra("track")) {
|
|
|
|
musicSpec.track = intent.getStringExtra("track");
|
|
|
|
}
|
|
|
|
else if (intent.hasExtra("title")) {
|
|
|
|
musicSpec.track = intent.getStringExtra("title");
|
|
|
|
}
|
|
|
|
|
2016-06-11 23:37:03 +02:00
|
|
|
musicSpec.duration = intent.getIntExtra("duration", 0) / 1000;
|
|
|
|
|
2016-06-10 23:13:33 +02:00
|
|
|
if (!lastMusicSpec.equals(musicSpec)) {
|
|
|
|
lastMusicSpec = musicSpec;
|
|
|
|
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.");
|
|
|
|
}
|
2016-06-11 23:37:03 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
LOG.info("got state changed intent, but not enough has changed, ignoring.");
|
|
|
|
}
|
|
|
|
lastStatecSpec = stateSpec;
|
|
|
|
}
|
2015-02-08 23:53:40 +01:00
|
|
|
}
|
|
|
|
}
|