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; |
|
import android.location.Criteria; |
|
6 |
import android.location.Location; |
import android.location.Location; |
7 |
import android.location.LocationListener; |
import android.location.LocationListener; |
8 |
import android.location.LocationManager; |
import android.location.LocationManager; |
|
import android.location.LocationProvider; |
|
9 |
import android.os.Bundle; |
import android.os.Bundle; |
10 |
import android.os.Handler; |
import android.os.Handler; |
11 |
import android.util.Log; |
import android.util.Log; |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
|
|
import dk.thoerup.traininfo.provider.StationProvider; |
|
|
import dk.thoerup.traininfo.util.DownloadUtil; |
|
12 |
|
|
13 |
public class StationLocator implements LocationListener{ |
|
14 |
|
public class LocationLookup implements LocationListener{ |
15 |
|
|
16 |
LocationManager locManager; |
LocationManager locManager; |
17 |
Context cntx; |
Context cntx; |
18 |
Handler hndl; |
Handler hndl; |
19 |
|
|
|
List<StationBean> stationList = new ArrayList<StationBean>(); |
|
|
|
|
|
|
|
20 |
Location savedLocation = null; |
Location savedLocation = null; |
21 |
|
|
22 |
|
boolean isSearching = false; |
23 |
|
boolean hasGps; |
24 |
|
|
25 |
public StationLocator(Context c, Handler h) { |
public LocationLookup(Context c, Handler h) { |
26 |
cntx = c; |
cntx = c; |
27 |
hndl = h; |
hndl = h; |
28 |
} |
} |
29 |
|
|
30 |
public List<StationBean> getStations() { |
|
31 |
return stationList; |
public boolean hasLocation() { |
32 |
|
return savedLocation != null; |
33 |
} |
} |
34 |
|
|
35 |
public void abortLocationListener() { |
public Location getLocation() |
36 |
locManager.removeUpdates(this); |
{ |
37 |
|
return savedLocation; |
38 |
} |
} |
39 |
|
|
40 |
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); |
|
41 |
|
|
42 |
|
isSearching = true; |
43 |
|
|
44 |
|
hasGps = false; |
45 |
|
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
46 |
|
|
47 |
if (bestProv != null) { |
List<String> providers = locManager.getProviders(true); |
48 |
savedLocation = locManager.getLastKnownLocation(bestProv); |
|
49 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
if (providers.size() > 0) { |
50 |
|
for(String provider : providers) { |
51 |
|
Log.i("Provider", ""+provider); |
52 |
|
if (provider.equalsIgnoreCase("gps")) |
53 |
|
hasGps = true; |
54 |
|
locManager.requestLocationUpdates(provider, 0, 0, this); |
55 |
|
} |
56 |
} else { |
} else { |
57 |
// message that no suitable provider was found |
// message that no suitable provider was found |
58 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
hndl.sendEmptyMessage(StationList.NOPROVIDER); |
59 |
} |
} |
60 |
} |
} |
61 |
@Override |
@Override |
62 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
63 |
|
if (isSearching == false) |
64 |
|
return; |
65 |
|
|
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 |
|
|
69 |
savedLocation = new Location(location); //save a copy |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
70 |
|
savedLocation = new Location(location); //save a copy |
71 |
|
|
72 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
if (hasGps) { |
73 |
return; |
if (!location.getProvider().equals("gps")) { |
74 |
|
return; // at least give the gps a chance |
75 |
|
} else if (location.getAccuracy() > 256) { |
76 |
|
return; //if we have a gps provider lets wait for a more precise fix |
77 |
|
} |
78 |
|
|
79 |
locManager.removeUpdates(this); |
} |
80 |
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
stopSearch(); |
81 |
} |
hndl.sendEmptyMessage(StationList.GOTLOCATION); |
|
|
|
|
public void findNearestStations() { |
|
|
findNearestStations(savedLocation); |
|
82 |
} |
} |
83 |
|
|
84 |
public void findNearestStations(Location location) { |
public void stopSearch() |
85 |
StationProvider prov = ProviderFactory.getStationProvider(); |
{ |
86 |
prov.lookupStations(location); |
if (isSearching) { |
87 |
this.stationList = prov.getStations(); |
isSearching = false; |
88 |
|
locManager.removeUpdates(this); |
89 |
if (stationList.size() > 0) |
} |
|
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
|
|
else |
|
|
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
90 |
} |
} |
91 |
|
|
92 |
|
|
93 |
@Override |
@Override |
94 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
95 |
} |
} |
96 |
|
|
97 |
|
|
98 |
@Override |
@Override |
99 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
100 |
} |
} |
101 |
|
|
102 |
|
|
105 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
106 |
|
|
107 |
} |
} |
|
|
|
|
|
|
|
public static void injectMockLocation(Context cntx) { |
|
|
Location odder = new Location("gps2"); |
|
|
odder.setLatitude(55.976632); |
|
|
odder.setLongitude(10.16407); |
|
|
|
|
|
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573 |
|
|
kbh.setLatitude(55.675092); |
|
|
kbh.setLongitude(12.578573); |
|
|
|
|
|
Location bjbro = new Location("gps2"); |
|
|
bjbro.setLatitude(56.380745); |
|
|
bjbro.setLongitude(9.655609); |
|
|
|
|
|
|
|
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
|
|
if (lm.getProvider("gps2") == null) |
|
|
lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); |
|
|
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"); |
|
|
} |
|
108 |
} |
} |