/[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 1446 - (hide annotations) (download)
Wed May 4 20:25:15 2011 UTC (13 years, 1 month ago) by torben
File size: 3991 byte(s)
Add preferences
1 torben 237 package dk.thoerup.traininfo;
2    
3    
4     import android.content.Context;
5 torben 1446 import android.content.SharedPreferences;
6 torben 1142 import android.location.GpsSatellite;
7     import android.location.GpsStatus;
8 torben 237 import android.location.Location;
9     import android.location.LocationListener;
10     import android.location.LocationManager;
11     import android.os.Bundle;
12 torben 1446 import android.preference.PreferenceManager;
13 torben 237 import android.util.Log;
14    
15    
16 torben 1142 public class LocationLookup implements LocationListener, GpsStatus.Listener {
17 torben 1143
18     public enum LookupStates {
19     GOTLOCATION,
20     NOPROVIDER,
21     STARTED,
22     IDLE
23     }
24 torben 319
25 torben 1143 private LocationManager locManager;
26     private Context cntx;
27 torben 237
28 torben 1143 private Location savedLocation = null;
29     private int satCount;
30 torben 286
31 torben 1143 private boolean hasGps;
32    
33     private LookupStates state;
34    
35     private long startTime;
36 torben 241
37 torben 1143 public LocationLookup(Context c) {
38     state = LookupStates.IDLE;
39 torben 237 cntx = c;
40     }
41    
42 torben 285
43     public boolean hasLocation() {
44     return savedLocation != null;
45     }
46 torben 319
47     public Location getLocation()
48     {
49     return savedLocation;
50     }
51 torben 488
52 torben 1143 public boolean hasGps() {
53     return hasGps;
54     }
55    
56     public int getSatCount() {
57     return satCount;
58     }
59    
60     public LookupStates getState() {
61     return state;
62     }
63    
64    
65     public long elapsedTime() {
66     long now = android.os.SystemClock.elapsedRealtime();
67    
68     return now - startTime;
69     }
70 torben 237
71     public void locateStations() {
72 torben 336
73 torben 1143 state = LookupStates.STARTED;
74 torben 336
75 torben 286 hasGps = false;
76 torben 237 locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
77 torben 1143 satCount = 0;
78    
79     startTime = android.os.SystemClock.elapsedRealtime();
80 torben 241
81 torben 1209 boolean hasProvider = false;
82    
83 torben 1446 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
84     String networkPref = prefs.getString("location", "GPS"); //default value is gps
85    
86     if (networkPref.equals("GPS")) {
87     if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
88 torben 1209 locManager.addGpsStatusListener(this);
89 torben 1446 locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
90    
91     hasGps = true;
92     hasProvider = true;
93 torben 1209 }
94     }
95 torben 1446
96     if (locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
97     locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
98     hasProvider = true;
99     }
100    
101 torben 1209
102     if (hasProvider == false) {
103 torben 237 // message that no suitable provider was found
104 torben 1142 //hndl.sendEmptyMessage(StationList.NOPROVIDER);
105 torben 1143 state = LookupStates.NOPROVIDER;
106 torben 1142
107 torben 237 }
108     }
109     @Override
110 torben 1143 public void onLocationChanged(Location location) {
111 torben 237 Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
112 torben 336
113 torben 237
114 torben 286 if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy())
115     savedLocation = new Location(location); //save a copy
116 torben 241
117 torben 286 if (hasGps) {
118     if (!location.getProvider().equals("gps")) {
119     return; // at least give the gps a chance
120 torben 948 } else if (location.getAccuracy() > 512) {
121 torben 286 return; //if we have a gps provider lets wait for a more precise fix
122     }
123 torben 237
124 torben 286 }
125 torben 336 stopSearch();
126 torben 1143 state = LookupStates.GOTLOCATION;
127    
128 torben 241 }
129 torben 336
130 torben 488
131 torben 336 public void stopSearch()
132     {
133 torben 1143 if (state == LookupStates.STARTED) {
134     state = LookupStates.IDLE;
135 torben 1142 locManager.removeGpsStatusListener(this);
136 torben 336 locManager.removeUpdates(this);
137     }
138     }
139 torben 319
140 torben 237
141     @Override
142     public void onProviderDisabled(String provider) {
143     }
144    
145    
146     @Override
147     public void onProviderEnabled(String provider) {
148     }
149    
150    
151     @Override
152     public void onStatusChanged(String provider, int status, Bundle extras) {
153     // TODO Auto-generated method stub
154    
155     }
156 torben 1142
157    
158     @Override //GpsStatus.Listener
159     public void onGpsStatusChanged(int event) {
160     if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
161     int count = 0;
162     GpsStatus status = locManager.getGpsStatus(null);
163     for (GpsSatellite sat : status.getSatellites()) {
164     count ++;
165 torben 1143 }
166 torben 1142
167 torben 1143 satCount = count;
168 torben 1142 }
169    
170     }
171 torben 237 }

  ViewVC Help
Powered by ViewVC 1.1.20