1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
|
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; |
8 |
import android.location.LocationListener; |
import android.location.LocationListener; |
9 |
import android.location.LocationManager; |
import android.location.LocationManager; |
|
import android.location.LocationProvider; |
|
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; |
|
import dk.thoerup.traininfo.util.DownloadUtil; |
|
13 |
|
|
14 |
public class StationLocator implements LocationListener{ |
|
15 |
|
public class LocationLookup implements LocationListener{ |
16 |
|
|
17 |
LocationManager locManager; |
LocationManager locManager; |
18 |
Context cntx; |
Context cntx; |
19 |
Handler hndl; |
Handler hndl; |
20 |
|
|
|
ArrayList<StationBean> stationList = new ArrayList<StationBean>(); |
|
|
List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList); |
|
|
|
|
|
|
|
21 |
Location savedLocation = null; |
Location savedLocation = null; |
22 |
|
|
23 |
|
boolean isSearching = false; |
24 |
|
boolean hasGps; |
25 |
|
|
26 |
public StationLocator(Context c, Handler h) { |
public LocationLookup(Context c, Handler h) { |
27 |
cntx = c; |
cntx = c; |
28 |
hndl = h; |
hndl = h; |
29 |
} |
} |
30 |
|
|
31 |
public List<StationBean> getStations() { |
|
32 |
return safeStationList; |
public boolean hasLocation() { |
33 |
|
return savedLocation != null; |
34 |
} |
} |
35 |
|
|
36 |
public void abortLocationListener() { |
public Location getLocation() |
37 |
locManager.removeUpdates(this); |
{ |
38 |
|
return savedLocation; |
39 |
} |
} |
40 |
|
|
41 |
public void locateStations() { |
public void locateStations() { |
|
//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 |
|
|
|
|
|
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.i("BestProvider", ""+bestProv); |
|
42 |
|
|
43 |
|
isSearching = true; |
44 |
|
|
45 |
|
hasGps = false; |
46 |
|
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
47 |
|
|
48 |
if (bestProv != null) { |
List<String> providers = locManager.getProviders(true); |
49 |
savedLocation = locManager.getLastKnownLocation(bestProv); |
|
50 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
if (providers.size() > 0) { |
51 |
|
for(String provider : providers) { |
52 |
|
Log.i("Provider", ""+provider); |
53 |
|
if (provider.equalsIgnoreCase("gps")) |
54 |
|
hasGps = true; |
55 |
|
locManager.requestLocationUpdates(provider, 0, 0, this); |
56 |
|
} |
57 |
} else { |
} else { |
58 |
// message that no suitable provider was found |
// message that no suitable provider was found |
59 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
hndl.sendEmptyMessage(StationList.NOPROVIDER); |
60 |
} |
} |
61 |
} |
} |
62 |
@Override |
@Override |
63 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
64 |
|
if (isSearching == false) |
65 |
|
return; |
66 |
|
|
67 |
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()); |
68 |
|
|
69 |
|
|
70 |
savedLocation = new Location(location); //save a copy |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
71 |
|
savedLocation = new Location(location); //save a copy |
72 |
|
|
73 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
if (hasGps) { |
74 |
return; |
if (!location.getProvider().equals("gps")) { |
75 |
|
return; // at least give the gps a chance |
76 |
|
} else if (location.getAccuracy() > 256) { |
77 |
|
return; //if we have a gps provider lets wait for a more precise fix |
78 |
|
} |
79 |
|
|
80 |
locManager.removeUpdates(this); |
} |
81 |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
stopSearch(); |
82 |
} |
hndl.sendEmptyMessage(StationList.GOTLOCATION); |
|
|
|
|
public void findNearestStations() { |
|
|
findNearestStations(savedLocation); |
|
83 |
} |
} |
84 |
|
|
85 |
public void findNearestStations(Location location) { |
public void stopSearch() |
86 |
//ToDo: to be nice put the api key into the request |
{ |
87 |
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"; |
if (isSearching) { |
88 |
//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"; |
isSearching = false; |
89 |
|
locManager.removeUpdates(this); |
|
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); |
|
|
} |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
|
|
|
|
|
} catch (Exception e) { |
|
|
Log.e("Location","Location",e); |
|
|
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
90 |
} |
} |
|
|
|
91 |
} |
} |
92 |
|
|
93 |
|
|
94 |
@Override |
@Override |
95 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
96 |
} |
} |
97 |
|
|
98 |
|
|
99 |
@Override |
@Override |
100 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
101 |
} |
} |
102 |
|
|
103 |
|
|
106 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
107 |
|
|
108 |
} |
} |
109 |
|
|
110 |
|
|
111 |
public static void injectMockLocation(Context cntx) { |
public static void injectMockLocation(Context cntx) { |
112 |
Location odder = new Location("gps2"); |
Location odder = new Location("gps2"); |
117 |
kbh.setLatitude(55.675092); |
kbh.setLatitude(55.675092); |
118 |
kbh.setLongitude(12.578573); |
kbh.setLongitude(12.578573); |
119 |
|
|
120 |
|
Location bjbro = new Location("gps2"); |
121 |
|
bjbro.setLatitude(56.380745); |
122 |
|
bjbro.setLongitude(9.655609); |
123 |
|
|
124 |
|
Location hillerod = new Location("gps"); |
125 |
|
hillerod.setLatitude(55.929177); |
126 |
|
hillerod.setLongitude(12.308095); |
127 |
|
|
128 |
|
Location aarhus = new Location("gps"); //Aros |
129 |
|
aarhus.setLatitude(56.153828); |
130 |
|
aarhus.setLongitude(10.200369); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
135 |
if (lm.getProvider("gps2") == null) |
if (lm.getProvider("gps2") == null) |
136 |
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |