1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
|
import java.io.BufferedReader; |
|
|
import java.io.InputStreamReader; |
|
|
import java.net.URL; |
|
|
import java.net.URLConnection; |
|
|
import java.util.ArrayList; |
|
3 |
import java.util.List; |
import java.util.List; |
4 |
|
|
|
import org.json.JSONArray; |
|
|
import org.json.JSONObject; |
|
|
|
|
5 |
import android.content.Context; |
import android.content.Context; |
6 |
import android.location.Criteria; |
import android.location.Criteria; |
7 |
import android.location.Location; |
import android.location.Location; |
10 |
import android.os.Bundle; |
import android.os.Bundle; |
11 |
import android.os.Handler; |
import android.os.Handler; |
12 |
import android.util.Log; |
import android.util.Log; |
13 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
14 |
|
import dk.thoerup.traininfo.provider.StationProvider; |
15 |
|
|
16 |
public class StationLocator implements LocationListener{ |
public class StationLocator implements LocationListener{ |
17 |
|
|
19 |
Context cntx; |
Context cntx; |
20 |
Handler hndl; |
Handler hndl; |
21 |
|
|
22 |
ArrayList<StationBean> stationList = new ArrayList<StationBean>(); |
StationProvider provider; |
23 |
List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList); |
Location savedLocation = null; |
24 |
|
|
25 |
public StationLocator(Context c, Handler h) { |
public StationLocator(Context c, Handler h) { |
26 |
cntx = c; |
cntx = c; |
27 |
hndl = h; |
hndl = h; |
28 |
|
|
29 |
|
provider = ProviderFactory.getStationProvider(); |
30 |
} |
} |
31 |
|
|
32 |
public List<StationBean> getStations() { |
public List<StationBean> getStations() { |
33 |
return safeStationList; |
return provider.getStations(); |
34 |
} |
} |
35 |
|
|
36 |
public void abortLocationListener() { |
public void abortLocationListener() { |
54 |
Criteria c = new Criteria(); |
Criteria c = new Criteria(); |
55 |
c.setAccuracy(Criteria.ACCURACY_FINE); |
c.setAccuracy(Criteria.ACCURACY_FINE); |
56 |
String bestProv = locManager.getBestProvider(c, true); |
String bestProv = locManager.getBestProvider(c, true); |
57 |
Log.e("BestProvider", bestProv); |
Log.i("BestProvider", ""+bestProv); |
58 |
|
|
59 |
|
|
60 |
if (bestProv != null) { |
if (bestProv != null) { |
61 |
|
savedLocation = locManager.getLastKnownLocation(bestProv); |
62 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
63 |
} else { |
} else { |
64 |
// message that no suitable provider was found |
// message that no suitable provider was found |
69 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
70 |
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); |
Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); |
71 |
|
|
72 |
|
savedLocation = new Location(location); //save a copy |
73 |
|
|
74 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
75 |
return; |
return; |
76 |
|
|
77 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
78 |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
79 |
|
} |
80 |
|
|
81 |
|
public void findNearestStations() { |
82 |
|
findNearestStations(savedLocation); |
83 |
|
} |
84 |
|
|
85 |
|
public void findNearestStations(Location location) { |
86 |
|
provider.lookupStations(location); |
87 |
|
|
88 |
//ToDo: to be nice put the api key into the request |
if ( provider.getStations().size() > 0) |
|
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"; |
|
|
//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"; |
|
|
|
|
|
try { |
|
|
Log.e("Url", urlSource); |
|
|
URL url = new URL(urlSource); |
|
|
URLConnection connection = url.openConnection(); |
|
|
connection.setConnectTimeout(5000); |
|
|
|
|
|
String line; |
|
|
StringBuilder builder = new StringBuilder(); |
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 8192); |
|
|
while((line = reader.readLine()) != null) { |
|
|
builder.append(line); |
|
|
} |
|
|
|
|
|
while (builder.charAt(0) != '{') |
|
|
builder.deleteCharAt(0); |
|
|
while (builder.charAt(builder.length()-1) != '}') |
|
|
builder.deleteCharAt(builder.length()-1); |
|
|
|
|
|
JSONObject json = new JSONObject(builder.toString()); |
|
|
// now have some fun with the results... |
|
|
JSONArray res = json.getJSONArray("results"); |
|
|
|
|
|
Location tmpLocation = new Location("gps"); |
|
|
stationList.clear(); |
|
|
|
|
|
for (int i=0; i<res.length(); i++) { |
|
|
JSONObject jsonStation = res.getJSONObject(i); |
|
|
|
|
|
double lat = jsonStation.getDouble("lat"); |
|
|
double lng = jsonStation.getDouble("lng"); |
|
|
String title = jsonStation.getString("title"); |
|
|
String addr = jsonStation.getString("streetAddress"); |
|
|
tmpLocation.setLatitude(lat); |
|
|
tmpLocation.setLongitude(lng); |
|
|
|
|
|
float distance = location.distanceTo(tmpLocation); |
|
|
|
|
|
stationList.add( new StationBean(title,lat,lng,(int)distance,addr) ); |
|
|
|
|
|
Log.e("Location", title + " " + lat +"," + lng); |
|
|
} |
|
89 |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
90 |
|
else |
|
} catch (Exception e) { |
|
|
Log.e("Location","Location",e); |
|
91 |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
} |
|
|
|
|
92 |
} |
} |
93 |
|
|
94 |
|
|
111 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
112 |
|
|
113 |
} |
} |
114 |
|
|
115 |
|
|
116 |
|
public static void injectMockLocation(Context cntx) { |
117 |
|
Location odder = new Location("gps2"); |
118 |
|
odder.setLatitude(55.976632); |
119 |
|
odder.setLongitude(10.16407); |
120 |
|
|
121 |
|
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573 |
122 |
|
kbh.setLatitude(55.675092); |
123 |
|
kbh.setLongitude(12.578573); |
124 |
|
|
125 |
|
Location bjbro = new Location("gps2"); |
126 |
|
bjbro.setLatitude(56.380745); |
127 |
|
bjbro.setLongitude(9.655609); |
128 |
|
|
129 |
|
Location hillerod = new Location("gps"); |
130 |
|
hillerod.setLatitude(55.929177); |
131 |
|
hillerod.setLongitude(12.308095); |
132 |
|
|
133 |
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
134 |
|
if (lm.getProvider("gps2") == null) |
135 |
|
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |
136 |
|
lm.setTestProviderEnabled("gps2", true); |
137 |
|
lm.setTestProviderLocation("gps2", hillerod); |
138 |
|
} |
139 |
|
|
140 |
|
public static void removeMockLocation(Context cntx) { |
141 |
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
142 |
|
if (lm.getProvider("gps2") != null) |
143 |
|
lm.removeTestProvider("gps2"); |
144 |
|
} |
145 |
} |
} |