Skip to content

Commit 41e11b7

Browse files
Merge pull request #1241 from commons-app/realTimeLocation
Real time location track
2 parents e38450b + aeaafa4 commit 41e11b7

18 files changed

+486
-273
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/ContributionController.java

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void startGalleryPick() {
8686
return;
8787
}
8888
Timber.d("startGalleryPick() called with pickImageIntent");
89+
8990
fragment.startActivityForResult(pickImageIntent, SELECT_FROM_GALLERY);
9091
}
9192

@@ -115,6 +116,7 @@ public void handleImagePicked(int requestCode, Intent data, boolean isDirectUplo
115116
if (isDirectUpload) {
116117
shareIntent.putExtra("isDirectUpload", true);
117118
}
119+
118120
break;
119121
default:
120122
break;

app/src/main/java/fr/free/nrw/commons/location/LatLng.java

+1
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,4 @@ public Uri getGmmIntentUri() {
159159
return Uri.parse("geo:0,0?q=" + latitude + "," + longitude);
160160
}
161161
}
162+

app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java

+47-26
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Bundle;
1111
import android.support.v4.app.ActivityCompat;
1212
import android.support.v4.content.ContextCompat;
13+
import android.util.Log;
1314

1415
import java.util.List;
1516
import java.util.concurrent.CopyOnWriteArrayList;
@@ -19,7 +20,8 @@
1920
public class LocationServiceManager implements LocationListener {
2021
public static final int LOCATION_REQUEST = 1;
2122

22-
private static final long MIN_LOCATION_UPDATE_REQUEST_TIME_IN_MILLIS = 2 * 60 * 1000;
23+
// Maybe these values can be improved for efficiency
24+
private static final long MIN_LOCATION_UPDATE_REQUEST_TIME_IN_MILLIS = 2 * 60 * 100;
2325
private static final long MIN_LOCATION_UPDATE_REQUEST_DISTANCE_IN_METERS = 10;
2426

2527
private Context context;
@@ -120,12 +122,14 @@ private boolean requestLocationUpdatesFromProvider(String locationProvider) {
120122
*
121123
* @param location the location to be tested
122124
* @param currentBestLocation the current best location
123-
* @return true if the given location is better
125+
* @return LOCATION_SIGNIFICANTLY_CHANGED if location changed significantly
126+
* LOCATION_SLIGHTLY_CHANGED if location changed slightly
124127
*/
125-
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
128+
protected LocationChangeType isBetterLocation(Location location, Location currentBestLocation) {
129+
126130
if (currentBestLocation == null) {
127131
// A new location is always better than no location
128-
return true;
132+
return LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED;
129133
}
130134

131135
// Check whether the new location fix is newer or older
@@ -134,15 +138,6 @@ protected boolean isBetterLocation(Location location, Location currentBestLocati
134138
boolean isSignificantlyOlder = timeDelta < -MIN_LOCATION_UPDATE_REQUEST_TIME_IN_MILLIS;
135139
boolean isNewer = timeDelta > 0;
136140

137-
// If it's been more than two minutes since the current location, use the new location
138-
// because the user has likely moved
139-
if (isSignificantlyNewer) {
140-
return true;
141-
// If the new location is more than two minutes older, it must be worse
142-
} else if (isSignificantlyOlder) {
143-
return false;
144-
}
145-
146141
// Check whether the new location fix is more or less accurate
147142
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
148143
boolean isLessAccurate = accuracyDelta > 0;
@@ -153,15 +148,28 @@ protected boolean isBetterLocation(Location location, Location currentBestLocati
153148
boolean isFromSameProvider = isSameProvider(location.getProvider(),
154149
currentBestLocation.getProvider());
155150

156-
// Determine location quality using a combination of timeliness and accuracy
157-
if (isMoreAccurate) {
158-
return true;
159-
} else if (isNewer && !isLessAccurate) {
160-
return true;
161-
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
162-
return true;
151+
float[] results = new float[5];
152+
Location.distanceBetween(
153+
currentBestLocation.getLatitude(),
154+
currentBestLocation.getLongitude(),
155+
location.getLatitude(),
156+
location.getLongitude(),
157+
results);
158+
159+
// If it's been more than two minutes since the current location, use the new location
160+
// because the user has likely moved
161+
if (isSignificantlyNewer
162+
|| isMoreAccurate
163+
|| (isNewer && !isLessAccurate)
164+
|| (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)) {
165+
if (results[0] < 1000) { // Means change is smaller than 1000 meter
166+
return LocationChangeType.LOCATION_SLIGHTLY_CHANGED;
167+
} else {
168+
return LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED;
169+
}
170+
} else{
171+
return LocationChangeType.LOCATION_NOT_CHANGED;
163172
}
164-
return false;
165173
}
166174

167175
/**
@@ -208,12 +216,19 @@ public void removeLocationListener(LocationUpdateListener listener) {
208216

209217
@Override
210218
public void onLocationChanged(Location location) {
211-
if (isBetterLocation(location, lastLocation)) {
212-
lastLocation = location;
213-
for (LocationUpdateListener listener : locationListeners) {
214-
listener.onLocationChanged(LatLng.from(lastLocation));
219+
if (isBetterLocation(location, lastLocation)
220+
.equals(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)) {
221+
lastLocation = location;
222+
for (LocationUpdateListener listener : locationListeners) {
223+
listener.onLocationChangedSignificantly(LatLng.from(lastLocation));
224+
}
225+
} else if (isBetterLocation(location, lastLocation)
226+
.equals(LocationChangeType.LOCATION_SLIGHTLY_CHANGED)) {
227+
lastLocation = location;
228+
for (LocationUpdateListener listener : locationListeners) {
229+
listener.onLocationChangedSlightly(LatLng.from(lastLocation));
230+
}
215231
}
216-
}
217232
}
218233

219234
@Override
@@ -230,4 +245,10 @@ public void onProviderEnabled(String provider) {
230245
public void onProviderDisabled(String provider) {
231246
Timber.d("Provider %s disabled", provider);
232247
}
248+
249+
public enum LocationChangeType{
250+
LOCATION_SIGNIFICANTLY_CHANGED, //Went out of borders of nearby markers
251+
LOCATION_SLIGHTLY_CHANGED, //User might be walking or driving
252+
LOCATION_NOT_CHANGED
253+
}
233254
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons.location;
22

33
public interface LocationUpdateListener {
4-
void onLocationChanged(LatLng latLng);
4+
void onLocationChangedSignificantly(LatLng latLng);
5+
void onLocationChangedSlightly(LatLng latLng);
56
}

0 commit comments

Comments
 (0)