This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
FusedLocationProviderApiImpl.java
321 lines (278 loc) · 11 KB
/
FusedLocationProviderApiImpl.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.mapzen.android.lost.internal;
import com.mapzen.android.lost.api.FusedLocationProviderApi;
import com.mapzen.android.lost.api.LocationAvailability;
import com.mapzen.android.lost.api.LocationCallback;
import com.mapzen.android.lost.api.LocationListener;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LostApiClient;
import com.mapzen.android.lost.api.PendingResult;
import com.mapzen.android.lost.api.Status;
import com.mapzen.android.lost.internal.FusedLocationServiceConnectionManager.EventCallbacks;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static android.os.Process.myPid;
/**
* Implementation of the {@link FusedLocationProviderApi}.
*/
public class FusedLocationProviderApiImpl extends ApiImpl
implements FusedLocationProviderApi, EventCallbacks, ServiceConnection {
private static final String TAG = FusedLocationProviderApiImpl.class.getSimpleName();
private Context context;
private FusedLocationServiceConnectionManager serviceConnectionManager;
private FusedLocationServiceCallbackManager serviceCallbackManager;
private RequestManager requestManager;
private ClientManager clientManager;
private boolean isBound;
IFusedLocationProviderService service;
IFusedLocationProviderCallback.Stub remoteCallback
= new IFusedLocationProviderCallback.Stub() {
public long pid() throws RemoteException {
return myPid();
}
public void onLocationChanged(final Location location) throws RemoteException {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override public void run() {
// #224: this call is async, service may have been legally set to null in the meantime
if (service != null) {
serviceCallbackManager.onLocationChanged(context, location, clientManager, service);
}
}
});
}
@Override public void onLocationAvailabilityChanged(LocationAvailability locationAvailability)
throws RemoteException {
serviceCallbackManager.onLocationAvailabilityChanged(locationAvailability, clientManager);
}
};
@Override public void onConnect(Context context) {
this.context = context;
final Intent intent = new Intent(context, FusedLocationProviderService.class);
context.bindService(intent, this, Context.BIND_AUTO_CREATE);
}
@Override public void onServiceConnected(IBinder binder) {
service = IFusedLocationProviderService.Stub.asInterface(binder);
isBound = true;
registerRemoteCallback();
}
@Override public void onDisconnect() {
if (isBound) {
unregisterRemoteCallback();
context.unbindService(this);
isBound = false;
}
service = null;
}
@Override public void onServiceConnected(ComponentName name, IBinder binder) {
serviceConnectionManager.onServiceConnected(binder);
isBound = true;
}
@Override public void onServiceDisconnected(ComponentName name) {
serviceConnectionManager.onServiceDisconnected();
isBound = false;
}
public FusedLocationProviderApiImpl(FusedLocationServiceConnectionManager connectionManager,
FusedLocationServiceCallbackManager callbackManager, RequestManager requestManager,
ClientManager clientManager) {
serviceConnectionManager = connectionManager;
serviceCallbackManager = callbackManager;
this.requestManager = requestManager;
this.clientManager = clientManager;
serviceConnectionManager.setEventCallbacks(this);
}
public boolean isConnecting() {
return serviceConnectionManager.isConnecting();
}
public void addConnectionCallbacks(LostApiClient.ConnectionCallbacks callbacks) {
serviceConnectionManager.addCallbacks(callbacks);
}
public void connect(Context context, LostApiClient.ConnectionCallbacks callbacks) {
serviceConnectionManager.connect(context, callbacks);
}
public void disconnect() {
serviceConnectionManager.disconnect();
}
public boolean isConnected() {
return serviceConnectionManager.isConnected();
}
@Override public Location getLastLocation(LostApiClient client) {
throwIfNotConnected(client);
Location location = null;
try {
location = service.getLastLocation();
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to get last Location", e);
} finally {
return location;
}
}
@Override public LocationAvailability getLocationAvailability(LostApiClient client) {
throwIfNotConnected(client);
LocationAvailability availability = null;
try {
availability = service.getLocationAvailability();
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to get LocationAvailability", e);
} finally {
return availability;
}
}
@Override public PendingResult<Status> requestLocationUpdates(LostApiClient client,
LocationRequest request, LocationListener listener) {
throwIfNotConnected(client);
requestManager.requestLocationUpdates(client, request, listener);
clientManager.addListener(client, request, listener);
requestLocationUpdatesInternal(request);
return new SimplePendingResult(true);
}
@Override public PendingResult<Status> requestLocationUpdates(LostApiClient client,
LocationRequest request, LocationListener listener, Looper looper) {
throw new RuntimeException("Sorry, not yet implemented");
}
@Override public PendingResult<Status> requestLocationUpdates(LostApiClient client,
LocationRequest request, LocationCallback callback, Looper looper) {
throwIfNotConnected(client);
requestManager.requestLocationUpdates(client, request, callback);
clientManager.addLocationCallback(client, request, callback, looper);
requestLocationUpdatesInternal(request);
return new SimplePendingResult(true);
}
@Override public PendingResult<Status> requestLocationUpdates(LostApiClient client,
LocationRequest request, PendingIntent callbackIntent) {
throwIfNotConnected(client);
requestManager.requestLocationUpdates(client, request, callbackIntent);
clientManager.addPendingIntent(client, request, callbackIntent);
requestLocationUpdatesInternal(request);
return new SimplePendingResult(true);
}
/**
* Forwards location update request to the {@link FusedLocationProviderService} to initiate
* and/or update request params.
*/
private void requestLocationUpdatesInternal(LocationRequest request) {
try {
service.requestLocationUpdates(request);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to request location updates", e);
}
}
private void removeLocationUpdatesInternal(List<LocationRequest> requests) {
if (requests == null) {
return;
}
try {
service.removeLocationUpdates(requests);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to remove location updates", e);
}
}
@Override public PendingResult<Status> removeLocationUpdates(LostApiClient client,
LocationListener listener) {
throwIfNotConnected(client);
List<LocationRequest> requests = requestManager.removeLocationUpdates(client, listener);
removeLocationUpdatesInternal(requests);
boolean hasResult = clientManager.removeListener(client, listener);
checkAllListenersPendingIntentsAndCallbacks();
return new SimplePendingResult(hasResult);
}
@Override public PendingResult<Status> removeLocationUpdates(LostApiClient client,
PendingIntent callbackIntent) {
throwIfNotConnected(client);
List<LocationRequest> requests = requestManager.removeLocationUpdates(client, callbackIntent);
removeLocationUpdatesInternal(requests);
boolean hasResult = clientManager.removePendingIntent(client, callbackIntent);
checkAllListenersPendingIntentsAndCallbacks();
return new SimplePendingResult(hasResult);
}
@Override public PendingResult<Status> removeLocationUpdates(LostApiClient client,
LocationCallback callback) {
throwIfNotConnected(client);
List<LocationRequest> requests = requestManager.removeLocationUpdates(client, callback);
removeLocationUpdatesInternal(requests);
boolean hasResult = clientManager.removeLocationCallback(client, callback);
checkAllListenersPendingIntentsAndCallbacks();
return new SimplePendingResult(hasResult);
}
/**
* Checks if any listeners or pending intents are still registered for location updates. If not,
* then shutdown the location engines.
*/
private void checkAllListenersPendingIntentsAndCallbacks() {
//TODO: potentially remove hasNoListeners method, not needed anymore
//if (clientManager.hasNoListeners()) {
// try {
// service.removeAllLocationUpdates();
// } catch (RemoteException e) {
// throw new RuntimeException(e);
// }
//}
}
@Override public PendingResult<Status> setMockMode(LostApiClient client, boolean isMockMode) {
throwIfNotConnected(client);
try {
service.setMockMode(isMockMode);
} catch (RemoteException e) {
String mode = isMockMode ? "enabled" : "disabled";
Log.e(TAG, "Error occurred trying to set mock mode " + mode, e);
}
return new SimplePendingResult(true);
}
@Override public PendingResult<Status> setMockLocation(LostApiClient client,
Location mockLocation) {
throwIfNotConnected(client);
try {
service.setMockLocation(mockLocation);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to set mock location", e);
}
return new SimplePendingResult(true);
}
@Override public PendingResult<Status> setMockTrace(LostApiClient client, String path,
String filename) {
throwIfNotConnected(client);
try {
service.setMockTrace(path, filename);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to set mock trace", e);
}
return new SimplePendingResult(true);
}
public Map<LostApiClient, Set<LocationListener>> getLocationListeners() {
return clientManager.getLocationListeners();
}
void removeConnectionCallbacks(LostApiClient.ConnectionCallbacks callbacks) {
serviceConnectionManager.removeCallbacks(callbacks);
}
FusedLocationServiceConnectionManager getServiceConnectionManager() {
return serviceConnectionManager;
}
void registerRemoteCallback() {
if (service != null) {
try {
service.add(remoteCallback);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to register remote callback", e);
}
}
}
void unregisterRemoteCallback() {
if (service != null) {
try {
service.remove(remoteCallback);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to unregister remote callback", e);
}
}
}
}