diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java index 8ba021e5..4a598aef 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java @@ -3,6 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,8 +30,14 @@ public class WeatherNotificationReceiver extends BroadcastReceiver { } if (weather != null) { - LOG.info("weather in " + weather.location + " is " + (weather.currentTemp - 273) + "°C"); - } + LOG.info("weather in " + weather.location + " is " + weather.currentCondition + " (" + (weather.currentTemp - 273) + "°C)"); + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor edit = sharedPrefs.edit(); + edit.putString("weather_location", weather.location); + edit.putString("weather_current_condition", weather.currentCondition); + edit.putInt("weather_current_temp", weather.currentTemp); + edit.apply(); + } } } \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerWeatherNeat.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerWeatherNeat.java index c32074d3..ac09bc7a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerWeatherNeat.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerWeatherNeat.java @@ -1,5 +1,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.Pair; import org.slf4j.Logger; @@ -9,6 +11,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.UUID; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; @@ -16,7 +19,7 @@ public class AppMessageHandlerWeatherNeat extends AppMessageHandler { public static final int KEY_REQUEST = 0; public static final int KEY_CITY = 1; - public static final int KEY_TEMPERATUR = 2; + public static final int KEY_TEMPERATURE = 2; public static final int KEY_CONDITION = 3; public static final int KEY_LIGHT_TIME = 5; @@ -28,10 +31,10 @@ public class AppMessageHandlerWeatherNeat extends AppMessageHandler { private byte[] encodeWeatherNeatMessage(String city, String temperature, String condition, int light_time) { ArrayList> pairs = new ArrayList<>(4); - pairs.add(new Pair<>(1, (Object) city)); - pairs.add(new Pair<>(2, (Object) temperature)); - pairs.add(new Pair<>(3, (Object) condition)); - pairs.add(new Pair<>(5, (Object) light_time)); // seconds for backlight on shake + pairs.add(new Pair<>(KEY_CITY, (Object) city)); + pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) temperature)); + pairs.add(new Pair<>(KEY_CONDITION, (Object) condition)); + pairs.add(new Pair<>(KEY_LIGHT_TIME, (Object) light_time)); // seconds for backlight on shake byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id); byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); @@ -47,8 +50,12 @@ public class AppMessageHandlerWeatherNeat extends AppMessageHandler { @Override public GBDeviceEvent[] handleMessage(ArrayList> pairs) { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext()); + String currentTemp = (sharedPrefs.getInt("weather_current_temp", 0) - 273) + "°C"; + String location = sharedPrefs.getString("weather_location", "unknown"); + String condition = sharedPrefs.getString("weather_current_condition", "unknown"); GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); - sendBytes.encodedBytes = encodeWeatherNeatMessage("Berlin", "22 C", "cloudy", 0); + sendBytes.encodedBytes = encodeWeatherNeatMessage(location, currentTemp, condition, 3); return new GBDeviceEvent[]{sendBytes}; } } diff --git a/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java b/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java index b340898b..ab04cace 100644 --- a/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java +++ b/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java @@ -17,24 +17,31 @@ public class ParcelableWeather2 implements Parcelable { public int version = 0; public String location = ""; public int currentTemp = 0; + public String currentCondition = ""; private ParcelableWeather2(Parcel in) { int version = in.readInt(); if (version != 2) { return; } - Bundle bundle = in.readBundle(this.getClass().getClassLoader()); + Bundle bundle = in.readBundle(); location = bundle.getString("weather_location"); time = bundle.getLong("weather_time"); queryTime = bundle.getLong("weather_query_time"); bundle.getString("weather_forecast_url"); int conditions = bundle.getInt("weather_conditions"); if (conditions > 0) { - Bundle conditionBundle = in.readBundle(this.getClass().getClassLoader()); - conditionBundle.getString("weather_condition_text"); + Bundle conditionBundle = in.readBundle(); + currentCondition = conditionBundle.getString("weather_condition_text"); conditionBundle.getStringArray("weather_condition_types"); currentTemp = conditionBundle.getInt("weather_current_temp"); - + } + // get the rest + while(--conditions > 0) { + Bundle conditionBundle = in.readBundle(); + conditionBundle.getString("weather_condition_text"); + conditionBundle.getStringArray("weather_condition_types"); + conditionBundle.getInt("weather_current_temp"); } }