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 lastKnownLocation = null; |
28 |
Location savedLocation = null; |
private Location savedLocation = null; |
29 |
|
private int satCount; |
30 |
|
|
31 |
|
private boolean hasGps; |
32 |
|
|
33 |
boolean isSearching = false; |
private LookupStates state; |
34 |
boolean hasGps; |
|
35 |
|
private long startTime; |
36 |
|
|
37 |
public LocationLookup(Context c, Handler h) { |
public LocationLookup(Context c) { |
38 |
|
state = LookupStates.IDLE; |
39 |
cntx = c; |
cntx = c; |
|
hndl = h; |
|
40 |
} |
} |
41 |
|
|
42 |
|
|
49 |
return savedLocation; |
return savedLocation; |
50 |
} |
} |
51 |
|
|
52 |
|
public boolean hasGps() { |
53 |
|
return hasGps; |
54 |
|
} |
55 |
|
|
56 |
|
public int getSatCount() { |
57 |
|
return satCount; |
58 |
|
} |
59 |
|
|
60 |
|
public LookupStates getState() { |
61 |
|
return state; |
62 |
|
} |
63 |
|
|
64 |
public Location getLastKnownLocation() { |
public Location getLastKnownLocation() { |
65 |
return lastKnownLocation; |
return lastKnownLocation; |
66 |
} |
} |
67 |
|
|
68 |
|
public long elapsedTime() { |
69 |
|
long now = android.os.SystemClock.elapsedRealtime(); |
70 |
|
|
71 |
|
return now - startTime; |
72 |
|
} |
73 |
|
|
74 |
public void locateStations() { |
public void locateStations() { |
75 |
|
|
76 |
isSearching = true; |
state = LookupStates.STARTED; |
77 |
|
|
78 |
hasGps = false; |
hasGps = false; |
79 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
80 |
|
satCount = 0; |
81 |
|
|
82 |
|
startTime = android.os.SystemClock.elapsedRealtime(); |
83 |
|
|
84 |
List<String> providers = locManager.getProviders(true); |
List<String> providers = locManager.getProviders(true); |
85 |
|
|
100 |
} else { |
} else { |
101 |
// message that no suitable provider was found |
// message that no suitable provider was found |
102 |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
103 |
hndl.sendEmptyMessage(StationList.LookupStates.NOPROVIDER.ordinal()); |
state = LookupStates.NOPROVIDER; |
104 |
|
|
105 |
} |
} |
106 |
} |
} |
107 |
@Override |
@Override |
108 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
|
if (isSearching == false) |
|
|
return; |
|
|
|
|
109 |
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()); |
110 |
|
|
111 |
|
|
121 |
|
|
122 |
} |
} |
123 |
stopSearch(); |
stopSearch(); |
124 |
hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal()); |
state = LookupStates.GOTLOCATION; |
125 |
|
|
126 |
} |
} |
127 |
|
|
128 |
private void saveLastKnownLocation(Location loc) { |
private void saveLastKnownLocation(Location loc) { |
137 |
|
|
138 |
public void stopSearch() |
public void stopSearch() |
139 |
{ |
{ |
140 |
if (isSearching) { |
if (state == LookupStates.STARTED) { |
141 |
isSearching = false; |
state = LookupStates.IDLE; |
142 |
locManager.removeGpsStatusListener(this); |
locManager.removeGpsStatusListener(this); |
143 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
144 |
} |
} |
169 |
GpsStatus status = locManager.getGpsStatus(null); |
GpsStatus status = locManager.getGpsStatus(null); |
170 |
for (GpsSatellite sat : status.getSatellites()) { |
for (GpsSatellite sat : status.getSatellites()) { |
171 |
count ++; |
count ++; |
172 |
} |
} |
173 |
|
|
174 |
Message msg = new Message(); |
satCount = count; |
|
msg.what = StationList.LookupStates.GPS_SAT_COUNT.ordinal(); |
|
|
msg.arg1 = count; |
|
|
hndl.sendMessage(msg); |
|
175 |
} |
} |
176 |
|
|
177 |
} |
} |