Skip to content

Commit

Permalink
5195: Fix crash on setting location for pictures with no EXIF location (
Browse files Browse the repository at this point in the history
#5205)

* LocationPickerActivity: fix crash on setting location for pictures with no EXIF location

* strings.xml: remove extra string introduced

* optimise constants

* LocationPickerActivity: request for permission only on tapping target icon

* remove newline

* handle location permissions using LocationPermissionsHelper
  • Loading branch information
RitikaPahwa4444 authored Sep 24, 2023
1 parent 6881158 commit 4caa8a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.location.LocationPermissionsHelper;
import fr.free.nrw.commons.location.LocationPermissionsHelper.Dialog;
import fr.free.nrw.commons.location.LocationPermissionsHelper.LocationPermissionCallback;
import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.SystemThemeUtils;
import javax.inject.Inject;
Expand Down Expand Up @@ -148,6 +152,9 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
SystemThemeUtils systemThemeUtils;
private boolean isDarkTheme;

@Inject
LocationServiceManager locationManager;

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
Expand Down Expand Up @@ -452,11 +459,40 @@ private void addCenterOnGPSButton(){
fabCenterOnLocation = findViewById(R.id.center_on_gps);
fabCenterOnLocation.setOnClickListener(view -> getCenter());
}

/**
* Animate map to move to desired Latitude and Longitude
* Center the map at user's current location
*/
void getCenter() {
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),15.0));
private void getCenter() {
LocationPermissionsHelper.Dialog locationAccessDialog = new Dialog(
R.string.location_permission_title,
R.string.upload_map_location_access
);

LocationPermissionsHelper.Dialog locationOffDialog = new Dialog(
R.string.ask_to_turn_location_on,
R.string.upload_map_location_access
);
LocationPermissionsHelper locationPermissionsHelper = new LocationPermissionsHelper(
this, locationManager, new LocationPermissionCallback() {
@Override
public void onLocationPermissionDenied(String toastMessage) {
// Do nothing
}

@Override
public void onLocationPermissionGranted() {
fr.free.nrw.commons.location.LatLng currLocation = locationManager.getLastLocation();
final CameraPosition position;
position = new CameraPosition.Builder()
.target(new com.mapbox.mapboxsdk.geometry.LatLng(currLocation.getLatitude(), currLocation.getLongitude(), 0)) // Sets the new camera position
.zoom(mapboxMap.getCameraPosition().zoom) // Same zoom level
.build();

mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1000);
}
});
locationPermissionsHelper.handleLocationPermissions(locationAccessDialog, locationOffDialog);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ private void createDialogsAndHandleLocationPermissions(Activity activity) {
activity, locationManager,
new LocationPermissionCallback() {
@Override
public void onLocationPermissionDenied() {
public void onLocationPermissionDenied(String toastMessage) {
Toast.makeText(
activity,
toastMessage,
Toast.LENGTH_LONG
).show();
initiateCameraUpload(activity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ private void requestForLocationAccess(
},
() -> {
if (callback != null) {
Toast.makeText(
activity,
R.string.in_app_camera_location_permission_denied,
Toast.LENGTH_LONG
).show();
callback.onLocationPermissionDenied();
callback.onLocationPermissionDenied(activity.getString(
R.string.in_app_camera_location_permission_denied));
}
},
locationAccessDialog.dialogTitleResource,
Expand Down Expand Up @@ -102,14 +98,8 @@ private void showLocationOffDialog(Dialog locationOffDialog) {
activity.getString(R.string.title_app_shortcut_setting),
activity.getString(R.string.cancel),
() -> openLocationSettings(),
() -> {
Toast.makeText(
activity,
R.string.in_app_camera_location_unavailable,
Toast.LENGTH_LONG
).show();
callback.onLocationPermissionDenied();
});
() -> callback.onLocationPermissionDenied(activity.getString(
R.string.in_app_camera_location_unavailable)));
}

/**
Expand All @@ -131,7 +121,7 @@ private void openLocationSettings() {
* Handle onPermissionDenied within individual classes based on the requirements
*/
public interface LocationPermissionCallback {
void onLocationPermissionDenied();
void onLocationPermissionDenied(String toastMessage);
void onLocationPermissionGranted();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private void createDialogsAndHandleLocationPermissions(Activity activity) {
LocationPermissionsHelper locationPermissionsHelper = new LocationPermissionsHelper(
activity, locationManager, new LocationPermissionCallback() {
@Override
public void onLocationPermissionDenied() {
public void onLocationPermissionDenied(String toastMessage) {
// dismiss the dialog
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
<string name="copy_wikicode">Copy the wikitext to the clipboard</string>
<string name="wikicode_copied">The wikitext was copied to the clipboard</string>
<string name="nearby_location_not_available">Nearby might not work properly, Location not available.</string>
<string name="upload_location_access_denied">Location access denied. Please set your location manually to use this feature.</string>
<string name="location_permission_rationale_nearby">Permission required to display a list of nearby places</string>

<string name="nearby_directions">Directions</string>
Expand Down Expand Up @@ -621,6 +622,7 @@ Upload your first media by tapping on the add button.</string>
<string name="recommend_high_accuracy_mode">For best results, choose the High Accuracy mode.</string>
<string name="ask_to_turn_location_on">Turn on location?</string>
<string name="nearby_needs_location">Nearby needs location enabled to work properly</string>
<string name="upload_map_location_access">You need to give access to your current location to set location automatically.</string>
<string name="use_location_from_similar_image">Did you shoot these two pictures at the same place? Do you want to use the latitude/longitude of the picture on the right?</string>
<string name="load_more">Load More</string>
<string name="nearby_no_results">No places found, try changing your search criteria.</string>
Expand Down

0 comments on commit 4caa8a5

Please sign in to comment.