Fix first time location request
This commit is contained in:
parent
a8d92d7fa0
commit
2bc2eb3792
6 changed files with 37 additions and 14 deletions
|
@ -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<Location> lastLocation = fusedLocationProviderClient.getLastLocation();
|
||||
lastLocation.addOnSuccessListener(new OnSuccessListener<Location>() {
|
||||
@Override
|
||||
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) {
|
||||
|
@ -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
|
||||
|
|
|
@ -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<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.
|
||||
|
|
|
@ -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<net.osmand.Location> locations) {
|
||||
locationListener.updateLocation(locations.isEmpty() ? null : locations.get(0));
|
||||
}
|
||||
} : null) : null;
|
||||
}
|
||||
|
||||
public synchronized void registerOrUnregisterCompassListener(boolean register) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue