Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes issues related to location permission denial even when allowed inside in-app camera flow #5313

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.LAST_ZOOM;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
Expand All @@ -21,7 +22,6 @@
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
Expand Down Expand Up @@ -56,6 +56,7 @@
import fr.free.nrw.commons.MapStyle;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.filepicker.Constants;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.location.LocationPermissionsHelper;
import fr.free.nrw.commons.location.LocationPermissionsHelper.Dialog;
Expand All @@ -72,7 +73,7 @@
* Helps to pick location and return the result with an intent
*/
public class LocationPickerActivity extends BaseActivity implements OnMapReadyCallback,
OnCameraMoveStartedListener, OnCameraIdleListener, Observer<CameraPosition> {
OnCameraMoveStartedListener, OnCameraIdleListener, Observer<CameraPosition>, LocationPermissionCallback {

/**
* DROPPED_MARKER_LAYER_ID : id for layer
Expand Down Expand Up @@ -474,30 +475,21 @@ private void getCenter() {
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();
if (currLocation != null) {
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);
}
}
});
this, locationManager, this);
locationPermissionsHelper.handleLocationPermissions(locationAccessDialog, locationOffDialog);
}

@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions,
@NonNull final int[] grantResults) {
if (requestCode == Constants.RequestCodes.LOCATION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onLocationPermissionGranted();
} else {
onLocationPermissionDenied("");
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onStart() {
super.onStart();
Expand Down Expand Up @@ -539,4 +531,24 @@ public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
public void onLocationPermissionDenied(String toastMessage) {
//do nothing
}

@Override
public void onLocationPermissionGranted() {
fr.free.nrw.commons.location.LatLng currLocation = locationManager.getLastLocation();
if (currLocation != null) {
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.free.nrw.commons.bookmarks.locations;

import android.Manifest.permission;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -8,6 +9,9 @@
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -21,6 +25,7 @@
import fr.free.nrw.commons.nearby.fragments.CommonPlaceClickActions;
import fr.free.nrw.commons.nearby.fragments.PlaceAdapter;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import kotlin.Unit;

Expand All @@ -36,6 +41,25 @@ public class BookmarkLocationsFragment extends DaggerFragment {
@Inject BookmarkLocationsDao bookmarkLocationDao;
@Inject CommonPlaceClickActions commonPlaceClickActions;
private PlaceAdapter adapter;
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> result) {
boolean areAllGranted = true;
for(final boolean b : result.values()) {
areAllGranted = areAllGranted && b;
}

if (areAllGranted) {
contributionController.locationPermissionCallback.onLocationPermissionGranted();
} else {
if (shouldShowRequestPermissionRationale(permission.ACCESS_FINE_LOCATION)) {
contributionController.handleShowRationaleFlowCameraLocation(getActivity());
} else {
contributionController.locationPermissionCallback.onLocationPermissionDenied(getActivity().getString(R.string.in_app_camera_location_permission_denied));
}
}
}
});

/**
* Create an instance of the fragment with the right bundle parameters
Expand Down Expand Up @@ -67,7 +91,8 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
adapter.remove(place);
return Unit.INSTANCE;
},
commonPlaceClickActions
commonPlaceClickActions,
inAppCameraLocationPermissionLauncher
);
recyclerView.setAdapter(adapter);
}
Expand Down
Loading