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

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 253 - (show annotations) (download)
Mon Aug 10 16:58:22 2009 UTC (14 years, 9 months ago) by torben
File size: 4598 byte(s)
Refactored the station lookup into a provider interface
1 package dk.thoerup.traininfo;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.json.JSONArray;
7 import org.json.JSONObject;
8
9 import android.content.Context;
10 import android.location.Criteria;
11 import android.location.Location;
12 import android.location.LocationListener;
13 import android.location.LocationManager;
14 import android.location.LocationProvider;
15 import android.os.Bundle;
16 import android.os.Handler;
17 import android.util.Log;
18 import dk.thoerup.traininfo.provider.ProviderFactory;
19 import dk.thoerup.traininfo.provider.StationProvider;
20 import dk.thoerup.traininfo.util.DownloadUtil;
21
22 public class StationLocator implements LocationListener{
23
24 LocationManager locManager;
25 Context cntx;
26 Handler hndl;
27
28 List<StationBean> stationList = new ArrayList<StationBean>();
29
30
31 Location savedLocation = null;
32
33 public StationLocator(Context c, Handler h) {
34 cntx = c;
35 hndl = h;
36 }
37
38 public List<StationBean> getStations() {
39 return stationList;
40 }
41
42 public void abortLocationListener() {
43 locManager.removeUpdates(this);
44 }
45
46 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
48
49 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
50 /*//testcode
51 List<String> provs = locManager.getAllProviders();
52 for (String p : provs) {
53 Log.e("Provider", p);
54 }
55 provs = locManager.getProviders(true);
56 for (String p : provs) {
57 Log.e("ActiveProvider", p);
58 }
59 */
60 Criteria c = new Criteria();
61 c.setAccuracy(Criteria.ACCURACY_FINE);
62 String bestProv = locManager.getBestProvider(c, true);
63 Log.i("BestProvider", ""+bestProv);
64
65
66 if (bestProv != null) {
67 savedLocation = locManager.getLastKnownLocation(bestProv);
68 locManager.requestLocationUpdates(bestProv, 0, 0, this);
69 } else {
70 // message that no suitable provider was found
71 hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
72 }
73 }
74 @Override
75 public void onLocationChanged(Location location) {
76 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
77
78 savedLocation = new Location(location); //save a copy
79
80 if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
81 return;
82
83 locManager.removeUpdates(this);
84 hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
85 }
86
87 public void findNearestStations() {
88 findNearestStations(savedLocation);
89 }
90
91 public void findNearestStations(Location location) {
92 StationProvider prov = ProviderFactory.getStationProvider();
93 prov.lookupStations(location);
94 this.stationList = prov.getStations();
95
96 if (stationList.size() > 0)
97 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
98 else
99 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
100 }
101
102
103 @Override
104 public void onProviderDisabled(String provider) {
105 // TODO Auto-generated method stub
106
107 }
108
109
110 @Override
111 public void onProviderEnabled(String provider) {
112 // TODO Auto-generated method stub
113
114 }
115
116
117 @Override
118 public void onStatusChanged(String provider, int status, Bundle extras) {
119 // TODO Auto-generated method stub
120
121 }
122
123
124 public static void injectMockLocation(Context cntx) {
125 Location odder = new Location("gps2");
126 odder.setLatitude(55.976632);
127 odder.setLongitude(10.16407);
128
129 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
130 kbh.setLatitude(55.675092);
131 kbh.setLongitude(12.578573);
132
133 Location bjbro = new Location("gps2");
134 bjbro.setLatitude(56.380745);
135 bjbro.setLongitude(9.655609);
136
137
138 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
139 if (lm.getProvider("gps2") == null)
140 lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
141 lm.setTestProviderEnabled("gps2", true);
142 lm.setTestProviderLocation("gps2", kbh);
143 }
144
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 }

  ViewVC Help
Powered by ViewVC 1.1.20