Skip to content

Commit

Permalink
Add pedometer low power automation
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jun 29, 2024
1 parent 4f4e570 commit 689a353
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.kylecorry.trail_sense.shared.automations.Automation
import com.kylecorry.trail_sense.shared.automations.AutomationReceiver
import com.kylecorry.trail_sense.shared.automations.BooleanParameterTransformer
import com.kylecorry.trail_sense.tools.battery.BatteryToolRegistration
import com.kylecorry.trail_sense.tools.pedometer.PedometerToolRegistration
import com.kylecorry.trail_sense.tools.pedometer.receivers.SetPedometerStateReceiver
import com.kylecorry.trail_sense.tools.weather.WeatherToolRegistration
import com.kylecorry.trail_sense.tools.weather.receivers.SetWeatherMonitorStateReceiver

Expand All @@ -25,6 +27,16 @@ object PowerSavingModeAutomation {
)
),
UserPreferences(context).lowPowerModeDisablesWeather
),
AutomationReceiver(
PedometerToolRegistration.RECEIVER_SET_PEDOMETER_STATE,
listOf(
BooleanParameterTransformer(
BatteryToolRegistration.PARAM_POWER_SAVING_MODE_ENABLED,
SetPedometerStateReceiver.PARAM_PEDOMETER_STATE,
invert = true
)
)
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,33 @@ class LowPowerMode(val context: Context) {
fun enable(activity: Activity? = null) {
prefs.isLowPowerModeOn = true

context.sendBroadcast(Intents.localIntent(context, BatteryToolRegistration.ACTION_POWER_SAVING_MODE_CHANGED).also {
it.putExtra(BatteryToolRegistration.PARAM_POWER_SAVING_MODE_ENABLED, true)
})
context.sendBroadcast(
Intents.localIntent(
context,
BatteryToolRegistration.ACTION_POWER_SAVING_MODE_CHANGED
).also {
it.putExtra(BatteryToolRegistration.PARAM_POWER_SAVING_MODE_ENABLED, true)
})

if (prefs.lowPowerModeDisablesBacktrack) {
BacktrackScheduler.stop(context)
}

StepCounterService.stop(context)

activity?.recreate()
}

fun disable(activity: Activity? = null) {
prefs.isLowPowerModeOn = false

context.sendBroadcast(Intents.localIntent(context, BatteryToolRegistration.ACTION_POWER_SAVING_MODE_CHANGED).also {
it.putExtra(BatteryToolRegistration.PARAM_POWER_SAVING_MODE_ENABLED, false)
})
context.sendBroadcast(
Intents.localIntent(
context,
BatteryToolRegistration.ACTION_POWER_SAVING_MODE_CHANGED
).also {
it.putExtra(BatteryToolRegistration.PARAM_POWER_SAVING_MODE_ENABLED, false)
})

if (activity != null){
if (activity != null) {
activity.recreate()
return
}
Expand All @@ -48,10 +54,6 @@ class LowPowerMode(val context: Context) {
if (BacktrackScheduler.isOn(context)) {
BacktrackScheduler.start(context, false)
}

if (prefs.pedometer.isEnabled) {
StepCounterService.start(context)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.tools.pedometer.infrastructure.DistanceAlerter
import com.kylecorry.trail_sense.tools.pedometer.infrastructure.StepCounterService
import com.kylecorry.trail_sense.tools.pedometer.quickactions.QuickActionPedometer
import com.kylecorry.trail_sense.tools.pedometer.receivers.SetPedometerStateReceiver
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tool
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolCategory
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolNotificationChannel
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolQuickAction
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolReceiver
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolRegistration
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolService
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
Expand Down Expand Up @@ -95,7 +97,17 @@ object PedometerToolRegistration : ToolRegistration {
),
ToolDiagnosticFactory.powerSaver(context),
ToolDiagnosticFactory.backgroundService(context)
),
receivers = listOf(
ToolReceiver(
RECEIVER_SET_PEDOMETER_STATE,
context.getString(R.string.pedometer),
SetPedometerStateReceiver()
)
)
)
}

const val RECEIVER_SET_PEDOMETER_STATE =
"com.kylecorry.trail_sense.tools.pedometer.RECEIVER_SET_PEDOMETER_STATE"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kylecorry.trail_sense.tools.pedometer.receivers

import android.content.Context
import android.os.Bundle
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.tools.pedometer.infrastructure.StepCounterService
import com.kylecorry.trail_sense.tools.tools.infrastructure.Receiver

// TODO: Add support for enable/disable pedometer
class SetPedometerStateReceiver : Receiver {
override fun onReceive(context: Context, data: Bundle) {
val desiredState = data.getBoolean(PARAM_PEDOMETER_STATE, false)
val prefs = UserPreferences(context)

if (desiredState && prefs.pedometer.isEnabled) {
StepCounterService.start(context)
} else if (!desiredState) {
StepCounterService.stop(context)
}
}

companion object {
const val PARAM_PEDOMETER_STATE = "state"
}
}

0 comments on commit 689a353

Please sign in to comment.