From e386d6da439c38340818ef11922349f9fbb1396d Mon Sep 17 00:00:00 2001 From: Steffen Liebergeld Date: Wed, 8 Jun 2016 20:27:25 +0200 Subject: [PATCH] Add onSetMusicState(MusicStateSpec stateSpec) This commit contains the infrastructure needed for the NotificationHandler to send music state information to the device. That is, it introduces a call onSetMusicState(MusicStateSpec stateSpec), that in turn sets up an intent to the service, which will then call the encodeSetMusicState() function of the device. encodeSetMusicState is available for pebble only. There are empty stubs for other devices. --- .../gadgetbridge/devices/EventHandler.java | 3 +++ .../gadgetbridge/impl/GBDeviceService.java | 12 ++++++++++++ .../gadgetbridge/model/DeviceService.java | 6 ++++++ .../service/DeviceCommunicationService.java | 16 ++++++++++++++++ .../service/ServiceDeviceSupport.java | 9 +++++++++ .../service/devices/miband/MiBandSupport.java | 6 ++++++ .../service/devices/pebble/PebbleSupport.java | 8 ++++++++ .../serial/AbstractSerialDeviceSupport.java | 7 +++++++ .../service/serial/GBDeviceProtocol.java | 4 ++++ .../gadgetbridge/service/TestDeviceSupport.java | 6 ++++++ 10 files changed, 77 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java index 602ca5c85..a275ace51 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/EventHandler.java @@ -9,6 +9,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; /** @@ -25,6 +26,8 @@ public interface EventHandler { void onSetCallState(CallSpec callSpec); + void onSetMusicState(MusicStateSpec stateSpec); + void onSetMusicInfo(MusicSpec musicSpec); void onEnableRealtimeSteps(boolean enable); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java index 2e598eacf..60ff7b526 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -14,6 +14,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; @@ -125,6 +126,17 @@ public class GBDeviceService implements DeviceService { invokeService(intent); } + @Override + public void onSetMusicState(MusicStateSpec stateSpec) { + Intent intent = createIntent().setAction(ACTION_SETMUSICSTATE) + .putExtra(EXTRA_MUSIC_REPEAT, stateSpec.repeat) + .putExtra(EXTRA_MUSIC_RATE, stateSpec.playRate) + .putExtra(EXTRA_MUSIC_STATE, stateSpec.state) + .putExtra(EXTRA_MUSIC_SHUFFLE, stateSpec.shuffle) + .putExtra(EXTRA_MUSIC_POSITION, stateSpec.position); + invokeService(intent); + } + @Override public void onSetMusicInfo(MusicSpec musicSpec) { Intent intent = createIntent().setAction(ACTION_SETMUSICINFO) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java index aaf02798b..b3921b1b8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -18,6 +18,7 @@ public interface DeviceService extends EventHandler { String ACTION_CALLSTATE = PREFIX + ".action.callstate"; String ACTION_SETTIME = PREFIX + ".action.settime"; String ACTION_SETMUSICINFO = PREFIX + ".action.setmusicinfo"; + String ACTION_SETMUSICSTATE = PREFIX + ".action.setmusicstate"; String ACTION_REQUEST_DEVICEINFO = PREFIX + ".action.request_deviceinfo"; String ACTION_REQUEST_APPINFO = PREFIX + ".action.request_appinfo"; String ACTION_REQUEST_SCREENSHOT = PREFIX + ".action.request_screenshot"; @@ -57,6 +58,11 @@ public interface DeviceService extends EventHandler { String EXTRA_MUSIC_DURATION = "music_duration"; String EXTRA_MUSIC_TRACKNR = "music_tracknr"; String EXTRA_MUSIC_TRACKCOUNT = "music_trackcount"; + String EXTRA_MUSIC_STATE = "music_state"; + String EXTRA_MUSIC_SHUFFLE = "music_shuffle"; + String EXTRA_MUSIC_REPEAT = "music_repeat"; + String EXTRA_MUSIC_POSITION = "music_position"; + String EXTRA_MUSIC_RATE = "music_rate"; String EXTRA_APP_UUID = "app_uuid"; String EXTRA_APP_START = "app_start"; String EXTRA_APP_CONFIG = "app_config"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index fdafa5265..7903ff22a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -38,6 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; @@ -65,6 +66,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_RE import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_DEVICEINFO; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_SCREENSHOT; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETMUSICINFO; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETMUSICSTATE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETTIME; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_ALARMS; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_START; @@ -87,6 +89,11 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_FIN import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ALBUM; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ARTIST; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_DURATION; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_POSITION; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_RATE; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_REPEAT; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_SHUFFLE; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_STATE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_TRACK; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_TRACKCOUNT; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_TRACKNR; @@ -351,6 +358,15 @@ public class DeviceCommunicationService extends Service implements SharedPrefere musicSpec.trackNr = intent.getIntExtra(EXTRA_MUSIC_TRACKNR, 0); mDeviceSupport.onSetMusicInfo(musicSpec); break; + case ACTION_SETMUSICSTATE: + MusicStateSpec stateSpec = new MusicStateSpec(); + stateSpec.shuffle = intent.getByteExtra(EXTRA_MUSIC_SHUFFLE, (byte)0); + stateSpec.repeat = intent.getByteExtra(EXTRA_MUSIC_REPEAT, (byte)0); + stateSpec.position = intent.getIntExtra(EXTRA_MUSIC_POSITION, 0); + stateSpec.playRate = intent.getIntExtra(EXTRA_MUSIC_RATE, 0); + stateSpec.state = intent.getByteExtra(EXTRA_MUSIC_STATE, (byte)0); + mDeviceSupport.onSetMusicState(stateSpec); + break; case ACTION_REQUEST_APPINFO: mDeviceSupport.onAppInfoReq(); break; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/ServiceDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/ServiceDeviceSupport.java index 019722b54..e284f83d8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/ServiceDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/ServiceDeviceSupport.java @@ -16,6 +16,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; /** @@ -150,6 +151,14 @@ public class ServiceDeviceSupport implements DeviceSupport { delegate.onSetCallState(callSpec); } + @Override + public void onSetMusicState(MusicStateSpec stateSpec) { + if (checkBusy("set music state")) { + return; + } + delegate.onSetMusicState(stateSpec); + } + @Override public void onSetMusicInfo(MusicSpec musicSpec) { if (checkBusy("set music info")) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java index fd00d02ac..165e72e50 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/MiBandSupport.java @@ -38,6 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.model.GenericItem; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction; @@ -599,6 +600,11 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { return telephoneRinging; } + @Override + public void onSetMusicState(MusicStateSpec stateSpec) { + // not supported + } + @Override public void onSetMusicInfo(MusicSpec musicSpec) { // not supported diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleSupport.java index b0bc3b47d..7233a2be8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleSupport.java @@ -15,6 +15,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.serial.AbstractSerialDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread; @@ -108,6 +109,13 @@ public class PebbleSupport extends AbstractSerialDeviceSupport { } } + @Override + public void onSetMusicState(MusicStateSpec musicStateSpec) { + if (reconnect()) { + super.onSetMusicState(musicStateSpec); + } + } + @Override public void onSetMusicInfo(MusicSpec musicSpec) { if (reconnect()) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/AbstractSerialDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/AbstractSerialDeviceSupport.java index 78e15667d..e963155eb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/AbstractSerialDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/AbstractSerialDeviceSupport.java @@ -11,6 +11,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.EventHandler; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport; @@ -123,6 +124,12 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport sendToDevice(bytes); } + @Override + public void onSetMusicState(MusicStateSpec stateSpec) { + byte[] bytes = gbDeviceProtocol.encodeSetMusicState(stateSpec.state, stateSpec.position, stateSpec.playRate, stateSpec.shuffle, stateSpec.repeat); + sendToDevice(bytes); + } + @Override public void onSetMusicInfo(MusicSpec musicSpec) { byte[] bytes = gbDeviceProtocol.encodeSetMusicInfo(musicSpec.artist, musicSpec.album, musicSpec.track, musicSpec.duration, musicSpec.trackCount, musicSpec.trackNr); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/GBDeviceProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/GBDeviceProtocol.java index a226c0947..2492f2773 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/GBDeviceProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/serial/GBDeviceProtocol.java @@ -24,6 +24,10 @@ public abstract class GBDeviceProtocol { return null; } + public byte[] encodeSetMusicState(byte state, int position, int playRate, byte shuffle, byte repeat) { + return null; + } + public byte[] encodeFirmwareVersionReq() { return null; } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceSupport.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceSupport.java index 8070cb1eb..42b4dd5ce 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceSupport.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceSupport.java @@ -12,6 +12,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.Alarm; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; +import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; public class TestDeviceSupport extends AbstractDeviceSupport { @@ -66,6 +67,11 @@ public class TestDeviceSupport extends AbstractDeviceSupport { } + @Override + public void onSetMusicState(MusicStateSpec stateSpec) { + + } + @Override public void onSetMusicInfo(MusicSpec musicSpec) {