From d10745b0b3e8b1720328438cb430be3956b201e9 Mon Sep 17 00:00:00 2001 From: Arjan Schrijver Date: Sun, 27 Aug 2023 20:48:40 +0200 Subject: [PATCH] Add getLocation method to WeatherSpec and use it for Zepp OS and Fossil/Skagen --- .../gadgetbridge/model/WeatherSpec.java | 14 ++++++++++++++ .../service/devices/huami/Huami2021Weather.java | 7 ++----- .../adapter/fossil_hr/FossilHRWatchAdapter.java | 15 +++++++++++---- 3 files changed, 27 insertions(+), 9 deletions(-) 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 081f916d2..5c8a4368e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java @@ -18,9 +18,12 @@ package nodomain.freeyourgadget.gadgetbridge.model; +import android.location.Location; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.Nullable; + import java.io.Serializable; import java.util.ArrayList; @@ -95,6 +98,17 @@ public class WeatherSpec implements Parcelable, Serializable { return toBeaufort(this.windSpeed); } + @Nullable + public Location getLocation() { + if (latitude == 0 && longitude == 0) { + return null; + } + final Location location = new Location("weatherSpec"); + location.setLatitude(latitude); + location.setLatitude(longitude); + return location; + } + protected WeatherSpec(Parcel in) { int version = in.readInt(); if (version >= 2) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java index a27c4a9a0..b143e8bcd 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Weather.java @@ -202,11 +202,8 @@ public class Huami2021Weather { weather.add(new Range(currentWeatherCode, currentWeatherCode)); if (weatherSpec.sunRise != 0 && weatherSpec.sunSet != 0) { sunRiseSet.add(getSunriseSunset(new Date(weatherSpec.sunRise * 1000L), new Date(weatherSpec.sunSet * 1000L))); - } else if (weatherSpec.latitude != 0 && weatherSpec.longitude != 0) { - final Location weatherSpecLocation = new Location("weatherSpec"); - weatherSpecLocation.setLatitude(weatherSpec.latitude); - weatherSpecLocation.setLatitude(weatherSpec.longitude); - sunRiseSet.add(getSunriseSunset(sunriseDate, weatherSpecLocation)); + } else if (weatherSpec.getLocation() != null) { + sunRiseSet.add(getSunriseSunset(sunriseDate, weatherSpec.getLocation())); } else { sunRiseSet.add(getSunriseSunset(sunriseDate, lastKnownLocation)); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java index 173103820..bdeb76578 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java @@ -1443,10 +1443,17 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { @Override public void onSendWeather(WeatherSpec weatherSpec) { - // TODO: We should send sunrise on the same location as the weather - final Location lastKnownLocation = new CurrentPosition().getLastKnownLocation(); - GregorianCalendar[] sunrise = SPA.calculateSunriseTransitSet(new GregorianCalendar(), lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude(), DeltaT.estimate(new GregorianCalendar())); - Boolean isNight = sunrise[0].getTimeInMillis() > System.currentTimeMillis() || sunrise[2].getTimeInMillis() < System.currentTimeMillis(); + boolean isNight; + if (weatherSpec.sunRise != 0 && weatherSpec.sunSet != 0) { + isNight = weatherSpec.sunRise * 1000L > System.currentTimeMillis() || weatherSpec.sunSet * 1000L < System.currentTimeMillis(); + } else { + Location location = weatherSpec.getLocation(); + if (location == null) { + location = new CurrentPosition().getLastKnownLocation(); + } + GregorianCalendar[] sunrise = SPA.calculateSunriseTransitSet(new GregorianCalendar(), location.getLatitude(), location.getLongitude(), DeltaT.estimate(new GregorianCalendar())); + isNight = sunrise[0].getTimeInMillis() > System.currentTimeMillis() || sunrise[2].getTimeInMillis() < System.currentTimeMillis(); + } long ts = System.currentTimeMillis(); ts /= 1000;