mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Merge remote-tracking branch 'refs/remotes/origin/gps-pref' into optional-gps
This commit is contained in:
commit
c99452e9c6
2 changed files with 37 additions and 21 deletions
|
|
@ -7,6 +7,11 @@
|
||||||
android:title="@string/preference_license"
|
android:title="@string/preference_license"
|
||||||
android:defaultValue="CC BY-SA"
|
android:defaultValue="CC BY-SA"
|
||||||
/>
|
/>
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:title="Automatically get current location"
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:summary="Retrieve current location to offer category suggestions if image is not geotagged"
|
||||||
|
android:key="allowGps" />
|
||||||
|
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,17 @@
|
||||||
package fr.free.nrw.commons.upload;
|
package fr.free.nrw.commons.upload;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.SharedPreferences;
|
||||||
import android.location.Criteria;
|
import android.location.Criteria;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.media.ExifInterface;
|
import android.media.ExifInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.Settings;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
public class GPSExtractor {
|
public class GPSExtractor {
|
||||||
|
|
||||||
private String filePath;
|
private String filePath;
|
||||||
|
|
@ -30,6 +27,13 @@ public class GPSExtractor {
|
||||||
this.context = context;
|
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
|
//Extract GPS coords of image
|
||||||
public String getCoords() {
|
public String getCoords() {
|
||||||
|
|
||||||
|
|
@ -51,24 +55,32 @@ public class GPSExtractor {
|
||||||
imageCoordsExists = false;
|
imageCoordsExists = false;
|
||||||
Log.d(TAG, "Picture has no GPS info");
|
Log.d(TAG, "Picture has no GPS info");
|
||||||
|
|
||||||
|
//Check what user's preference is for automatic location detection
|
||||||
|
boolean gpsPrefEnabled = gpsPreferenceEnabled();
|
||||||
|
|
||||||
|
if (gpsPrefEnabled) {
|
||||||
|
//If pref enabled, set up LocationListener to get current location
|
||||||
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||||
Criteria criteria = new Criteria();
|
Criteria criteria = new Criteria();
|
||||||
String provider = locationManager.getBestProvider(criteria, true);
|
String provider = locationManager.getBestProvider(criteria, true);
|
||||||
myLocationListener = new MyLocationListener();
|
|
||||||
|
|
||||||
|
myLocationListener = new MyLocationListener();
|
||||||
locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
|
locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
|
||||||
Location location = locationManager.getLastKnownLocation(provider);
|
Location location = locationManager.getLastKnownLocation(provider);
|
||||||
|
|
||||||
if (location != null) {
|
if (location != null) {
|
||||||
myLocationListener.onLocationChanged(location);
|
myLocationListener.onLocationChanged(location);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
//calling method is equipped to deal with null return value
|
//calling method is equipped to deal with null return value
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
|
Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
|
||||||
String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
|
String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
|
||||||
return currentCoords;
|
return currentCoords;
|
||||||
|
} else {
|
||||||
|
//Otherwise treat as if no coords found
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
imageCoordsExists = true;
|
imageCoordsExists = true;
|
||||||
|
|
@ -86,7 +98,6 @@ public class GPSExtractor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class MyLocationListener implements LocationListener {
|
private class MyLocationListener implements LocationListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue