/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 286 - (hide annotations) (download)
Fri Aug 28 07:21:04 2009 UTC (14 years, 9 months ago) by torben
Original Path: android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java
File size: 4238 byte(s)
Better balancing if both gps and network location providers are enabled
1 torben 237 package dk.thoerup.traininfo;
2    
3     import java.util.List;
4    
5     import android.content.Context;
6     import android.location.Criteria;
7     import android.location.Location;
8     import android.location.LocationListener;
9     import android.location.LocationManager;
10     import android.os.Bundle;
11     import android.os.Handler;
12     import android.util.Log;
13 torben 253 import dk.thoerup.traininfo.provider.ProviderFactory;
14     import dk.thoerup.traininfo.provider.StationProvider;
15 torben 237
16     public class StationLocator implements LocationListener{
17    
18     LocationManager locManager;
19     Context cntx;
20     Handler hndl;
21    
22 torben 254 StationProvider provider;
23 torben 241 Location savedLocation = null;
24 torben 286
25     boolean hasGps;
26 torben 241
27 torben 237 public StationLocator(Context c, Handler h) {
28     cntx = c;
29     hndl = h;
30 torben 254
31     provider = ProviderFactory.getStationProvider();
32 torben 237 }
33    
34     public List<StationBean> getStations() {
35 torben 254 return provider.getStations();
36 torben 237 }
37    
38     public void abortLocationListener() {
39     locManager.removeUpdates(this);
40     }
41 torben 285
42     public boolean hasLocation() {
43     return savedLocation != null;
44     }
45 torben 237
46     public void locateStations() {
47 torben 286 hasGps = false;
48 torben 237
49     locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
50 torben 286
51 torben 237 Criteria c = new Criteria();
52     c.setAccuracy(Criteria.ACCURACY_FINE);
53 torben 286 List<String> providers = locManager.getProviders(true);
54 torben 241
55 torben 286
56     if (providers.size() > 0) {
57     for(String provider : providers) {
58     Log.i("Provider", ""+provider);
59     if (provider.equalsIgnoreCase("gps"))
60     hasGps = true;
61     locManager.requestLocationUpdates(provider, 0, 0, this);
62     }
63 torben 237 } else {
64     // message that no suitable provider was found
65     hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
66     }
67     }
68     @Override
69     public void onLocationChanged(Location location) {
70     Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
71    
72 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
73     savedLocation = new Location(location); //save a copy
74 torben 241
75 torben 286 if (hasGps) {
76     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 torben 237
82 torben 286 }
83    
84 torben 237 locManager.removeUpdates(this);
85     hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
86 torben 241 }
87    
88     public void findNearestStations() {
89     findNearestStations(savedLocation);
90     }
91    
92     public void findNearestStations(Location location) {
93 torben 254 provider.lookupStations(location);
94 torben 253
95 torben 254 if ( provider.getStations().size() > 0)
96 torben 237 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
97 torben 253 else
98 torben 237 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
99     }
100    
101    
102     @Override
103     public void onProviderDisabled(String provider) {
104     // TODO Auto-generated method stub
105    
106     }
107    
108    
109     @Override
110     public void onProviderEnabled(String provider) {
111     // TODO Auto-generated method stub
112    
113     }
114    
115    
116     @Override
117     public void onStatusChanged(String provider, int status, Bundle extras) {
118     // TODO Auto-generated method stub
119    
120     }
121 torben 253
122 torben 252
123 torben 241 public static void injectMockLocation(Context cntx) {
124 torben 252 Location odder = new Location("gps2");
125     odder.setLatitude(55.976632);
126     odder.setLongitude(10.16407);
127 torben 241
128 torben 252 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
129     kbh.setLatitude(55.675092);
130     kbh.setLongitude(12.578573);
131    
132 torben 253 Location bjbro = new Location("gps2");
133     bjbro.setLatitude(56.380745);
134     bjbro.setLongitude(9.655609);
135    
136 torben 256 Location hillerod = new Location("gps");
137     hillerod.setLatitude(55.929177);
138     hillerod.setLongitude(12.308095);
139 torben 253
140 torben 241 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
141 torben 252 if (lm.getProvider("gps2") == null)
142     lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
143 torben 241 lm.setTestProviderEnabled("gps2", true);
144 torben 256 lm.setTestProviderLocation("gps2", hillerod);
145 torben 241 }
146 torben 252
147     public static void removeMockLocation(Context cntx) {
148     LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
149     if (lm.getProvider("gps2") != null)
150     lm.removeTestProvider("gps2");
151     }
152 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20