1 |
torben |
237 |
package dk.thoerup.traininfo;
|
2 |
|
|
|
3 |
|
|
import java.util.List;
|
4 |
|
|
|
5 |
|
|
import android.content.Context;
|
6 |
torben |
1142 |
import android.location.GpsSatellite;
|
7 |
|
|
import android.location.GpsStatus;
|
8 |
torben |
237 |
import android.location.Location;
|
9 |
|
|
import android.location.LocationListener;
|
10 |
|
|
import android.location.LocationManager;
|
11 |
|
|
import android.os.Bundle;
|
12 |
|
|
import android.os.Handler;
|
13 |
torben |
1142 |
import android.os.Message;
|
14 |
torben |
237 |
import android.util.Log;
|
15 |
|
|
|
16 |
|
|
|
17 |
torben |
1142 |
public class LocationLookup implements LocationListener, GpsStatus.Listener {
|
18 |
torben |
319 |
|
19 |
torben |
237 |
LocationManager locManager;
|
20 |
|
|
Context cntx;
|
21 |
|
|
Handler hndl;
|
22 |
|
|
|
23 |
torben |
488 |
Location lastKnownLocation = null;
|
24 |
torben |
241 |
Location savedLocation = null;
|
25 |
torben |
286 |
|
26 |
torben |
336 |
boolean isSearching = false;
|
27 |
torben |
286 |
boolean hasGps;
|
28 |
torben |
241 |
|
29 |
torben |
319 |
public LocationLookup(Context c, Handler h) {
|
30 |
torben |
237 |
cntx = c;
|
31 |
|
|
hndl = h;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
torben |
285 |
|
35 |
|
|
public boolean hasLocation() {
|
36 |
|
|
return savedLocation != null;
|
37 |
|
|
}
|
38 |
torben |
319 |
|
39 |
|
|
public Location getLocation()
|
40 |
|
|
{
|
41 |
|
|
return savedLocation;
|
42 |
|
|
}
|
43 |
torben |
488 |
|
44 |
|
|
public Location getLastKnownLocation() {
|
45 |
|
|
return lastKnownLocation;
|
46 |
|
|
}
|
47 |
torben |
237 |
|
48 |
|
|
public void locateStations() {
|
49 |
torben |
336 |
|
50 |
|
|
isSearching = true;
|
51 |
|
|
|
52 |
torben |
286 |
hasGps = false;
|
53 |
torben |
237 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
|
54 |
torben |
286 |
|
55 |
torben |
288 |
List<String> providers = locManager.getProviders(true);
|
56 |
torben |
241 |
|
57 |
torben |
286 |
if (providers.size() > 0) {
|
58 |
|
|
for(String provider : providers) {
|
59 |
|
|
Log.i("Provider", ""+provider);
|
60 |
torben |
1142 |
if (provider.equalsIgnoreCase("gps")) {
|
61 |
|
|
locManager.addGpsStatusListener(this);
|
62 |
|
|
hasGps = true;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
torben |
286 |
locManager.requestLocationUpdates(provider, 0, 0, this);
|
66 |
torben |
570 |
Location tmpLastKnown = locManager.getLastKnownLocation(provider);
|
67 |
|
|
if (tmpLastKnown != null) {
|
68 |
|
|
saveLastKnownLocation(tmpLastKnown);
|
69 |
|
|
}
|
70 |
torben |
286 |
}
|
71 |
torben |
237 |
} else {
|
72 |
|
|
// message that no suitable provider was found
|
73 |
torben |
1142 |
//hndl.sendEmptyMessage(StationList.NOPROVIDER);
|
74 |
|
|
hndl.sendEmptyMessage(StationList.LookupStates.NOPROVIDER.ordinal());
|
75 |
|
|
|
76 |
torben |
237 |
}
|
77 |
|
|
}
|
78 |
|
|
@Override
|
79 |
|
|
public void onLocationChanged(Location location) {
|
80 |
torben |
336 |
if (isSearching == false)
|
81 |
|
|
return;
|
82 |
|
|
|
83 |
torben |
237 |
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
|
84 |
torben |
336 |
|
85 |
torben |
237 |
|
86 |
torben |
286 |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
|
87 |
|
|
savedLocation = new Location(location); //save a copy
|
88 |
torben |
241 |
|
89 |
torben |
286 |
if (hasGps) {
|
90 |
|
|
if (!location.getProvider().equals("gps")) {
|
91 |
|
|
return; // at least give the gps a chance
|
92 |
torben |
948 |
} else if (location.getAccuracy() > 512) {
|
93 |
torben |
286 |
return; //if we have a gps provider lets wait for a more precise fix
|
94 |
|
|
}
|
95 |
torben |
237 |
|
96 |
torben |
286 |
}
|
97 |
torben |
336 |
stopSearch();
|
98 |
torben |
1142 |
hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal());
|
99 |
torben |
241 |
}
|
100 |
torben |
336 |
|
101 |
torben |
488 |
private void saveLastKnownLocation(Location loc) {
|
102 |
|
|
if (lastKnownLocation == null) {
|
103 |
|
|
lastKnownLocation = loc;
|
104 |
|
|
} else {
|
105 |
|
|
if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved
|
106 |
|
|
lastKnownLocation = loc;
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
|
111 |
torben |
336 |
public void stopSearch()
|
112 |
|
|
{
|
113 |
|
|
if (isSearching) {
|
114 |
|
|
isSearching = false;
|
115 |
torben |
1142 |
locManager.removeGpsStatusListener(this);
|
116 |
torben |
336 |
locManager.removeUpdates(this);
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
torben |
319 |
|
120 |
torben |
237 |
|
121 |
|
|
@Override
|
122 |
|
|
public void onProviderDisabled(String provider) {
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
@Override
|
127 |
|
|
public void onProviderEnabled(String provider) {
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
@Override
|
132 |
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
133 |
|
|
// TODO Auto-generated method stub
|
134 |
|
|
|
135 |
|
|
}
|
136 |
torben |
1142 |
|
137 |
|
|
|
138 |
|
|
@Override //GpsStatus.Listener
|
139 |
|
|
public void onGpsStatusChanged(int event) {
|
140 |
|
|
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
|
141 |
|
|
int count = 0;
|
142 |
|
|
GpsStatus status = locManager.getGpsStatus(null);
|
143 |
|
|
for (GpsSatellite sat : status.getSatellites()) {
|
144 |
|
|
count ++;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
Message msg = new Message();
|
148 |
|
|
msg.what = StationList.LookupStates.GPS_SAT_COUNT.ordinal();
|
149 |
|
|
msg.arg1 = count;
|
150 |
|
|
hndl.sendMessage(msg);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
}
|
154 |
torben |
237 |
}
|