1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package com.mapbox.mapboxsdk.testapp.activity.userlocation;
import android.location.Location;
import android.os.Handler;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
/**
* Sample LocationEngine that provides mocked locations simulating GPS updates
*/
public class MockLocationEngine extends LocationEngine {
// Mocked data
private static final int UPDATE_INTERVAL_MS = 1000;
private static final double[][] locations = new double[][] {
new double[] {39.489309, -0.360415},
new double[] {39.492469, -0.358777},
new double[] {40.393285, -3.707260},
new double[] {40.394374, -3.707767},
new double[] {40.398012, -3.715943},
new double[] {40.416913, -3.703861}};
private Handler handler;
int currentIndex;
public MockLocationEngine() {
super();
}
@Override
public void activate() {
currentIndex = 0;
// "Connection" is immediate here
for (LocationEngineListener listener : locationListeners) {
listener.onConnected();
}
}
@Override
public void deactivate() {
handler = null;
}
@Override
public boolean isConnected() {
return true; // Always connected
}
@Override
public Location getLastLocation() {
return getNextLocation();
}
@Override
public void requestLocationUpdates() {
// Fake regular updates with a handler
handler = new Handler();
handler.postDelayed(new LocationUpdateRunnable(), UPDATE_INTERVAL_MS);
}
@Override
public void removeLocationUpdates() {
handler.removeCallbacksAndMessages(null);
}
private Location getNextLocation() {
// Build the next location and rotate the index
Location location = new Location(MockLocationEngine.class.getSimpleName());
location.setLatitude(locations[currentIndex][0]);
location.setLongitude(locations[currentIndex][1]);
currentIndex = (currentIndex == locations.length - 1 ? 0 : currentIndex + 1);
return location;
}
private class LocationUpdateRunnable implements Runnable {
@Override
public void run() {
// Notify of an update
Location location = getNextLocation();
for (LocationEngineListener listener : locationListeners) {
listener.onLocationChanged(location);
}
if (handler != null) {
// Schedule the next update
handler.postDelayed(new LocationUpdateRunnable(), UPDATE_INTERVAL_MS);
}
}
}
}
|