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 2036b4c2..3edfb881 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/MusicPlaybackReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/MusicPlaybackReceiver.java @@ -9,17 +9,29 @@ import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +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(); @Override public void onReceive(Context context, Intent intent) { + /* + 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")); + } + */ MusicSpec musicSpec = new MusicSpec(); musicSpec.artist = intent.getStringExtra("artist"); musicSpec.album = intent.getStringExtra("album"); musicSpec.track = intent.getStringExtra("track"); + musicSpec.duration = intent.getIntExtra("duration", 0) / 1000; + if (!lastMusicSpec.equals(musicSpec)) { lastMusicSpec = musicSpec; LOG.info("Update Music Info: " + musicSpec.artist + " / " + musicSpec.album + " / " + musicSpec.track); @@ -27,5 +39,18 @@ public class MusicPlaybackReceiver extends BroadcastReceiver { } else { LOG.info("got metadata changed intent, but nothing changed, ignoring."); } + + 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; + } } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index 1d39d552..cf74be6a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -18,7 +18,6 @@ import android.media.session.PlaybackState; import android.os.Build; import android.os.Bundle; import android.os.PowerManager; -import android.provider.MediaStore; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.support.v4.app.NotificationCompat; @@ -354,7 +353,7 @@ public class NotificationListener extends NotificationListenerService { } PlaybackState s = c.getPlaybackState(); - stateSpec.position = (int)s.getPosition(); + stateSpec.position = (int) (s.getPosition() / 1000); stateSpec.playRate = Math.round(100 * s.getPlaybackSpeed()); stateSpec.repeat = 1; stateSpec.shuffle = 1; 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 ee5510fb..fb15948d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicStateSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/MusicStateSpec.java @@ -14,4 +14,21 @@ public class MusicStateSpec { public int playRate; public byte shuffle; public byte repeat; + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof MusicStateSpec)) { + return false; + } + MusicStateSpec stateSpec = (MusicStateSpec) obj; + + return this.state == stateSpec.state && + Math.abs(this.position - stateSpec.position)<=2 && + this.playRate == stateSpec.playRate && + this.shuffle == stateSpec.shuffle && + this.repeat == stateSpec.repeat; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index 7672c5fb..1862f3c1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -1128,7 +1128,7 @@ public class PebbleProtocol extends GBDeviceProtocol { buf.order(ByteOrder.LITTLE_ENDIAN); buf.put(MUSICCONTROL_SETPLAYSTATE); buf.put(playState); - buf.putInt(position); + buf.putInt(position * 1000); buf.putInt(playRate); buf.put(shuffle); buf.put(repeat);