Skip to content

Commit 8222c4a

Browse files
Resolves Issue #5413 Crash when opening Nearby when location permission hasn't been granted yet (#5418)
* Resolves Issue #5413 * Added Javadoc and formatted code
1 parent 71de19f commit 8222c4a

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

+44-35
Original file line numberDiff line numberDiff line change
@@ -304,37 +304,28 @@ public void onActivityResult(Map<String, Boolean> result) {
304304
}
305305
});
306306

307-
private ActivityResultLauncher<String[]> locationPermissionLauncher = registerForActivityResult(
308-
new ActivityResultContracts.RequestMultiplePermissions(),
309-
new ActivityResultCallback<Map<String, Boolean>>() {
310-
@Override
311-
public void onActivityResult(Map<String, Boolean> result) {
312-
boolean areAllGranted = true;
313-
for (final boolean b : result.values()) {
314-
areAllGranted = areAllGranted && b;
315-
}
316-
317-
if (areAllGranted) {
318-
locationPermissionGranted();
307+
private ActivityResultLauncher<String> locationPermissionLauncher = registerForActivityResult(
308+
new ActivityResultContracts.RequestPermission(), isGranted -> {
309+
if (isGranted) {
310+
locationPermissionGranted();
311+
} else {
312+
if (shouldShowRequestPermissionRationale(permission.ACCESS_FINE_LOCATION)) {
313+
DialogUtil.showAlertDialog(getActivity(),
314+
getActivity().getString(R.string.location_permission_title),
315+
getActivity().getString(R.string.location_permission_rationale_nearby),
316+
getActivity().getString(android.R.string.ok),
317+
getActivity().getString(android.R.string.cancel),
318+
() -> {
319+
if (!(locationManager.isNetworkProviderEnabled()
320+
|| locationManager.isGPSProviderEnabled())) {
321+
showLocationOffDialog();
322+
}
323+
},
324+
() -> isPermissionDenied = true,
325+
null,
326+
false);
319327
} else {
320-
if (shouldShowRequestPermissionRationale(permission.ACCESS_FINE_LOCATION)) {
321-
DialogUtil.showAlertDialog(getActivity(),
322-
getActivity().getString(R.string.location_permission_title),
323-
getActivity().getString(R.string.location_permission_rationale_nearby),
324-
getActivity().getString(android.R.string.ok),
325-
getActivity().getString(android.R.string.cancel),
326-
() -> {
327-
if (!(locationManager.isNetworkProviderEnabled()
328-
|| locationManager.isGPSProviderEnabled())) {
329-
showLocationOffDialog();
330-
}
331-
},
332-
() -> isPermissionDenied = true,
333-
null,
334-
false);
335-
} else {
336-
isPermissionDenied = true;
337-
}
328+
isPermissionDenied = true;
338329
}
339330
}
340331
});
@@ -1037,10 +1028,28 @@ public fr.free.nrw.commons.location.LatLng getLastMapFocus() {
10371028
return latLng;
10381029
}
10391030

1031+
/**
1032+
* Computes location where map should be centered
1033+
*
1034+
* @return returns the last location, if available, else returns default location
1035+
*/
10401036
@Override
10411037
public fr.free.nrw.commons.location.LatLng getMapCenter() {
1042-
fr.free.nrw.commons.location.LatLng latLnge = new fr.free.nrw.commons.location.LatLng(
1043-
mapCenter.getLatitude(), mapCenter.getLongitude(), 100);
1038+
if (applicationKvStore.getString("LastLocation") != null) {
1039+
final String[] locationLatLng
1040+
= applicationKvStore.getString("LastLocation").split(",");
1041+
lastKnownLocation
1042+
= new fr.free.nrw.commons.location.LatLng(Double.parseDouble(locationLatLng[0]),
1043+
Double.parseDouble(locationLatLng[1]), 1f);
1044+
} else {
1045+
lastKnownLocation = new fr.free.nrw.commons.location.LatLng(51.50550,
1046+
-0.07520, 1f);
1047+
}
1048+
fr.free.nrw.commons.location.LatLng latLnge = lastKnownLocation;
1049+
if (mapCenter != null) {
1050+
latLnge = new fr.free.nrw.commons.location.LatLng(
1051+
mapCenter.getLatitude(), mapCenter.getLongitude(), 100);
1052+
}
10441053
return latLnge;
10451054
}
10461055

@@ -1320,7 +1329,7 @@ public void setTabItemContributions() {
13201329
@Override
13211330
public void checkPermissionsAndPerformAction() {
13221331
Timber.d("Checking permission and perfoming action");
1323-
locationPermissionLauncher.launch(new String[]{permission.ACCESS_FINE_LOCATION});
1332+
locationPermissionLauncher.launch(permission.ACCESS_FINE_LOCATION);
13241333
}
13251334

13261335
/**
@@ -1625,7 +1634,7 @@ public fr.free.nrw.commons.location.LatLng getCameraTarget() {
16251634
*/
16261635
public void updateMarker(final boolean isBookmarked, final Place place,
16271636
@Nullable final fr.free.nrw.commons.location.LatLng curLatLng) {
1628-
addMarkerToMap(place, isBookmarked);
1637+
addMarkerToMap(place, isBookmarked);
16291638
}
16301639

16311640
/**
@@ -1770,7 +1779,7 @@ private void removeMarker(Place place){
17701779
mapView.invalidate();
17711780
break;
17721781
}
1773-
}
1782+
}
17741783
}
17751784
}
17761785

0 commit comments

Comments
 (0)