From 2bc2eb3792288f6a4baa132dae00278451eb3521 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Tue, 19 Jan 2021 16:39:42 +0300 Subject: [PATCH] Fix first time location request --- .../plus/LocationServiceHelperImpl.java | 13 ++++++------ .../plus/LocationServiceHelperImpl.java | 2 +- .../osmand/plus/OsmAndLocationProvider.java | 10 +++++++-- .../osmand/plus/activities/MapActivity.java | 21 ++++++++++++++++--- .../osmand/plus/helpers/DayNightHelper.java | 2 +- .../plus/helpers/LocationServiceHelper.java | 3 ++- 6 files changed, 37 insertions(+), 14 deletions(-) diff --git a/OsmAnd/src-gms/net/osmand/plus/LocationServiceHelperImpl.java b/OsmAnd/src-gms/net/osmand/plus/LocationServiceHelperImpl.java index e8182955ec..41c74012a3 100644 --- a/OsmAnd/src-gms/net/osmand/plus/LocationServiceHelperImpl.java +++ b/OsmAnd/src-gms/net/osmand/plus/LocationServiceHelperImpl.java @@ -131,15 +131,17 @@ public class LocationServiceHelperImpl extends LocationServiceHelper { } @Nullable - public net.osmand.Location getFirstTimeRunDefaultLocation() { - final net.osmand.Location[] location = {null}; - /* + public net.osmand.Location getFirstTimeRunDefaultLocation(@Nullable final LocationCallback locationCallback) { + if (locationCallback == null) { + return null; + } try { Task lastLocation = fusedLocationProviderClient.getLastLocation(); lastLocation.addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Location loc) { - location[0] = convertLocation(loc); + locationCallback.onLocationResult(loc != null + ? Collections.singletonList(convertLocation(loc)) : Collections.emptyList() ); } }); } catch (SecurityException e) { @@ -147,8 +149,7 @@ public class LocationServiceHelperImpl extends LocationServiceHelper { } catch (IllegalArgumentException e) { LOG.debug("GPS location provider not available"); } - */ - return location[0]; + return null; } @Nullable diff --git a/OsmAnd/src-nogms/net/osmand/plus/LocationServiceHelperImpl.java b/OsmAnd/src-nogms/net/osmand/plus/LocationServiceHelperImpl.java index 5f49607954..9a2caa6a5f 100644 --- a/OsmAnd/src-nogms/net/osmand/plus/LocationServiceHelperImpl.java +++ b/OsmAnd/src-nogms/net/osmand/plus/LocationServiceHelperImpl.java @@ -125,7 +125,7 @@ public class LocationServiceHelperImpl extends LocationServiceHelper implements } @Nullable - public net.osmand.Location getFirstTimeRunDefaultLocation() { + public net.osmand.Location getFirstTimeRunDefaultLocation(@Nullable LocationCallback locationCallback) { LocationManager locationManager = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE); List providers = new ArrayList<>(locationManager.getProviders(true)); // note, passive provider is from API_LEVEL 8 but it is a constant, we can check for it. diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index ee875fbb17..15936ded34 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -389,8 +389,14 @@ public class OsmAndLocationProvider implements SensorEventListener { } @Nullable - public net.osmand.Location getFirstTimeRunDefaultLocation() { - return isLocationPermissionAvailable(app) ? locationServiceHelper.getFirstTimeRunDefaultLocation() : null; + public net.osmand.Location getFirstTimeRunDefaultLocation(@Nullable final OsmAndLocationListener locationListener) { + return isLocationPermissionAvailable(app) + ? locationServiceHelper.getFirstTimeRunDefaultLocation(locationListener != null ? new LocationServiceHelper.LocationCallback() { + @Override + public void onLocationResult(@NonNull List locations) { + locationListener.updateLocation(locations.isEmpty() ? null : locations.get(0)); + } + } : null) : null; } public synchronized void registerOrUnregisterCompassListener(boolean register) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 007280144e..a94997caaa 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -70,6 +70,8 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OnDismissDialogFragmentListener; import net.osmand.plus.OsmAndConstants; +import net.osmand.plus.OsmAndLocationProvider; +import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmAndLocationSimulation; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -347,11 +349,17 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (!settings.isLastKnownMapLocation()) { // show first time when application ran - net.osmand.Location location = app.getLocationProvider().getFirstTimeRunDefaultLocation(); + net.osmand.Location location = app.getLocationProvider().getFirstTimeRunDefaultLocation(new OsmAndLocationListener() { + @Override + public void updateLocation(Location location) { + if (app.getLocationProvider().getLastKnownLocation() == null) { + setMapInitialLatLon(location); + } + } + }); mapViewTrackingUtilities.setMapLinkedToLocation(true); if (location != null) { - mapView.setLatLon(location.getLatitude(), location.getLongitude()); - mapView.setIntZoom(14); + setMapInitialLatLon(location); } } addDialogProvider(mapActions); @@ -376,6 +384,13 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven mIsDestroyed = false; } + private void setMapInitialLatLon(@Nullable Location location) { + if (location != null) { + mapView.setLatLon(location.getLatitude(), location.getLongitude()); + mapView.setIntZoom(14); + } + } + public void exitFromFullScreen(View view) { AndroidUtils.exitFromFullScreen(this, view); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/DayNightHelper.java b/OsmAnd/src/net/osmand/plus/helpers/DayNightHelper.java index 20820d379e..0721350197 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/DayNightHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/DayNightHelper.java @@ -110,7 +110,7 @@ public class DayNightHelper implements SensorEventListener { public SunriseSunset getSunriseSunset() { Location lastKnownLocation = app.getLocationProvider().getLastKnownLocation(); if (lastKnownLocation == null) { - lastKnownLocation = app.getLocationProvider().getFirstTimeRunDefaultLocation(); + lastKnownLocation = app.getLocationProvider().getFirstTimeRunDefaultLocation(null); } if (lastKnownLocation == null) { return null; diff --git a/OsmAnd/src/net/osmand/plus/helpers/LocationServiceHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LocationServiceHelper.java index 94602e98fc..472dcf3b7f 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LocationServiceHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LocationServiceHelper.java @@ -1,6 +1,7 @@ package net.osmand.plus.helpers; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import net.osmand.Location; @@ -25,5 +26,5 @@ public abstract class LocationServiceHelper { public abstract void removeLocationUpdates(); - public abstract Location getFirstTimeRunDefaultLocation(); + public abstract Location getFirstTimeRunDefaultLocation(@Nullable LocationCallback locationCallback); }