/[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 474 - (show annotations) (download)
Tue Oct 27 13:40:15 2009 UTC (14 years, 6 months ago) by torben
File size: 2498 byte(s)
Remove unneeded imports
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 savedLocation = null;
21
22 boolean isSearching = false;
23 boolean hasGps;
24
25 public LocationLookup(Context c, Handler h) {
26 cntx = c;
27 hndl = h;
28 }
29
30
31 public boolean hasLocation() {
32 return savedLocation != null;
33 }
34
35 public Location getLocation()
36 {
37 return savedLocation;
38 }
39
40 public void locateStations() {
41
42 isSearching = true;
43
44 hasGps = false;
45 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
46
47 List<String> providers = locManager.getProviders(true);
48
49 if (providers.size() > 0) {
50 for(String provider : providers) {
51 Log.i("Provider", ""+provider);
52 if (provider.equalsIgnoreCase("gps"))
53 hasGps = true;
54 locManager.requestLocationUpdates(provider, 0, 0, this);
55 }
56 } else {
57 // message that no suitable provider was found
58 hndl.sendEmptyMessage(StationList.NOPROVIDER);
59 }
60 }
61 @Override
62 public void onLocationChanged(Location location) {
63 if (isSearching == false)
64 return;
65
66 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
67
68
69 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
70 savedLocation = new Location(location); //save a copy
71
72 if (hasGps) {
73 if (!location.getProvider().equals("gps")) {
74 return; // at least give the gps a chance
75 } else if (location.getAccuracy() > 256) {
76 return; //if we have a gps provider lets wait for a more precise fix
77 }
78
79 }
80 stopSearch();
81 hndl.sendEmptyMessage(StationList.GOTLOCATION);
82 }
83
84 public void stopSearch()
85 {
86 if (isSearching) {
87 isSearching = false;
88 locManager.removeUpdates(this);
89 }
90 }
91
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 }

  ViewVC Help
Powered by ViewVC 1.1.20