@@ -71,6 +71,11 @@ public class BackendService extends LocationBackendService {
71
71
72
72
public static final String LOCATION_PROVIDER = "DejaVu" ;
73
73
74
+ private static final
75
+ String [] myPerms = new String []{
76
+ ACCESS_WIFI_STATE , CHANGE_WIFI_STATE ,
77
+ ACCESS_COARSE_LOCATION , ACCESS_FINE_LOCATION };
78
+
74
79
public static final double DEG_TO_METER = 111225.0 ;
75
80
public static final double METER_TO_DEG = 1.0 / DEG_TO_METER ;
76
81
public static final double MIN_COS = 0.01 ; // for things that are dividing by the cosine
@@ -99,6 +104,8 @@ public class BackendService extends LocationBackendService {
99
104
100
105
private static BackendService instance ;
101
106
private boolean gpsMonitorRunning = false ;
107
+ private boolean wifiBroadcastReceiverRegistered = false ;
108
+ private boolean permissionsOkay = true ;
102
109
103
110
// We use a threads for potentially slow operations.
104
111
private Thread mobileThread ;
@@ -198,12 +205,25 @@ protected void onOpen() {
198
205
nextMobileScanTime = 0 ;
199
206
nextWlanScanTime = 0 ;
200
207
lastMobileId = "" ;
208
+ wifiBroadcastReceiverRegistered = false ;
201
209
202
210
if (emitterCache == null )
203
211
emitterCache = new Cache (this );
204
212
205
- setgpsMonitorRunning (true );
206
- this .registerReceiver (wifiBroadcastReceiver , wifiBroadcastFilter );
213
+ permissionsOkay = true ;
214
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
215
+ // Check our needed permissions, don't run unless we can.
216
+ for (String s : myPerms ) {
217
+ permissionsOkay &= (checkSelfPermission (s ) == PackageManager .PERMISSION_GRANTED );
218
+ }
219
+ }
220
+ if (permissionsOkay ) {
221
+ setgpsMonitorRunning (true );
222
+ this .registerReceiver (wifiBroadcastReceiver , wifiBroadcastFilter );
223
+ wifiBroadcastReceiverRegistered = true ;
224
+ } else {
225
+ Log .d (TAG , "onOpen() - Permissions not granted, soft fail." );
226
+ }
207
227
}
208
228
209
229
/**
@@ -213,7 +233,9 @@ protected void onOpen() {
213
233
protected void onClose () {
214
234
super .onClose ();
215
235
Log .d (TAG , "onClose()" );
216
- this .unregisterReceiver (wifiBroadcastReceiver );
236
+ if (wifiBroadcastReceiverRegistered ) {
237
+ this .unregisterReceiver (wifiBroadcastReceiver );
238
+ }
217
239
setgpsMonitorRunning (false );
218
240
219
241
if (emitterCache != null ) {
@@ -236,11 +258,6 @@ protected void onClose() {
236
258
@ Override
237
259
protected Intent getInitIntent () {
238
260
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
239
- // List of all needed permissions
240
- String [] myPerms = new String []{
241
- ACCESS_WIFI_STATE , CHANGE_WIFI_STATE ,
242
- ACCESS_COARSE_LOCATION , ACCESS_FINE_LOCATION };
243
-
244
261
// Build list of permissions we need but have not been granted
245
262
List <String > perms = new LinkedList <String >();
246
263
for (String s : myPerms ) {
@@ -269,7 +286,11 @@ protected Intent getInitIntent() {
269
286
@ Override
270
287
protected Location update () {
271
288
//Log.d(TAG, "update() entry.");
272
- scanAllSensors ();
289
+ if (permissionsOkay ) {
290
+ scanAllSensors ();
291
+ } else {
292
+ Log .d (TAG , "update() - Permissions not granted, soft fail." );
293
+ }
273
294
return null ;
274
295
}
275
296
@@ -302,13 +323,17 @@ public static void instanceGpsLocationUpdated(final android.location.Location lo
302
323
*/
303
324
private void onGpsChanged (Location updt ) {
304
325
synchronized (this ) {
305
- //Log.d(TAG, "onGpsChanged() entry.");
306
- if (gpsLocation == null )
307
- gpsLocation = new Kalman (updt , GPS_COORDINATE_NOISE );
308
- else
309
- gpsLocation .update (updt );
310
-
311
- scanAllSensors ();
326
+ if (permissionsOkay ) {
327
+ //Log.d(TAG, "onGpsChanged() entry.");
328
+ if (gpsLocation == null )
329
+ gpsLocation = new Kalman (updt , GPS_COORDINATE_NOISE );
330
+ else
331
+ gpsLocation .update (updt );
332
+
333
+ scanAllSensors ();
334
+ } else {
335
+ Log .d (TAG , "onGpsChanged() - Permissions not granted, soft fail." );
336
+ }
312
337
}
313
338
}
314
339
0 commit comments