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.GpsSatellite; |
7 |
|
import android.location.GpsStatus; |
8 |
import android.location.Location; |
import android.location.Location; |
9 |
import android.location.LocationListener; |
import android.location.LocationListener; |
10 |
import android.location.LocationManager; |
import android.location.LocationManager; |
|
import android.location.LocationProvider; |
|
11 |
import android.os.Bundle; |
import android.os.Bundle; |
|
import android.os.Handler; |
|
12 |
import android.util.Log; |
import android.util.Log; |
|
import dk.thoerup.traininfo.util.DownloadUtil; |
|
13 |
|
|
|
public class StationLocator implements LocationListener{ |
|
14 |
|
|
15 |
LocationManager locManager; |
public class LocationLookup implements LocationListener, GpsStatus.Listener { |
16 |
Context cntx; |
|
17 |
Handler hndl; |
public enum LookupStates { |
18 |
|
GOTLOCATION, |
19 |
|
NOPROVIDER, |
20 |
|
STARTED, |
21 |
|
IDLE |
22 |
|
} |
23 |
|
|
24 |
ArrayList<StationBean> stationList = new ArrayList<StationBean>(); |
private LocationManager locManager; |
25 |
List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList); |
private Context cntx; |
26 |
|
|
27 |
|
private Location lastKnownLocation = null; |
28 |
|
private Location savedLocation = null; |
29 |
|
private int satCount; |
30 |
|
|
31 |
|
private boolean hasGps; |
32 |
|
|
33 |
Location savedLocation = null; |
private LookupStates state; |
34 |
|
|
35 |
|
private long startTime; |
36 |
|
|
37 |
public StationLocator(Context c, Handler h) { |
public LocationLookup(Context c) { |
38 |
|
state = LookupStates.IDLE; |
39 |
cntx = c; |
cntx = c; |
|
hndl = h; |
|
40 |
} |
} |
41 |
|
|
42 |
public List<StationBean> getStations() { |
|
43 |
return safeStationList; |
public boolean hasLocation() { |
44 |
|
return savedLocation != null; |
45 |
|
} |
46 |
|
|
47 |
|
public Location getLocation() |
48 |
|
{ |
49 |
|
return savedLocation; |
50 |
|
} |
51 |
|
|
52 |
|
public boolean hasGps() { |
53 |
|
return hasGps; |
54 |
|
} |
55 |
|
|
56 |
|
public int getSatCount() { |
57 |
|
return satCount; |
58 |
|
} |
59 |
|
|
60 |
|
public LookupStates getState() { |
61 |
|
return state; |
62 |
|
} |
63 |
|
|
64 |
|
public Location getLastKnownLocation() { |
65 |
|
return lastKnownLocation; |
66 |
} |
} |
67 |
|
|
68 |
public void abortLocationListener() { |
public long elapsedTime() { |
69 |
locManager.removeUpdates(this); |
long now = android.os.SystemClock.elapsedRealtime(); |
70 |
|
|
71 |
|
return now - startTime; |
72 |
} |
} |
73 |
|
|
74 |
public void locateStations() { |
public void locateStations() { |
75 |
//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 |
|
76 |
|
state = LookupStates.STARTED; |
77 |
|
|
78 |
|
hasGps = false; |
79 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
80 |
/*//testcode |
satCount = 0; |
|
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); |
|
81 |
|
|
82 |
|
startTime = android.os.SystemClock.elapsedRealtime(); |
83 |
|
|
84 |
if (bestProv != null) { |
List<String> providers = locManager.getProviders(true); |
85 |
savedLocation = locManager.getLastKnownLocation(bestProv); |
|
86 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
if (providers.size() > 0) { |
87 |
|
for(String provider : providers) { |
88 |
|
Log.i("Provider", ""+provider); |
89 |
|
if (provider.equalsIgnoreCase("gps")) { |
90 |
|
locManager.addGpsStatusListener(this); |
91 |
|
hasGps = true; |
92 |
|
} |
93 |
|
|
94 |
|
locManager.requestLocationUpdates(provider, 0, 0, this); |
95 |
|
Location tmpLastKnown = locManager.getLastKnownLocation(provider); |
96 |
|
if (tmpLastKnown != null) { |
97 |
|
saveLastKnownLocation(tmpLastKnown); |
98 |
|
} |
99 |
|
} |
100 |
} else { |
} else { |
101 |
// message that no suitable provider was found |
// message that no suitable provider was found |
102 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
103 |
|
state = LookupStates.NOPROVIDER; |
104 |
|
|
105 |
} |
} |
106 |
} |
} |
107 |
@Override |
@Override |
108 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
109 |
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()); |
110 |
|
|
111 |
|
|
112 |
savedLocation = new Location(location); //save a copy |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
113 |
|
savedLocation = new Location(location); //save a copy |
114 |
|
|
115 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
if (hasGps) { |
116 |
return; |
if (!location.getProvider().equals("gps")) { |
117 |
|
return; // at least give the gps a chance |
118 |
|
} else if (location.getAccuracy() > 512) { |
119 |
|
return; //if we have a gps provider lets wait for a more precise fix |
120 |
|
} |
121 |
|
|
122 |
|
} |
123 |
|
stopSearch(); |
124 |
|
state = LookupStates.GOTLOCATION; |
125 |
|
|
|
locManager.removeUpdates(this); |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
|
126 |
} |
} |
127 |
|
|
128 |
public void findNearestStations() { |
private void saveLastKnownLocation(Location loc) { |
129 |
findNearestStations(savedLocation); |
if (lastKnownLocation == null) { |
130 |
|
lastKnownLocation = loc; |
131 |
|
} else { |
132 |
|
if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved |
133 |
|
lastKnownLocation = loc; |
134 |
|
} |
135 |
|
} |
136 |
} |
} |
137 |
|
|
138 |
public void findNearestStations(Location location) { |
public void stopSearch() |
139 |
//ToDo: to be nice put the api key into the request |
{ |
140 |
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 (state == LookupStates.STARTED) { |
141 |
//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"; |
state = LookupStates.IDLE; |
142 |
|
locManager.removeGpsStatusListener(this); |
143 |
try { |
locManager.removeUpdates(this); |
|
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); |
|
144 |
} |
} |
|
|
|
145 |
} |
} |
146 |
|
|
147 |
|
|
148 |
@Override |
@Override |
149 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
150 |
} |
} |
151 |
|
|
152 |
|
|
153 |
@Override |
@Override |
154 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
155 |
} |
} |
156 |
|
|
157 |
|
|
161 |
|
|
162 |
} |
} |
163 |
|
|
164 |
public static void injectMockLocation(Context cntx) { |
|
165 |
Location odder = new Location("gps2"); |
@Override //GpsStatus.Listener |
166 |
odder.setLatitude(55.976632); |
public void onGpsStatusChanged(int event) { |
167 |
odder.setLongitude(10.16407); |
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { |
168 |
|
int count = 0; |
169 |
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573 |
GpsStatus status = locManager.getGpsStatus(null); |
170 |
kbh.setLatitude(55.675092); |
for (GpsSatellite sat : status.getSatellites()) { |
171 |
kbh.setLongitude(12.578573); |
count ++; |
172 |
|
} |
173 |
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
|
174 |
if (lm.getProvider("gps2") == null) |
satCount = count; |
175 |
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |
} |
176 |
lm.setTestProviderEnabled("gps2", true); |
|
|
lm.setTestProviderLocation("gps2", kbh); |
|
|
} |
|
|
|
|
|
public static void removeMockLocation(Context cntx) { |
|
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
|
|
if (lm.getProvider("gps2") != null) |
|
|
lm.removeTestProvider("gps2"); |
|
177 |
} |
} |
178 |
} |
} |