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 |
torben |
253 |
import dk.thoerup.traininfo.provider.ProviderFactory;
|
14 |
|
|
import dk.thoerup.traininfo.provider.StationProvider;
|
15 |
torben |
237 |
|
16 |
|
|
public class StationLocator implements LocationListener{
|
17 |
|
|
|
18 |
|
|
LocationManager locManager;
|
19 |
|
|
Context cntx;
|
20 |
|
|
Handler hndl;
|
21 |
|
|
|
22 |
torben |
254 |
StationProvider provider;
|
23 |
torben |
241 |
Location savedLocation = null;
|
24 |
|
|
|
25 |
torben |
237 |
public StationLocator(Context c, Handler h) {
|
26 |
|
|
cntx = c;
|
27 |
|
|
hndl = h;
|
28 |
torben |
254 |
|
29 |
|
|
provider = ProviderFactory.getStationProvider();
|
30 |
torben |
237 |
}
|
31 |
|
|
|
32 |
|
|
public List<StationBean> getStations() {
|
33 |
torben |
254 |
return provider.getStations();
|
34 |
torben |
237 |
}
|
35 |
|
|
|
36 |
|
|
public void abortLocationListener() {
|
37 |
|
|
locManager.removeUpdates(this);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
public void locateStations() {
|
41 |
|
|
//http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&lstkp=0&rsz=small&hl=en&source=gsc&gss=.com&sig=fadf0e8d483d0f70bea11d5905010a16&q=Train%20station&near=56.377424%2C9.656695&key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q&v=1.0&nocache=1249640467498
|
42 |
|
|
|
43 |
|
|
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
44 |
|
|
/*//testcode
|
45 |
|
|
List<String> provs = locManager.getAllProviders();
|
46 |
|
|
for (String p : provs) {
|
47 |
|
|
Log.e("Provider", p);
|
48 |
|
|
}
|
49 |
|
|
provs = locManager.getProviders(true);
|
50 |
|
|
for (String p : provs) {
|
51 |
|
|
Log.e("ActiveProvider", p);
|
52 |
|
|
}
|
53 |
|
|
*/
|
54 |
|
|
Criteria c = new Criteria();
|
55 |
|
|
c.setAccuracy(Criteria.ACCURACY_FINE);
|
56 |
|
|
String bestProv = locManager.getBestProvider(c, true);
|
57 |
torben |
242 |
Log.i("BestProvider", ""+bestProv);
|
58 |
torben |
241 |
|
59 |
torben |
237 |
|
60 |
|
|
if (bestProv != null) {
|
61 |
torben |
241 |
savedLocation = locManager.getLastKnownLocation(bestProv);
|
62 |
torben |
237 |
locManager.requestLocationUpdates(bestProv, 0, 0, this);
|
63 |
|
|
} else {
|
64 |
|
|
// message that no suitable provider was found
|
65 |
|
|
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
|
66 |
|
|
}
|
67 |
|
|
}
|
68 |
|
|
@Override
|
69 |
|
|
public void onLocationChanged(Location location) {
|
70 |
|
|
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
|
71 |
|
|
|
72 |
torben |
241 |
savedLocation = new Location(location); //save a copy
|
73 |
|
|
|
74 |
torben |
237 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
|
75 |
|
|
return;
|
76 |
|
|
|
77 |
|
|
locManager.removeUpdates(this);
|
78 |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
|
79 |
torben |
241 |
}
|
80 |
|
|
|
81 |
|
|
public void findNearestStations() {
|
82 |
|
|
findNearestStations(savedLocation);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
public void findNearestStations(Location location) {
|
86 |
torben |
254 |
provider.lookupStations(location);
|
87 |
torben |
253 |
|
88 |
torben |
254 |
if ( provider.getStations().size() > 0)
|
89 |
torben |
237 |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
|
90 |
torben |
253 |
else
|
91 |
torben |
237 |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
@Override
|
96 |
|
|
public void onProviderDisabled(String provider) {
|
97 |
|
|
// TODO Auto-generated method stub
|
98 |
|
|
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
@Override
|
103 |
|
|
public void onProviderEnabled(String provider) {
|
104 |
|
|
// TODO Auto-generated method stub
|
105 |
|
|
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
@Override
|
110 |
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
111 |
|
|
// TODO Auto-generated method stub
|
112 |
|
|
|
113 |
|
|
}
|
114 |
torben |
253 |
|
115 |
torben |
252 |
|
116 |
torben |
241 |
public static void injectMockLocation(Context cntx) {
|
117 |
torben |
252 |
Location odder = new Location("gps2");
|
118 |
|
|
odder.setLatitude(55.976632);
|
119 |
|
|
odder.setLongitude(10.16407);
|
120 |
torben |
241 |
|
121 |
torben |
252 |
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
|
122 |
|
|
kbh.setLatitude(55.675092);
|
123 |
|
|
kbh.setLongitude(12.578573);
|
124 |
|
|
|
125 |
torben |
253 |
Location bjbro = new Location("gps2");
|
126 |
|
|
bjbro.setLatitude(56.380745);
|
127 |
|
|
bjbro.setLongitude(9.655609);
|
128 |
|
|
|
129 |
torben |
256 |
Location hillerod = new Location("gps");
|
130 |
|
|
hillerod.setLatitude(55.929177);
|
131 |
|
|
hillerod.setLongitude(12.308095);
|
132 |
torben |
253 |
|
133 |
torben |
241 |
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
134 |
torben |
252 |
if (lm.getProvider("gps2") == null)
|
135 |
|
|
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
|
136 |
torben |
241 |
lm.setTestProviderEnabled("gps2", true);
|
137 |
torben |
256 |
lm.setTestProviderLocation("gps2", hillerod);
|
138 |
torben |
241 |
}
|
139 |
torben |
252 |
|
140 |
|
|
public static void removeMockLocation(Context cntx) {
|
141 |
|
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
142 |
|
|
if (lm.getProvider("gps2") != null)
|
143 |
|
|
lm.removeTestProvider("gps2");
|
144 |
|
|
}
|
145 |
torben |
237 |
}
|