From 16c4f1a5cadcd2726e0a5768caefeea8f904316c Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 6 Nov 2016 15:42:52 +0100 Subject: [PATCH] Pebble: add toggle to use last known location for sunrise and sunset This adds the feature discussed in #415, the used location is the last recorded by the network location provider, if it's not available then the stored location is used. --- CHANGELOG.md | 3 +++ .../externalevents/AlarmReceiver.java | 21 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/changelog_master.xml | 3 +++ app/src/main/res/xml/preferences.xml | 6 ++++++ 5 files changed, 35 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2023361fc..80cb16a8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ###Changelog +####Version next +* Pebble: use the last known location for setting sunrise and sunset + ####Version 0.13.8 * Mi Band2: attempt at fixing connection issues for users of Mi Fit (#408, #425) 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 e9470d31a..9c4a581ac 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/AlarmReceiver.java @@ -1,11 +1,17 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents; +import android.Manifest; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.location.Criteria; +import android.location.Location; +import android.location.LocationManager; +import android.support.v4.app.ActivityCompat; import net.e175.klaus.solarpositioning.DeltaT; import net.e175.klaus.solarpositioning.SPA; @@ -64,6 +70,21 @@ public class AlarmReceiver extends BroadcastReceiver { float latitude = prefs.getFloat("location_latitude", 0); float longitude = prefs.getFloat("location_longitude", 0); LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude); + + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && + prefs.getBoolean("use_updated_location_if_available", false)) { + LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + Criteria criteria = new Criteria(); + String provider = locationManager.getBestProvider(criteria, false); + if (provider != null) { + Location lastKnownLocation = locationManager.getLastKnownLocation(provider); + if (lastKnownLocation != null) { + latitude = (float) lastKnownLocation.getLatitude(); + longitude = (float) lastKnownLocation.getLongitude(); + LOG.info("got longitude/latitude from last known location: " + latitude + "/" + longitude); + } + } + } GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeTomorrow)); CalendarEventSpec calendarEventSpec = new CalendarEventSpec(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62a5c8b2c..00ece7d21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -106,6 +106,8 @@ Acquire Location Latitude Longitude + Keep location updated + Try to get the current location at runtime, use the stored location as fallback Please enable network location location acquired diff --git a/app/src/main/res/xml/changelog_master.xml b/app/src/main/res/xml/changelog_master.xml index 28a8722f0..158ab7428 100644 --- a/app/src/main/res/xml/changelog_master.xml +++ b/app/src/main/res/xml/changelog_master.xml @@ -1,5 +1,8 @@ + + Pebble: use the last known location for setting sunrise and sunset + Mi Band 2: attempt at fixing connection issues for users of Mi Fit diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 663ca61f1..483f6deb6 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -206,6 +206,12 @@ android:defaultValue="0" android:key="location_longitude" android:title="@string/pref_title_location_longitude" /> +