Skip to content

Commit 021105a

Browse files
author
Aryan Tyagi
authored
Add a dialog to prompt user if location is off in Nearby when L… (#3438)
1 parent 15bccdf commit 021105a

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.location.LocationListener;
77
import android.location.LocationManager;
88
import android.os.Bundle;
9-
109
import java.util.HashSet;
1110
import java.util.List;
1211
import java.util.Set;
@@ -214,6 +213,14 @@ public void onProviderDisabled(String provider) {
214213
Timber.d("Provider %s disabled", provider);
215214
}
216215

216+
public boolean isNetworkProviderEnabled() {
217+
return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
218+
}
219+
220+
public boolean isGPSProviderEnabled() {
221+
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
222+
}
223+
217224
public enum LocationChangeType{
218225
LOCATION_SIGNIFICANTLY_CHANGED, //Went out of borders of nearby markers
219226
LOCATION_SLIGHTLY_CHANGED, //User might be walking or driving

app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface View {
2828
void setFABRecenterAction(android.view.View.OnClickListener onClickListener);
2929
void animateFABs();
3030
void recenterMap(LatLng curLatLng);
31+
void showLocationOffDialog();
32+
void openLocationSettings();
3133
void hideBottomSheet();
3234
void hideBottomDetailsSheet();
3335
void displayBottomSheetWithInfo(Marker marker);

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

+28
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import android.content.Context;
77
import android.content.Intent;
88
import android.content.IntentFilter;
9+
import android.content.pm.PackageManager;
910
import android.content.res.Configuration;
1011
import android.graphics.Bitmap;
1112
import android.os.Bundle;
13+
import android.provider.Settings;
1214
import android.util.Log;
1315
import android.view.Gravity;
1416
import android.view.LayoutInflater;
@@ -56,6 +58,7 @@
5658
import com.mapbox.pluginscalebar.ScaleBarPlugin;
5759
import com.pedrogomez.renderers.RVRendererAdapter;
5860

61+
import fr.free.nrw.commons.utils.DialogUtil;
5962
import java.util.ArrayList;
6063
import java.util.List;
6164
import java.util.concurrent.TimeUnit;
@@ -1251,6 +1254,9 @@ private void setMapMarkerActions(Marker selectedMarker) {
12511254
@Override
12521255
public void recenterMap(fr.free.nrw.commons.location.LatLng curLatLng) {
12531256
if (curLatLng == null) {
1257+
if (!(locationManager.isNetworkProviderEnabled() || locationManager.isGPSProviderEnabled())) {
1258+
showLocationOffDialog();
1259+
}
12541260
return;
12551261
}
12561262
addCurrentLocationMarker(curLatLng);
@@ -1281,6 +1287,28 @@ public void recenterMap(fr.free.nrw.commons.location.LatLng curLatLng) {
12811287
mapBox.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1000);
12821288
}
12831289

1290+
@Override
1291+
public void showLocationOffDialog() {
1292+
// This creates a dialog box that prompts the user to enable location
1293+
DialogUtil
1294+
.showAlertDialog(getActivity(), getString(R.string.ask_to_turn_location_on), getString(R.string.nearby_needs_location),
1295+
getString(R.string.yes), getString(R.string.no), this::openLocationSettings, null);
1296+
}
1297+
1298+
@Override
1299+
public void openLocationSettings() {
1300+
// This method opens the location settings of the device along with a followup toast.
1301+
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
1302+
PackageManager packageManager = getActivity().getPackageManager();
1303+
1304+
if (intent.resolveActivity(packageManager)!= null) {
1305+
startActivity(intent);
1306+
Toast.makeText(getContext(), R.string.recommend_high_accuracy_mode, Toast.LENGTH_LONG).show();
1307+
} else {
1308+
Toast.makeText(getContext(), R.string.cannot_open_location_settings, Toast.LENGTH_LONG).show();
1309+
}
1310+
}
1311+
12841312
@Override
12851313
public void hideBottomSheet() {
12861314
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

app/src/main/res/values/strings.xml

+6
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,10 @@ Upload your first media by tapping on the add button.</string>
602602
<string name="theme_default_name">Default</string>
603603
<string name="theme_dark_name">Dark</string>
604604
<string name="theme_light_name">Light</string>
605+
606+
<string name="cannot_open_location_settings">Failed to open location settings. Please turn on location manually</string>
607+
<string name="recommend_high_accuracy_mode">For best results, choose the High Accuracy mode.</string>
608+
<string name="ask_to_turn_location_on">Turn on location?</string>
609+
<string name="nearby_needs_location">Nearby needs location enabled to work properly</string>
610+
605611
</resources>

0 commit comments

Comments
 (0)