From 82c0f35c58fcbbcc0cabe38d52f8a8da86c1626e Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 31 Dec 2016 18:56:24 +0100 Subject: [PATCH] Pebble: add encodeUpadteWeather() to AppMessageHandler for easier watchface support Now in Timestyle weather is in sync with what we get from weather notification --- .../devices/pebble/AppMessageHandler.java | 9 +++- .../AppMessageHandlerTimeStylePebble.java | 43 +++++++++---------- .../devices/pebble/PebbleProtocol.java | 22 +++++++++- .../DeviceCommunicationServiceTestCase.java | 4 +- .../service/TestDeviceService.java | 4 +- .../service/TestDeviceSupport.java | 4 +- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java index 1944f2d5f..693bf9b1f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java @@ -8,10 +8,11 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; public class AppMessageHandler { - protected final PebbleProtocol mPebbleProtocol; - protected final UUID mUUID; + final PebbleProtocol mPebbleProtocol; + final UUID mUUID; AppMessageHandler(UUID uuid, PebbleProtocol pebbleProtocol) { mUUID = uuid; @@ -34,6 +35,10 @@ public class AppMessageHandler { return null; } + public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) { + return null; + } + protected GBDevice getDevice() { return mPebbleProtocol.getDevice(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java index 784603a42..e5d494027 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java @@ -10,8 +10,7 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; -import nodomain.freeyourgadget.gadgetbridge.model.Weather; -import ru.gelin.android.weather.notification.ParcelableWeather2; +import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; public class AppMessageHandlerTimeStylePebble extends AppMessageHandler { private static final int MESSAGE_KEY_WeatherCondition = 10000; @@ -95,35 +94,35 @@ public class AppMessageHandlerTimeStylePebble extends AppMessageHandler { return iconToLoad; } - private byte[] encodeTimeStylePebbleWeather() { - ArrayList> pairs = new ArrayList<>(); - ParcelableWeather2 weather = Weather.getInstance().getWeather2(); + private byte[] encodeTimeStylePebbleWeather(WeatherSpec weatherSpec) { - boolean isNight = false; //TODO: use the night icons when night - if (weather != null) { - pairs.add(new Pair<>(MESSAGE_KEY_WeatherUseNightIcon, (Object) (isNight ? 1 : 0))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherTemperature, (Object) (weather.currentTemp - 273))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherCondition, (Object) (getIconForConditionCode(weather.currentConditionCode, isNight)))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastCondition, (Object) (getIconForConditionCode(weather.forecastConditionCode, isNight)))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastHighTemp, (Object) (weather.todayHighTemp - 273))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastLowTemp, (Object) (weather.todayLowTemp - 273))); + if (weatherSpec == null) { + return null; } - return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); + ArrayList> pairs = new ArrayList<>(); + boolean isNight = false; //TODO: use the night icons when night + pairs.add(new Pair<>(MESSAGE_KEY_WeatherUseNightIcon, (Object) (isNight ? 1 : 0))); + pairs.add(new Pair<>(MESSAGE_KEY_WeatherTemperature, (Object) (weatherSpec.currentTemp - 273))); + pairs.add(new Pair<>(MESSAGE_KEY_WeatherCondition, (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight)))); + pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastCondition, (Object) (getIconForConditionCode(weatherSpec.tomorrowConditionCode, isNight)))); + pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastHighTemp, (Object) (weatherSpec.todayMaxTemp - 273))); + + pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastLowTemp, (Object) (weatherSpec.todayMinTemp - 273))); + + return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); } @Override public GBDeviceEvent[] handleMessage(ArrayList> pairs) { - return pushMessage(); + GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes(); + sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id); + return new GBDeviceEvent[]{sendBytesAck}; + // TODO: trigger update of weather? } @Override - public GBDeviceEvent[] pushMessage() { - GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes(); - sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id); - - GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); - sendBytes.encodedBytes = encodeTimeStylePebbleWeather(); - return new GBDeviceEvent[]{sendBytesAck, sendBytes}; + public byte[] encodeUpdateWeather(WeatherSpec weatherSpec) { + return encodeTimeStylePebbleWeather(weatherSpec); } } \ No newline at end of file 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 9e23ace4e..6c4efc21c 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 @@ -386,6 +386,8 @@ public class PebbleProtocol extends GBDeviceProtocol { private final Map mAppMessageHandlers = new HashMap<>(); + private UUID currentRunningApp = UUID_ZERO; + public PebbleProtocol(GBDevice device) { super(device); mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this)); @@ -1134,7 +1136,8 @@ public class PebbleProtocol extends GBDeviceProtocol { if (mFwMajor < 4) { return null; } - return encodeWeatherForecast(weatherSpec.timestamp, + byte[] watchfaceProtocol = null; + byte[] forecastProtocol = encodeWeatherForecast(weatherSpec.timestamp, weatherSpec.location, weatherSpec.currentTemp - 273, weatherSpec.todayMaxTemp - 273, @@ -1145,6 +1148,19 @@ public class PebbleProtocol extends GBDeviceProtocol { weatherSpec.tomorrowMinTemp - 273, Weather.mapToPebbleCondition(weatherSpec.tomorrowConditionCode) ); + AppMessageHandler handler = mAppMessageHandlers.get(currentRunningApp); + if (handler != null) { + watchfaceProtocol = handler.encodeUpdateWeather(weatherSpec); + } + + if (watchfaceProtocol != null) { + ByteBuffer buf = ByteBuffer.allocate(forecastProtocol.length + watchfaceProtocol.length); + buf.put(forecastProtocol); + buf.put(watchfaceProtocol); + return buf.array(); + } + + return forecastProtocol; } private byte[] encodeWeatherForecast(int timestamp, String location, int tempNow, int tempHighToday, int tempLowToday, int conditionCodeToday, String conditionToday, int tempHighTomorrow, int tempLowTomorrow, int conditionCodeTomorrow) { @@ -2107,7 +2123,7 @@ public class PebbleProtocol extends GBDeviceProtocol { switch (command) { case APPRUNSTATE_START: LOG.info(ENDPOINT_NAME + ": started " + uuid); - + currentRunningApp = uuid; AppMessageHandler handler = mAppMessageHandlers.get(uuid); if (handler != null) { return handler.pushMessage(); @@ -2442,6 +2458,7 @@ public class PebbleProtocol extends GBDeviceProtocol { devEvts = handler.handleMessage(dict); } else { + currentRunningApp = uuid; devEvts = handler.pushMessage(); } } else { @@ -2453,6 +2470,7 @@ public class PebbleProtocol extends GBDeviceProtocol { devEvts = decodeDictToJSONAppMessage(uuid, buf); } else { + currentRunningApp = uuid; GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement(); gbDeviceEventAppManagement.uuid = uuid; gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.START; diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java index 68af48ad8..58dccd72b 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationServiceTestCase.java @@ -22,7 +22,7 @@ public class DeviceCommunicationServiceTestCase extends TestBase { * Factory that always returns the mockSupport instance */ private class TestDeviceSupportFactory extends DeviceSupportFactory { - public TestDeviceSupportFactory(Context context) { + TestDeviceSupportFactory(Context context) { super(context); } @@ -53,7 +53,7 @@ public class DeviceCommunicationServiceTestCase extends TestBase { mDeviceService = new TestDeviceService(getContext()); } - protected GBDevice getDevice() { + private GBDevice getDevice() { return realSupport.getDevice(); } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java index ba798a586..8eff6f79c 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceService.java @@ -13,11 +13,11 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; * Extends GBDeviceServer so that communication with the service works * with Robolectric. */ -public class TestDeviceService extends GBDeviceService { +class TestDeviceService extends GBDeviceService { private final ServiceController serviceController; private final DeviceCommunicationService service; - public TestDeviceService(Context context) throws Exception { + TestDeviceService(Context context) throws Exception { super(context); serviceController = Robolectric.buildService(DeviceCommunicationService.class, createIntent()); 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 6d7565de1..160b855b9 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceSupport.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/service/TestDeviceSupport.java @@ -17,9 +17,9 @@ import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; -public class TestDeviceSupport extends AbstractDeviceSupport { +class TestDeviceSupport extends AbstractDeviceSupport { - public TestDeviceSupport() { + TestDeviceSupport() { } @Override