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; |
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.List; |
import java.util.List; |
5 |
|
|
6 |
import org.json.JSONArray; |
import org.json.JSONArray; |
7 |
import org.json.JSONObject; |
import org.json.JSONObject; |
8 |
|
|
|
import dk.thoerup.traininfo.util.DownloadUtil; |
|
|
|
|
9 |
import android.content.Context; |
import android.content.Context; |
10 |
import android.location.Criteria; |
import android.location.Criteria; |
11 |
import android.location.Location; |
import android.location.Location; |
12 |
import android.location.LocationListener; |
import android.location.LocationListener; |
13 |
import android.location.LocationManager; |
import android.location.LocationManager; |
14 |
|
import android.location.LocationProvider; |
15 |
import android.os.Bundle; |
import android.os.Bundle; |
16 |
import android.os.Handler; |
import android.os.Handler; |
17 |
import android.util.Log; |
import android.util.Log; |
18 |
|
import dk.thoerup.traininfo.util.DownloadUtil; |
19 |
|
|
20 |
public class StationLocator implements LocationListener{ |
public class StationLocator implements LocationListener{ |
21 |
|
|
26 |
ArrayList<StationBean> stationList = new ArrayList<StationBean>(); |
ArrayList<StationBean> stationList = new ArrayList<StationBean>(); |
27 |
List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList); |
List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList); |
28 |
|
|
29 |
|
|
30 |
|
Location savedLocation = null; |
31 |
|
|
32 |
public StationLocator(Context c, Handler h) { |
public StationLocator(Context c, Handler h) { |
33 |
cntx = c; |
cntx = c; |
34 |
hndl = h; |
hndl = h; |
59 |
Criteria c = new Criteria(); |
Criteria c = new Criteria(); |
60 |
c.setAccuracy(Criteria.ACCURACY_FINE); |
c.setAccuracy(Criteria.ACCURACY_FINE); |
61 |
String bestProv = locManager.getBestProvider(c, true); |
String bestProv = locManager.getBestProvider(c, true); |
62 |
Log.e("BestProvider", bestProv); |
Log.i("BestProvider", ""+bestProv); |
63 |
|
|
64 |
|
|
65 |
if (bestProv != null) { |
if (bestProv != null) { |
66 |
|
savedLocation = locManager.getLastKnownLocation(bestProv); |
67 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
68 |
} else { |
} else { |
69 |
// message that no suitable provider was found |
// message that no suitable provider was found |
74 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
75 |
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()); |
76 |
|
|
77 |
|
savedLocation = new Location(location); //save a copy |
78 |
|
|
79 |
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 |
80 |
return; |
return; |
81 |
|
|
82 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
83 |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
84 |
|
} |
85 |
|
|
86 |
|
public void findNearestStations() { |
87 |
|
findNearestStations(savedLocation); |
88 |
|
} |
89 |
|
|
90 |
|
public void findNearestStations(Location location) { |
91 |
//ToDo: to be nice put the api key into the request |
//ToDo: to be nice put the api key into the request |
92 |
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=" + location.getLatitude() + "%2C" + location.getLongitude() + "&v=1.0"; |
93 |
//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"; |
//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"; |
130 |
Log.e("Location","Location",e); |
Log.e("Location","Location",e); |
131 |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
132 |
} |
} |
133 |
|
|
134 |
} |
} |
135 |
|
|
136 |
|
|
153 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
154 |
|
|
155 |
} |
} |
156 |
|
|
157 |
|
public static void injectMockLocation(Context cntx) { |
158 |
|
Location loc = new Location("gps2"); |
159 |
|
loc.setLatitude(56.378084); |
160 |
|
loc.setLongitude(9.659815); |
161 |
|
|
162 |
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
163 |
|
lm.setTestProviderEnabled("gps2", true); |
164 |
|
lm.setTestProviderLocation("gps2", loc); |
165 |
|
|
166 |
|
} |
167 |
} |
} |