Skip to content

Commit

Permalink
Add new force_on and force_off options to command_high_accuracy_mode. (
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskemp authored Oct 1, 2022
1 parent c2937e7 commit 203e686
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class LocationSensorManager : LocationSensorManagerBase() {
private var lastHighAccuracyMode = false
private var lastHighAccuracyUpdateInterval = DEFAULT_UPDATE_INTERVAL_HA_SECONDS
private var forceHighAccuracyModeOn = false
private var forceHighAccuracyModeOff = false
private var highAccuracyModeEnabled = false

private var lastEnteredGeoZones: MutableList<String> = ArrayList()
Expand Down Expand Up @@ -182,17 +183,27 @@ class LocationSensorManager : LocationSensorManagerBase() {
ACTION_FORCE_HIGH_ACCURACY -> {
var command = intent.extras?.get("command")?.toString()
when (command) {
MessagingManager.TURN_ON, MessagingManager.TURN_OFF -> {
var turnOn = command == MessagingManager.TURN_ON
MessagingManager.TURN_ON, MessagingManager.TURN_OFF, MessagingManager.FORCE_ON -> {
var turnOn = command != MessagingManager.TURN_OFF
if (turnOn) Log.d(TAG, "Forcing of high accuracy mode enabled")
else Log.d(TAG, "Forcing of high accuracy mode disabled")
forceHighAccuracyModeOn = turnOn
forceHighAccuracyModeOff = false
setHighAccuracyModeSetting(latestContext, turnOn)
ioScope.launch {
setupBackgroundLocation()
}
}

MessagingManager.FORCE_OFF -> {
Log.d(TAG, "High accuracy mode forced off")
forceHighAccuracyModeOn = false
forceHighAccuracyModeOff = true
ioScope.launch {
setupBackgroundLocation()
}
}

MessagingManager.HIGH_ACCURACY_SET_UPDATE_INTERVAL -> {
if (lastHighAccuracyMode)
restartHighAccuracyService(getHighAccuracyModeIntervalSetting(latestContext))
Expand Down Expand Up @@ -388,17 +399,26 @@ class LocationSensorManager : LocationSensorManagerBase() {

val shouldEnableHighAccuracyMode = shouldEnableHighAccuracyMode()

// As soon as the high accuracy mode should be enabled, disable the force of high accuracy mode!
if (shouldEnableHighAccuracyMode) {
// As soon as the high accuracy mode should be enabled, disable the force_on of high accuracy mode!
if (shouldEnableHighAccuracyMode && forceHighAccuracyModeOn) {
Log.d(TAG, "Forcing of high accuracy mode disabled, because high accuracy mode had to be enabled anyway.")
forceHighAccuracyModeOn = false
}

return if (!forceHighAccuracyModeOn) {
shouldEnableHighAccuracyMode
} else {
// As soon as the high accuracy mode shouldn't be enabled, disable the force_off of high accuracy mode!
if (!shouldEnableHighAccuracyMode && forceHighAccuracyModeOff) {
Log.d(TAG, "Forcing off of high accuracy mode disabled, because high accuracy mode had to be disabled anyway.")
forceHighAccuracyModeOff = false
}

return if (forceHighAccuracyModeOn) {
Log.d(TAG, "High accuracy mode enabled, because command_high_accuracy_mode was used to turn it on")
true
} else if (forceHighAccuracyModeOff) {
Log.d(TAG, "High accuracy mode disabled, because command_high_accuracy_mode was used to force it off")
false
} else {
shouldEnableHighAccuracyMode
}
}

Expand Down Expand Up @@ -461,7 +481,7 @@ class LocationSensorManager : LocationSensorManagerBase() {

btDevConnected = bluetoothDevices.any { it.connected && highAccuracyModeBTDevices.contains(it.address) }

if (!forceHighAccuracyModeOn) {
if (!forceHighAccuracyModeOn && !forceHighAccuracyModeOff) {
if (!btDevConnected) Log.d(TAG, "High accuracy mode disabled, because defined ($highAccuracyModeBTDevices) bluetooth device(s) not connected (Connected devices: $bluetoothDevices)")
else Log.d(TAG, "High accuracy mode enabled, because defined ($highAccuracyModeBTDevices) bluetooth device(s) connected (Connected devices: $bluetoothDevices)")
}
Expand All @@ -479,7 +499,7 @@ class LocationSensorManager : LocationSensorManagerBase() {

inZone = zoneExpEntered || zoneExited

if (!forceHighAccuracyModeOn) {
if (!forceHighAccuracyModeOn && !forceHighAccuracyModeOff) {
if (!inZone) Log.d(TAG, "High accuracy mode disabled, because not in zone $highAccuracyExpZones")
else Log.d(TAG, "High accuracy mode enabled, because in zone $highAccuracyExpZones")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class MessagingManager @Inject constructor(

// High accuracy commands
const val HIGH_ACCURACY_SET_UPDATE_INTERVAL = "high_accuracy_set_update_interval"
const val FORCE_ON = "force_on"
const val FORCE_OFF = "force_off"

// Command groups
val DEVICE_COMMANDS = listOf(
Expand Down Expand Up @@ -256,6 +258,7 @@ class MessagingManager @Inject constructor(
SYSTEM_STREAM, DTMF_STREAM
)
val ENABLE_COMMANDS = listOf(TURN_OFF, TURN_ON)
val FORCE_COMMANDS = listOf(FORCE_OFF, FORCE_ON)
val MEDIA_COMMANDS = listOf(
MEDIA_FAST_FORWARD, MEDIA_NEXT, MEDIA_PAUSE, MEDIA_PLAY,
MEDIA_PLAY_PAUSE, MEDIA_PREVIOUS, MEDIA_REWIND, MEDIA_STOP
Expand Down Expand Up @@ -435,6 +438,7 @@ class MessagingManager @Inject constructor(
}
COMMAND_HIGH_ACCURACY_MODE -> {
if ((!jsonData[COMMAND].isNullOrEmpty() && jsonData[COMMAND] in ENABLE_COMMANDS) ||
(!jsonData[COMMAND].isNullOrEmpty() && jsonData[COMMAND] in FORCE_COMMANDS) ||
(
!jsonData[COMMAND].isNullOrEmpty() && jsonData[COMMAND] == HIGH_ACCURACY_SET_UPDATE_INTERVAL &&
jsonData[HIGH_ACCURACY_UPDATE_INTERVAL]?.toIntOrNull() != null && jsonData[HIGH_ACCURACY_UPDATE_INTERVAL]?.toInt()!! >= 5
Expand Down Expand Up @@ -835,6 +839,7 @@ class MessagingManager @Inject constructor(
when (command) {
TURN_OFF -> LocationSensorManager.setHighAccuracyModeSetting(context, false)
TURN_ON -> LocationSensorManager.setHighAccuracyModeSetting(context, true)
FORCE_ON -> LocationSensorManager.setHighAccuracyModeSetting(context, true)
HIGH_ACCURACY_SET_UPDATE_INTERVAL -> LocationSensorManager.setHighAccuracyModeIntervalSetting(context, data[HIGH_ACCURACY_UPDATE_INTERVAL]!!.toInt())
}
val intent = Intent(context, LocationSensorManager::class.java)
Expand Down

0 comments on commit 203e686

Please sign in to comment.