/[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 319 by torben, Fri Sep 11 12:24:53 2009 UTC revision 354 by torben, Tue Sep 29 18:24:23 2009 UTC
# Line 20  public class LocationLookup implements L Line 20  public class LocationLookup implements L
20    
21          Location savedLocation = null;          Location savedLocation = null;
22                    
23            boolean isSearching = false;
24          boolean hasGps;          boolean hasGps;
25    
26          public LocationLookup(Context c, Handler h) {          public LocationLookup(Context c, Handler h) {
27                  cntx = c;                  cntx = c;
28                  hndl = h;                  hndl = h;
                   
29          }          }
30                    
         public void abortLocationListener() {  
                 locManager.removeUpdates(this);  
         }  
31                    
32          public boolean hasLocation() {          public boolean hasLocation() {
33                  return savedLocation != null;                  return savedLocation != null;
# Line 42  public class LocationLookup implements L Line 39  public class LocationLookup implements L
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    
# Line 56  public class LocationLookup implements L Line 56  public class LocationLookup 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 69  public class LocationLookup implements L Line 73  public class LocationLookup 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() > 256) {
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);
83                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);          }
84            
85            public void stopSearch()
86            {
87                    if (isSearching) {
88                            isSearching = false;
89                            locManager.removeUpdates(this);
90                    }
91          }          }
         /*  
   
         public void findNearestStations(Location location) {  
                 provider.lookupStations(location);  
                   
                 if ( provider.getStations().size() > 0)  
                         hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);  
                 else  
                         hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);  
         }*/  
92    
93    
94          @Override          @Override
# Line 105  public class LocationLookup implements L Line 106  public class LocationLookup implements L
106                  // TODO Auto-generated method stub                  // TODO Auto-generated method stub
107    
108          }          }
           
   
         public static void injectMockLocation(Context cntx) {  
                 Location odder = new Location("gps2");  
                 odder.setLatitude(55.976632);  
                 odder.setLongitude(10.16407);  
                   
                 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573  
                 kbh.setLatitude(55.675092);  
                 kbh.setLongitude(12.578573);  
                   
                 Location bjbro = new Location("gps2");  
                 bjbro.setLatitude(56.380745);  
                 bjbro.setLongitude(9.655609);  
                   
                 Location hillerod = new Location("gps");  
                 hillerod.setLatitude(55.929177);  
                 hillerod.setLongitude(12.308095);  
                   
                 Location aarhus = new Location("gps"); //Aros  
                 aarhus.setLatitude(56.153828);  
                 aarhus.setLongitude(10.200369);  
                   
                   
                   
                 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);  
                 if (lm.getProvider("gps2") == null)  
                         lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );  
                 lm.setTestProviderEnabled("gps2", true);  
                 lm.setTestProviderLocation("gps2", kbh);  
         }  
           
         public static void removeMockLocation(Context cntx) {  
                 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);  
                 if (lm.getProvider("gps2") != null)  
                         lm.removeTestProvider("gps2");  
         }  
109  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20