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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 354 by torben, Tue Sep 29 18:24:23 2009 UTC revision 1143 by torben, Tue Sep 28 15:30:13 2010 UTC
# Line 3  package dk.thoerup.traininfo; Line 3  package dk.thoerup.traininfo;
3  import java.util.List;  import java.util.List;
4    
5  import android.content.Context;  import android.content.Context;
6  import android.location.Criteria;  import android.location.GpsSatellite;
7    import android.location.GpsStatus;
8  import android.location.Location;  import android.location.Location;
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;  
12  import android.util.Log;  import android.util.Log;
13    
14    
15  public class LocationLookup implements LocationListener{  public class LocationLookup implements LocationListener, GpsStatus.Listener {
16            
17          LocationManager locManager;          public enum LookupStates {
18          Context cntx;                  GOTLOCATION,
19          Handler hndl;                  NOPROVIDER,
20                    STARTED,
21                    IDLE
22            }
23    
24          Location savedLocation = null;          private LocationManager locManager;
25            private Context cntx;
26    
27            private Location lastKnownLocation = null;
28            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                    
# Line 37  public class LocationLookup implements L Line 48  public class LocationLookup implements L
48          {          {
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() {
65                    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                                    
86                  if (providers.size() > 0) {                  if (providers.size() > 0) {
87                          for(String provider : providers) {                          for(String provider : providers) {
88                                  Log.i("Provider", ""+provider);                                  Log.i("Provider", ""+provider);
89                                  if (provider.equalsIgnoreCase("gps"))                                  if (provider.equalsIgnoreCase("gps")) {
90                                          hasGps = true;                                          locManager.addGpsStatusListener(this);
91                                            hasGps = true;                                  
92                                    }
93    
94                                  locManager.requestLocationUpdates(provider, 0, 0, this);                                  locManager.requestLocationUpdates(provider, 0, 0, this);
95                                    Location tmpLastKnown = locManager.getLastKnownLocation(provider);
96                                    if (tmpLastKnown != null) {
97                                            saveLastKnownLocation(tmpLastKnown);
98                                    }
99                          }                          }
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                            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    
# Line 73  public class LocationLookup implements L Line 115  public class LocationLookup implements L
115                  if (hasGps) {                  if (hasGps) {
116                          if (!location.getProvider().equals("gps")) {                          if (!location.getProvider().equals("gps")) {
117                                  return; // at least give the gps a chance                                  return; // at least give the gps a chance
118                          } else if (location.getAccuracy() > 256) {                          } else if (location.getAccuracy() > 512) {
119                                  return; //if we have a gps provider lets wait for a more precise fix                                  return; //if we have a gps provider lets wait for a more precise fix
120                          }                          }
121    
122                  }                  }
123                  stopSearch();                  stopSearch();
124                  hndl.sendEmptyMessage(StationList.GOTLOCATION);                  state = LookupStates.GOTLOCATION;
125    
126            }
127            
128            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          public void stopSearch()          public void stopSearch()
139          {          {
140                  if (isSearching) {                  if (state == LookupStates.STARTED) {
141                          isSearching = false;                          state = LookupStates.IDLE;
142                            locManager.removeGpsStatusListener(this);
143                          locManager.removeUpdates(this);                          locManager.removeUpdates(this);
144                  }                  }
145          }          }
# Line 106  public class LocationLookup implements L Line 160  public class LocationLookup implements L
160                  // TODO Auto-generated method stub                  // TODO Auto-generated method stub
161    
162          }          }
163    
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                            }                      
173                            
174                            satCount = count;
175                    }
176                    
177            }
178  }  }

Legend:
Removed from v.354  
changed lines
  Added in v.1143

  ViewVC Help
Powered by ViewVC 1.1.20