Protect against NPE on failed restore of ScanState
@@ -11,6 +11,7 @@ import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
+import android.support.annotation.Nullable;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconManager;
@@ -47,7 +48,8 @@ public class ScanJob extends JobService {
private static int sOverrideImmediateScanJobId = -1;
private static int sOverridePeriodicScanJobId = -1;
- private ScanState mScanState;
+ @Nullable
+ private ScanState mScanState = null;
private Handler mStopHandler = new Handler();
private ScanHelper mScanHelper;
private boolean mInitialized = false;
@@ -92,6 +94,7 @@ public class ScanJob extends JobService {
mStopHandler.removeCallbacksAndMessages(null);
if (startedScan) {
+ if (mScanState != null) {
LogManager.i(TAG, "Scan job running for "+mScanState.getScanJobRuntimeMillis()+" millis");
mStopHandler.postDelayed(new Runnable() {
@Override
@@ -112,6 +115,7 @@ public class ScanJob extends JobService {
}
}, mScanState.getScanJobRuntimeMillis());
}
+ }
else {
LogManager.i(TAG, "Scanning not started so Scan job is complete.");
ScanJob.this.jobFinished(jobParameters , false);
@@ -123,6 +127,7 @@ public class ScanJob extends JobService {
}
private void scheduleNextScan(){
+ if (mScanState != null) {
if(!mScanState.getBackgroundMode()){
// immediately reschedule scan if running in foreground
LogManager.d(TAG, "In foreground mode, schedule next scan");
@@ -131,8 +136,10 @@ public class ScanJob extends JobService {
startPassiveScanIfNeeded();
}
}
+ }
private void startPassiveScanIfNeeded() {
+ if (mScanState != null) {
LogManager.d(TAG, "Checking to see if we need to start a passive scan");
boolean insideAnyRegion = false;
// Clone the collection before iterating to prevent ConcurrentModificationException per #577
@@ -156,6 +163,7 @@ public class ScanJob extends JobService {
}
}
}
+ }
@Override
public boolean onStopJob(JobParameters params) {
@@ -190,6 +198,7 @@ public class ScanJob extends JobService {
private boolean initialzeScanHelper() {
mScanHelper = new ScanHelper(this);
mScanState = ScanState.restore(ScanJob.this);
+ if (mScanState != null) {
mScanState.setLastScanStartTimeMillis(System.currentTimeMillis());
mScanHelper.setMonitoringStatus(mScanState.getMonitoringStatus());
mScanHelper.setRangedRegionState(mScanState.getRangedRegionState());
@@ -204,11 +213,13 @@ public class ScanJob extends JobService {
return false;
}
}
+ }
return true;
}
// Returns true of scanning actually was started, false if it did not need to be
private boolean restartScanning() {
+ if (mScanState != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mScanHelper.stopAndroidOBackgroundScan();
}
@@ -241,6 +252,10 @@ public class ScanJob extends JobService {
return false;
}
}
+ else {
+ return false;
+ }
+ }
// Returns true of scanning actually was started, false if it did not need to be
private boolean startScanning() {
