/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1143 - (hide annotations) (download)
Tue Sep 28 15:30:13 2010 UTC (13 years, 8 months ago) by torben
File size: 4110 byte(s)
Poll geo posisions from LocationLookup instead of pushing via a handler
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 lastKnownLocation = null;
28     private Location savedLocation = null;
29     private int satCount;
30 torben 286
31 torben 1143 private boolean hasGps;
32    
33     private LookupStates state;
34    
35     private long startTime;
36 torben 241
37 torben 1143 public LocationLookup(Context c) {
38     state = LookupStates.IDLE;
39 torben 237 cntx = c;
40     }
41    
42 torben 285
43     public boolean hasLocation() {
44     return savedLocation != null;
45     }
46 torben 319
47     public Location getLocation()
48     {
49     return savedLocation;
50     }
51 torben 488
52 torben 1143 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 torben 488 public Location getLastKnownLocation() {
65     return lastKnownLocation;
66     }
67 torben 1143
68     public long elapsedTime() {
69     long now = android.os.SystemClock.elapsedRealtime();
70    
71     return now - startTime;
72     }
73 torben 237
74     public void locateStations() {
75 torben 336
76 torben 1143 state = LookupStates.STARTED;
77 torben 336
78 torben 286 hasGps = false;
79 torben 237 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
80 torben 1143 satCount = 0;
81    
82     startTime = android.os.SystemClock.elapsedRealtime();
83 torben 286
84 torben 288 List<String> providers = locManager.getProviders(true);
85 torben 241
86 torben 286 if (providers.size() > 0) {
87     for(String provider : providers) {
88     Log.i("Provider", ""+provider);
89 torben 1142 if (provider.equalsIgnoreCase("gps")) {
90     locManager.addGpsStatusListener(this);
91     hasGps = true;
92     }
93    
94 torben 286 locManager.requestLocationUpdates(provider, 0, 0, this);
95 torben 570 Location tmpLastKnown = locManager.getLastKnownLocation(provider);
96     if (tmpLastKnown != null) {
97     saveLastKnownLocation(tmpLastKnown);
98     }
99 torben 286 }
100 torben 237 } else {
101     // message that no suitable provider was found
102 torben 1142 //hndl.sendEmptyMessage(StationList.NOPROVIDER);
103 torben 1143 state = LookupStates.NOPROVIDER;
104 torben 1142
105 torben 237 }
106     }
107     @Override
108 torben 1143 public void onLocationChanged(Location location) {
109 torben 237 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
110 torben 336
111 torben 237
112 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
113     savedLocation = new Location(location); //save a copy
114 torben 241
115 torben 286 if (hasGps) {
116     if (!location.getProvider().equals("gps")) {
117     return; // at least give the gps a chance
118 torben 948 } else if (location.getAccuracy() > 512) {
119 torben 286 return; //if we have a gps provider lets wait for a more precise fix
120     }
121 torben 237
122 torben 286 }
123 torben 336 stopSearch();
124 torben 1143 state = LookupStates.GOTLOCATION;
125    
126 torben 241 }
127 torben 336
128 torben 488 private void saveLastKnownLocation(Location loc) {
129     if (lastKnownLocation == null) {
130     lastKnownLocation = loc;
131     } else {
132     if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved
133     lastKnownLocation = loc;
134     }
135     }
136     }
137    
138 torben 336 public void stopSearch()
139     {
140 torben 1143 if (state == LookupStates.STARTED) {
141     state = LookupStates.IDLE;
142 torben 1142 locManager.removeGpsStatusListener(this);
143 torben 336 locManager.removeUpdates(this);
144     }
145     }
146 torben 319
147 torben 237
148     @Override
149     public void onProviderDisabled(String provider) {
150     }
151    
152    
153     @Override
154     public void onProviderEnabled(String provider) {
155     }
156    
157    
158     @Override
159     public void onStatusChanged(String provider, int status, Bundle extras) {
160     // TODO Auto-generated method stub
161    
162     }
163 torben 1142
164    
165     @Override //GpsStatus.Listener
166     public void onGpsStatusChanged(int event) {
167     if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
168     int count = 0;
169     GpsStatus status = locManager.getGpsStatus(null);
170     for (GpsSatellite sat : status.getSatellites()) {
171     count ++;
172 torben 1143 }
173 torben 1142
174 torben 1143 satCount = count;
175 torben 1142 }
176    
177     }
178 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20