/[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 1143 by torben, Tue Sep 28 15:30:13 2010 UTC revision 1446 by torben, Wed May 4 20:25:15 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
 import java.util.List;  
3    
4  import android.content.Context;  import android.content.Context;
5    import android.content.SharedPreferences;
6  import android.location.GpsSatellite;  import android.location.GpsSatellite;
7  import android.location.GpsStatus;  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;
12    import android.preference.PreferenceManager;
13  import android.util.Log;  import android.util.Log;
14    
15    
# Line 24  public class LocationLookup implements L Line 25  public class LocationLookup implements L
25          private LocationManager locManager;          private LocationManager locManager;
26          private Context cntx;          private Context cntx;
27    
         private Location lastKnownLocation = null;  
28          private Location savedLocation = null;          private Location savedLocation = null;
29          private int satCount;          private int satCount;
30                    
# Line 61  public class LocationLookup implements L Line 61  public class LocationLookup implements L
61                  return state;                  return state;
62          }          }
63                    
         public Location getLastKnownLocation() {  
                 return lastKnownLocation;  
         }  
64                    
65          public long elapsedTime() {          public long elapsedTime() {
66                  long now = android.os.SystemClock.elapsedRealtime();                  long now = android.os.SystemClock.elapsedRealtime();
# Line 80  public class LocationLookup implements L Line 77  public class LocationLookup implements L
77                  satCount = 0;                  satCount = 0;
78                                    
79                  startTime = android.os.SystemClock.elapsedRealtime();                  startTime = android.os.SystemClock.elapsedRealtime();
   
                 List<String> providers = locManager.getProviders(true);          
80                                    
81                  if (providers.size() > 0) {                  boolean hasProvider = false;
82                          for(String provider : providers) {                  
83                                  Log.i("Provider", ""+provider);                  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
84                                  if (provider.equalsIgnoreCase("gps")) {                  String networkPref = prefs.getString("location", "GPS"); //default value is gps
85                                          locManager.addGpsStatusListener(this);                  
86                                          hasGps = true;                                                    if (networkPref.equals("GPS")) {
87                                  }                          if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
88                                    locManager.addGpsStatusListener(this);
89                                  locManager.requestLocationUpdates(provider, 0, 0, this);                                  locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
90                                  Location tmpLastKnown = locManager.getLastKnownLocation(provider);                                  
91                                  if (tmpLastKnown != null) {                                  hasGps = true;
92                                          saveLastKnownLocation(tmpLastKnown);                                  hasProvider = true;
                                 }  
93                          }                          }
94                  } else {                  }
95                    
96                    if (locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
97                            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
98                            hasProvider = true;                    
99                    }
100                    
101                            
102                    if (hasProvider == false) {
103                          // message that no suitable provider was found                          // message that no suitable provider was found
104                          //hndl.sendEmptyMessage(StationList.NOPROVIDER);                          //hndl.sendEmptyMessage(StationList.NOPROVIDER);
105                          state = LookupStates.NOPROVIDER;                          state = LookupStates.NOPROVIDER;
# Line 125  public class LocationLookup implements L Line 127  public class LocationLookup implements L
127    
128          }          }
129                    
         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;  
                         }  
                 }  
         }  
130                    
131          public void stopSearch()          public void stopSearch()
132          {          {

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

  ViewVC Help
Powered by ViewVC 1.1.20