1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
|
import java.util.List; |
|
3 |
|
|
4 |
import android.content.Context; |
import android.content.Context; |
5 |
|
import android.content.SharedPreferences; |
6 |
import android.location.GpsSatellite; |
import android.location.GpsSatellite; |
7 |
import android.location.GpsStatus; |
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; |
12 |
import android.os.Handler; |
import android.preference.PreferenceManager; |
|
import android.os.Message; |
|
13 |
import android.util.Log; |
import android.util.Log; |
14 |
|
|
15 |
|
|
16 |
public class LocationLookup implements LocationListener, GpsStatus.Listener { |
public class LocationLookup implements LocationListener, GpsStatus.Listener { |
17 |
|
|
18 |
|
public enum LookupStates { |
19 |
|
GOTLOCATION, |
20 |
|
NOPROVIDER, |
21 |
|
STARTED, |
22 |
|
IDLE |
23 |
|
} |
24 |
|
|
25 |
LocationManager locManager; |
private LocationManager locManager; |
26 |
Context cntx; |
private Context cntx; |
|
Handler hndl; |
|
27 |
|
|
28 |
Location lastKnownLocation = null; |
private Location savedLocation = null; |
29 |
Location savedLocation = null; |
private int satCount; |
30 |
|
|
31 |
|
private boolean hasGps; |
32 |
|
|
33 |
|
private LookupStates state; |
34 |
|
|
35 |
boolean isSearching = false; |
private long startTime; |
|
boolean hasGps; |
|
36 |
|
|
37 |
public LocationLookup(Context c, Handler h) { |
public LocationLookup(Context c) { |
38 |
|
state = LookupStates.IDLE; |
39 |
cntx = c; |
cntx = c; |
|
hndl = h; |
|
40 |
} |
} |
41 |
|
|
42 |
|
|
49 |
return savedLocation; |
return savedLocation; |
50 |
} |
} |
51 |
|
|
52 |
public Location getLastKnownLocation() { |
public boolean hasGps() { |
53 |
return lastKnownLocation; |
return hasGps; |
54 |
|
} |
55 |
|
|
56 |
|
public int getSatCount() { |
57 |
|
return satCount; |
58 |
|
} |
59 |
|
|
60 |
|
public LookupStates getState() { |
61 |
|
return state; |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
|
public long elapsedTime() { |
66 |
|
long now = android.os.SystemClock.elapsedRealtime(); |
67 |
|
|
68 |
|
return now - startTime; |
69 |
} |
} |
70 |
|
|
71 |
public void locateStations() { |
public void locateStations() { |
72 |
|
|
73 |
isSearching = true; |
state = LookupStates.STARTED; |
74 |
|
|
75 |
hasGps = false; |
hasGps = false; |
76 |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); |
77 |
|
satCount = 0; |
78 |
List<String> providers = locManager.getProviders(true); |
|
79 |
|
startTime = android.os.SystemClock.elapsedRealtime(); |
80 |
|
|
81 |
if (providers.size() > 0) { |
boolean hasProvider = false; |
82 |
for(String provider : providers) { |
|
83 |
Log.i("Provider", ""+provider); |
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cntx); |
84 |
if (provider.equalsIgnoreCase("gps")) { |
String networkPref = prefs.getString("location", "GPS"); //default value is gps |
85 |
locManager.addGpsStatusListener(this); |
|
86 |
hasGps = true; |
if (networkPref.equals("GPS")) { |
87 |
} |
if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { |
88 |
|
locManager.addGpsStatusListener(this); |
89 |
locManager.requestLocationUpdates(provider, 0, 0, this); |
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); |
90 |
Location tmpLastKnown = locManager.getLastKnownLocation(provider); |
|
91 |
if (tmpLastKnown != null) { |
hasGps = true; |
92 |
saveLastKnownLocation(tmpLastKnown); |
hasProvider = true; |
|
} |
|
93 |
} |
} |
94 |
} else { |
} |
95 |
|
|
96 |
|
if (locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { |
97 |
|
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); |
98 |
|
hasProvider = true; |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
if (hasProvider == false) { |
103 |
// message that no suitable provider was found |
// message that no suitable provider was found |
104 |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
//hndl.sendEmptyMessage(StationList.NOPROVIDER); |
105 |
hndl.sendEmptyMessage(StationList.LookupStates.NOPROVIDER.ordinal()); |
state = LookupStates.NOPROVIDER; |
106 |
|
|
107 |
} |
} |
108 |
} |
} |
109 |
@Override |
@Override |
110 |
public void onLocationChanged(Location location) { |
public void onLocationChanged(Location location) { |
|
if (isSearching == false) |
|
|
return; |
|
|
|
|
111 |
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()); |
112 |
|
|
113 |
|
|
123 |
|
|
124 |
} |
} |
125 |
stopSearch(); |
stopSearch(); |
126 |
hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal()); |
state = LookupStates.GOTLOCATION; |
127 |
|
|
128 |
} |
} |
129 |
|
|
|
private void saveLastKnownLocation(Location loc) { |
|
|
if (lastKnownLocation == null) { |
|
|
lastKnownLocation = loc; |
|
|
} else { |
|
|
if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved |
|
|
lastKnownLocation = loc; |
|
|
} |
|
|
} |
|
|
} |
|
130 |
|
|
131 |
public void stopSearch() |
public void stopSearch() |
132 |
{ |
{ |
133 |
if (isSearching) { |
if (state == LookupStates.STARTED) { |
134 |
isSearching = false; |
state = LookupStates.IDLE; |
135 |
locManager.removeGpsStatusListener(this); |
locManager.removeGpsStatusListener(this); |
136 |
locManager.removeUpdates(this); |
locManager.removeUpdates(this); |
137 |
} |
} |
162 |
GpsStatus status = locManager.getGpsStatus(null); |
GpsStatus status = locManager.getGpsStatus(null); |
163 |
for (GpsSatellite sat : status.getSatellites()) { |
for (GpsSatellite sat : status.getSatellites()) { |
164 |
count ++; |
count ++; |
165 |
} |
} |
166 |
|
|
167 |
Message msg = new Message(); |
satCount = count; |
|
msg.what = StationList.LookupStates.GPS_SAT_COUNT.ordinal(); |
|
|
msg.arg1 = count; |
|
|
hndl.sendMessage(msg); |
|
168 |
} |
} |
169 |
|
|
170 |
} |
} |