mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Fixes #4211 "Location not available" while fetching location updates
This commit is contained in:
parent
7cb5ff9167
commit
850d9411a5
2 changed files with 47 additions and 5 deletions
|
|
@ -1,11 +1,16 @@
|
||||||
package fr.free.nrw.commons.location;
|
package fr.free.nrw.commons.location;
|
||||||
|
|
||||||
|
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
|
||||||
|
import android.Manifest.permission;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
@ -37,11 +42,42 @@ public class LocationServiceManager implements LocationListener {
|
||||||
|
|
||||||
public LatLng getLastLocation() {
|
public LatLng getLastLocation() {
|
||||||
if (lastLocation == null) {
|
if (lastLocation == null) {
|
||||||
return null;
|
lastLocation = getLastKnownLocation();
|
||||||
|
if(lastLocation != null) {
|
||||||
|
return LatLng.from(lastLocation);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return LatLng.from(lastLocation);
|
return LatLng.from(lastLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Location getLastKnownLocation() {
|
||||||
|
List<String> providers = locationManager.getProviders(true);
|
||||||
|
Location bestLocation = null;
|
||||||
|
for (String provider : providers) {
|
||||||
|
Location l=null;
|
||||||
|
if (ActivityCompat.checkSelfPermission(getApplicationContext(), permission.ACCESS_FINE_LOCATION)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED
|
||||||
|
&& ActivityCompat.checkSelfPermission(getApplicationContext(), permission.ACCESS_COARSE_LOCATION)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
l = locationManager.getLastKnownLocation(provider);
|
||||||
|
}
|
||||||
|
if (l == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (bestLocation == null
|
||||||
|
|| l.getAccuracy() < bestLocation.getAccuracy()) {
|
||||||
|
bestLocation = l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestLocation == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return bestLocation;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a LocationManager to listen for current location.
|
* Registers a LocationManager to listen for current location.
|
||||||
*/
|
*/
|
||||||
|
|
@ -58,11 +94,10 @@ public class LocationServiceManager implements LocationListener {
|
||||||
* @param locationProvider the location provider
|
* @param locationProvider the location provider
|
||||||
* @return true if successful
|
* @return true if successful
|
||||||
*/
|
*/
|
||||||
private boolean requestLocationUpdatesFromProvider(String locationProvider) {
|
public boolean requestLocationUpdatesFromProvider(String locationProvider) {
|
||||||
try {
|
try {
|
||||||
// If both providers are not available
|
// If both providers are not available
|
||||||
if (locationManager == null || !(locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
|
if (locationManager == null || !(locationManager.getAllProviders().contains(locationProvider))) {
|
||||||
|| !(locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
locationManager.requestLocationUpdates(locationProvider,
|
locationManager.requestLocationUpdates(locationProvider,
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import android.content.pm.PackageManager;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.drawable.VectorDrawable;
|
import android.graphics.drawable.VectorDrawable;
|
||||||
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
|
|
@ -351,7 +352,13 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
|
||||||
.zoom(ZOOM_LEVEL) // Same zoom level
|
.zoom(ZOOM_LEVEL) // Same zoom level
|
||||||
.build();
|
.build();
|
||||||
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
|
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
|
||||||
} else {
|
}
|
||||||
|
else if(locationManager.isGPSProviderEnabled()||locationManager.isNetworkProviderEnabled()){
|
||||||
|
locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER);
|
||||||
|
locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER);
|
||||||
|
setProgressBarVisibility(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
Toast.makeText(getContext(), getString(R.string.nearby_location_not_available), Toast.LENGTH_LONG).show();
|
Toast.makeText(getContext(), getString(R.string.nearby_location_not_available), Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
presenter.onMapReady();
|
presenter.onMapReady();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue