/[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 252 - (show annotations) (download)
Mon Aug 10 10:49:43 2009 UTC (14 years, 9 months ago) by torben
File size: 5906 byte(s)
Add more mock locations and a method for removing the test provider
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.util.DownloadUtil;
19
20 public class StationLocator implements LocationListener{
21
22 LocationManager locManager;
23 Context cntx;
24 Handler hndl;
25
26 ArrayList<StationBean> stationList = new ArrayList<StationBean>();
27 List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);
28
29
30 Location savedLocation = null;
31
32 public StationLocator(Context c, Handler h) {
33 cntx = c;
34 hndl = h;
35 }
36
37 public List<StationBean> getStations() {
38 return safeStationList;
39 }
40
41 public void abortLocationListener() {
42 locManager.removeUpdates(this);
43 }
44
45 public void locateStations() {
46 //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
47
48 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
49 /*//testcode
50 List<String> provs = locManager.getAllProviders();
51 for (String p : provs) {
52 Log.e("Provider", p);
53 }
54 provs = locManager.getProviders(true);
55 for (String p : provs) {
56 Log.e("ActiveProvider", p);
57 }
58 */
59 Criteria c = new Criteria();
60 c.setAccuracy(Criteria.ACCURACY_FINE);
61 String bestProv = locManager.getBestProvider(c, true);
62 Log.i("BestProvider", ""+bestProv);
63
64
65 if (bestProv != null) {
66 savedLocation = locManager.getLastKnownLocation(bestProv);
67 locManager.requestLocationUpdates(bestProv, 0, 0, this);
68 } else {
69 // message that no suitable provider was found
70 hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
71 }
72 }
73 @Override
74 public void onLocationChanged(Location location) {
75 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
76
77 savedLocation = new Location(location); //save a copy
78
79 if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
80 return;
81
82 locManager.removeUpdates(this);
83 hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
84 }
85
86 public void findNearestStations() {
87 findNearestStations(savedLocation);
88 }
89
90 public void findNearestStations(Location location) {
91 //ToDo: to be nice put the api key into the request
92 String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=" + location.getLatitude() + "%2C" + location.getLongitude() + "&v=1.0";
93 //String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=56.2%2C9.0&v=1.0";
94
95 try {
96 String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");
97 StringBuilder builder = new StringBuilder(data);
98
99 while (builder.charAt(0) != '{')
100 builder.deleteCharAt(0);
101 while (builder.charAt(builder.length()-1) != '}')
102 builder.deleteCharAt(builder.length()-1);
103
104 JSONObject json = new JSONObject(builder.toString());
105 // now have some fun with the results...
106 JSONArray res = json.getJSONArray("results");
107
108 Location tmpLocation = new Location("gps");
109 stationList.clear();
110
111 for (int i=0; i<res.length(); i++) {
112 JSONObject jsonStation = res.getJSONObject(i);
113
114 double lat = jsonStation.getDouble("lat");
115 double lng = jsonStation.getDouble("lng");
116 String title = jsonStation.getString("title");
117 String addr = jsonStation.getString("streetAddress");
118 tmpLocation.setLatitude(lat);
119 tmpLocation.setLongitude(lng);
120
121 float distance = location.distanceTo(tmpLocation);
122
123 stationList.add( new StationBean(title,lat,lng,(int)distance,addr) );
124
125 Log.e("Location", title + " " + lat +"," + lng);
126 }
127 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
128
129 } catch (Exception e) {
130 Log.e("Location","Location",e);
131 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
132 }
133
134 }
135
136
137 @Override
138 public void onProviderDisabled(String provider) {
139 // TODO Auto-generated method stub
140
141 }
142
143
144 @Override
145 public void onProviderEnabled(String provider) {
146 // TODO Auto-generated method stub
147
148 }
149
150
151 @Override
152 public void onStatusChanged(String provider, int status, Bundle extras) {
153 // TODO Auto-generated method stub
154
155 }
156
157 public static void injectMockLocation(Context cntx) {
158 Location odder = new Location("gps2");
159 odder.setLatitude(55.976632);
160 odder.setLongitude(10.16407);
161
162 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
163 kbh.setLatitude(55.675092);
164 kbh.setLongitude(12.578573);
165
166 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
167 if (lm.getProvider("gps2") == null)
168 lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
169 lm.setTestProviderEnabled("gps2", true);
170 lm.setTestProviderLocation("gps2", kbh);
171 }
172
173 public static void removeMockLocation(Context cntx) {
174 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
175 if (lm.getProvider("gps2") != null)
176 lm.removeTestProvider("gps2");
177 }
178 }

  ViewVC Help
Powered by ViewVC 1.1.20