/[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 336 by torben, Wed Sep 23 12:51:49 2009 UTC revision 570 by torben, Fri Jan 29 12:34:17 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;
 import android.location.Criteria;  
6  import android.location.Location;  import android.location.Location;
7  import android.location.LocationListener;  import android.location.LocationListener;
8  import android.location.LocationManager;  import android.location.LocationManager;
# Line 18  public class LocationLookup implements L Line 17  public class LocationLookup implements L
17          Context cntx;          Context cntx;
18          Handler hndl;          Handler hndl;
19    
20            Location lastKnownLocation = null;
21          Location savedLocation = null;          Location savedLocation = null;
22                    
23          boolean isSearching = false;          boolean isSearching = false;
# Line 37  public class LocationLookup implements L Line 37  public class LocationLookup implements L
37          {          {
38                  return savedLocation;                  return savedLocation;
39          }          }
40            
41            public Location getLastKnownLocation() {
42                    return lastKnownLocation;
43            }
44    
45          public void locateStations() {          public void locateStations() {
46                                    
# Line 53  public class LocationLookup implements L Line 57  public class LocationLookup implements L
57                                  if (provider.equalsIgnoreCase("gps"))                                  if (provider.equalsIgnoreCase("gps"))
58                                          hasGps = true;                                          hasGps = true;
59                                  locManager.requestLocationUpdates(provider, 0, 0, this);                                  locManager.requestLocationUpdates(provider, 0, 0, this);
60                                    Location tmpLastKnown = locManager.getLastKnownLocation(provider);
61                                    if (tmpLastKnown != null) {
62                                            saveLastKnownLocation(tmpLastKnown);
63                                    }
64                          }                          }
65                  } else {                  } else {
66                          // message that no suitable provider was found                          // message that no suitable provider was found
# Line 73  public class LocationLookup implements L Line 81  public class LocationLookup implements L
81                  if (hasGps) {                  if (hasGps) {
82                          if (!location.getProvider().equals("gps")) {                          if (!location.getProvider().equals("gps")) {
83                                  return; // at least give the gps a chance                                  return; // at least give the gps a chance
84                          } else if (location.getAccuracy() > 128) {                          } else if (location.getAccuracy() > 256) {
85                                  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
86                          }                          }
87    
# Line 82  public class LocationLookup implements L Line 90  public class LocationLookup implements L
90                  hndl.sendEmptyMessage(StationList.GOTLOCATION);                  hndl.sendEmptyMessage(StationList.GOTLOCATION);
91          }          }
92                    
93            private void saveLastKnownLocation(Location loc) {
94                    if (lastKnownLocation == null) {
95                            lastKnownLocation = loc;
96                    } else {
97                            if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved
98                                    lastKnownLocation = loc;
99                            }
100                    }
101            }
102            
103          public void stopSearch()          public void stopSearch()
104          {          {
105                  if (isSearching) {                  if (isSearching) {
# Line 106  public class LocationLookup implements L Line 124  public class LocationLookup implements L
124                  // TODO Auto-generated method stub                  // TODO Auto-generated method stub
125    
126          }          }
           
   
         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");  
         }  
127  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20