Fix first time location request

This commit is contained in:
max-klaus 2021-01-19 16:39:42 +03:00
parent a8d92d7fa0
commit 2bc2eb3792
6 changed files with 37 additions and 14 deletions

View file

@ -131,15 +131,17 @@ public class LocationServiceHelperImpl extends LocationServiceHelper {
} }
@Nullable @Nullable
public net.osmand.Location getFirstTimeRunDefaultLocation() { public net.osmand.Location getFirstTimeRunDefaultLocation(@Nullable final LocationCallback locationCallback) {
final net.osmand.Location[] location = {null}; if (locationCallback == null) {
/* return null;
}
try { try {
Task<Location> lastLocation = fusedLocationProviderClient.getLastLocation(); Task<Location> lastLocation = fusedLocationProviderClient.getLastLocation();
lastLocation.addOnSuccessListener(new OnSuccessListener<Location>() { lastLocation.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override @Override
public void onSuccess(Location loc) { public void onSuccess(Location loc) {
location[0] = convertLocation(loc); locationCallback.onLocationResult(loc != null
? Collections.singletonList(convertLocation(loc)) : Collections.<net.osmand.Location>emptyList() );
} }
}); });
} catch (SecurityException e) { } catch (SecurityException e) {
@ -147,8 +149,7 @@ public class LocationServiceHelperImpl extends LocationServiceHelper {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOG.debug("GPS location provider not available"); LOG.debug("GPS location provider not available");
} }
*/ return null;
return location[0];
} }
@Nullable @Nullable

View file

@ -125,7 +125,7 @@ public class LocationServiceHelperImpl extends LocationServiceHelper implements
} }
@Nullable @Nullable
public net.osmand.Location getFirstTimeRunDefaultLocation() { public net.osmand.Location getFirstTimeRunDefaultLocation(@Nullable LocationCallback locationCallback) {
LocationManager locationManager = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE); LocationManager locationManager = (LocationManager) app.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = new ArrayList<>(locationManager.getProviders(true)); List<String> providers = new ArrayList<>(locationManager.getProviders(true));
// note, passive provider is from API_LEVEL 8 but it is a constant, we can check for it. // note, passive provider is from API_LEVEL 8 but it is a constant, we can check for it.

View file

@ -389,8 +389,14 @@ public class OsmAndLocationProvider implements SensorEventListener {
} }
@Nullable @Nullable
public net.osmand.Location getFirstTimeRunDefaultLocation() { public net.osmand.Location getFirstTimeRunDefaultLocation(@Nullable final OsmAndLocationListener locationListener) {
return isLocationPermissionAvailable(app) ? locationServiceHelper.getFirstTimeRunDefaultLocation() : null; return isLocationPermissionAvailable(app)
? locationServiceHelper.getFirstTimeRunDefaultLocation(locationListener != null ? new LocationServiceHelper.LocationCallback() {
@Override
public void onLocationResult(@NonNull List<net.osmand.Location> locations) {
locationListener.updateLocation(locations.isEmpty() ? null : locations.get(0));
}
} : null) : null;
} }
public synchronized void registerOrUnregisterCompassListener(boolean register) { public synchronized void registerOrUnregisterCompassListener(boolean register) {

View file

@ -70,6 +70,8 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OnDismissDialogFragmentListener; import net.osmand.plus.OnDismissDialogFragmentListener;
import net.osmand.plus.OsmAndConstants; 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.OsmAndLocationSimulation;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
@ -347,11 +349,17 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
if (!settings.isLastKnownMapLocation()) { if (!settings.isLastKnownMapLocation()) {
// show first time when application ran // 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); mapViewTrackingUtilities.setMapLinkedToLocation(true);
if (location != null) { if (location != null) {
mapView.setLatLon(location.getLatitude(), location.getLongitude()); setMapInitialLatLon(location);
mapView.setIntZoom(14);
} }
} }
addDialogProvider(mapActions); addDialogProvider(mapActions);
@ -376,6 +384,13 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
mIsDestroyed = false; 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) { public void exitFromFullScreen(View view) {
AndroidUtils.exitFromFullScreen(this, view); AndroidUtils.exitFromFullScreen(this, view);
} }

View file

@ -110,7 +110,7 @@ public class DayNightHelper implements SensorEventListener {
public SunriseSunset getSunriseSunset() { public SunriseSunset getSunriseSunset() {
Location lastKnownLocation = app.getLocationProvider().getLastKnownLocation(); Location lastKnownLocation = app.getLocationProvider().getLastKnownLocation();
if (lastKnownLocation == null) { if (lastKnownLocation == null) {
lastKnownLocation = app.getLocationProvider().getFirstTimeRunDefaultLocation(); lastKnownLocation = app.getLocationProvider().getFirstTimeRunDefaultLocation(null);
} }
if (lastKnownLocation == null) { if (lastKnownLocation == null) {
return null; return null;

View file

@ -1,6 +1,7 @@
package net.osmand.plus.helpers; package net.osmand.plus.helpers;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.Location; import net.osmand.Location;
@ -25,5 +26,5 @@ public abstract class LocationServiceHelper {
public abstract void removeLocationUpdates(); public abstract void removeLocationUpdates();
public abstract Location getFirstTimeRunDefaultLocation(); public abstract Location getFirstTimeRunDefaultLocation(@Nullable LocationCallback locationCallback);
} }