/[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 310 - (show annotations) (download)
Thu Sep 10 19:09:09 2009 UTC (14 years, 8 months ago) by torben
File size: 4205 byte(s)
Make code work with new servlets (and new db layout)
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 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
49
50 List<String> providers = locManager.getProviders(true);
51
52 if (providers.size() > 0) {
53 for(String provider : providers) {
54 Log.i("Provider", ""+provider);
55 if (provider.equalsIgnoreCase("gps"))
56 hasGps = true;
57 locManager.requestLocationUpdates(provider, 0, 0, this);
58 }
59 } else {
60 // message that no suitable provider was found
61 hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
62 }
63 }
64 @Override
65 public void onLocationChanged(Location location) {
66 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
67
68 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
69 savedLocation = new Location(location); //save a copy
70
71 if (hasGps) {
72 if (!location.getProvider().equals("gps")) {
73 return; // at least give the gps a chance
74 } else if (location.getAccuracy() > 100) {
75 return; //if we have a gps provider lets wait for a more precise fix
76 }
77
78 }
79
80 locManager.removeUpdates(this);
81 hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
82 }
83
84 public void findNearestStations() {
85 findNearestStations(savedLocation);
86 }
87
88 public void findNearestStations(Location location) {
89 provider.lookupStations(location);
90
91 if ( provider.getStations().size() > 0)
92 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
93 else
94 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
95 }
96
97
98 @Override
99 public void onProviderDisabled(String provider) {
100 }
101
102
103 @Override
104 public void onProviderEnabled(String provider) {
105 }
106
107
108 @Override
109 public void onStatusChanged(String provider, int status, Bundle extras) {
110 // TODO Auto-generated method stub
111
112 }
113
114
115 public static void injectMockLocation(Context cntx) {
116 Location odder = new Location("gps2");
117 odder.setLatitude(55.976632);
118 odder.setLongitude(10.16407);
119
120 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
121 kbh.setLatitude(55.675092);
122 kbh.setLongitude(12.578573);
123
124 Location bjbro = new Location("gps2");
125 bjbro.setLatitude(56.380745);
126 bjbro.setLongitude(9.655609);
127
128 Location hillerod = new Location("gps");
129 hillerod.setLatitude(55.929177);
130 hillerod.setLongitude(12.308095);
131
132 Location aarhus = new Location("gps"); //Aros
133 aarhus.setLatitude(56.153828);
134 aarhus.setLongitude(10.200369);
135
136
137
138 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
139 if (lm.getProvider("gps2") == null)
140 lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
141 lm.setTestProviderEnabled("gps2", true);
142 lm.setTestProviderLocation("gps2", kbh);
143 }
144
145 public static void removeMockLocation(Context cntx) {
146 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
147 if (lm.getProvider("gps2") != null)
148 lm.removeTestProvider("gps2");
149 }
150 }

  ViewVC Help
Powered by ViewVC 1.1.20