#3732 - Nearby Tab Accessible Without Location Permission (#4259)

* Added ability to access nearby tab without location permissions

* added ability to remember user choice if the permission is denied.

* fixed the issue with permission dialog box in contribution tab.

* changed name for stored variables

* minor change

Co-authored-by: Pratham2305 <Pratham2305@users.noreply.github.com>
This commit is contained in:
Pratham Pahariya 2021-02-26 18:19:11 +05:30 committed by GitHub
parent ba000eb26e
commit 630c2a5dcd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 36 deletions

View file

@ -454,6 +454,7 @@ public class ContributionsFragment
onLocationPermissionGranted(); onLocationPermissionGranted();
} else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) } else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
&& store.getBoolean("displayLocationPermissionForCardView", true) && store.getBoolean("displayLocationPermissionForCardView", true)
&& !store.getBoolean("doNotAskForLocationPermission", false)
&& (((MainActivity) getActivity()).activeFragment == ActiveFragment.CONTRIBUTIONS)) { && (((MainActivity) getActivity()).activeFragment == ActiveFragment.CONTRIBUTIONS)) {
nearbyNotificationCardView.permissionType = NearbyNotificationCardView.PermissionType.ENABLE_LOCATION_PERMISSION; nearbyNotificationCardView.permissionType = NearbyNotificationCardView.PermissionType.ENABLE_LOCATION_PERMISSION;
showNearbyCardPermissionRationale(); showNearbyCardPermissionRationale();
@ -486,6 +487,7 @@ public class ContributionsFragment
private void displayYouWontSeeNearbyMessage() { private void displayYouWontSeeNearbyMessage() {
ViewUtil.showLongToast(getActivity(), getResources().getString(R.string.unable_to_display_nearest_place)); ViewUtil.showLongToast(getActivity(), getResources().getString(R.string.unable_to_display_nearest_place));
store.putBoolean("doNotAskForLocationPermission", true);
} }

View file

@ -22,7 +22,7 @@ public interface NearbyParentFragmentContract {
void listOptionMenuItemClicked(); void listOptionMenuItemClicked();
void populatePlaces(LatLng curlatLng); void populatePlaces(LatLng curlatLng);
boolean isListBottomSheetExpanded(); boolean isListBottomSheetExpanded();
void checkPermissionsAndPerformAction(Runnable runnable); void checkPermissionsAndPerformAction();
void displayLoginSkippedWarning(); void displayLoginSkippedWarning();
void setFABPlusAction(android.view.View.OnClickListener onClickListener); void setFABPlusAction(android.view.View.OnClickListener onClickListener);
void setFABRecenterAction(android.view.View.OnClickListener onClickListener); void setFABRecenterAction(android.view.View.OnClickListener onClickListener);

View file

@ -191,6 +191,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private Animation rotate_forward; private Animation rotate_forward;
private static final float ZOOM_LEVEL = 14f; private static final float ZOOM_LEVEL = 14f;
private static final float ZOOM_OUT = 0f;
private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
private BroadcastReceiver broadcastReceiver; private BroadcastReceiver broadcastReceiver;
private boolean isNetworkErrorOccurred; private boolean isNetworkErrorOccurred;
@ -206,6 +207,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private final double CAMERA_TARGET_SHIFT_FACTOR_LANDSCAPE = 0.004; private final double CAMERA_TARGET_SHIFT_FACTOR_LANDSCAPE = 0.004;
private boolean isMapBoxReady; private boolean isMapBoxReady;
private boolean isPermissionDenied;
private boolean recenterToUserLocation;
private MapboxMap mapBox; private MapboxMap mapBox;
IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION); IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION);
private Marker currentLocationMarker; private Marker currentLocationMarker;
@ -259,6 +262,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
cameraMoveListener= () -> presenter.onCameraMove(mapBox.getCameraPosition().target); cameraMoveListener= () -> presenter.onCameraMove(mapBox.getCameraPosition().target);
addCheckBoxCallback(); addCheckBoxCallback();
presenter.attachView(this); presenter.attachView(this);
isPermissionDenied = false;
recenterToUserLocation = false;
initRvNearbyList(); initRvNearbyList();
initThemePreferences(); initThemePreferences();
mapView.onCreate(savedInstanceState); mapView.onCreate(savedInstanceState);
@ -281,7 +286,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
performMapReadyActions(); performMapReadyActions();
final CameraPosition cameraPosition = new CameraPosition.Builder() final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(51.50550, -0.07520)) .target(new LatLng(51.50550, -0.07520))
.zoom(ZOOM_LEVEL) .zoom(ZOOM_OUT)
.build(); .build();
mapBoxMap.setCameraPosition(cameraPosition); mapBoxMap.setCameraPosition(cameraPosition);
@ -342,34 +347,45 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private void performMapReadyActions() { private void performMapReadyActions() {
if (((MainActivity)getActivity()).activeFragment == ActiveFragment.NEARBY && isMapBoxReady) { if (((MainActivity)getActivity()).activeFragment == ActiveFragment.NEARBY && isMapBoxReady) {
checkPermissionsAndPerformAction(() -> { if(!applicationKvStore.getBoolean("doNotAskForLocationPermission", false) ||
lastKnownLocation = locationManager.getLastLocation(); PermissionUtils.hasPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)){
fr.free.nrw.commons.location.LatLng target=lastFocusLocation; checkPermissionsAndPerformAction();
if(null==lastFocusLocation){ }else{
target=lastKnownLocation; isPermissionDenied = true;
}
if (lastKnownLocation != null) {
final CameraPosition position = new CameraPosition.Builder()
.target(LocationUtils.commonsLatLngToMapBoxLatLng(target)) // Sets the new camera position
.zoom(ZOOM_LEVEL) // Same zoom level
.build();
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
}
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();
}
presenter.onMapReady();
registerUnregisterLocationListener(false);
addOnCameraMoveListener(); addOnCameraMoveListener();
}); }
} }
} }
private void locationPermissionGranted() {
isPermissionDenied = false;
applicationKvStore.putBoolean("doNotAskForLocationPermission", false);
lastKnownLocation = locationManager.getLastLocation();
fr.free.nrw.commons.location.LatLng target=lastFocusLocation;
if(null==lastFocusLocation){
target=lastKnownLocation;
}
if (lastKnownLocation != null) {
final CameraPosition position = new CameraPosition.Builder()
.target(LocationUtils.commonsLatLngToMapBoxLatLng(target)) // Sets the new camera position
.zoom(ZOOM_LEVEL) // Same zoom level
.build();
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
}
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();
}
presenter.onMapReady();
registerUnregisterLocationListener(false);
addOnCameraMoveListener();
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
@ -377,10 +393,31 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
presenter.attachView(this); presenter.attachView(this);
registerNetworkReceiver(); registerNetworkReceiver();
if (isResumed() && ((MainActivity)getActivity()).activeFragment == ActiveFragment.NEARBY) { if (isResumed() && ((MainActivity)getActivity()).activeFragment == ActiveFragment.NEARBY) {
startTheMap(); if(!isPermissionDenied && !applicationKvStore.getBoolean("doNotAskForLocationPermission", false)){
startTheMap();
}else{
startMapWithoutPermission();
}
} }
} }
private void startMapWithoutPermission() {
mapView.onStart();
applicationKvStore.putBoolean("doNotAskForLocationPermission", true);
lastKnownLocation = new fr.free.nrw.commons.location.LatLng(51.50550,-0.07520,1f);
final CameraPosition position = new CameraPosition.Builder()
.target(LocationUtils.commonsLatLngToMapBoxLatLng(lastKnownLocation))
.zoom(ZOOM_OUT)
.build();
if(mapBox != null){
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
addOnCameraMoveListener();
}
presenter.onMapReady();
removeCurrentLocationMarker();
}
private void registerNetworkReceiver() { private void registerNetworkReceiver() {
if (getActivity() != null) { if (getActivity() != null) {
getActivity().registerReceiver(broadcastReceiver, intentFilter); getActivity().registerReceiver(broadcastReceiver, intentFilter);
@ -795,11 +832,14 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void populatePlaces(final fr.free.nrw.commons.location.LatLng curlatLng) { public void populatePlaces(final fr.free.nrw.commons.location.LatLng curlatLng) {
if (curlatLng.equals(lastFocusLocation)|| lastFocusLocation==null) { // Means we are checking around current location if (curlatLng.equals(lastFocusLocation) || lastFocusLocation == null || recenterToUserLocation) { // Means we are checking around current location
populatePlacesForCurrentLocation(lastKnownLocation, curlatLng); populatePlacesForCurrentLocation(lastKnownLocation, curlatLng);
} else { } else {
populatePlacesForAnotherLocation(lastKnownLocation, curlatLng); populatePlacesForAnotherLocation(lastKnownLocation, curlatLng);
} }
if(recenterToUserLocation) {
recenterToUserLocation = false;
}
} }
private void populatePlacesForCurrentLocation(final fr.free.nrw.commons.location.LatLng curlatLng, private void populatePlacesForCurrentLocation(final fr.free.nrw.commons.location.LatLng curlatLng,
@ -918,12 +958,12 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} }
@Override @Override
public void checkPermissionsAndPerformAction(final Runnable runnable) { public void checkPermissionsAndPerformAction() {
Timber.d("Checking permission and perfoming action"); Timber.d("Checking permission and perfoming action");
PermissionUtils.checkPermissionsAndPerformAction(getActivity(), PermissionUtils.checkPermissionsAndPerformAction(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
runnable, () -> locationPermissionGranted(),
() -> ((MainActivity) getActivity()).setSelectedItemId(NavTab.CONTRIBUTIONS.code()), () -> isPermissionDenied = true,
R.string.location_permission_title, R.string.location_permission_title,
R.string.location_permission_rationale_nearby); R.string.location_permission_rationale_nearby);
} }
@ -1095,7 +1135,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
*/ */
@Override @Override
public void addCurrentLocationMarker(final fr.free.nrw.commons.location.LatLng curLatLng) { public void addCurrentLocationMarker(final fr.free.nrw.commons.location.LatLng curLatLng) {
if (null != curLatLng) { if (null != curLatLng && !isPermissionDenied) {
ExecutorUtils.get().submit(() -> { ExecutorUtils.get().submit(() -> {
mapView.post(() -> removeCurrentLocationMarker()); mapView.post(() -> removeCurrentLocationMarker());
Timber.d("Adds current location marker"); Timber.d("Adds current location marker");
@ -1141,8 +1181,15 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void updateMapToTrackPosition(final fr.free.nrw.commons.location.LatLng curLatLng) { public void updateMapToTrackPosition(final fr.free.nrw.commons.location.LatLng curLatLng) {
Timber.d("Updates map camera to track user position"); Timber.d("Updates map camera to track user position");
final CameraPosition cameraPosition = new CameraPosition.Builder().target final CameraPosition cameraPosition;
if(isPermissionDenied){
cameraPosition = new CameraPosition.Builder().target
(LocationUtils.commonsLatLngToMapBoxLatLng(curLatLng)).build(); (LocationUtils.commonsLatLngToMapBoxLatLng(curLatLng)).build();
}else{
cameraPosition = new CameraPosition.Builder().target
(LocationUtils.commonsLatLngToMapBoxLatLng(curLatLng))
.zoom(ZOOM_LEVEL).build();
}
if(null!=mapBox) { if(null!=mapBox) {
mapBox.setCameraPosition(cameraPosition); mapBox.setCameraPosition(cameraPosition);
mapBox.animateCamera(CameraUpdateFactory mapBox.animateCamera(CameraUpdateFactory
@ -1326,8 +1373,10 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void recenterMap(final fr.free.nrw.commons.location.LatLng curLatLng) { public void recenterMap(final fr.free.nrw.commons.location.LatLng curLatLng) {
if (curLatLng == null) { if (isPermissionDenied || curLatLng == null) {
if (!(locationManager.isNetworkProviderEnabled() || locationManager.isGPSProviderEnabled())) { recenterToUserLocation = true;
checkPermissionsAndPerformAction();
if (!isPermissionDenied && !(locationManager.isNetworkProviderEnabled() || locationManager.isGPSProviderEnabled())) {
showLocationOffDialog(); showLocationOffDialog();
} }
return; return;

View file

@ -138,7 +138,10 @@ public class PermissionUtils {
activity.getString(rationaleMessage), activity.getString(rationaleMessage),
activity.getString(android.R.string.ok), activity.getString(android.R.string.ok),
activity.getString(android.R.string.cancel), activity.getString(android.R.string.cancel),
token::continuePermissionRequest, token::cancelPermissionRequest); token::continuePermissionRequest,
token::cancelPermissionRequest,
null,
false);
} }
}) })
.check(); .check();