1
1
package fr .free .nrw .commons .upload ;
2
2
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 ;
9
3
import android .media .ExifInterface ;
10
- import android .os .Bundle ;
11
4
import android .support .annotation .NonNull ;
12
5
import android .support .annotation .Nullable ;
13
6
import android .support .annotation .RequiresApi ;
19
12
20
13
/**
21
14
* 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.
24
16
*/
25
17
public class GPSExtractor {
26
18
27
- private final Context context ;
28
- private SharedPreferences prefs ;
29
19
private ExifInterface exif ;
30
20
private double decLatitude ;
31
21
private double decLongitude ;
32
- private Double currentLatitude = null ;
33
- private Double currentLongitude = null ;
34
22
public boolean imageCoordsExists ;
35
- private MyLocationListener myLocationListener ;
36
- private LocationManager locationManager ;
37
23
38
24
/**
39
25
* Construct from the file descriptor of the image (only for API 24 or newer).
40
26
* @param fileDescriptor the file descriptor of the image
41
- * @param context the context
42
27
*/
43
28
@ 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 ) {
47
30
try {
48
31
exif = new ExifInterface (fileDescriptor );
49
32
} catch (IOException | IllegalArgumentException e ) {
@@ -54,96 +37,32 @@ public GPSExtractor(@NonNull FileDescriptor fileDescriptor, Context context, Sha
54
37
/**
55
38
* Construct from the file path of the image.
56
39
* @param path file path of the image
57
- * @param context the context
40
+ *
58
41
*/
59
- public GPSExtractor (@ NonNull String path , Context context , SharedPreferences prefs ) {
60
- this .prefs = prefs ;
42
+ public GPSExtractor (@ NonNull String path ) {
61
43
try {
62
44
exif = new ExifInterface (path );
63
45
} catch (IOException | IllegalArgumentException e ) {
64
46
Timber .w (e );
65
47
}
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
- }
107
48
}
108
49
109
50
/**
110
51
* 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
112
52
* @return coordinates as string (needs to be passed as a String in API query)
113
53
*/
114
54
@ Nullable
115
- public String getCoords (boolean useGPS ) {
55
+ public String getCoords () {
116
56
String latitude ;
117
57
String longitude ;
118
58
String latitudeRef ;
119
59
String longitudeRef ;
120
60
String decimalCoords ;
121
61
122
62
//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
123
64
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 ;
147
66
} else {
148
67
//If image has EXIF data, extract image coords
149
68
imageCoordsExists = true ;
@@ -166,33 +85,6 @@ public String getCoords(boolean useGPS) {
166
85
}
167
86
}
168
87
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
-
196
88
public double getDecLatitude () {
197
89
return decLatitude ;
198
90
}
0 commit comments