Skip to content

Commit

Permalink
Merge pull request #974 from vladvladau/hw-filter-time-optimisation
Browse files Browse the repository at this point in the history
HW filter background time optimization
  • Loading branch information
davidgyoung authored Jun 7, 2020
2 parents 440af71 + 465fb96 commit d7a74f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2.17.1 / 2020-05-25

- Improve HW filter detection mechanism in order to detect beacons immediately after starting in background (#974, Vlad Vladau)

### 2.17 / 2020-04-19

- Make BeaconParser more flexible so as to support covid beacon proposal (#965, David G. Young)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ScanJobScheduler {
private List<ScanResult> mBackgroundScanResultQueue = new ArrayList<>();
@Nullable
private BeaconLocalBroadcastProcessor mBeaconNotificationProcessor;
@NonNull
private boolean mBackgroundScanJobFirstRun = true;

@NonNull
public static ScanJobScheduler getInstance() {
Expand Down Expand Up @@ -82,7 +84,16 @@ List<ScanResult> dumpBackgroundScanResultQueue() {
private void applySettingsToScheduledJob(Context context, BeaconManager beaconManager, ScanState scanState) {
scanState.applyChanges(beaconManager);
LogManager.d(TAG, "Applying scan job settings with background mode "+scanState.getBackgroundMode());
schedule(context, scanState, false);

// if this is the first time we want to schedule a job and we are in background mode
// trigger an immediate scan job in order to install the hw filter
boolean startBackgroundImmediateScan = false;
if (this.mBackgroundScanJobFirstRun && scanState.getBackgroundMode()) {
LogManager.d(TAG, "This is the first time we schedule a job and we are in background, set immediate scan flag to true in order to trigger the HW filter install.");
startBackgroundImmediateScan = true;
}

schedule(context, scanState, startBackgroundImmediateScan);
}

public void applySettingsToScheduledJob(Context context, BeaconManager beaconManager) {
Expand All @@ -100,6 +111,8 @@ public void cancelSchedule(Context context) {
if (mBeaconNotificationProcessor != null) {
mBeaconNotificationProcessor.unregister();
}

mBackgroundScanJobFirstRun = true;
}

public void scheduleAfterBackgroundWakeup(Context context, List<ScanResult> scanResults) {
Expand Down Expand Up @@ -133,7 +146,7 @@ private void schedule(Context context, ScanState scanState, boolean backgroundWa

long millisToNextJobStart;
if (backgroundWakeup) {
LogManager.d(TAG, "We just woke up in the background based on a new scan result. Start scan job immediately.");
LogManager.d(TAG, "We just woke up in the background based on a new scan result or first run of the app. Start scan job immediately.");
millisToNextJobStart = 0;
}
else {
Expand Down Expand Up @@ -170,7 +183,10 @@ private void schedule(Context context, ScanState scanState, boolean backgroundWa
.setOverrideDeadline(millisToNextJobStart).build();
int error = jobScheduler.schedule(immediateJob);
if (error < 0) {
LogManager.e(TAG, "Failed to schedule scan job. Beacons will not be detected. Error: "+error);
LogManager.e(TAG, "Failed to schedule an immediate scan job. Beacons will not be detected. Error: "+error);
} else if (this.mBackgroundScanJobFirstRun) {
LogManager.d(TAG, "First immediate scan job scheduled successful, change the flag to false.");
this.mBackgroundScanJobFirstRun = false;
}
} else {
LogManager.d(TAG, "Not scheduling immediate scan, assuming periodic is about to run");
Expand All @@ -194,10 +210,10 @@ private void schedule(Context context, ScanState scanState, boolean backgroundWa
periodicJobBuilder.setPeriodic(scanState.getScanJobIntervalMillis()).build();
}
final JobInfo jobInfo = periodicJobBuilder.build();
LogManager.d(TAG, "Scheduling ScanJob " + jobInfo + " to run every "+scanState.getScanJobIntervalMillis()+" millis");
LogManager.d(TAG, "Scheduling periodic ScanJob " + jobInfo + " to run every "+scanState.getScanJobIntervalMillis()+" millis");
int error = jobScheduler.schedule(jobInfo);
if (error < 0) {
LogManager.e(TAG, "Failed to schedule scan job. Beacons will not be detected. Error: "+error);
LogManager.e(TAG, "Failed to schedule a periodic scan job. Beacons will not be detected. Error: "+error);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public RegionBootstrap(final Context context, final MonitorNotifier monitorNotif

beaconManager = BeaconManager.getInstanceForApplication(context);
beaconConsumer = new InternalBeaconConsumer();
if (beaconManager.isBackgroundModeUninitialized()) {
beaconManager.setBackgroundMode(true);
}
beaconManager.bind(beaconConsumer);
LogManager.d(TAG, "Waiting for BeaconService connection");
}
Expand All @@ -82,6 +85,9 @@ public RegionBootstrap(final Context context, final MonitorNotifier monitorNotif

beaconManager = BeaconManager.getInstanceForApplication(context);
beaconConsumer = new InternalBeaconConsumer();
if (beaconManager.isBackgroundModeUninitialized()) {
beaconManager.setBackgroundMode(true);
}
beaconManager.bind(beaconConsumer);
LogManager.d(TAG, "Waiting for BeaconService connection");
}
Expand All @@ -102,6 +108,9 @@ public RegionBootstrap(BootstrapNotifier application, Region region) {
this.monitorNotifier = application;
beaconManager = BeaconManager.getInstanceForApplication(context);
beaconConsumer = new InternalBeaconConsumer();
if (beaconManager.isBackgroundModeUninitialized()) {
beaconManager.setBackgroundMode(true);
}
beaconManager.bind(beaconConsumer);
LogManager.d(TAG, "Waiting for BeaconService connection");
}
Expand All @@ -122,6 +131,9 @@ public RegionBootstrap(BootstrapNotifier application, List<Region> regions) {
this.monitorNotifier = application;
beaconManager = BeaconManager.getInstanceForApplication(context);
beaconConsumer = new InternalBeaconConsumer();
if (beaconManager.isBackgroundModeUninitialized()) {
beaconManager.setBackgroundMode(true);
}
beaconManager.bind(beaconConsumer);
LogManager.d(TAG, "Waiting for BeaconService connection");
}
Expand Down Expand Up @@ -201,9 +213,6 @@ public void onBeaconServiceConnect() {
for (Region region : regions) {
LogManager.d(TAG, "Background region monitoring activated for region %s", region);
beaconManager.startMonitoringBeaconsInRegion(region);
if (beaconManager.isBackgroundModeUninitialized()) {
beaconManager.setBackgroundMode(true);
}
}
} catch (RemoteException e) {
LogManager.e(e, TAG, "Can't set up bootstrap regions");
Expand Down

0 comments on commit d7a74f2

Please sign in to comment.