1 |
torben |
237 |
package dk.thoerup.traininfo;
|
2 |
|
|
|
3 |
|
|
import java.util.List;
|
4 |
|
|
|
5 |
|
|
import android.content.Context;
|
6 |
|
|
import android.location.Criteria;
|
7 |
|
|
import android.location.Location;
|
8 |
|
|
import android.location.LocationListener;
|
9 |
|
|
import android.location.LocationManager;
|
10 |
|
|
import android.os.Bundle;
|
11 |
|
|
import android.os.Handler;
|
12 |
|
|
import android.util.Log;
|
13 |
|
|
|
14 |
|
|
|
15 |
torben |
319 |
public class LocationLookup implements LocationListener{
|
16 |
|
|
|
17 |
torben |
237 |
LocationManager locManager;
|
18 |
|
|
Context cntx;
|
19 |
|
|
Handler hndl;
|
20 |
|
|
|
21 |
torben |
241 |
Location savedLocation = null;
|
22 |
torben |
286 |
|
23 |
torben |
336 |
boolean isSearching = false;
|
24 |
torben |
286 |
boolean hasGps;
|
25 |
torben |
241 |
|
26 |
torben |
319 |
public LocationLookup(Context c, Handler h) {
|
27 |
torben |
237 |
cntx = c;
|
28 |
|
|
hndl = h;
|
29 |
|
|
}
|
30 |
|
|
|
31 |
torben |
285 |
|
32 |
|
|
public boolean hasLocation() {
|
33 |
|
|
return savedLocation != null;
|
34 |
|
|
}
|
35 |
torben |
319 |
|
36 |
|
|
public Location getLocation()
|
37 |
|
|
{
|
38 |
|
|
return savedLocation;
|
39 |
|
|
}
|
40 |
torben |
237 |
|
41 |
|
|
public void locateStations() {
|
42 |
torben |
336 |
|
43 |
|
|
isSearching = true;
|
44 |
|
|
|
45 |
torben |
286 |
hasGps = false;
|
46 |
torben |
237 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
47 |
torben |
286 |
|
48 |
torben |
288 |
List<String> providers = locManager.getProviders(true);
|
49 |
torben |
241 |
|
50 |
torben |
286 |
if (providers.size() > 0) {
|
51 |
|
|
for(String provider : providers) {
|
52 |
|
|
Log.i("Provider", ""+provider);
|
53 |
|
|
if (provider.equalsIgnoreCase("gps"))
|
54 |
|
|
hasGps = true;
|
55 |
|
|
locManager.requestLocationUpdates(provider, 0, 0, this);
|
56 |
|
|
}
|
57 |
torben |
237 |
} else {
|
58 |
|
|
// message that no suitable provider was found
|
59 |
torben |
336 |
hndl.sendEmptyMessage(StationList.NOPROVIDER);
|
60 |
torben |
237 |
}
|
61 |
|
|
}
|
62 |
|
|
@Override
|
63 |
|
|
public void onLocationChanged(Location location) {
|
64 |
torben |
336 |
if (isSearching == false)
|
65 |
|
|
return;
|
66 |
|
|
|
67 |
torben |
237 |
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
|
68 |
torben |
336 |
|
69 |
torben |
237 |
|
70 |
torben |
286 |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
|
71 |
|
|
savedLocation = new Location(location); //save a copy
|
72 |
torben |
241 |
|
73 |
torben |
286 |
if (hasGps) {
|
74 |
|
|
if (!location.getProvider().equals("gps")) {
|
75 |
|
|
return; // at least give the gps a chance
|
76 |
torben |
341 |
} else if (location.getAccuracy() > 256) {
|
77 |
torben |
286 |
return; //if we have a gps provider lets wait for a more precise fix
|
78 |
|
|
}
|
79 |
torben |
237 |
|
80 |
torben |
286 |
}
|
81 |
torben |
336 |
stopSearch();
|
82 |
|
|
hndl.sendEmptyMessage(StationList.GOTLOCATION);
|
83 |
torben |
241 |
}
|
84 |
torben |
336 |
|
85 |
|
|
public void stopSearch()
|
86 |
|
|
{
|
87 |
|
|
if (isSearching) {
|
88 |
|
|
isSearching = false;
|
89 |
|
|
locManager.removeUpdates(this);
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
torben |
319 |
|
93 |
torben |
237 |
|
94 |
|
|
@Override
|
95 |
|
|
public void onProviderDisabled(String provider) {
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
@Override
|
100 |
|
|
public void onProviderEnabled(String provider) {
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
@Override
|
105 |
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
106 |
|
|
// TODO Auto-generated method stub
|
107 |
|
|
|
108 |
|
|
}
|
109 |
torben |
253 |
|
110 |
torben |
252 |
|
111 |
torben |
241 |
public static void injectMockLocation(Context cntx) {
|
112 |
torben |
252 |
Location odder = new Location("gps2");
|
113 |
|
|
odder.setLatitude(55.976632);
|
114 |
|
|
odder.setLongitude(10.16407);
|
115 |
torben |
241 |
|
116 |
torben |
252 |
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
|
117 |
|
|
kbh.setLatitude(55.675092);
|
118 |
|
|
kbh.setLongitude(12.578573);
|
119 |
|
|
|
120 |
torben |
253 |
Location bjbro = new Location("gps2");
|
121 |
|
|
bjbro.setLatitude(56.380745);
|
122 |
|
|
bjbro.setLongitude(9.655609);
|
123 |
|
|
|
124 |
torben |
256 |
Location hillerod = new Location("gps");
|
125 |
|
|
hillerod.setLatitude(55.929177);
|
126 |
|
|
hillerod.setLongitude(12.308095);
|
127 |
torben |
253 |
|
128 |
torben |
289 |
Location aarhus = new Location("gps"); //Aros
|
129 |
|
|
aarhus.setLatitude(56.153828);
|
130 |
|
|
aarhus.setLongitude(10.200369);
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
torben |
241 |
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
135 |
torben |
252 |
if (lm.getProvider("gps2") == null)
|
136 |
|
|
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
|
137 |
torben |
241 |
lm.setTestProviderEnabled("gps2", true);
|
138 |
torben |
310 |
lm.setTestProviderLocation("gps2", kbh);
|
139 |
torben |
241 |
}
|
140 |
torben |
252 |
|
141 |
|
|
public static void removeMockLocation(Context cntx) {
|
142 |
|
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
143 |
|
|
if (lm.getProvider("gps2") != null)
|
144 |
|
|
lm.removeTestProvider("gps2");
|
145 |
|
|
}
|
146 |
torben |
237 |
}
|