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.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; |
11 |
import android.os.Bundle; |
import android.os.Bundle; |
|
import android.os.Handler; |
|
12 |
import android.util.Log; |
import android.util.Log; |
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 savedLocation = null; |
28 |
|
private int satCount; |
29 |
|
|
30 |
|
private boolean hasGps; |
31 |
|
|
32 |
|
private LookupStates state; |
33 |
|
|
34 |
|
private long startTime; |
35 |
|
|
36 |
public StationLocator(Context c, Handler h) { |
public LocationLookup(Context c) { |
37 |
|
state = LookupStates.IDLE; |
38 |
cntx = c; |
cntx = c; |
|
hndl = h; |
|
39 |
} |
} |
40 |
|
|
41 |
public List<StationBean> getStations() { |
|
42 |
return safeStationList; |
public boolean hasLocation() { |
43 |
|
return savedLocation != null; |
44 |
|
} |
45 |
|
|
46 |
|
public Location getLocation() |
47 |
|
{ |
48 |
|
return savedLocation; |
49 |
} |
} |
50 |
|
|
51 |
public void abortLocationListener() { |
public boolean hasGps() { |
52 |
locManager.removeUpdates(this); |
return hasGps; |
53 |
|
} |
54 |
|
|
55 |
|
public int getSatCount() { |
56 |
|
return satCount; |
57 |
|
} |
58 |
|
|
59 |
|
public LookupStates getState() { |
60 |
|
return state; |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
public long elapsedTime() { |
65 |
|
long now = android.os.SystemClock.elapsedRealtime(); |
66 |
|
|
67 |
|
return now - startTime; |
68 |
} |
} |
69 |
|
|
70 |
public void locateStations() { |
public void locateStations() { |
71 |
//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 |
|
72 |
|
state = LookupStates.STARTED; |
73 |
|
|
74 |
|
hasGps = false; |
75 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
76 |
/*//testcode |
satCount = 0; |
77 |
List<String> provs = locManager.getAllProviders(); |
|
78 |
for (String p : provs) { |
startTime = android.os.SystemClock.elapsedRealtime(); |
79 |
Log.e("Provider", p); |
|
80 |
} |
List<String> providers = locManager.getProviders(true); |
81 |
provs = locManager.getProviders(true); |
|
82 |
for (String p : provs) { |
if (providers.size() > 0) { |
83 |
Log.e("ActiveProvider", p); |
for(String provider : providers) { |
84 |
} |
Log.i("Provider", ""+provider); |
85 |
*/ |
if (provider.equalsIgnoreCase("gps")) { |
86 |
Criteria c = new Criteria(); |
locManager.addGpsStatusListener(this); |
87 |
c.setAccuracy(Criteria.ACCURACY_FINE); |
hasGps = true; |
88 |
String bestProv = locManager.getBestProvider(c, true); |
} |
|
Log.e("BestProvider", bestProv); |
|
89 |
|
|
90 |
if (bestProv != null) { |
locManager.requestLocationUpdates(provider, 0, 0, this); |
91 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
} |
92 |
} else { |
} else { |
93 |
// message that no suitable provider was found |
// message that no suitable provider was found |
94 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
95 |
|
state = LookupStates.NOPROVIDER; |
96 |
|
|
97 |
} |
} |
98 |
} |
} |
99 |
@Override |
@Override |
100 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
101 |
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()); |
|
|
|
|
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
|
|
return; |
|
|
|
|
|
locManager.removeUpdates(this); |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
|
102 |
|
|
|
//ToDo: to be nice put the api key into the request |
|
|
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(10000); |
|
|
|
|
|
String line; |
|
|
StringBuilder builder = new StringBuilder(); |
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 8192); |
|
|
while((line = reader.readLine()) != null) { |
|
|
builder.append(line); |
|
|
}*/ |
|
|
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); |
|
103 |
|
|
104 |
float distance = location.distanceTo(tmpLocation); |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
105 |
|
savedLocation = new Location(location); //save a copy |
106 |
stationList.add( new StationBean(title,lat,lng,(int)distance,addr) ); |
|
107 |
|
if (hasGps) { |
108 |
Log.e("Location", title + " " + lat +"," + lng); |
if (!location.getProvider().equals("gps")) { |
109 |
|
return; // at least give the gps a chance |
110 |
|
} else if (location.getAccuracy() > 512) { |
111 |
|
return; //if we have a gps provider lets wait for a more precise fix |
112 |
} |
} |
|
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
|
113 |
|
|
|
} catch (Exception e) { |
|
|
Log.e("Location","Location",e); |
|
|
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
114 |
} |
} |
115 |
|
stopSearch(); |
116 |
|
state = LookupStates.GOTLOCATION; |
117 |
|
|
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
public void stopSearch() |
122 |
|
{ |
123 |
|
if (state == LookupStates.STARTED) { |
124 |
|
state = LookupStates.IDLE; |
125 |
|
locManager.removeGpsStatusListener(this); |
126 |
|
locManager.removeUpdates(this); |
127 |
|
} |
128 |
} |
} |
129 |
|
|
130 |
|
|
131 |
@Override |
@Override |
132 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
133 |
} |
} |
134 |
|
|
135 |
|
|
136 |
@Override |
@Override |
137 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
138 |
} |
} |
139 |
|
|
140 |
|
|
143 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
144 |
|
|
145 |
} |
} |
146 |
|
|
147 |
|
|
148 |
|
@Override //GpsStatus.Listener |
149 |
|
public void onGpsStatusChanged(int event) { |
150 |
|
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { |
151 |
|
int count = 0; |
152 |
|
GpsStatus status = locManager.getGpsStatus(null); |
153 |
|
for (GpsSatellite sat : status.getSatellites()) { |
154 |
|
count ++; |
155 |
|
} |
156 |
|
|
157 |
|
satCount = count; |
158 |
|
} |
159 |
|
|
160 |
|
} |
161 |
} |
} |