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.provider.ProviderFactory; |
|
|
import dk.thoerup.traininfo.provider.StationProvider; |
|
|
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 |
List<StationBean> stationList = new ArrayList<StationBean>(); |
private LocationManager locManager; |
25 |
|
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 |
Location savedLocation = null; |
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 stationList; |
public boolean hasLocation() { |
43 |
|
return savedLocation != null; |
44 |
|
} |
45 |
|
|
46 |
|
public Location getLocation() |
47 |
|
{ |
48 |
|
return savedLocation; |
49 |
|
} |
50 |
|
|
51 |
|
public boolean hasGps() { |
52 |
|
return hasGps; |
53 |
} |
} |
54 |
|
|
55 |
public void abortLocationListener() { |
public int getSatCount() { |
56 |
locManager.removeUpdates(this); |
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; |
|
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); |
|
77 |
|
|
78 |
|
startTime = android.os.SystemClock.elapsedRealtime(); |
79 |
|
|
80 |
if (bestProv != null) { |
List<String> providers = locManager.getProviders(true); |
81 |
savedLocation = locManager.getLastKnownLocation(bestProv); |
|
82 |
locManager.requestLocationUpdates(bestProv, 0, 0, this); |
boolean hasProvider = false; |
83 |
} else { |
|
84 |
|
for(String provider : providers) { |
85 |
|
if (provider.equals( LocationManager.PASSIVE_PROVIDER)) { |
86 |
|
continue; |
87 |
|
} |
88 |
|
|
89 |
|
Log.i("Provider", ""+provider); |
90 |
|
hasProvider = true; |
91 |
|
if (provider.equalsIgnoreCase("gps")) { |
92 |
|
locManager.addGpsStatusListener(this); |
93 |
|
hasGps = true; |
94 |
|
} |
95 |
|
|
96 |
|
locManager.requestLocationUpdates(provider, 0, 0, this); |
97 |
|
} |
98 |
|
|
99 |
|
if (hasProvider == false) { |
100 |
// message that no suitable provider was found |
// message that no suitable provider was found |
101 |
hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
102 |
|
state = LookupStates.NOPROVIDER; |
103 |
|
|
104 |
} |
} |
105 |
} |
} |
106 |
@Override |
@Override |
107 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
108 |
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()); |
109 |
|
|
110 |
|
|
111 |
savedLocation = new Location(location); //save a copy |
if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) |
112 |
|
savedLocation = new Location(location); //save a copy |
113 |
|
|
114 |
if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix |
if (hasGps) { |
115 |
return; |
if (!location.getProvider().equals("gps")) { |
116 |
|
return; // at least give the gps a chance |
117 |
|
} else if (location.getAccuracy() > 512) { |
118 |
|
return; //if we have a gps provider lets wait for a more precise fix |
119 |
|
} |
120 |
|
|
121 |
|
} |
122 |
|
stopSearch(); |
123 |
|
state = LookupStates.GOTLOCATION; |
124 |
|
|
|
locManager.removeUpdates(this); |
|
|
hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); |
|
125 |
} |
} |
126 |
|
|
|
public void findNearestStations() { |
|
|
findNearestStations(savedLocation); |
|
|
} |
|
127 |
|
|
128 |
public void findNearestStations(Location location) { |
public void stopSearch() |
129 |
StationProvider prov = ProviderFactory.getStationProvider(); |
{ |
130 |
prov.lookupStations(location); |
if (state == LookupStates.STARTED) { |
131 |
this.stationList = prov.getStations(); |
state = LookupStates.IDLE; |
132 |
|
locManager.removeGpsStatusListener(this); |
133 |
if (stationList.size() > 0) |
locManager.removeUpdates(this); |
134 |
hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); |
} |
|
else |
|
|
hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); |
|
135 |
} |
} |
136 |
|
|
137 |
|
|
138 |
@Override |
@Override |
139 |
public void onProviderDisabled(String provider) { |
public void onProviderDisabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
140 |
} |
} |
141 |
|
|
142 |
|
|
143 |
@Override |
@Override |
144 |
public void onProviderEnabled(String provider) { |
public void onProviderEnabled(String provider) { |
|
// TODO Auto-generated method stub |
|
|
|
|
145 |
} |
} |
146 |
|
|
147 |
|
|
150 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
151 |
|
|
152 |
} |
} |
|
|
|
153 |
|
|
154 |
public static void injectMockLocation(Context cntx) { |
|
155 |
Location odder = new Location("gps2"); |
@Override //GpsStatus.Listener |
156 |
odder.setLatitude(55.976632); |
public void onGpsStatusChanged(int event) { |
157 |
odder.setLongitude(10.16407); |
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { |
158 |
|
int count = 0; |
159 |
Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573 |
GpsStatus status = locManager.getGpsStatus(null); |
160 |
kbh.setLatitude(55.675092); |
for (GpsSatellite sat : status.getSatellites()) { |
161 |
kbh.setLongitude(12.578573); |
count ++; |
162 |
|
} |
163 |
Location bjbro = new Location("gps2"); |
|
164 |
bjbro.setLatitude(56.380745); |
satCount = count; |
165 |
bjbro.setLongitude(9.655609); |
} |
166 |
|
|
|
|
|
|
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"); |
|
167 |
} |
} |
168 |
} |
} |