Skip to content

Commit

Permalink
Detect if ScanStrategy actually changed before unbind/rebind
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgyoung committed May 25, 2024
1 parent b1b8ad6 commit 481b247
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/src/main/java/org/altbeacon/beacon/BeaconManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private void applySettingsChange() {
setRegionStatePersistenceEnabled(Boolean.TRUE.equals(settings.getRegionStatePersistenceEnabled()));

// Check if ScanStrategry has changed
boolean scanStrategyChanged = settings.getScanStrategy() != getActiveSettings().getScanStrategy();
boolean scanStrategyChanged = settings.getScanStrategy().compareTo(getActiveSettings().getScanStrategy()) != 0;
synchronized(consumers) {
if (scanStrategyChanged && consumers.size() > 0) {
LogManager.i(TAG, "ScanStrategy has changed. Unbinding and rebinding consumers");
Expand Down
46 changes: 44 additions & 2 deletions lib/src/main/java/org/altbeacon/beacon/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ data class Settings(
val backgroundScanPeriodMillis: Long = 30000,
val backgroundBetweenScanPeriodMillis: Long = 0
)
interface ScanStrategy {
interface ScanStrategy: Comparable<ScanStrategy> {
fun clone(): ScanStrategy

/**
Expand Down Expand Up @@ -219,7 +219,21 @@ data class Settings(
beaconManager.setEnableScheduledScanJobs(true)
beaconManager.setIntentScanningStrategyEnabled(false)
}
}

override fun compareTo(other: ScanStrategy): Int {
return if (other is JobServiceScanStrategy) {
if (this.immediateJobId == other.immediateJobId &&
this.periodicJobId == other.periodicJobId &&
this.jobPersistenceEnabled == other.jobPersistenceEnabled) {
0
} else {
-1
}
} else {
-1
}
}
}
class BackgroundServiceScanStrategy(): ScanStrategy
{
override fun clone(): BackgroundServiceScanStrategy {
Expand All @@ -236,6 +250,13 @@ data class Settings(
beaconManager.setEnableScheduledScanJobs(false)
beaconManager.setIntentScanningStrategyEnabled(false)
}
override fun compareTo(other: ScanStrategy): Int {
return if (other is BackgroundServiceScanStrategy) {
0
} else {
-1
}
}
}
class ForegroundServiceScanStrategy(val notification: Notification, val notificationId: Int): ScanStrategy {
var androidLScanningDisabled = true
Expand All @@ -259,6 +280,19 @@ data class Settings(
beaconManager.setIntentScanningStrategyEnabled(false)
beaconManager.enableForegroundServiceScanning(notification, notificationId)
}

override fun compareTo(other: ScanStrategy): Int {
return if (other is ForegroundServiceScanStrategy) {
if (this.notificationId == other.notificationId &&
this.androidLScanningDisabled == other.androidLScanningDisabled) {
0
} else {
-1
}
} else {
-1
}
}
}
class IntentScanStrategy: ScanStrategy {
override fun clone(): IntentScanStrategy {
Expand All @@ -276,6 +310,14 @@ data class Settings(
beaconManager.setIntentScanningStrategyEnabled(true)
}

override fun compareTo(other: ScanStrategy): Int {
return if (other is IntentScanStrategy) {
0
} else {
-1
}
}


}

Expand Down

0 comments on commit 481b247

Please sign in to comment.