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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 319 - (hide annotations) (download)
Fri Sep 11 12:24:53 2009 UTC (14 years, 8 months ago) by torben
File size: 3922 byte(s)
Provider's : differentiate between an empty returned list or a communication error

Re-organize some of the background work
1 torben 237 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    
14    
15 torben 319 public class LocationLookup implements LocationListener{
16    
17 torben 237 LocationManager locManager;
18     Context cntx;
19     Handler hndl;
20    
21 torben 241 Location savedLocation = null;
22 torben 286
23     boolean hasGps;
24 torben 241
25 torben 319 public LocationLookup(Context c, Handler h) {
26 torben 237 cntx = c;
27     hndl = h;
28 torben 254
29 torben 237 }
30    
31     public void abortLocationListener() {
32     locManager.removeUpdates(this);
33     }
34 torben 285
35     public boolean hasLocation() {
36     return savedLocation != null;
37     }
38 torben 319
39     public Location getLocation()
40     {
41     return savedLocation;
42     }
43 torben 237
44     public void locateStations() {
45 torben 286 hasGps = false;
46 torben 237 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
47 torben 286
48 torben 288 List<String> providers = locManager.getProviders(true);
49 torben 241
50 torben 286 if (providers.size() > 0) {
51     for(String provider : providers) {
52     Log.i("Provider", ""+provider);
53     if (provider.equalsIgnoreCase("gps"))
54     hasGps = true;
55     locManager.requestLocationUpdates(provider, 0, 0, this);
56     }
57 torben 237 } else {
58     // message that no suitable provider was found
59     hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER);
60     }
61     }
62     @Override
63     public void onLocationChanged(Location location) {
64     Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
65    
66 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
67     savedLocation = new Location(location); //save a copy
68 torben 241
69 torben 286 if (hasGps) {
70     if (!location.getProvider().equals("gps")) {
71     return; // at least give the gps a chance
72     } else if (location.getAccuracy() > 100) {
73     return; //if we have a gps provider lets wait for a more precise fix
74     }
75 torben 237
76 torben 286 }
77    
78 torben 237 locManager.removeUpdates(this);
79     hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
80 torben 241 }
81 torben 319 /*
82    
83 torben 241 public void findNearestStations(Location location) {
84 torben 254 provider.lookupStations(location);
85 torben 253
86 torben 254 if ( provider.getStations().size() > 0)
87 torben 237 hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
88 torben 253 else
89 torben 237 hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
90 torben 319 }*/
91 torben 237
92    
93     @Override
94     public void onProviderDisabled(String provider) {
95     }
96    
97    
98     @Override
99     public void onProviderEnabled(String provider) {
100     }
101    
102    
103     @Override
104     public void onStatusChanged(String provider, int status, Bundle extras) {
105     // TODO Auto-generated method stub
106    
107     }
108 torben 253
109 torben 252
110 torben 241 public static void injectMockLocation(Context cntx) {
111 torben 252 Location odder = new Location("gps2");
112     odder.setLatitude(55.976632);
113     odder.setLongitude(10.16407);
114 torben 241
115 torben 252 Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
116     kbh.setLatitude(55.675092);
117     kbh.setLongitude(12.578573);
118    
119 torben 253 Location bjbro = new Location("gps2");
120     bjbro.setLatitude(56.380745);
121     bjbro.setLongitude(9.655609);
122    
123 torben 256 Location hillerod = new Location("gps");
124     hillerod.setLatitude(55.929177);
125     hillerod.setLongitude(12.308095);
126 torben 253
127 torben 289 Location aarhus = new Location("gps"); //Aros
128     aarhus.setLatitude(56.153828);
129     aarhus.setLongitude(10.200369);
130    
131    
132    
133 torben 241 LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
134 torben 252 if (lm.getProvider("gps2") == null)
135     lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
136 torben 241 lm.setTestProviderEnabled("gps2", true);
137 torben 310 lm.setTestProviderLocation("gps2", kbh);
138 torben 241 }
139 torben 252
140     public static void removeMockLocation(Context cntx) {
141     LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
142     if (lm.getProvider("gps2") != null)
143     lm.removeTestProvider("gps2");
144     }
145 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20