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; |
|
|
|
|
|
import dk.thoerup.traininfo.util.DownloadUtil; |
|
|
|
|
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 |
|
boolean hasGps; |
26 |
|
|
27 |
public StationLocator(Context c, Handler h) { |
public StationLocator(Context c, Handler h) { |
28 |
cntx = c; |
cntx = c; |
29 |
hndl = h; |
hndl = h; |
30 |
|
|
31 |
|
provider = ProviderFactory.getStationProvider(); |
32 |
} |
} |
33 |
|
|
34 |
public List<StationBean> getStations() { |
public List<StationBean> getStations() { |
35 |
return safeStationList; |
return provider.getStations(); |
36 |
} |
} |
37 |
|
|
38 |
public void abortLocationListener() { |
public void abortLocationListener() { |
39 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
40 |
} |
} |
41 |
|
|
42 |
|
public boolean hasLocation() { |
43 |
|
return savedLocation != null; |
44 |
|
} |
45 |
|
|
46 |
public void locateStations() { |
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 |
hasGps = false; |
|
|
|
48 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
|
/*//testcode |
|
|
List<String> provs = locManager.getAllProviders(); |
|
|
for (String p : provs) { |
|
|
Log.e("Provider", p); |
|
|
} |
|
|
provs = locManager.getProviders(true); |
|
|
for (String p : provs) { |
|
|
Log.e("ActiveProvider", p); |
|
|
} |
|
|
*/ |
|
|
Criteria c = new Criteria(); |
|
|
c.setAccuracy(Criteria.ACCURACY_FINE); |
|
|
String bestProv = locManager.getBestProvider(c, true); |
|
|
Log.e("BestProvider", bestProv); |
|
49 |
|
|
50 |
if (bestProv != null) { |
List<String> providers = locManager.getProviders(true); |
51 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
|
52 |
|
if (providers.size() > 0) { |
53 |
|
for(String provider : providers) { |
54 |
|
Log.i("Provider", ""+provider); |
55 |
|
if (provider.equalsIgnoreCase("gps")) |
56 |
|
hasGps = true; |
57 |
|
locManager.requestLocationUpdates(provider, 0, 0, this); |
58 |
|
} |
59 |
} else { |
} else { |
60 |
// message that no suitable provider was found |
// message that no suitable provider was found |
61 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
65 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
66 |
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()); |
67 |
|
|
68 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
69 |
return; |
savedLocation = new Location(location); //save a copy |
70 |
|
|
71 |
|
if (hasGps) { |
72 |
|
if (!location.getProvider().equals("gps")) { |
73 |
|
return; // at least give the gps a chance |
74 |
|
} else if (location.getAccuracy() > 100) { |
75 |
|
return; //if we have a gps provider lets wait for a more precise fix |
76 |
|
} |
77 |
|
|
78 |
|
} |
79 |
|
|
80 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
81 |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
82 |
|
} |
83 |
|
|
84 |
|
public void findNearestStations() { |
85 |
|
findNearestStations(savedLocation); |
86 |
|
} |
87 |
|
|
88 |
|
public void findNearestStations(Location location) { |
89 |
|
provider.lookupStations(location); |
90 |
|
|
91 |
//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 { |
|
|
String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8"); |
|
|
StringBuilder builder = new StringBuilder(data); |
|
|
|
|
|
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); |
|
|
} |
|
92 |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
93 |
|
else |
|
} catch (Exception e) { |
|
|
Log.e("Location","Location",e); |
|
94 |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
} |
|
|
|
|
95 |
} |
} |
96 |
|
|
97 |
|
|
98 |
@Override |
@Override |
99 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
100 |
} |
} |
101 |
|
|
102 |
|
|
103 |
@Override |
@Override |
104 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
105 |
} |
} |
106 |
|
|
107 |
|
|
110 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
111 |
|
|
112 |
} |
} |
113 |
|
|
114 |
|
|
115 |
|
public static void injectMockLocation(Context cntx) { |
116 |
|
Location odder = new Location("gps2"); |
117 |
|
odder.setLatitude(55.976632); |
118 |
|
odder.setLongitude(10.16407); |
119 |
|
|
120 |
|
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573 |
121 |
|
kbh.setLatitude(55.675092); |
122 |
|
kbh.setLongitude(12.578573); |
123 |
|
|
124 |
|
Location bjbro = new Location("gps2"); |
125 |
|
bjbro.setLatitude(56.380745); |
126 |
|
bjbro.setLongitude(9.655609); |
127 |
|
|
128 |
|
Location hillerod = new Location("gps"); |
129 |
|
hillerod.setLatitude(55.929177); |
130 |
|
hillerod.setLongitude(12.308095); |
131 |
|
|
132 |
|
Location aarhus = new Location("gps"); //Aros |
133 |
|
aarhus.setLatitude(56.153828); |
134 |
|
aarhus.setLongitude(10.200369); |
135 |
|
|
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 |
} |
} |