forked from thtro/cwac-locpoll
/
LocationPollerService.java
298 lines (251 loc) · 9.02 KB
/
LocationPollerService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
Copyright (c) 2010 CommonsWare, LLC
Copyright (c) 2011 Birkett Enterprise Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.commonsware.cwac.locpoll;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
/**
* Service providing the guts of the location polling
* engine. Uses a WakeLock to ensure the CPU stays on while
* the location lookup is going on. Handles both successful
* and timeout conditions.
*
* Those wishing to leverage this service should do so via
* the LocationPoller class.
*/
public class LocationPollerService extends Service {
private static final String LOCK_NAME_STATIC = "com.commonsware.cwac.locpoll.LocationPoller";
private static volatile PowerManager.WakeLock lockStatic;
private LocationManager locationManager;
/**
* Lazy-initializes the WakeLock when we first use it. We
* use a partial WakeLock since we only need the CPU on,
* not the screen.
*/
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context.getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
/**
* Called by LocationPoller to trigger a poll for the
* location. Acquires the WakeLock, then starts the
* service using the supplied Intent (setting the
* component so routing always goes to the service).
*/
public static void requestLocation(Context context, Intent intent)
throws InvalidParameterException {
assertValidParameters(intent);
getLock(context.getApplicationContext()).acquire();
intent.setClass(context, LocationPollerService.class);
context.startService(intent);
}
public static void assertValidParameters(Intent intent)
throws InvalidParameterException {
LocationPollerParameter parameters = getParametersFromIntent(intent);
if (parameters.getProviders() == null
|| parameters.getProviders().length == 0) {
throw new InvalidParameterException(
"at least one provider must be set");
}
}
public static LocationPollerParameter getParametersFromIntent(Intent intent)
throws InvalidParameterException {
LocationPollerParameter parameter = new LocationPollerParameter(intent.getExtras());
return parameter;
}
/**
* Obtain the LocationManager on startup
*/
@Override
public void onCreate() {
locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
//android.os.Debug.waitForDebugger();
}
/**
* No-op implementation as required by superclass
*/
@Override
public IBinder onBind(Intent i) {
return(null);
}
/**
* Validates the required extras (EXTRA_PROVIDER and
* EXTRA_INTENT). If valid, updates the Intent to be
* broadcast with the application's own package (required
* to keep the broadcast within this application, so we do
* not leak security information). Then, forks a
* PollerThread to do the actual location lookup.
*
* @return START_REDELIVER_INTENT to ensure we get the
* last request again
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (!lock.isHeld() || (flags & START_FLAG_REDELIVERY) != 0) {
lock.acquire();
}
LocationPollerParameter parameters = getParametersFromIntent(intent);
PollerThread pollerThread = new PollerThread(lock, locationManager, parameters);
pollerThread.start();
return (START_REDELIVER_INTENT);
}
/**
* A WakefulThread subclass that knows how to look up the current location,
* plus handle the timeout scenario.
*/
private class PollerThread extends WakefulThread {
private LocationManager locationManager;
private Handler handler = new Handler();
private LocationPollerParameter locationPollerParameter;
private int currentLocationProviderIndex;
private Runnable onTimeout = new Runnable() {
public void run() {
locationManager.removeUpdates(listener);
if (isTriedAllProviders()) {
broadCastFailureMessage();
quit();
} else {
currentLocationProviderIndex++;
tryNextProvider();
}
}
};
private LocationListener listener = new LocationListener() {
/**
* If we get a fix, get rid of the timeout condition,
* then attach the location as an extra
* (EXTRA_LOCATION) on the Intent, broadcast it, then
* exit the polling loop so the thread terminates.
*/
public void onLocationChanged(Location location) {
handler.removeCallbacks(onTimeout);
Intent toBroadcast = createIntentToBroadcastOnCompletion();
toBroadcast.putExtra(LocationPollerResult.LOCATION_KEY, location);
sendBroadcast(toBroadcast);
quit();
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
/**
* Constructor.
*
* @param lock
* Already-locked WakeLock
* @param locationManager
* LocationManager for doing the location
* lookup
* @param provider
* name of the location provider to use
* @param intentTemplate
* Intent to be broadcast when location found
* or timeout occurs
*/
PollerThread(PowerManager.WakeLock lock,
LocationManager locationManager,
LocationPollerParameter locationPollerParameter) {
super(lock, "LocationPoller-PollerThread");
this.locationManager = locationManager;
this.locationPollerParameter = locationPollerParameter;
}
/**
* Called before the Handler loop begins. Registers a
* timeout, so we do not wait forever for a location.
* When a timeout occurs, broadcast an Intent containing
* an error extra, then terminate the thread. Also,
* requests a location update from the LocationManager.
*/
@Override
protected void onPreExecute() {
tryNextProvider();
}
private void tryNextProvider() {
handler.postDelayed(onTimeout, locationPollerParameter.getTimeout());
requestLocationUdpate();
}
private void requestLocationUdpate() {
try {
locationManager.requestLocationUpdates(getCurrentProvider(), 0,
0, listener);
}
catch (IllegalArgumentException e) {
// see http://code.google.com/p/android/issues/detail?id=21237
Log.w(getClass().getSimpleName(),
"Exception requesting updates -- may be emulator issue",
e);
quit();
}
}
private void broadCastFailureMessage() {
Intent toBroadcast = createIntentToBroadcastOnCompletion();
toBroadcast.putExtra(LocationPollerResult.ERROR_KEY, "Timeout!");
Location location = locationManager.getLastKnownLocation(getCurrentProvider());
toBroadcast.putExtra(LocationPollerResult.LASTKNOWN_LOCATION_KEY, location);
sendBroadcast(toBroadcast);
}
private String getCurrentProvider() {
String currentProvider = locationPollerParameter.getProviders()[currentLocationProviderIndex];
return currentProvider;
}
private boolean isTriedAllProviders() {
return currentLocationProviderIndex == locationPollerParameter
.getProviders().length - 1;
}
private Intent createIntentToBroadcastOnCompletion() {
Intent toBroadcast = new Intent(locationPollerParameter.getIntentToBroadcastOnCompletion());
toBroadcast.setPackage(getPackageName());
return toBroadcast;
}
/**
* Called when the Handler loop ends. Removes the
* location listener.
*/
@Override
protected void onPostExecute() {
locationManager.removeUpdates(listener);
super.onPostExecute();
}
/**
* Called when the WakeLock is completely unlocked.
* Stops the service, so everything shuts down.
*/
@Override
protected void onUnlocked() {
stopSelf();
}
}
}