1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-28 19:45:50 +01:00

Improvements to MusicPlayback receiver

- Also send duration if "duration" extra is present
- If "playing" and "postion" extras are present send a music state update

treat previous state and current state as equal if position delta is <=2 seconds
(Neccessary for some players which update every second - the pebble however counts by itself)
This commit is contained in:
Andreas Shimokawa 2016-06-11 23:37:03 +02:00
parent 2d080cabb2
commit f812fb1b1f
4 changed files with 44 additions and 3 deletions

View File

@ -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;
}
}
}

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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);