diff --git a/commons/res/xml/preferences.xml b/commons/res/xml/preferences.xml index 802e60b6d..23fc30f5d 100644 --- a/commons/res/xml/preferences.xml +++ b/commons/res/xml/preferences.xml @@ -7,6 +7,11 @@ android:title="@string/preference_license" android:defaultValue="CC BY-SA" /> + diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index ba04b2222..866ce7f74 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -1,20 +1,17 @@ package fr.free.nrw.commons.upload; import android.content.Context; -import android.content.Intent; +import android.content.SharedPreferences; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.media.ExifInterface; import android.os.Bundle; -import android.provider.Settings; +import android.preference.PreferenceManager; import android.util.Log; -import android.widget.Toast; - import java.io.IOException; - public class GPSExtractor { private String filePath; @@ -30,6 +27,13 @@ public class GPSExtractor { this.context = context; } + private boolean gpsPreferenceEnabled() { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); + boolean gpsPref = sharedPref.getBoolean("allowGps", false); + Log.d(TAG, "Gps pref set to: " + gpsPref); + return gpsPref; + } + //Extract GPS coords of image public String getCoords() { @@ -51,24 +55,32 @@ public class GPSExtractor { imageCoordsExists = false; Log.d(TAG, "Picture has no GPS info"); - LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); - Criteria criteria = new Criteria(); - String provider = locationManager.getBestProvider(criteria, true); - myLocationListener = new MyLocationListener(); - - locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener); - Location location = locationManager.getLastKnownLocation(provider); + //Check what user's preference is for automatic location detection + boolean gpsPrefEnabled = gpsPreferenceEnabled(); - if (location != null) { - myLocationListener.onLocationChanged(location); - } - else { - //calling method is equipped to deal with null return value + if (gpsPrefEnabled) { + //If pref enabled, set up LocationListener to get current location + LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + Criteria criteria = new Criteria(); + String provider = locationManager.getBestProvider(criteria, true); + + myLocationListener = new MyLocationListener(); + locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener); + Location location = locationManager.getLastKnownLocation(provider); + + if (location != null) { + myLocationListener.onLocationChanged(location); + } else { + //calling method is equipped to deal with null return value + return null; + } + Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude); + String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude); + return currentCoords; + } else { + //Otherwise treat as if no coords found return null; } - Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude); - String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude); - return currentCoords; } else { imageCoordsExists = true; @@ -86,7 +98,6 @@ public class GPSExtractor { } } - private class MyLocationListener implements LocationListener { @Override