Merge pull request #55 from misaochan/optional-gps

Get current location if image has no GPS coords
This commit is contained in:
Nicolas Raoul 2016-02-12 12:27:51 +09:00
commit 79ed98c5fa
6 changed files with 136 additions and 42 deletions

View file

@ -17,6 +17,7 @@
<uses-permission android:name="android.permission.READ_SYNC_STATS"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".CommonsApplication"

View file

@ -93,6 +93,9 @@
<string name="menu_download">Download</string>
<string name="preference_license">License</string>
<string name="allow_gps">Automatically get current location</string>
<string name="allow_gps_summary">Retrieve current location to offer category suggestions if image is not geotagged</string>
<!-- These three are semi-legacy entries, and should be changed in future -->
<string name="license_name_cc_by_sa">CC\u00A0Attribution-ShareAlike\u00A03.0</string>
<string name="license_name_cc_by">CC\u00A0Attribution\u00A03.0</string>

View file

@ -7,6 +7,11 @@
android:title="@string/preference_license"
android:defaultValue="CC BY-SA"
/>
<CheckBoxPreference
android:title="@string/allow_gps"
android:defaultValue="false"
android:summary="@string/allow_gps_summary"
android:key="allowGps" />
</PreferenceScreen>

View file

@ -1,18 +1,56 @@
package fr.free.nrw.commons.upload;
import android.content.Context;
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.preference.PreferenceManager;
import android.util.Log;
import java.io.IOException;
public class GPSExtractor {
private static final String TAG = GPSExtractor.class.getName();
private String filePath;
private double decLatitude, decLongitude;
private double currentLatitude, currentLongitude;
private Context context;
public boolean imageCoordsExists;
private MyLocationListener myLocationListener;
private LocationManager locationManager;
private String provider;
private Criteria criteria;
public GPSExtractor(String filePath){
public GPSExtractor(String filePath, Context context){
this.filePath = filePath;
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;
}
protected void registerLocationManager() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
myLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
Location location = locationManager.getLastKnownLocation(provider);
myLocationListener.onLocationChanged(location);
}
protected void unregisterLocationManager() {
locationManager.removeUpdates(myLocationListener);
}
//Extract GPS coords of image
@ -33,21 +71,58 @@ public class GPSExtractor {
}
if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) {
Log.d("Image", "Picture has no GPS info");
return null;
}
else {
imageCoordsExists = false;
Log.d(TAG, "Picture has no GPS info");
//Check what user's preference is for automatic location detection
boolean gpsPrefEnabled = gpsPreferenceEnabled();
if (gpsPrefEnabled) {
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;
}
} else {
imageCoordsExists = true;
Log.d(TAG, "Picture has GPS info");
latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
Log.d("Image", "Latitude: " + latitude + " " + latitude_ref);
Log.d("Image", "Longitude: " + longitude + " " + longitude_ref);
decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
return decimalCoords;
}
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, provider + "'s status changed to " + status);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "Provider " + provider + " enabled");
}
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "Provider " + provider + " disabled");
}
}
@ -62,19 +137,15 @@ public class GPSExtractor {
//Converts format of coords into decimal coords as required by MediaWiki API
private String getDecimalCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) {
if(latitude_ref.equals("N")){
if (latitude_ref.equals("N")) {
decLatitude = convertToDegree(latitude);
}
else{
} else {
decLatitude = 0 - convertToDegree(latitude);
}
if(longitude_ref.equals("E")){
if (longitude_ref.equals("E")) {
decLongitude = convertToDegree(longitude);
}
else{
} else {
decLongitude = 0 - convertToDegree(longitude);
}
@ -83,7 +154,6 @@ public class GPSExtractor {
return decimalCoords;
}
private double convertToDegree(String stringDMS){
double result;
String[] DMS = stringDMS.split(",", 3);
@ -106,5 +176,4 @@ public class GPSExtractor {
result = degrees + (minutes/60) + (seconds/3600);
return result;
}
}

View file

@ -196,13 +196,16 @@ public class MwVolleyApi {
@Override
public String toString() {
StringBuilder builder = new StringBuilder("pages=" + "\n");
for (Page page : pages) {
builder.append(page.toString());
builder.append("\n");
if (pages != null) {
for (Page page : pages) {
builder.append(page.toString());
builder.append("\n");
}
builder.replace(builder.length() - 1, builder.length(), "");
return builder.toString();
} else {
return "No pages found";
}
builder.replace(builder.length() - 1, builder.length(), "");
return builder.toString();
}
}

View file

@ -34,6 +34,8 @@ public class ShareActivity
implements SingleUploadFragment.OnUploadActionInitiated,
CategorizationFragment.OnCategoriesSaveHandler {
private static final String TAG = ShareActivity.class.getName();
private SingleUploadFragment shareView;
private CategorizationFragment categorizationFragment;
@ -44,17 +46,14 @@ public class ShareActivity
private String mediaUriString;
private Uri mediaUri;
private Contribution contribution;
private ImageView backgroundImageView;
private UploadController uploadController;
private CommonsApplication cacheObj;
private boolean cacheFound;
private static final String TAG = ShareActivity.class.getName();
private GPSExtractor imageObj;
public ShareActivity() {
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
@ -190,6 +189,16 @@ public class ShareActivity
mediaUriString = mediaUri.toString();
ImageLoader.getInstance().displayImage(mediaUriString, backgroundImageView);
if(savedInstanceState != null) {
contribution = savedInstanceState.getParcelable("contribution");
}
requestAuthToken();
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "Uri: " + mediaUriString);
Log.d(TAG, "Ext storage dir: " + Environment.getExternalStorageDirectory());
@ -197,19 +206,23 @@ public class ShareActivity
String filePath = FileUtils.getPath(this, mediaUri);
Log.d(TAG, "Filepath: " + filePath);
Log.d(TAG, "Calling GPSExtractor");
imageObj = new GPSExtractor(filePath, this);
imageObj.registerLocationManager();
if (filePath != null && !filePath.equals("")) {
//extract the coordinates of image in decimal degrees
Log.d(TAG, "Calling GPSExtractor");
GPSExtractor imageObj = new GPSExtractor(filePath);
//Gets image coords if exist, otherwise gets last known coords
String decimalCoords = imageObj.getCoords();
if (decimalCoords != null) {
double decLongitude = imageObj.getDecLongitude();
double decLatitude = imageObj.getDecLatitude();
Log.d(TAG, "Decimal coords of image: " + decimalCoords);
app.cacheData.setQtPoint(decLongitude, decLatitude);
//Only set cache for this point if image has coords
if (imageObj.imageCoordsExists) {
double decLongitude = imageObj.getDecLongitude();
double decLatitude = imageObj.getDecLatitude();
app.cacheData.setQtPoint(decLongitude, decLatitude);
}
MwVolleyApi apiCall = new MwVolleyApi(this);
@ -229,13 +242,13 @@ public class ShareActivity
}
}
}
if(savedInstanceState != null) {
contribution = savedInstanceState.getParcelable("contribution");
}
requestAuthToken();
}
@Override
public void onPause() {
super.onPause();
imageObj.unregisterLocationManager();
}
@Override
protected void onDestroy() {