From 879272deb7956ff4e3ba3aa68f86cae7dcd79233 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Wed, 29 Nov 2017 23:57:36 +0100 Subject: [PATCH] Weather: refactoring and support forecast for more than one day --- .../externalevents/AlarmReceiver.java | 7 +- .../WeatherNotificationReceiver.java | 6 +- .../gadgetbridge/impl/GBDeviceService.java | 11 +-- .../gadgetbridge/model/DeviceService.java | 11 +-- .../gadgetbridge/model/WeatherSpec.java | 98 ++++++++++++++++++- .../service/DeviceCommunicationService.java | 47 +++++---- .../devices/amazfitbip/AmazfitBipSupport.java | 19 ++-- .../AppMessageHandlerTimeStylePebble.java | 7 +- .../devices/pebble/PebbleProtocol.java | 14 ++- .../pebble/webview/CurrentPosition.java | 24 ++++- .../devices/pebble/webview/JSInterface.java | 4 +- .../notification/ParcelableWeather2.java | 90 ++++++----------- 12 files changed, 209 insertions(+), 129 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java index 3b02d63a3..6a5823beb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java @@ -54,7 +54,12 @@ public class AlarmReceiver extends BroadcastReceiver { PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent("DAILY_ALARM"), 0); AlarmManager am = (AlarmManager) (context.getSystemService(Context.ALARM_SERVICE)); - am.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 10000, AlarmManager.INTERVAL_DAY, pendingIntent); + if (am != null) { + am.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 10000, AlarmManager.INTERVAL_DAY, pendingIntent); + } + else { + LOG.warn("could not get alarm manager!"); + } } @Override 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 809ebe0f5..8a02294df 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java @@ -35,7 +35,7 @@ public class WeatherNotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (!intent.getAction().contains("WEATHER_UPDATE_2")) { + if (intent.getAction() == null || !intent.getAction().contains("WEATHER_UPDATE_2")) { LOG.info("Wrong action"); return; } @@ -58,9 +58,7 @@ public class WeatherNotificationReceiver extends BroadcastReceiver { weatherSpec.currentConditionCode = weather.currentConditionCode; weatherSpec.todayMaxTemp = weather.todayHighTemp; weatherSpec.todayMinTemp = weather.todayLowTemp; - weatherSpec.tomorrowConditionCode = weather.forecastConditionCode; - weatherSpec.tomorrowMaxTemp = weather.forecastHighTemp; - weatherSpec.tomorrowMinTemp = weather.forecastLowTemp; + weatherSpec.forecasts = weather.forecasts; Weather.getInstance().setWeatherSpec(weatherSpec); GBApplication.deviceService().onSendWeather(weatherSpec); } 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 f7c2e09a0..9e9210ccc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -373,16 +373,7 @@ public class GBDeviceService implements DeviceService { @Override public void onSendWeather(WeatherSpec weatherSpec) { Intent intent = createIntent().setAction(ACTION_SEND_WEATHER) - .putExtra(EXTRA_WEATHER_TIMESTAMP, weatherSpec.timestamp) - .putExtra(EXTRA_WEATHER_LOCATION, weatherSpec.location) - .putExtra(EXTRA_WEATHER_CURRENTTEMP, weatherSpec.currentTemp) - .putExtra(EXTRA_WEATHER_CURRENTCONDITIONCODE, weatherSpec.currentConditionCode) - .putExtra(EXTRA_WEATHER_CURRENTCONDITION, weatherSpec.currentCondition) - .putExtra(EXTRA_WEATHER_TODAYMAXTEMP, weatherSpec.todayMaxTemp) - .putExtra(EXTRA_WEATHER_TODAYMINTEMP, weatherSpec.todayMinTemp) - .putExtra(EXTRA_WEATHER_TOMORROWMAXTEMP, weatherSpec.tomorrowMaxTemp) - .putExtra(EXTRA_WEATHER_TOMORROWMINTEMP, weatherSpec.tomorrowMinTemp) - .putExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, weatherSpec.tomorrowConditionCode); + .putExtra(EXTRA_WEATHER, weatherSpec); invokeService(intent); } 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 f9e9575b1..1e68558c5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -104,16 +104,7 @@ public interface DeviceService extends EventHandler { String EXTRA_BOOLEAN_ENABLE = "enable_realtime_steps"; String EXTRA_INTERVAL_SECONDS = "interval_seconds"; - String EXTRA_WEATHER_TIMESTAMP = "weather_timestamp"; - String EXTRA_WEATHER_LOCATION = "weather_location"; - String EXTRA_WEATHER_CURRENTTEMP = "weather_currenttemp"; - String EXTRA_WEATHER_CURRENTCONDITIONCODE = "weather_currentconditioncode"; - String EXTRA_WEATHER_CURRENTCONDITION = "currentcondition"; - String EXTRA_WEATHER_TODAYMAXTEMP = "weather_todaymaxtemp"; - String EXTRA_WEATHER_TODAYMINTEMP = "weather_todaymintemp"; - String EXTRA_WEATHER_TOMORROWMAXTEMP = "weather_tomorrowmaxtemp"; - String EXTRA_WEATHER_TOMORROWMINTEMP = "weather_tomorrowmintemp"; - String EXTRA_WEATHER_TOMORROWCONDITIONCODE = "weather_tomorrowconditioncode"; + String EXTRA_WEATHER = "weather"; /** * Use EXTRA_REALTIME_SAMPLE instead diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java index a728afa05..267c5c98a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java @@ -16,8 +16,24 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.model; +import android.os.Parcel; +import android.os.Parcelable; + +import java.util.ArrayList; + // FIXME: document me and my fields, including units -public class WeatherSpec { +public class WeatherSpec implements Parcelable { + public static final Creator CREATOR = new Creator() { + @Override + public WeatherSpec createFromParcel(Parcel in) { + return new WeatherSpec(in); + } + + @Override + public WeatherSpec[] newArray(int size) { + return new WeatherSpec[size]; + } + }; public int timestamp; public String location; public int currentTemp; @@ -25,7 +41,81 @@ public class WeatherSpec { public String currentCondition; public int todayMaxTemp; public int todayMinTemp; - public int tomorrowMaxTemp; - public int tomorrowMinTemp; - public int tomorrowConditionCode; + public ArrayList forecasts = new ArrayList<>(); + + public WeatherSpec() { + + } + + protected WeatherSpec(Parcel in) { + timestamp = in.readInt(); + location = in.readString(); + currentTemp = in.readInt(); + currentConditionCode = in.readInt(); + currentCondition = in.readString(); + todayMaxTemp = in.readInt(); + todayMinTemp = in.readInt(); + in.readList(forecasts, Forecast.class.getClassLoader()); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(timestamp); + dest.writeString(location); + dest.writeInt(currentTemp); + dest.writeInt(currentConditionCode); + dest.writeString(currentCondition); + dest.writeInt(todayMaxTemp); + dest.writeInt(todayMinTemp); + dest.writeList(forecasts); + } + + public static class Forecast implements Parcelable { + public static final Creator CREATOR = new Creator() { + @Override + public Forecast createFromParcel(Parcel in) { + return new Forecast(in); + } + + @Override + public Forecast[] newArray(int size) { + return new Forecast[size]; + } + }; + public int minTemp; + public int maxTemp; + public int conditionCode; + + public Forecast() { + } + + public Forecast(int minTemp, int maxTemp, int conditionCode) { + this.minTemp = minTemp; + this.maxTemp = maxTemp; + this.conditionCode = conditionCode; + } + + Forecast(Parcel in) { + minTemp = in.readInt(); + maxTemp = in.readInt(); + conditionCode = in.readInt(); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(minTemp); + dest.writeInt(maxTemp); + dest.writeInt(conditionCode); + } + } } 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 1276b1a29..f50ce7723 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -50,6 +50,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmClockReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothPairingRequestReceiver; +//import nodomain.freeyourgadget.gadgetbridge.externalevents.CMWeatherReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver; @@ -149,16 +150,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOT import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_URI; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_VIBRATION_INTENSITY; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITION; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITIONCODE; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTTEMP; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_LOCATION; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TIMESTAMP; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TODAYMAXTEMP; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TODAYMINTEMP; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWCONDITIONCODE; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWMAXTEMP; -import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWMINTEMP; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER; public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener { private static final Logger LOG = LoggerFactory.getLogger(DeviceCommunicationService.class); @@ -182,6 +174,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere private AlarmReceiver mAlarmReceiver = null; private CalendarReceiver mCalendarReceiver = null; + //private CMWeatherReceiver mCMWeatherReceiver = null; private Random mRandom = new Random(); private final String[] mMusicActions = { @@ -214,7 +207,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) { + if (GBDevice.ACTION_DEVICE_CHANGED.equals(action)) { GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); if (mGBDevice != null && mGBDevice.equals(device)) { mGBDevice = device; @@ -540,18 +533,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere break; } case ACTION_SEND_WEATHER: { - WeatherSpec weatherSpec = new WeatherSpec(); - weatherSpec.timestamp = intent.getIntExtra(EXTRA_WEATHER_TIMESTAMP, 0); - weatherSpec.location = intent.getStringExtra(EXTRA_WEATHER_LOCATION); - weatherSpec.currentTemp = intent.getIntExtra(EXTRA_WEATHER_CURRENTTEMP, 0); - weatherSpec.currentConditionCode = intent.getIntExtra(EXTRA_WEATHER_CURRENTCONDITIONCODE, 0); - weatherSpec.currentCondition = intent.getStringExtra(EXTRA_WEATHER_CURRENTCONDITION); - weatherSpec.todayMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMAXTEMP, 0); - weatherSpec.todayMinTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMINTEMP, 0); - weatherSpec.tomorrowMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMAXTEMP, 0); - weatherSpec.tomorrowMinTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMINTEMP, 0); - weatherSpec.tomorrowConditionCode = intent.getIntExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, 0); - mDeviceSupport.onSendWeather(weatherSpec); + WeatherSpec weatherSpec = intent.getParcelableExtra(EXTRA_WEATHER); + if (weatherSpec != null) { + mDeviceSupport.onSendWeather(weatherSpec); + } break; } } @@ -674,6 +659,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere filter.addAction(AlarmClockReceiver.ALARM_DONE_ACTION); registerReceiver(mAlarmClockReceiver, filter); } + /* + if (mCMWeatherReceiver == null) { + mCMWeatherReceiver = new CMWeatherReceiver(); + registerReceiver(mCMWeatherReceiver, new IntentFilter("HOURLY_ALARM")); + } + */ } else { if (mPhoneCallReceiver != null) { unregisterReceiver(mPhoneCallReceiver); @@ -708,6 +699,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere unregisterReceiver(mAlarmClockReceiver); mAlarmClockReceiver = null; } + /* + if (mCMWeatherReceiver != null) { + unregisterReceiver(mCMWeatherReceiver); + mCMWeatherReceiver = null; + } + */ } } @@ -725,7 +722,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere setDeviceSupport(null); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops + if (nm != null) { + nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops + } } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java index 2fda8ca7a..0fe9486e3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java @@ -137,7 +137,7 @@ public class AmazfitBipSupport extends MiBand2Support { supportsConditionString = true; } - final byte NR_DAYS = 2; + final byte NR_DAYS = (byte) (1 + weatherSpec.forecasts.size()); int bytesPerDay = 4; int conditionsLength = 0; if (supportsConditionString) { @@ -164,14 +164,17 @@ public class AmazfitBipSupport extends MiBand2Support { buf.put(weatherSpec.currentCondition.getBytes()); buf.put((byte) 0); // } - condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(weatherSpec.tomorrowConditionCode); - buf.put(condition); - buf.put(condition); - buf.put((byte) (weatherSpec.tomorrowMaxTemp - 273)); - buf.put((byte) (weatherSpec.tomorrowMinTemp - 273)); - if (supportsConditionString) { - buf.put((byte) 0); // not yet in weatherspec + for (WeatherSpec.Forecast forecast : weatherSpec.forecasts) { + condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(forecast.conditionCode); + + buf.put(condition); + buf.put(condition); + buf.put((byte) (forecast.maxTemp - 273)); + buf.put((byte) (forecast.minTemp - 273)); + if (supportsConditionString) { + buf.put((byte) 0); // not yet in weatherspec + } } builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array()); 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 243081a6e..b31241ab4 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 @@ -141,7 +141,12 @@ class AppMessageHandlerTimeStylePebble extends AppMessageHandler { pairs.add(new Pair<>(messageKeys.get("WeatherUseNightIcon"), (Object) (isNight ? 1 : 0))); pairs.add(new Pair<>(messageKeys.get("WeatherTemperature"), (Object) (weatherSpec.currentTemp - 273))); pairs.add(new Pair<>(messageKeys.get("WeatherCondition"), (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight)))); - pairs.add(new Pair<>(messageKeys.get("WeatherForecastCondition"), (Object) (getIconForConditionCode(weatherSpec.tomorrowConditionCode, isNight)))); + + if (weatherSpec.forecasts.size() > 0) { + WeatherSpec.Forecast tomorrow = weatherSpec.forecasts.get(0); + pairs.add(new Pair<>(messageKeys.get("WeatherForecastCondition"), (Object) (getIconForConditionCode(tomorrow.conditionCode, isNight)))); + } + pairs.add(new Pair<>(messageKeys.get("WeatherForecastHighTemp"), (Object) (weatherSpec.todayMaxTemp - 273))); pairs.add(new Pair<>(messageKeys.get("WeatherForecastLowTemp"), (Object) (weatherSpec.todayMinTemp - 273))); 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 e9d6a21dd..c5c7750e2 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 @@ -1227,8 +1227,16 @@ public class PebbleProtocol extends GBDeviceProtocol { short currentTemp = (short) (weatherSpec.currentTemp - 273); short todayMax = (short) (weatherSpec.todayMaxTemp - 273); short todayMin = (short) (weatherSpec.todayMinTemp - 273); - short tomorrowMax = (short) (weatherSpec.tomorrowMaxTemp - 273); - short tomorrowMin = (short) (weatherSpec.tomorrowMinTemp - 273); + short tomorrowMax = 0; + short tomorrowMin = 0; + int tomorrowConditionCode = 0; + if (weatherSpec.forecasts.size() > 0) { + WeatherSpec.Forecast tomorrow = weatherSpec.forecasts.get(0); + tomorrowMax = (short) (tomorrow.maxTemp - 273); + tomorrowMin = (short) (tomorrow.minTemp - 273); + tomorrowConditionCode = tomorrow.conditionCode; + } + String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric)); if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) { currentTemp = (short) (currentTemp * 1.8f + 32); @@ -1261,7 +1269,7 @@ public class PebbleProtocol extends GBDeviceProtocol { buf.put(Weather.mapToPebbleCondition(weatherSpec.currentConditionCode)); buf.putShort(todayMax); buf.putShort(todayMin); - buf.put(Weather.mapToPebbleCondition(weatherSpec.tomorrowConditionCode)); + buf.put(Weather.mapToPebbleCondition(tomorrowConditionCode)); buf.putShort(tomorrowMax); buf.putShort(tomorrowMin); buf.putInt(weatherSpec.timestamp); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/CurrentPosition.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/CurrentPosition.java index d1e961dca..89efb96d4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/CurrentPosition.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/CurrentPosition.java @@ -30,13 +30,16 @@ import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; -class CurrentPosition { +public class CurrentPosition { private static final Logger LOG = LoggerFactory.getLogger(CurrentPosition.class); + private Location lastKnownLocation; + private float latitude, longitude; + long timestamp; double altitude; - float latitude, longitude, accuracy, speed; + float accuracy, speed; float getLatitude() { return latitude; @@ -46,10 +49,19 @@ class CurrentPosition { return longitude; } - CurrentPosition() { + public Location getLastKnownLocation() { + return lastKnownLocation; + } + + public CurrentPosition() { Prefs prefs = GBApplication.getPrefs(); this.latitude = prefs.getFloat("location_latitude", 0); this.longitude = prefs.getFloat("location_longitude", 0); + + lastKnownLocation = new Location("preferences"); + lastKnownLocation.setLatitude(this.latitude); + lastKnownLocation.setLongitude(this.longitude); + LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude); this.timestamp = System.currentTimeMillis() - 86400000; //let accessor know this value is really old @@ -58,10 +70,14 @@ class CurrentPosition { prefs.getBoolean("use_updated_location_if_available", false)) { LocationManager locationManager = (LocationManager) GBApplication.getContext().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); - String provider = locationManager.getBestProvider(criteria, false); + String provider = null; + if (locationManager != null) { + provider = locationManager.getBestProvider(criteria, false); + } if (provider != null) { Location lastKnownLocation = locationManager.getLastKnownLocation(provider); if (lastKnownLocation != null) { + this.lastKnownLocation = lastKnownLocation; this.timestamp = lastKnownLocation.getTime(); this.timestamp = System.currentTimeMillis() - 1000; //TODO: request updating the location and don't fake its age diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java index 6e972e5a8..7ba1b4fc9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/JSInterface.java @@ -227,8 +227,8 @@ public class JSInterface { geoPosition.put("timestamp", currentPosition.timestamp); - coords.put("latitude", currentPosition.latitude); - coords.put("longitude", currentPosition.longitude); + coords.put("latitude", currentPosition.getLatitude()); + coords.put("longitude", currentPosition.getLongitude()); coords.put("accuracy", currentPosition.accuracy); coords.put("altitude", currentPosition.altitude); coords.put("speed", currentPosition.speed); 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 70870a08d..a44ac0757 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 @@ -26,7 +26,10 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; + import nodomain.freeyourgadget.gadgetbridge.model.Weather; +import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; public class ParcelableWeather2 implements Parcelable { private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class); @@ -42,12 +45,9 @@ public class ParcelableWeather2 implements Parcelable { private String[] currentConditionType = null; public int currentConditionCode = 3200; - private String[] forecastConditionType = null; - public int forecastConditionCode = 3200; public int todayLowTemp = 0; public int todayHighTemp = 0; - public int forecastLowTemp = 0; - public int forecastHighTemp = 0; + public ArrayList forecasts = new ArrayList<>(); public JSONObject reconstructedWeather = null; public JSONObject reconstructedForecast = null; @@ -98,21 +98,23 @@ public class ParcelableWeather2 implements Parcelable { e.printStackTrace(); } LOG.debug("Weather JSON for WEBVIEW: " + reconstructedWeather.toString()); - //fetch immediate next forecast - if (--conditions > 0) { - int timeOffset = 86400000; //manually determined - reconstructedForecast = new JSONObject(); - JSONArray list = new JSONArray(); - JSONObject city = new JSONObject(); + //fetch forecasts + int timeOffset = 0; + + JSONArray list = new JSONArray(); + JSONObject city = new JSONObject(); + while (--conditions > 0) { + timeOffset += 86400000; //manually determined JSONObject item = new JSONObject(); condition = new JSONObject(); main = new JSONObject(); weather = new JSONArray(); Bundle forecastBundle = in.readBundle(); - forecastConditionType = forecastBundle.getStringArray("weather_condition_types"); - forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]); - forecastLowTemp = forecastBundle.getInt("weather_low_temp"); - forecastHighTemp = forecastBundle.getInt("weather_high_temp"); + String[] forecastConditionType = forecastBundle.getStringArray("weather_condition_types"); + int forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]); + int forecastLowTemp = forecastBundle.getInt("weather_low_temp"); + int forecastHighTemp = forecastBundle.getInt("weather_high_temp"); + forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode)); try { condition.put("id", forecastConditionCode); condition.put("main", forecastBundle.getString("weather_condition_text")); @@ -125,58 +127,29 @@ public class ParcelableWeather2 implements Parcelable { main.put("temp_max", forecastHighTemp); //forecast - //"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"} - city.put("name", location); - city.put("country", "World"); - reconstructedForecast.put("city", city); item.put("dt", (time / 1000) + timeOffset); item.put("main", main); item.put("weather", weather); list.put(item); } catch (JSONException e) { - e.printStackTrace(); + LOG.error("error while construction JSON", e); } - - // get the rest - while (--conditions > 0) { - conditionBundle = in.readBundle(); - conditionBundle.getString("weather_condition_text"); - weatherConditionTypesToOpenWeatherMapIds(conditionBundle.getStringArray("weather_condition_types")[0]); - conditionBundle.getInt("weather_current_temp"); - item = new JSONObject(); - condition = new JSONObject(); - main = new JSONObject(); - weather = new JSONArray(); - timeOffset += 86400000; - try { - condition.put("id", weatherConditionTypesToOpenWeatherMapIds(conditionBundle.getStringArray("weather_condition_types")[0])); - condition.put("main", conditionBundle.getString("weather_condition_text")); - condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherConditionTypesToOpenWeatherMapIds(conditionBundle.getStringArray("weather_condition_types")[0]))); - weather.put(condition); - - main.put("temp", conditionBundle.getInt("weather_current_temp")); - main.put("humidity", conditionBundle.getInt("weather_humidity_value")); - main.put("temp_min", conditionBundle.getInt("weather_low_temp")); - main.put("temp_max", conditionBundle.getInt("weather_high_temp")); - - item.put("dt", (time / 1000) + timeOffset); - item.put("main", main); - item.put("weather", weather); - list.put(item); - } catch (JSONException e) { - e.printStackTrace(); - } - } - try { - reconstructedForecast.put("cnt", list.length()); - reconstructedForecast.put("list", list); - - } catch (JSONException e) { - e.printStackTrace(); - } - LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString()); } + try { + //"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"} + city.put("name", location); + city.put("country", "World"); + + reconstructedForecast = new JSONObject(); + reconstructedForecast.put("city", city); + reconstructedForecast.put("cnt", list.length()); + reconstructedForecast.put("list", list); + + } catch (JSONException e) { + LOG.error("error while construction JSON", e); + } + LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString()); } } @@ -309,4 +282,5 @@ public class ParcelableWeather2 implements Parcelable { } return 3200; } + }