/[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

android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java revision 286 by torben, Fri Aug 28 07:21:04 2009 UTC android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java revision 336 by torben, Wed Sep 23 12:51:49 2009 UTC
# Line 10  import android.location.LocationManager; Line 10  import android.location.LocationManager;
10  import android.os.Bundle;  import android.os.Bundle;
11  import android.os.Handler;  import android.os.Handler;
12  import android.util.Log;  import android.util.Log;
 import dk.thoerup.traininfo.provider.ProviderFactory;  
 import dk.thoerup.traininfo.provider.StationProvider;  
13    
14  public class StationLocator implements LocationListener{  
15    public class LocationLookup implements LocationListener{
16    
17          LocationManager locManager;          LocationManager locManager;
18          Context cntx;          Context cntx;
19          Handler hndl;          Handler hndl;
20    
         StationProvider provider;  
21          Location savedLocation = null;          Location savedLocation = null;
22                    
23            boolean isSearching = false;
24          boolean hasGps;          boolean hasGps;
25    
26          public StationLocator(Context c, Handler h) {          public LocationLookup(Context c, Handler h) {
27                  cntx = c;                  cntx = c;
28                  hndl = h;                  hndl = h;
                   
                 provider = ProviderFactory.getStationProvider();  
29          }          }
30                    
         public List<StationBean> getStations() {  
                 return provider.getStations();  
         }  
           
         public void abortLocationListener() {  
                 locManager.removeUpdates(this);  
         }  
31                    
32          public boolean hasLocation() {          public boolean hasLocation() {
33                  return savedLocation != null;                  return savedLocation != null;
34          }          }
35            
36            public Location getLocation()
37            {
38                    return savedLocation;
39            }
40    
41          public void locateStations() {          public void locateStations() {
42                    
43                    isSearching = true;
44                    
45                  hasGps = false;                  hasGps = false;
   
46                  locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
47    
48                  Criteria c = new Criteria();                  List<String> providers = locManager.getProviders(true);        
                 c.setAccuracy(Criteria.ACCURACY_FINE);  
                 List<String> providers = locManager.getProviders(true);  
                   
49                                    
50                  if (providers.size() > 0) {                  if (providers.size() > 0) {
51                          for(String provider : providers) {                          for(String provider : providers) {
# Line 62  public class StationLocator implements L Line 56  public class StationLocator implements L
56                          }                          }
57                  } else {                  } else {
58                          // message that no suitable provider was found                          // message that no suitable provider was found
59                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);                          hndl.sendEmptyMessage(StationList.NOPROVIDER);
60                  }                  }
61          }          }
62          @Override          @Override
63          public void onLocationChanged(Location location) {          public void onLocationChanged(Location location) {
64                    if (isSearching == false)
65                            return;
66                    
67                  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());
68                    
69    
70                  if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())                  if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
71                          savedLocation = new Location(location); //save a copy                          savedLocation = new Location(location); //save a copy
# Line 75  public class StationLocator implements L Line 73  public class StationLocator implements L
73                  if (hasGps) {                  if (hasGps) {
74                          if (!location.getProvider().equals("gps")) {                          if (!location.getProvider().equals("gps")) {
75                                  return; // at least give the gps a chance                                  return; // at least give the gps a chance
76                          } else if (location.getAccuracy() > 100) {                          } else if (location.getAccuracy() > 128) {
77                                  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
78                          }                          }
79    
80                  }                  }
81                    stopSearch();
82                  locManager.removeUpdates(this);                  hndl.sendEmptyMessage(StationList.GOTLOCATION);
                 hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);  
         }  
           
         public void findNearestStations() {  
                 findNearestStations(savedLocation);  
83          }          }
84                    
85          public void findNearestStations(Location location) {          public void stopSearch()
86                  provider.lookupStations(location);          {
87                                    if (isSearching) {
88                  if ( provider.getStations().size() > 0)                          isSearching = false;
89                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);                          locManager.removeUpdates(this);
90                  else                  }
                         hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);  
91          }          }
92    
93    
94          @Override          @Override
95          public void onProviderDisabled(String provider) {          public void onProviderDisabled(String provider) {
                 // TODO Auto-generated method stub  
   
96          }          }
97    
98    
99          @Override          @Override
100          public void onProviderEnabled(String provider) {          public void onProviderEnabled(String provider) {
                 // TODO Auto-generated method stub  
   
101          }          }
102    
103    
# Line 137  public class StationLocator implements L Line 125  public class StationLocator implements L
125                  hillerod.setLatitude(55.929177);                  hillerod.setLatitude(55.929177);
126                  hillerod.setLongitude(12.308095);                  hillerod.setLongitude(12.308095);
127                                    
128                    Location aarhus = new Location("gps"); //Aros
129                    aarhus.setLatitude(56.153828);
130                    aarhus.setLongitude(10.200369);
131                    
132                    
133                    
134                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
135                  if (lm.getProvider("gps2") == null)                  if (lm.getProvider("gps2") == null)
136                          lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );                          lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
137                  lm.setTestProviderEnabled("gps2", true);                  lm.setTestProviderEnabled("gps2", true);
138                  lm.setTestProviderLocation("gps2", hillerod);                  lm.setTestProviderLocation("gps2", kbh);
139          }          }
140                    
141          public static void removeMockLocation(Context cntx) {          public static void removeMockLocation(Context cntx) {

Legend:
Removed from v.286  
changed lines
  Added in v.336

  ViewVC Help
Powered by ViewVC 1.1.20