From 4de45787c3c81ed33906060b94142f7093fdbe1c Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Tue, 5 Jul 2016 23:52:48 +0200 Subject: [PATCH] Properly acquire network location if last location is not known A toast will be shown if the network location provider is disabled. Location will be automatically acquired after enabling it. Fixes #346 --- .../activities/SettingsActivity.java | 46 +++++++++++++++---- app/src/main/res/values/strings.xml | 3 ++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java index b9d1eea2e..990c90851 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -7,6 +7,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.location.Criteria; import android.location.Location; +import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.preference.EditTextPreference; @@ -143,17 +144,30 @@ public class SettingsActivity extends AbstractSettingsActivity { if (provider != null) { Location location = locationManager.getLastKnownLocation(provider); if (location != null) { - String latitude = String.format(Locale.US, "%.6g", location.getLatitude()); - String longitude = String.format(Locale.US, "%.6g", location.getLongitude()); - LOG.info("got location. Lat: " + latitude + " Lng: " + longitude); - EditTextPreference pref_latitude = (EditTextPreference) findPreference("location_latitude"); - EditTextPreference pref_longitude = (EditTextPreference) findPreference("location_longitude"); - pref_latitude.setText(latitude); - pref_longitude.setText(longitude); - pref_latitude.setSummary(latitude); - pref_longitude.setSummary(longitude); + setLocationPreferences(location); } else { - GB.toast(SettingsActivity.this, "no last known position", 3000, 0); + locationManager.requestSingleUpdate(provider, new LocationListener() { + @Override + public void onLocationChanged(Location location) { + setLocationPreferences(location); + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + LOG.info("provider status changed to " + status + " (" + provider + ")"); + } + + @Override + public void onProviderEnabled(String provider) { + LOG.info("provider enabled (" + provider + ")"); + } + + @Override + public void onProviderDisabled(String provider) { + LOG.info("provider disabled (" + provider + ")"); + GB.toast(SettingsActivity.this, getString(R.string.toast_enable_networklocationprovider), 3000, 0); + } + }, null); } } else { LOG.warn("No location provider found, did you deny location permission?"); @@ -255,4 +269,16 @@ public class SettingsActivity extends AbstractSettingsActivity { }; } + private void setLocationPreferences(Location location) { + String latitude = String.format(Locale.US, "%.6g", location.getLatitude()); + String longitude = String.format(Locale.US, "%.6g", location.getLongitude()); + LOG.info("got location. Lat: " + latitude + " Lng: " + longitude); + GB.toast(SettingsActivity.this, getString(R.string.toast_aqurired_networklocation), 2000, 0); + EditTextPreference pref_latitude = (EditTextPreference) findPreference("location_latitude"); + EditTextPreference pref_longitude = (EditTextPreference) findPreference("location_longitude"); + pref_latitude.setText(latitude); + pref_longitude.setText(longitude); + pref_latitude.setSummary(latitude); + pref_longitude.setSummary(longitude); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bb3f700d0..842f32995 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -95,6 +95,9 @@ Latitude Longitude + Please enable network location + location acquired + Force Notification Protocol This option forces using the latest notification protocol depending on the firmware version. ENABLE ONLY IF YOU KNOW WHAT YOU ARE DOING! Enable untested features