From af9ee90383025ebdce8006096c785dbadf600700 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 28 Dec 2015 20:55:59 +0100 Subject: [PATCH] actually get weather, seems to be the only way --- .../WeatherNotificationReceiver.java | 76 +++---------------- .../notification/ParcelableWeather2.java | 62 +++++++++++++++ 2 files changed, 72 insertions(+), 66 deletions(-) create mode 100644 app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java 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 029637c12..8ba021e59 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java @@ -1,68 +1,18 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents; -import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ru.gelin.android.weather.notification.ParcelableWeather2; + public class WeatherNotificationReceiver extends BroadcastReceiver { private static final Logger LOG = LoggerFactory.getLogger(WeatherNotificationReceiver.class); - static class Weather implements Parcelable { - // getters and setters suck ;) - - public long time = 0; - public long queryTime = 0; - public int version = 0; - public String location = ""; - int currentTemp = 0; - - private Weather(Parcel in) { - int version = in.readInt(); - if (version != 2) { - LOG.info("wrong version" + version); - return; - } - Bundle bundle = in.readBundle(); - location = bundle.getString("weather_location"); - time = bundle.getLong("weather_time"); - queryTime = bundle.getLong("weather_query_time"); - int conditions = bundle.getInt("weather_conditions"); - if (conditions > 0) { - currentTemp = bundle.getInt("weather_current_temp"); - } - } - - public static final Creator CREATOR = new Creator() { - @Override - public Weather createFromParcel(Parcel in) { - return new Weather(in); - } - - @Override - public Weather[] newArray(int size) { - return new Weather[size]; - } - }; - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - // we do not really want to use this at all - } - } - @Override public void onReceive(Context context, Intent intent) { @@ -70,22 +20,16 @@ public class WeatherNotificationReceiver extends BroadcastReceiver { LOG.info("Wrong action"); return; } - Bundle bundle = intent.getExtras(); - -/* for (String key : bundle.keySet()) { - Object value = bundle.get(key); - LOG.info(String.format("%s %s (%s)", key, - value.toString(), value.getClass().getName())); + ParcelableWeather2 weather = null; + try { + weather = intent.getParcelableExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER"); + } catch (RuntimeException e) { + e.printStackTrace(); } - if (!intent.hasExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER")) { - LOG.info("no weather extra"); - return; - } - - Weather weather = intent.getParcelableExtra("ru.gelin.android.weather.notification.EXTRA_WEATHER"); if (weather != null) { - LOG.info("weather in " + weather.location + " is " + weather.currentTemp); - }*/ + LOG.info("weather in " + weather.location + " is " + (weather.currentTemp - 273) + "°C"); + } + } } \ No newline at end of file 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 new file mode 100644 index 000000000..b340898b2 --- /dev/null +++ b/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java @@ -0,0 +1,62 @@ +package ru.gelin.android.weather.notification; + +import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ParcelableWeather2 implements Parcelable { + private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class); + + // getters and setters suck ;) + + public long time = 0; + public long queryTime = 0; + public int version = 0; + public String location = ""; + public int currentTemp = 0; + + private ParcelableWeather2(Parcel in) { + int version = in.readInt(); + if (version != 2) { + return; + } + Bundle bundle = in.readBundle(this.getClass().getClassLoader()); + 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"); + conditionBundle.getStringArray("weather_condition_types"); + currentTemp = conditionBundle.getInt("weather_current_temp"); + + } + } + + public static final Creator CREATOR = new Creator() { + @Override + public ParcelableWeather2 createFromParcel(Parcel in) { + return new ParcelableWeather2(in); + } + + @Override + public ParcelableWeather2[] newArray(int size) { + return new ParcelableWeather2[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + // we do not really want to use this at all + } +}