Skip to content

Commit 2d831c0

Browse files
author
Vivek Maskara
authored
Merge pull request #1644 from misaochan/fix-automatic-location
Remove current location retrieval from upload process entirely
2 parents b2a150a + b5161d6 commit 2d831c0

File tree

5 files changed

+23
-189
lines changed

5 files changed

+23
-189
lines changed

app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,20 @@ GPSExtractor processFileCoordinates(boolean gpsEnabled) {
106106
ParcelFileDescriptor descriptor = contentResolver.openFileDescriptor(mediaUri, "r");
107107
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
108108
if (descriptor != null) {
109-
imageObj = new GPSExtractor(descriptor.getFileDescriptor(), context, prefs);
109+
imageObj = new GPSExtractor(descriptor.getFileDescriptor());
110110
}
111111
} else {
112112
String filePath = getPathOfMediaOrCopy();
113113
if (filePath != null) {
114-
imageObj = new GPSExtractor(filePath, context, prefs);
114+
imageObj = new GPSExtractor(filePath);
115115
}
116116
}
117117

118-
decimalCoords = imageObj.getCoords(gpsEnabled);
118+
decimalCoords = imageObj.getCoords();
119119
if (decimalCoords == null || !imageObj.imageCoordsExists) {
120120
//Find other photos taken around the same time which has gps coordinates
121121
if (!haveCheckedForOtherImages)
122-
findOtherImages(gpsEnabled);// Do not do repeat the process
122+
findOtherImages();// Do not do repeat the process
123123
} else {
124124
useImageCoords();
125125
}
@@ -137,9 +137,8 @@ String getDecimalCoords() {
137137
/**
138138
* Find other images around the same location that were taken within the last 20 sec
139139
*
140-
* @param gpsEnabled True if GPS is enabled
141140
*/
142-
private void findOtherImages(boolean gpsEnabled) {
141+
private void findOtherImages() {
143142
Timber.d("filePath" + getPathOfMediaOrCopy());
144143

145144
long timeOfCreation = new File(filePath).lastModified();//Time when the original image was created
@@ -161,17 +160,17 @@ private void findOtherImages(boolean gpsEnabled) {
161160
}
162161
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
163162
if (descriptor != null) {
164-
tempImageObj = new GPSExtractor(descriptor.getFileDescriptor(), context, prefs);
163+
tempImageObj = new GPSExtractor(descriptor.getFileDescriptor());
165164
}
166165
} else {
167166
if (filePath != null) {
168-
tempImageObj = new GPSExtractor(file.getAbsolutePath(), context, prefs);
167+
tempImageObj = new GPSExtractor(file.getAbsolutePath());
169168
}
170169
}
171170

172171
if (tempImageObj != null) {
173-
Timber.d("not null fild EXIF" + tempImageObj.imageCoordsExists + " coords" + tempImageObj.getCoords(gpsEnabled));
174-
if (tempImageObj.getCoords(gpsEnabled) != null && tempImageObj.imageCoordsExists) {
172+
Timber.d("not null fild EXIF" + tempImageObj.imageCoordsExists + " coords" + tempImageObj.getCoords());
173+
if (tempImageObj.getCoords() != null && tempImageObj.imageCoordsExists) {
175174
// Current image has gps coordinates and it's not current gps locaiton
176175
Timber.d("This file has image coords:" + file.getAbsolutePath());
177176
SimilarImageDialogFragment newFragment = new SimilarImageDialogFragment();
@@ -250,7 +249,7 @@ void detectUnwantedPictures() {
250249
@Override
251250
public void onPositiveResponse() {
252251
imageObj = tempImageObj;
253-
decimalCoords = imageObj.getCoords(false);// Not necessary to use gps as image already ha EXIF data
252+
decimalCoords = imageObj.getCoords();// Not necessary to use gps as image already ha EXIF data
254253
Timber.d("EXIF from tempImageObj");
255254
useImageCoords();
256255
}

app/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java

+7-115
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package fr.free.nrw.commons.upload;
22

3-
import android.content.Context;
4-
import android.content.SharedPreferences;
5-
import android.location.Criteria;
6-
import android.location.Location;
7-
import android.location.LocationListener;
8-
import android.location.LocationManager;
93
import android.media.ExifInterface;
10-
import android.os.Bundle;
114
import android.support.annotation.NonNull;
125
import android.support.annotation.Nullable;
136
import android.support.annotation.RequiresApi;
@@ -19,31 +12,21 @@
1912

2013
/**
2114
* Extracts geolocation to be passed to API for category suggestions. If a picture with geolocation
22-
* is uploaded, extract latitude and longitude from EXIF data of image. If a picture without
23-
* geolocation is uploaded, retrieve user's location (if enabled in Settings).
15+
* is uploaded, extract latitude and longitude from EXIF data of image.
2416
*/
2517
public class GPSExtractor {
2618

27-
private final Context context;
28-
private SharedPreferences prefs;
2919
private ExifInterface exif;
3020
private double decLatitude;
3121
private double decLongitude;
32-
private Double currentLatitude = null;
33-
private Double currentLongitude = null;
3422
public boolean imageCoordsExists;
35-
private MyLocationListener myLocationListener;
36-
private LocationManager locationManager;
3723

3824
/**
3925
* Construct from the file descriptor of the image (only for API 24 or newer).
4026
* @param fileDescriptor the file descriptor of the image
41-
* @param context the context
4227
*/
4328
@RequiresApi(24)
44-
public GPSExtractor(@NonNull FileDescriptor fileDescriptor, Context context, SharedPreferences prefs) {
45-
this.context = context;
46-
this.prefs = prefs;
29+
public GPSExtractor(@NonNull FileDescriptor fileDescriptor) {
4730
try {
4831
exif = new ExifInterface(fileDescriptor);
4932
} catch (IOException | IllegalArgumentException e) {
@@ -54,96 +37,32 @@ public GPSExtractor(@NonNull FileDescriptor fileDescriptor, Context context, Sha
5437
/**
5538
* Construct from the file path of the image.
5639
* @param path file path of the image
57-
* @param context the context
40+
*
5841
*/
59-
public GPSExtractor(@NonNull String path, Context context, SharedPreferences prefs) {
60-
this.prefs = prefs;
42+
public GPSExtractor(@NonNull String path) {
6143
try {
6244
exif = new ExifInterface(path);
6345
} catch (IOException | IllegalArgumentException e) {
6446
Timber.w(e);
6547
}
66-
this.context = context;
67-
}
68-
69-
/**
70-
* Check if user enabled retrieval of their current location in Settings
71-
* @return true if enabled, false if disabled
72-
*/
73-
private boolean gpsPreferenceEnabled() {
74-
boolean gpsPref = prefs.getBoolean("allowGps", false);
75-
Timber.d("Gps pref set to: %b", gpsPref);
76-
return gpsPref;
77-
}
78-
79-
/**
80-
* Registers a LocationManager to listen for current location
81-
*/
82-
protected void registerLocationManager() {
83-
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
84-
Criteria criteria = new Criteria();
85-
String provider = locationManager.getBestProvider(criteria, true);
86-
myLocationListener = new MyLocationListener();
87-
88-
try {
89-
locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
90-
Location location = locationManager.getLastKnownLocation(provider);
91-
if (location != null) {
92-
myLocationListener.onLocationChanged(location);
93-
}
94-
} catch (IllegalArgumentException e) {
95-
Timber.e(e, "Illegal argument exception");
96-
} catch (SecurityException e) {
97-
Timber.e(e, "Security exception");
98-
}
99-
}
100-
101-
protected void unregisterLocationManager() {
102-
try {
103-
locationManager.removeUpdates(myLocationListener);
104-
} catch (SecurityException e) {
105-
Timber.e(e, "Security exception");
106-
}
10748
}
10849

10950
/**
11051
* Extracts geolocation (either of image from EXIF data, or of user)
111-
* @param useGPS set to true if location permissions allowed (by API 23), false if disallowed
11252
* @return coordinates as string (needs to be passed as a String in API query)
11353
*/
11454
@Nullable
115-
public String getCoords(boolean useGPS) {
55+
public String getCoords() {
11656
String latitude;
11757
String longitude;
11858
String latitudeRef;
11959
String longitudeRef;
12060
String decimalCoords;
12161

12262
//If image has no EXIF data and user has enabled GPS setting, get user's location
63+
//TODO: Always return null as a temporary fix for #1599
12364
if (exif == null || exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) {
124-
if (useGPS) {
125-
registerLocationManager();
126-
127-
imageCoordsExists = false;
128-
Timber.d("EXIF data has no location info");
129-
130-
//Check what user's preference is for automatic location detection
131-
boolean gpsPrefEnabled = gpsPreferenceEnabled();
132-
133-
//Check that currentLatitude and currentLongitude have been
134-
// explicitly set by MyLocationListener
135-
// and do not default to (0.0,0.0)
136-
if (gpsPrefEnabled && currentLatitude != null && currentLongitude != null) {
137-
Timber.d("Current location values: Lat = %f Long = %f",
138-
currentLatitude, currentLongitude);
139-
return String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
140-
} else {
141-
// No coords found
142-
return null;
143-
}
144-
} else {
145-
return null;
146-
}
65+
return null;
14766
} else {
14867
//If image has EXIF data, extract image coords
14968
imageCoordsExists = true;
@@ -166,33 +85,6 @@ public String getCoords(boolean useGPS) {
16685
}
16786
}
16887

169-
/**
170-
* Listen for user's location when it changes
171-
*/
172-
private class MyLocationListener implements LocationListener {
173-
174-
@Override
175-
public void onLocationChanged(Location location) {
176-
currentLatitude = location.getLatitude();
177-
currentLongitude = location.getLongitude();
178-
}
179-
180-
@Override
181-
public void onStatusChanged(String provider, int status, Bundle extras) {
182-
Timber.d("%s's status changed to %d", provider, status);
183-
}
184-
185-
@Override
186-
public void onProviderEnabled(String provider) {
187-
Timber.d("Provider %s enabled", provider);
188-
}
189-
190-
@Override
191-
public void onProviderDisabled(String provider) {
192-
Timber.d("Provider %s disabled", provider);
193-
}
194-
}
195-
19688
public double getDecLatitude() {
19789
return decLatitude;
19890
}

app/src/main/java/fr/free/nrw/commons/upload/MultipleShareActivity.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,18 @@ private String extractImageGpsData(Uri imageUri) {
329329
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
330330
ParcelFileDescriptor fd = getContentResolver().openFileDescriptor(imageUri,"r");
331331
if (fd != null) {
332-
gpsExtractor = new GPSExtractor(fd.getFileDescriptor(),this,prefs);
332+
gpsExtractor = new GPSExtractor(fd.getFileDescriptor());
333333
}
334334
} else {
335335
String filePath = FileUtils.getPath(this,imageUri);
336336
if (filePath != null) {
337-
gpsExtractor = new GPSExtractor(filePath,this,prefs);
337+
gpsExtractor = new GPSExtractor(filePath);
338338
}
339339
}
340340

341341
if (gpsExtractor != null) {
342342
//get image coordinates from exif data or user location
343-
return gpsExtractor.getCoords(locationPermitted);
343+
return gpsExtractor.getCoords();
344344
}
345345

346346
} catch (FileNotFoundException fnfe) {

app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java

+1-50
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import android.support.annotation.NonNull;
2222
import android.support.annotation.RequiresApi;
2323
import android.support.design.widget.FloatingActionButton;
24-
import android.support.design.widget.Snackbar;
2524
import android.support.graphics.drawable.VectorDrawableCompat;
26-
import android.support.v4.app.ActivityCompat;
2725
import android.support.v4.content.ContextCompat;
2826
import android.view.MenuItem;
2927
import android.view.View;
@@ -64,7 +62,6 @@
6462
import fr.free.nrw.commons.utils.ViewUtil;
6563
import timber.log.Timber;
6664

67-
import android.support.design.widget.FloatingActionButton;
6865
import static fr.free.nrw.commons.upload.ExistingFileAsync.Result.DUPLICATE_PROCEED;
6966
import static fr.free.nrw.commons.upload.ExistingFileAsync.Result.NO_DUPLICATE;
7067
import static fr.free.nrw.commons.upload.FileUtils.getSHA1;
@@ -78,7 +75,6 @@ public class ShareActivity
7875
extends AuthenticatedActivity
7976
implements SingleUploadFragment.OnUploadActionInitiated,
8077
OnCategoriesSaveHandler {
81-
private static final int REQUEST_PERM_ON_CREATE_LOCATION = 2;
8278
private static final int REQUEST_PERM_ON_SUBMIT_STORAGE = 4;
8379
//Had to make them class variables, to extract out the click listeners, also I see no harm in this
8480
final Rect startBounds = new Rect();
@@ -131,7 +127,6 @@ public class ShareActivity
131127
private String title;
132128
private String description;
133129
private String wikiDataEntityId;
134-
private Snackbar snackbar;
135130
private boolean duplicateCheckPassed = false;
136131
private boolean isNearbyUpload = false;
137132
private Animator CurrentAnimator;
@@ -276,22 +271,6 @@ R.drawable.ic_error_outline_black_24dp, getTheme()))
276271
Timber.d("Uri: %s", mediaUri.toString());
277272
Timber.d("Ext storage dir: %s", Environment.getExternalStorageDirectory());
278273

279-
useNewPermissions = false;
280-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
281-
useNewPermissions = true;
282-
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
283-
locationPermitted = true;
284-
}
285-
}
286-
287-
// Check location permissions if M or newer for category suggestions, request via snackbar if not present
288-
if (!locationPermitted) {
289-
requestPermissionUsingSnackBar(
290-
getString(R.string.location_permission_rationale),
291-
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
292-
REQUEST_PERM_ON_CREATE_LOCATION);
293-
}
294-
295274
SingleUploadFragment shareView = (SingleUploadFragment) getSupportFragmentManager().findFragmentByTag("shareView");
296275
categorizationFragment = (CategorizationFragment) getSupportFragmentManager().findFragmentByTag("categorization");
297276
if (shareView == null && categorizationFragment == null) {
@@ -392,7 +371,7 @@ protected boolean isNearbyUpload() {
392371
}
393372

394373
/**
395-
* Handles BOTH snackbar permission request (for location) and submit button permission request (for storage)
374+
* Handles submit button permission request (for storage)
396375
*
397376
* @param requestCode type of request
398377
* @param permissions permissions requested
@@ -401,41 +380,19 @@ protected boolean isNearbyUpload() {
401380
@Override
402381
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
403382
switch (requestCode) {
404-
case REQUEST_PERM_ON_CREATE_LOCATION: {
405-
if (grantResults.length >= 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
406-
locationPermitted = true;
407-
checkIfFileExists();
408-
}
409-
return;
410-
}
411-
412383
// Storage (from submit button) - this needs to be separate from (1) because only the
413384
// submit button should bring user to next screen
414385
case REQUEST_PERM_ON_SUBMIT_STORAGE: {
415386
if (grantResults.length >= 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
416-
//It is OK to call this at both (1) and (4) because if perm had been granted at
417-
//snackbar, user should not be prompted at submit button
418387
checkIfFileExists();
419388

420389
//Uploading only begins if storage permission granted from arrow icon
421390
uploadBegins();
422-
snackbar.dismiss();
423391
}
424392
}
425393
}
426394
}
427395

428-
/**
429-
* Displays Snackbar to ask for location permissions
430-
*/
431-
private Snackbar requestPermissionUsingSnackBar(String rationale, final String[] perms, final int code) {
432-
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), rationale,
433-
Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok,
434-
view -> ActivityCompat.requestPermissions(ShareActivity.this, perms, code));
435-
snackbar.show();
436-
return snackbar;
437-
}
438-
439396
/**
440397
* Check if file user wants to upload already exists on Commons
441398
*/
@@ -472,12 +429,6 @@ private void checkIfFileExists() {
472429
@Override
473430
public void onPause() {
474431
super.onPause();
475-
try {
476-
gpsObj.unregisterLocationManager();
477-
Timber.d("Unregistered locationManager");
478-
} catch (NullPointerException e) {
479-
Timber.d("locationManager does not exist, not unregistered");
480-
}
481432
}
482433

483434
@Override

0 commit comments

Comments
 (0)