/[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 253 by torben, Mon Aug 10 16:58:22 2009 UTC revision 286 by torben, Fri Aug 28 07:21:04 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
 import java.util.ArrayList;  
3  import java.util.List;  import java.util.List;
4    
 import org.json.JSONArray;  
 import org.json.JSONObject;  
   
5  import android.content.Context;  import android.content.Context;
6  import android.location.Criteria;  import android.location.Criteria;
7  import android.location.Location;  import android.location.Location;
8  import android.location.LocationListener;  import android.location.LocationListener;
9  import android.location.LocationManager;  import android.location.LocationManager;
 import android.location.LocationProvider;  
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;
13  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
14  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
 import dk.thoerup.traininfo.util.DownloadUtil;  
15    
16  public class StationLocator implements LocationListener{  public class StationLocator implements LocationListener{
17    
# Line 25  public class StationLocator implements L Line 19  public class StationLocator implements L
19          Context cntx;          Context cntx;
20          Handler hndl;          Handler hndl;
21    
22          List<StationBean> stationList = new ArrayList<StationBean>();          StationProvider provider;
   
           
23          Location savedLocation = null;          Location savedLocation = null;
24            
25            boolean hasGps;
26    
27          public StationLocator(Context c, Handler h) {          public StationLocator(Context c, Handler h) {
28                  cntx = c;                  cntx = c;
29                  hndl = h;                  hndl = h;
30                    
31                    provider = ProviderFactory.getStationProvider();
32          }          }
33                    
34          public List<StationBean> getStations() {          public List<StationBean> getStations() {
35                  return stationList;                  return provider.getStations();
36          }          }
37                    
38          public void abortLocationListener() {          public void abortLocationListener() {
39                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
40          }          }
41            
42            public boolean hasLocation() {
43                    return savedLocation != null;
44            }
45    
46          public void locateStations() {          public void locateStations() {
47                  //http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&lstkp=0&rsz=small&hl=en&source=gsc&gss=.com&sig=fadf0e8d483d0f70bea11d5905010a16&q=Train%20station&near=56.377424%2C9.656695&key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q&v=1.0&nocache=1249640467498                  hasGps = false;
48    
49                  locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
50                  /*//testcode  
                 List<String> provs = locManager.getAllProviders();  
                 for (String p  : provs) {  
                         Log.e("Provider", p);  
                 }  
                 provs = locManager.getProviders(true);  
                 for (String p  : provs) {  
                         Log.e("ActiveProvider", p);  
                 }  
                 */  
51                  Criteria c = new Criteria();                  Criteria c = new Criteria();
52                  c.setAccuracy(Criteria.ACCURACY_FINE);                  c.setAccuracy(Criteria.ACCURACY_FINE);
53                  String bestProv = locManager.getBestProvider(c, true);                  List<String> providers = locManager.getProviders(true);
                 Log.i("BestProvider", ""+bestProv);  
54                                    
55                    
56                  if (bestProv != null) {                  if (providers.size() > 0) {
57                          savedLocation = locManager.getLastKnownLocation(bestProv);                          for(String provider : providers) {
58                          locManager.requestLocationUpdates(bestProv, 0, 0, this);                                  Log.i("Provider", ""+provider);
59                                    if (provider.equalsIgnoreCase("gps"))
60                                            hasGps = true;
61                                    locManager.requestLocationUpdates(provider, 0, 0, this);
62                            }
63                  } else {                  } else {
64                          // message that no suitable provider was found                          // message that no suitable provider was found
65                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);                          hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
# Line 75  public class StationLocator implements L Line 69  public class StationLocator implements L
69          public void onLocationChanged(Location location) {          public void onLocationChanged(Location location) {
70                  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());
71    
72                  savedLocation = new Location(location); //save a copy                  if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
73                            savedLocation = new Location(location); //save a copy
74                                    
75                  if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix                  if (hasGps) {
76                          return;                          if (!location.getProvider().equals("gps")) {
77                                    return; // at least give the gps a chance
78                            } else if (location.getAccuracy() > 100) {
79                                    return; //if we have a gps provider lets wait for a more precise fix
80                            }
81    
82                    }
83    
84                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
85                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
# Line 89  public class StationLocator implements L Line 90  public class StationLocator implements L
90          }          }
91                    
92          public void findNearestStations(Location location) {          public void findNearestStations(Location location) {
93                  StationProvider prov = ProviderFactory.getStationProvider();                  provider.lookupStations(location);
                 prov.lookupStations(location);  
                 this.stationList = prov.getStations();  
94                                    
95                  if (stationList.size() > 0)                  if ( provider.getStations().size() > 0)
96                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
97                  else                  else
98                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
# Line 134  public class StationLocator implements L Line 133  public class StationLocator implements L
133                  bjbro.setLatitude(56.380745);                  bjbro.setLatitude(56.380745);
134                  bjbro.setLongitude(9.655609);                  bjbro.setLongitude(9.655609);
135                                    
136                    Location hillerod = new Location("gps");
137                    hillerod.setLatitude(55.929177);
138                    hillerod.setLongitude(12.308095);
139                                    
140                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);                  LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
141                  if (lm.getProvider("gps2") == null)                  if (lm.getProvider("gps2") == null)
142                          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 );
143                  lm.setTestProviderEnabled("gps2", true);                  lm.setTestProviderEnabled("gps2", true);
144                  lm.setTestProviderLocation("gps2", kbh);                  lm.setTestProviderLocation("gps2", hillerod);
145          }          }
146                    
147          public static void removeMockLocation(Context cntx) {          public static void removeMockLocation(Context cntx) {

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

  ViewVC Help
Powered by ViewVC 1.1.20