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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 252 - (hide 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 torben 237 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 torben 241 import android.location.LocationProvider;
15 torben 237 import android.os.Bundle;
16     import android.os.Handler;
17     import android.util.Log;
18 torben 241 import dk.thoerup.traininfo.util.DownloadUtil;
19 torben 237
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 torben 241
30     Location savedLocation = null;
31    
32 torben 237 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 torben 242 Log.i("BestProvider", ""+bestProv);
63 torben 241
64 torben 237
65     if (bestProv != null) {
66 torben 241 savedLocation = locManager.getLastKnownLocation(bestProv);
67 torben 237 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 torben 241 savedLocation = new Location(location); //save a copy
78    
79 torben 237 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 torben 241 }
85    
86     public void findNearestStations() {
87     findNearestStations(savedLocation);
88     }
89    
90     public void findNearestStations(Location location) {
91 torben 237 //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 torben 240 try {
96 torben 238 String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");
97     StringBuilder builder = new StringBuilder(data);
98 torben 237
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 torben 241
134 torben 237 }
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 torben 252
157 torben 241 public static void injectMockLocation(Context cntx) {
158 torben 252 Location odder = new Location("gps2");
159     odder.setLatitude(55.976632);
160     odder.setLongitude(10.16407);
161 torben 241
162 torben 252 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
163     kbh.setLatitude(55.675092);
164     kbh.setLongitude(12.578573);
165    
166 torben 241 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
167 torben 252 if (lm.getProvider("gps2") == null)
168     lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
169 torben 241 lm.setTestProviderEnabled("gps2", true);
170 torben 252 lm.setTestProviderLocation("gps2", kbh);
171 torben 241 }
172 torben 252
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 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20