9 |
import android.location.LocationListener; |
import android.location.LocationListener; |
10 |
import android.location.LocationManager; |
import android.location.LocationManager; |
11 |
import android.os.Bundle; |
import android.os.Bundle; |
|
import android.os.Handler; |
|
|
import android.os.Message; |
|
12 |
import android.util.Log; |
import android.util.Log; |
13 |
|
|
14 |
|
|
15 |
public class LocationLookup implements LocationListener, GpsStatus.Listener { |
public class LocationLookup implements LocationListener, GpsStatus.Listener { |
16 |
|
|
17 |
|
public enum LookupStates { |
18 |
|
GOTLOCATION, |
19 |
|
NOPROVIDER, |
20 |
|
STARTED, |
21 |
|
IDLE |
22 |
|
} |
23 |
|
|
24 |
LocationManager locManager; |
private LocationManager locManager; |
25 |
Context cntx; |
private Context cntx; |
|
Handler hndl; |
|
26 |
|
|
27 |
Location lastKnownLocation = null; |
private Location savedLocation = null; |
28 |
Location savedLocation = null; |
private int satCount; |
29 |
|
|
30 |
|
private boolean hasGps; |
31 |
|
|
32 |
boolean isSearching = false; |
private LookupStates state; |
33 |
boolean hasGps; |
|
34 |
|
private long startTime; |
35 |
|
|
36 |
public LocationLookup(Context c, Handler h) { |
public LocationLookup(Context c) { |
37 |
|
state = LookupStates.IDLE; |
38 |
cntx = c; |
cntx = c; |
|
hndl = h; |
|
39 |
} |
} |
40 |
|
|
41 |
|
|
48 |
return savedLocation; |
return savedLocation; |
49 |
} |
} |
50 |
|
|
51 |
public Location getLastKnownLocation() { |
public boolean hasGps() { |
52 |
return lastKnownLocation; |
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 |
|
|
70 |
public void locateStations() { |
public void locateStations() { |
71 |
|
|
72 |
isSearching = true; |
state = LookupStates.STARTED; |
73 |
|
|
74 |
hasGps = false; |
hasGps = false; |
75 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
76 |
|
satCount = 0; |
77 |
|
|
78 |
|
startTime = android.os.SystemClock.elapsedRealtime(); |
79 |
|
|
80 |
List<String> providers = locManager.getProviders(true); |
List<String> providers = locManager.getProviders(true); |
81 |
|
|
88 |
} |
} |
89 |
|
|
90 |
locManager.requestLocationUpdates(provider, 0, 0, this); |
locManager.requestLocationUpdates(provider, 0, 0, this); |
|
Location tmpLastKnown = locManager.getLastKnownLocation(provider); |
|
|
if (tmpLastKnown != null) { |
|
|
saveLastKnownLocation(tmpLastKnown); |
|
|
} |
|
91 |
} |
} |
92 |
} else { |
} else { |
93 |
// message that no suitable provider was found |
// message that no suitable provider was found |
94 |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
95 |
hndl.sendEmptyMessage(StationList.LookupStates.NOPROVIDER.ordinal()); |
state = LookupStates.NOPROVIDER; |
96 |
|
|
97 |
} |
} |
98 |
} |
} |
99 |
@Override |
@Override |
100 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
|
if (isSearching == false) |
|
|
return; |
|
|
|
|
101 |
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); |
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); |
102 |
|
|
103 |
|
|
113 |
|
|
114 |
} |
} |
115 |
stopSearch(); |
stopSearch(); |
116 |
hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal()); |
state = LookupStates.GOTLOCATION; |
117 |
|
|
118 |
} |
} |
119 |
|
|
|
private void saveLastKnownLocation(Location loc) { |
|
|
if (lastKnownLocation == null) { |
|
|
lastKnownLocation = loc; |
|
|
} else { |
|
|
if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved |
|
|
lastKnownLocation = loc; |
|
|
} |
|
|
} |
|
|
} |
|
120 |
|
|
121 |
public void stopSearch() |
public void stopSearch() |
122 |
{ |
{ |
123 |
if (isSearching) { |
if (state == LookupStates.STARTED) { |
124 |
isSearching = false; |
state = LookupStates.IDLE; |
125 |
locManager.removeGpsStatusListener(this); |
locManager.removeGpsStatusListener(this); |
126 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
127 |
} |
} |
152 |
GpsStatus status = locManager.getGpsStatus(null); |
GpsStatus status = locManager.getGpsStatus(null); |
153 |
for (GpsSatellite sat : status.getSatellites()) { |
for (GpsSatellite sat : status.getSatellites()) { |
154 |
count ++; |
count ++; |
155 |
} |
} |
156 |
|
|
157 |
Message msg = new Message(); |
satCount = count; |
|
msg.what = StationList.LookupStates.GPS_SAT_COUNT.ordinal(); |
|
|
msg.arg1 = count; |
|
|
hndl.sendMessage(msg); |
|
158 |
} |
} |
159 |
|
|
160 |
} |
} |