/[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 310 - (hide annotations) (download)
Thu Sep 10 19:09:09 2009 UTC (14 years, 8 months ago) by torben
Original Path: android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java
File size: 4205 byte(s)
Make code work with new servlets (and new db layout)
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 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
49 torben 286
50 torben 288 List<String> providers = locManager.getProviders(true);
51 torben 241
52 torben 286 if (providers.size() > 0) {
53     for(String provider : providers) {
54     Log.i("Provider", ""+provider);
55     if (provider.equalsIgnoreCase("gps"))
56     hasGps = true;
57     locManager.requestLocationUpdates(provider, 0, 0, this);
58     }
59 torben 237 } else {
60     // message that no suitable provider was found
61     hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
62     }
63     }
64     @Override
65     public void onLocationChanged(Location location) {
66     Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
67    
68 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
69     savedLocation = new Location(location); //save a copy
70 torben 241
71 torben 286 if (hasGps) {
72     if (!location.getProvider().equals("gps")) {
73     return; // at least give the gps a chance
74     } else if (location.getAccuracy() > 100) {
75     return; //if we have a gps provider lets wait for a more precise fix
76     }
77 torben 237
78 torben 286 }
79    
80 torben 237 locManager.removeUpdates(this);
81     hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
82 torben 241 }
83    
84     public void findNearestStations() {
85     findNearestStations(savedLocation);
86     }
87    
88     public void findNearestStations(Location location) {
89 torben 254 provider.lookupStations(location);
90 torben 253
91 torben 254 if ( provider.getStations().size() > 0)
92 torben 237 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
93 torben 253 else
94 torben 237 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
95     }
96    
97    
98     @Override
99     public void onProviderDisabled(String provider) {
100     }
101    
102    
103     @Override
104     public void onProviderEnabled(String provider) {
105     }
106    
107    
108     @Override
109     public void onStatusChanged(String provider, int status, Bundle extras) {
110     // TODO Auto-generated method stub
111    
112     }
113 torben 253
114 torben 252
115 torben 241 public static void injectMockLocation(Context cntx) {
116 torben 252 Location odder = new Location("gps2");
117     odder.setLatitude(55.976632);
118     odder.setLongitude(10.16407);
119 torben 241
120 torben 252 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
121     kbh.setLatitude(55.675092);
122     kbh.setLongitude(12.578573);
123    
124 torben 253 Location bjbro = new Location("gps2");
125     bjbro.setLatitude(56.380745);
126     bjbro.setLongitude(9.655609);
127    
128 torben 256 Location hillerod = new Location("gps");
129     hillerod.setLatitude(55.929177);
130     hillerod.setLongitude(12.308095);
131 torben 253
132 torben 289 Location aarhus = new Location("gps"); //Aros
133     aarhus.setLatitude(56.153828);
134     aarhus.setLongitude(10.200369);
135    
136    
137    
138 torben 241 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
139 torben 252 if (lm.getProvider("gps2") == null)
140     lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
141 torben 241 lm.setTestProviderEnabled("gps2", true);
142 torben 310 lm.setTestProviderLocation("gps2", kbh);
143 torben 241 }
144 torben 252
145     public static void removeMockLocation(Context cntx) {
146     LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
147     if (lm.getProvider("gps2") != null)
148     lm.removeTestProvider("gps2");
149     }
150 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20