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