/[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 238 - (hide annotations) (download)
Sat Aug 8 20:09:47 2009 UTC (14 years, 9 months ago) by torben
File size: 5218 byte(s)
Create a utility class for downloading xml docs, and make fetching of departure data asynchronous
1 torben 237 package dk.thoerup.traininfo;
2    
3     import java.io.BufferedReader;
4     import java.io.InputStreamReader;
5     import java.net.URL;
6     import java.net.URLConnection;
7     import java.util.ArrayList;
8     import java.util.List;
9    
10     import org.json.JSONArray;
11     import org.json.JSONObject;
12    
13 torben 238 import dk.thoerup.traininfo.util.DownloadUtil;
14    
15 torben 237 import android.content.Context;
16     import android.location.Criteria;
17     import android.location.Location;
18     import android.location.LocationListener;
19     import android.location.LocationManager;
20     import android.os.Bundle;
21     import android.os.Handler;
22     import android.util.Log;
23    
24     public class StationLocator implements LocationListener{
25    
26     LocationManager locManager;
27     Context cntx;
28     Handler hndl;
29    
30     ArrayList<StationBean> stationList = new ArrayList<StationBean>();
31     List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);
32    
33     public StationLocator(Context c, Handler h) {
34     cntx = c;
35     hndl = h;
36     }
37    
38     public List<StationBean> getStations() {
39     return safeStationList;
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.e("BestProvider", bestProv);
64    
65     if (bestProv != null) {
66     locManager.requestLocationUpdates(bestProv, 0, 0, this);
67     } else {
68     // message that no suitable provider was found
69     hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
70     }
71     }
72     @Override
73     public void onLocationChanged(Location location) {
74     Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
75    
76     if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
77     return;
78    
79     locManager.removeUpdates(this);
80     hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
81    
82     //ToDo: to be nice put the api key into the request
83     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";
84     //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";
85    
86 torben 238 try {/*
87 torben 237 Log.e("Url", urlSource);
88     URL url = new URL(urlSource);
89     URLConnection connection = url.openConnection();
90 torben 238 connection.setConnectTimeout(10000);
91 torben 237
92     String line;
93     StringBuilder builder = new StringBuilder();
94     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 8192);
95     while((line = reader.readLine()) != null) {
96     builder.append(line);
97 torben 238 }*/
98     String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");
99     StringBuilder builder = new StringBuilder(data);
100 torben 237
101     while (builder.charAt(0) != '{')
102     builder.deleteCharAt(0);
103     while (builder.charAt(builder.length()-1) != '}')
104     builder.deleteCharAt(builder.length()-1);
105    
106     JSONObject json = new JSONObject(builder.toString());
107     // now have some fun with the results...
108     JSONArray res = json.getJSONArray("results");
109    
110     Location tmpLocation = new Location("gps");
111     stationList.clear();
112    
113     for (int i=0; i<res.length(); i++) {
114     JSONObject jsonStation = res.getJSONObject(i);
115    
116     double lat = jsonStation.getDouble("lat");
117     double lng = jsonStation.getDouble("lng");
118     String title = jsonStation.getString("title");
119     String addr = jsonStation.getString("streetAddress");
120     tmpLocation.setLatitude(lat);
121     tmpLocation.setLongitude(lng);
122    
123     float distance = location.distanceTo(tmpLocation);
124    
125     stationList.add( new StationBean(title,lat,lng,(int)distance,addr) );
126    
127     Log.e("Location", title + " " + lat +"," + lng);
128     }
129     hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
130    
131     } catch (Exception e) {
132     Log.e("Location","Location",e);
133     hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
134     }
135    
136     }
137    
138    
139     @Override
140     public void onProviderDisabled(String provider) {
141     // TODO Auto-generated method stub
142    
143     }
144    
145    
146     @Override
147     public void onProviderEnabled(String provider) {
148     // TODO Auto-generated method stub
149    
150     }
151    
152    
153     @Override
154     public void onStatusChanged(String provider, int status, Bundle extras) {
155     // TODO Auto-generated method stub
156    
157     }
158     }

  ViewVC Help
Powered by ViewVC 1.1.20