3 |
import java.util.List; |
import java.util.List; |
4 |
|
|
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; |
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; |
|
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 |
|
|
20 |
StationProvider provider; |
Location lastKnownLocation = null; |
21 |
Location savedLocation = null; |
Location savedLocation = null; |
22 |
|
|
23 |
|
boolean isSearching = false; |
24 |
boolean hasGps; |
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; |
|
|
|
|
provider = ProviderFactory.getStationProvider(); |
|
29 |
} |
} |
30 |
|
|
31 |
public List<StationBean> getStations() { |
|
32 |
return provider.getStations(); |
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 boolean hasLocation() { |
public Location getLastKnownLocation() { |
42 |
return savedLocation != null; |
return lastKnownLocation; |
43 |
} |
} |
44 |
|
|
45 |
public void locateStations() { |
public void locateStations() { |
46 |
|
|
47 |
|
isSearching = true; |
48 |
|
|
49 |
hasGps = false; |
hasGps = false; |
|
|
|
50 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
51 |
|
|
52 |
Criteria c = new Criteria(); |
List<String> providers = locManager.getProviders(true); |
|
c.setAccuracy(Criteria.ACCURACY_FINE); |
|
|
List<String> providers = locManager.getProviders(true); |
|
|
|
|
53 |
|
|
54 |
if (providers.size() > 0) { |
if (providers.size() > 0) { |
55 |
for(String provider : providers) { |
for(String provider : providers) { |
57 |
if (provider.equalsIgnoreCase("gps")) |
if (provider.equalsIgnoreCase("gps")) |
58 |
hasGps = true; |
hasGps = true; |
59 |
locManager.requestLocationUpdates(provider, 0, 0, this); |
locManager.requestLocationUpdates(provider, 0, 0, this); |
60 |
|
Location tmpLastKnown = locManager.getLastKnownLocation(provider); |
61 |
|
if (tmpLastKnown != null) { |
62 |
|
saveLastKnownLocation(tmpLastKnown); |
63 |
|
} |
64 |
} |
} |
65 |
} else { |
} else { |
66 |
// message that no suitable provider was found |
// message that no suitable provider was found |
67 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
hndl.sendEmptyMessage(StationList.NOPROVIDER); |
68 |
} |
} |
69 |
} |
} |
70 |
@Override |
@Override |
71 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
72 |
|
if (isSearching == false) |
73 |
|
return; |
74 |
|
|
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 |
|
|
78 |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
79 |
savedLocation = new Location(location); //save a copy |
savedLocation = new Location(location); //save a copy |
81 |
if (hasGps) { |
if (hasGps) { |
82 |
if (!location.getProvider().equals("gps")) { |
if (!location.getProvider().equals("gps")) { |
83 |
return; // at least give the gps a chance |
return; // at least give the gps a chance |
84 |
} else if (location.getAccuracy() > 100) { |
} else if (location.getAccuracy() > 512) { |
85 |
return; //if we have a gps provider lets wait for a more precise fix |
return; //if we have a gps provider lets wait for a more precise fix |
86 |
} |
} |
87 |
|
|
88 |
} |
} |
89 |
|
stopSearch(); |
90 |
locManager.removeUpdates(this); |
hndl.sendEmptyMessage(StationList.GOTLOCATION); |
|
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
|
91 |
} |
} |
92 |
|
|
93 |
public void findNearestStations() { |
private void saveLastKnownLocation(Location loc) { |
94 |
findNearestStations(savedLocation); |
if (lastKnownLocation == null) { |
95 |
|
lastKnownLocation = loc; |
96 |
|
} else { |
97 |
|
if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved |
98 |
|
lastKnownLocation = loc; |
99 |
|
} |
100 |
|
} |
101 |
} |
} |
102 |
|
|
103 |
public void findNearestStations(Location location) { |
public void stopSearch() |
104 |
provider.lookupStations(location); |
{ |
105 |
|
if (isSearching) { |
106 |
if ( provider.getStations().size() > 0) |
isSearching = false; |
107 |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
locManager.removeUpdates(this); |
108 |
else |
} |
|
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
109 |
} |
} |
110 |
|
|
111 |
|
|
112 |
@Override |
@Override |
113 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
114 |
} |
} |
115 |
|
|
116 |
|
|
117 |
@Override |
@Override |
118 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
119 |
} |
} |
120 |
|
|
121 |
|
|
124 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
125 |
|
|
126 |
} |
} |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
Location hillerod = new Location("gps"); |
|
|
hillerod.setLatitude(55.929177); |
|
|
hillerod.setLongitude(12.308095); |
|
|
|
|
|
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", hillerod); |
|
|
} |
|
|
|
|
|
public static void removeMockLocation(Context cntx) { |
|
|
LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
|
|
if (lm.getProvider("gps2") != null) |
|
|
lm.removeTestProvider("gps2"); |
|
|
} |
|
127 |
} |
} |