Skip to content

Add a dialog to prompt user if location is off in Nearby when Locate me button is pressed. #3438

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

Merged
merged 18 commits into from
Mar 18, 2020
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 @@ -6,7 +6,6 @@
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -214,6 +213,14 @@ public void onProviderDisabled(String provider) {
Timber.d("Provider %s disabled", provider);
}

public boolean isNetworkProviderEnabled() {
return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

public boolean isGPSProviderEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

public enum LocationChangeType{
LOCATION_SIGNIFICANTLY_CHANGED, //Went out of borders of nearby markers
LOCATION_SLIGHTLY_CHANGED, //User might be walking or driving
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface View {
void setFABRecenterAction(android.view.View.OnClickListener onClickListener);
void animateFABs();
void recenterMap(LatLng curLatLng);
void showLocationOffDialog();
void openLocationSettings();
void hideBottomSheet();
void hideBottomDetailsSheet();
void displayBottomSheetWithInfo(Marker marker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -54,6 +56,7 @@
import com.mapbox.mapboxsdk.maps.UiSettings;
import com.pedrogomez.renderers.RVRendererAdapter;

import fr.free.nrw.commons.utils.DialogUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -1238,6 +1241,9 @@ private void setMapMarkerActions(Marker selectedMarker) {
@Override
public void recenterMap(fr.free.nrw.commons.location.LatLng curLatLng) {
if (curLatLng == null) {
if (!(locationManager.isNetworkProviderEnabled() || locationManager.isGPSProviderEnabled())) {
showLocationOffDialog();
}
return;
}
addCurrentLocationMarker(curLatLng);
Expand Down Expand Up @@ -1268,6 +1274,28 @@ public void recenterMap(fr.free.nrw.commons.location.LatLng curLatLng) {
mapBox.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1000);
}

@Override
public void showLocationOffDialog() {
// This creates a dialog box that prompts the user to enable location
DialogUtil
.showAlertDialog(getActivity(), getString(R.string.ask_to_turn_location_on), getString(R.string.nearby_needs_location),
getString(R.string.yes), getString(R.string.no), this::openLocationSettings, null);
}

@Override
public void openLocationSettings() {
// This method opens the location settings of the device along with a followup toast.
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
PackageManager packageManager = getActivity().getPackageManager();

if (intent.resolveActivity(packageManager)!= null) {
startActivity(intent);
Toast.makeText(getContext(), R.string.recommend_high_accuracy_mode, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), R.string.cannot_open_location_settings, Toast.LENGTH_LONG).show();
}
}

@Override
public void hideBottomSheet() {
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -602,4 +602,10 @@ Upload your first media by tapping on the add button.</string>
<string name="theme_default_name">Default</string>
<string name="theme_dark_name">Dark</string>
<string name="theme_light_name">Light</string>

<string name="cannot_open_location_settings">Failed to open location settings. Please turn on location manually</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>

</resources>