Skip to content

Commit 30c480e

Browse files
committed
Resolve #2
Signed-off-by: Tod Fitch <[email protected]>
1 parent 7346d41 commit 30c480e

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

app/src/main/java/org/fitchfamily/android/dejavu/BackendService.java

+41-16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public class BackendService extends LocationBackendService {
7171

7272
public static final String LOCATION_PROVIDER = "DejaVu";
7373

74+
private static final
75+
String[] myPerms = new String[]{
76+
ACCESS_WIFI_STATE, CHANGE_WIFI_STATE,
77+
ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION};
78+
7479
public static final double DEG_TO_METER = 111225.0;
7580
public static final double METER_TO_DEG = 1.0 / DEG_TO_METER;
7681
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 {
99104

100105
private static BackendService instance;
101106
private boolean gpsMonitorRunning = false;
107+
private boolean wifiBroadcastReceiverRegistered = false;
108+
private boolean permissionsOkay = true;
102109

103110
// We use a threads for potentially slow operations.
104111
private Thread mobileThread;
@@ -198,12 +205,25 @@ protected void onOpen() {
198205
nextMobileScanTime = 0;
199206
nextWlanScanTime = 0;
200207
lastMobileId = "";
208+
wifiBroadcastReceiverRegistered = false;
201209

202210
if (emitterCache == null)
203211
emitterCache = new Cache(this);
204212

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+
}
207227
}
208228

209229
/**
@@ -213,7 +233,9 @@ protected void onOpen() {
213233
protected void onClose() {
214234
super.onClose();
215235
Log.d(TAG, "onClose()");
216-
this.unregisterReceiver(wifiBroadcastReceiver);
236+
if (wifiBroadcastReceiverRegistered) {
237+
this.unregisterReceiver(wifiBroadcastReceiver);
238+
}
217239
setgpsMonitorRunning(false);
218240

219241
if (emitterCache != null) {
@@ -236,11 +258,6 @@ protected void onClose() {
236258
@Override
237259
protected Intent getInitIntent() {
238260
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-
244261
// Build list of permissions we need but have not been granted
245262
List<String> perms = new LinkedList<String>();
246263
for (String s : myPerms) {
@@ -269,7 +286,11 @@ protected Intent getInitIntent() {
269286
@Override
270287
protected Location update() {
271288
//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+
}
273294
return null;
274295
}
275296

@@ -302,13 +323,17 @@ public static void instanceGpsLocationUpdated(final android.location.Location lo
302323
*/
303324
private void onGpsChanged(Location updt) {
304325
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+
}
312337
}
313338
}
314339

0 commit comments

Comments
 (0)