/[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 488 by torben, Thu Oct 29 13:42:57 2009 UTC revision 1209 by torben, Sat Dec 25 21:44:00 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.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            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            private LookupStates state;
33                    
34          boolean isSearching = false;          private long startTime;
         boolean hasGps;  
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                    
# Line 38  public class LocationLookup implements L Line 48  public class LocationLookup implements L
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                                    
82                  if (providers.size() > 0) {                  boolean hasProvider = false;
83                          for(String provider : providers) {                  
84                                  Log.i("Provider", ""+provider);                  for(String provider : providers) {
85                                  if (provider.equalsIgnoreCase("gps"))                          if (provider.equals( LocationManager.PASSIVE_PROVIDER)) {
86                                          hasGps = true;                                  continue;
                                 locManager.requestLocationUpdates(provider, 0, 0, this);  
                                   
                                 saveLastKnownLocation(locManager.getLastKnownLocation(provider));  
87                          }                          }
88                  } else {                          
89                            Log.i("Provider", ""+provider);
90                            hasProvider = true;
91                            if (provider.equalsIgnoreCase("gps")) {
92                                    locManager.addGpsStatusListener(this);
93                                    hasGps = true;                                  
94                            }
95    
96                            locManager.requestLocationUpdates(provider, 0, 0, this);
97                    }
98                            
99                    if (hasProvider == false) {
100                          // message that no suitable provider was found                          // message that no suitable provider was found
101                          hndl.sendEmptyMessage(StationList.NOPROVIDER);                          //hndl.sendEmptyMessage(StationList.NOPROVIDER);
102                            state = LookupStates.NOPROVIDER;
103                            
104                  }                  }
105          }          }
106          @Override          @Override
107          public void onLocationChanged(Location location) {          public void onLocationChanged(Location location) {              
                 if (isSearching == false)  
                         return;  
                   
108                  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());
109                                    
110    
# Line 79  public class LocationLookup implements L Line 114  public class LocationLookup implements L
114                  if (hasGps) {                  if (hasGps) {
115                          if (!location.getProvider().equals("gps")) {                          if (!location.getProvider().equals("gps")) {
116                                  return; // at least give the gps a chance                                  return; // at least give the gps a chance
117                          } else if (location.getAccuracy() > 256) {                          } else if (location.getAccuracy() > 512) {
118                                  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
119                          }                          }
120    
121                  }                  }
122                  stopSearch();                  stopSearch();
123                  hndl.sendEmptyMessage(StationList.GOTLOCATION);                  state = LookupStates.GOTLOCATION;
124    
125          }          }
126                    
         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;  
                         }  
                 }  
         }  
127                    
128          public void stopSearch()          public void stopSearch()
129          {          {
130                  if (isSearching) {                  if (state == LookupStates.STARTED) {
131                          isSearching = false;                          state = LookupStates.IDLE;
132                            locManager.removeGpsStatusListener(this);
133                          locManager.removeUpdates(this);                          locManager.removeUpdates(this);
134                  }                  }
135          }          }
# Line 122  public class LocationLookup implements L Line 150  public class LocationLookup implements L
150                  // TODO Auto-generated method stub                  // TODO Auto-generated method stub
151    
152          }          }
153    
154    
155            @Override //GpsStatus.Listener
156            public void onGpsStatusChanged(int event) {
157                    if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
158                            int count = 0;
159                            GpsStatus status =  locManager.getGpsStatus(null);
160                            for (GpsSatellite sat : status.getSatellites()) {
161                                    count ++;
162                            }                      
163                            
164                            satCount = count;
165                    }
166                    
167            }
168  }  }

Legend:
Removed from v.488  
changed lines
  Added in v.1209

  ViewVC Help
Powered by ViewVC 1.1.20