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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 286 - (show annotations) (download)
Fri Aug 28 07:21:04 2009 UTC (14 years, 8 months ago) by torben
File size: 4238 byte(s)
Better balancing if both gps and network location providers are enabled
1 package dk.thoerup.traininfo;
2
3 import java.util.List;
4
5 import android.content.Context;
6 import android.location.Criteria;
7 import android.location.Location;
8 import android.location.LocationListener;
9 import android.location.LocationManager;
10 import android.os.Bundle;
11 import android.os.Handler;
12 import android.util.Log;
13 import dk.thoerup.traininfo.provider.ProviderFactory;
14 import dk.thoerup.traininfo.provider.StationProvider;
15
16 public class StationLocator implements LocationListener{
17
18 LocationManager locManager;
19 Context cntx;
20 Handler hndl;
21
22 StationProvider provider;
23 Location savedLocation = null;
24
25 boolean hasGps;
26
27 public StationLocator(Context c, Handler h) {
28 cntx = c;
29 hndl = h;
30
31 provider = ProviderFactory.getStationProvider();
32 }
33
34 public List<StationBean> getStations() {
35 return provider.getStations();
36 }
37
38 public void abortLocationListener() {
39 locManager.removeUpdates(this);
40 }
41
42 public boolean hasLocation() {
43 return savedLocation != null;
44 }
45
46 public void locateStations() {
47 hasGps = false;
48
49 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
50
51 Criteria c = new Criteria();
52 c.setAccuracy(Criteria.ACCURACY_FINE);
53 List<String> providers = locManager.getProviders(true);
54
55
56 if (providers.size() > 0) {
57 for(String provider : providers) {
58 Log.i("Provider", ""+provider);
59 if (provider.equalsIgnoreCase("gps"))
60 hasGps = true;
61 locManager.requestLocationUpdates(provider, 0, 0, this);
62 }
63 } else {
64 // message that no suitable provider was found
65 hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
66 }
67 }
68 @Override
69 public void onLocationChanged(Location location) {
70 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
71
72 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
73 savedLocation = new Location(location); //save a copy
74
75 if (hasGps) {
76 if (!location.getProvider().equals("gps")) {
77 return; // at least give the gps a chance
78 } else if (location.getAccuracy() > 100) {
79 return; //if we have a gps provider lets wait for a more precise fix
80 }
81
82 }
83
84 locManager.removeUpdates(this);
85 hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
86 }
87
88 public void findNearestStations() {
89 findNearestStations(savedLocation);
90 }
91
92 public void findNearestStations(Location location) {
93 provider.lookupStations(location);
94
95 if ( provider.getStations().size() > 0)
96 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
97 else
98 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
99 }
100
101
102 @Override
103 public void onProviderDisabled(String provider) {
104 // TODO Auto-generated method stub
105
106 }
107
108
109 @Override
110 public void onProviderEnabled(String provider) {
111 // TODO Auto-generated method stub
112
113 }
114
115
116 @Override
117 public void onStatusChanged(String provider, int status, Bundle extras) {
118 // TODO Auto-generated method stub
119
120 }
121
122
123 public static void injectMockLocation(Context cntx) {
124 Location odder = new Location("gps2");
125 odder.setLatitude(55.976632);
126 odder.setLongitude(10.16407);
127
128 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
129 kbh.setLatitude(55.675092);
130 kbh.setLongitude(12.578573);
131
132 Location bjbro = new Location("gps2");
133 bjbro.setLatitude(56.380745);
134 bjbro.setLongitude(9.655609);
135
136 Location hillerod = new Location("gps");
137 hillerod.setLatitude(55.929177);
138 hillerod.setLongitude(12.308095);
139
140 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
141 if (lm.getProvider("gps2") == null)
142 lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
143 lm.setTestProviderEnabled("gps2", true);
144 lm.setTestProviderLocation("gps2", hillerod);
145 }
146
147 public static void removeMockLocation(Context cntx) {
148 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
149 if (lm.getProvider("gps2") != null)
150 lm.removeTestProvider("gps2");
151 }
152 }

  ViewVC Help
Powered by ViewVC 1.1.20