/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 570 - (show annotations) (download)
Fri Jan 29 12:34:17 2010 UTC (14 years, 3 months ago) by torben
File size: 3038 byte(s)
Only try to save lastknown if there actually _is_ a last known location
1 package dk.thoerup.traininfo;
2
3 import java.util.List;
4
5 import android.content.Context;
6 import android.location.Location;
7 import android.location.LocationListener;
8 import android.location.LocationManager;
9 import android.os.Bundle;
10 import android.os.Handler;
11 import android.util.Log;
12
13
14 public class LocationLookup implements LocationListener{
15
16 LocationManager locManager;
17 Context cntx;
18 Handler hndl;
19
20 Location lastKnownLocation = null;
21 Location savedLocation = null;
22
23 boolean isSearching = false;
24 boolean hasGps;
25
26 public LocationLookup(Context c, Handler h) {
27 cntx = c;
28 hndl = h;
29 }
30
31
32 public boolean hasLocation() {
33 return savedLocation != null;
34 }
35
36 public Location getLocation()
37 {
38 return savedLocation;
39 }
40
41 public Location getLastKnownLocation() {
42 return lastKnownLocation;
43 }
44
45 public void locateStations() {
46
47 isSearching = true;
48
49 hasGps = false;
50 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
51
52 List<String> providers = locManager.getProviders(true);
53
54 if (providers.size() > 0) {
55 for(String provider : providers) {
56 Log.i("Provider", ""+provider);
57 if (provider.equalsIgnoreCase("gps"))
58 hasGps = true;
59 locManager.requestLocationUpdates(provider, 0, 0, this);
60 Location tmpLastKnown = locManager.getLastKnownLocation(provider);
61 if (tmpLastKnown != null) {
62 saveLastKnownLocation(tmpLastKnown);
63 }
64 }
65 } else {
66 // message that no suitable provider was found
67 hndl.sendEmptyMessage(StationList.NOPROVIDER);
68 }
69 }
70 @Override
71 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());
76
77
78 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
79 savedLocation = new Location(location); //save a copy
80
81 if (hasGps) {
82 if (!location.getProvider().equals("gps")) {
83 return; // at least give the gps a chance
84 } else if (location.getAccuracy() > 256) {
85 return; //if we have a gps provider lets wait for a more precise fix
86 }
87
88 }
89 stopSearch();
90 hndl.sendEmptyMessage(StationList.GOTLOCATION);
91 }
92
93 private void saveLastKnownLocation(Location loc) {
94 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 stopSearch()
104 {
105 if (isSearching) {
106 isSearching = false;
107 locManager.removeUpdates(this);
108 }
109 }
110
111
112 @Override
113 public void onProviderDisabled(String provider) {
114 }
115
116
117 @Override
118 public void onProviderEnabled(String provider) {
119 }
120
121
122 @Override
123 public void onStatusChanged(String provider, int status, Bundle extras) {
124 // TODO Auto-generated method stub
125
126 }
127 }

  ViewVC Help
Powered by ViewVC 1.1.20