Resolves Issue #5413 Crash when opening Nearby when location permission hasn't been granted yet (#5418)

* Resolves Issue #5413

* Added Javadoc and formatted code
This commit is contained in:
Shashwat Kedia 2024-01-27 19:17:36 +05:30 committed by GitHub
parent 71de19f27b
commit 8222c4a42c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -304,37 +304,28 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} }
}); });
private ActivityResultLauncher<String[]> locationPermissionLauncher = registerForActivityResult( private ActivityResultLauncher<String> locationPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultContracts.RequestPermission(), isGranted -> {
new ActivityResultCallback<Map<String, Boolean>>() { if (isGranted) {
@Override locationPermissionGranted();
public void onActivityResult(Map<String, Boolean> result) { } else {
boolean areAllGranted = true; if (shouldShowRequestPermissionRationale(permission.ACCESS_FINE_LOCATION)) {
for (final boolean b : result.values()) { DialogUtil.showAlertDialog(getActivity(),
areAllGranted = areAllGranted && b; getActivity().getString(R.string.location_permission_title),
} getActivity().getString(R.string.location_permission_rationale_nearby),
getActivity().getString(android.R.string.ok),
if (areAllGranted) { getActivity().getString(android.R.string.cancel),
locationPermissionGranted(); () -> {
if (!(locationManager.isNetworkProviderEnabled()
|| locationManager.isGPSProviderEnabled())) {
showLocationOffDialog();
}
},
() -> isPermissionDenied = true,
null,
false);
} else { } else {
if (shouldShowRequestPermissionRationale(permission.ACCESS_FINE_LOCATION)) { isPermissionDenied = true;
DialogUtil.showAlertDialog(getActivity(),
getActivity().getString(R.string.location_permission_title),
getActivity().getString(R.string.location_permission_rationale_nearby),
getActivity().getString(android.R.string.ok),
getActivity().getString(android.R.string.cancel),
() -> {
if (!(locationManager.isNetworkProviderEnabled()
|| locationManager.isGPSProviderEnabled())) {
showLocationOffDialog();
}
},
() -> isPermissionDenied = true,
null,
false);
} else {
isPermissionDenied = true;
}
} }
} }
}); });
@ -1037,10 +1028,28 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
return latLng; return latLng;
} }
/**
* Computes location where map should be centered
*
* @return returns the last location, if available, else returns default location
*/
@Override @Override
public fr.free.nrw.commons.location.LatLng getMapCenter() { public fr.free.nrw.commons.location.LatLng getMapCenter() {
fr.free.nrw.commons.location.LatLng latLnge = new fr.free.nrw.commons.location.LatLng( if (applicationKvStore.getString("LastLocation") != null) {
mapCenter.getLatitude(), mapCenter.getLongitude(), 100); final String[] locationLatLng
= applicationKvStore.getString("LastLocation").split(",");
lastKnownLocation
= new fr.free.nrw.commons.location.LatLng(Double.parseDouble(locationLatLng[0]),
Double.parseDouble(locationLatLng[1]), 1f);
} else {
lastKnownLocation = new fr.free.nrw.commons.location.LatLng(51.50550,
-0.07520, 1f);
}
fr.free.nrw.commons.location.LatLng latLnge = lastKnownLocation;
if (mapCenter != null) {
latLnge = new fr.free.nrw.commons.location.LatLng(
mapCenter.getLatitude(), mapCenter.getLongitude(), 100);
}
return latLnge; return latLnge;
} }
@ -1320,7 +1329,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void checkPermissionsAndPerformAction() { public void checkPermissionsAndPerformAction() {
Timber.d("Checking permission and perfoming action"); Timber.d("Checking permission and perfoming action");
locationPermissionLauncher.launch(new String[]{permission.ACCESS_FINE_LOCATION}); locationPermissionLauncher.launch(permission.ACCESS_FINE_LOCATION);
} }
/** /**
@ -1625,7 +1634,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
*/ */
public void updateMarker(final boolean isBookmarked, final Place place, public void updateMarker(final boolean isBookmarked, final Place place,
@Nullable final fr.free.nrw.commons.location.LatLng curLatLng) { @Nullable final fr.free.nrw.commons.location.LatLng curLatLng) {
addMarkerToMap(place, isBookmarked); addMarkerToMap(place, isBookmarked);
} }
/** /**
@ -1770,7 +1779,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
mapView.invalidate(); mapView.invalidate();
break; break;
} }
} }
} }
} }