Battery & Power Optimization
Relevant source files
The following files were used as context for generating this wiki page:
- app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt
- README.md
- app/src/main/kotlin/com/fathiraz/flowbell/presentation/app/App.kt
- app/src/main/kotlin/com/fathiraz/flowbell/data/local/preferences/DataStoreManager.kt
- app/src/main/kotlin/com/fathiraz/flowbell/presentation/screens/dashboard/DashboardScreen.kt
The Battery & Power Optimization system in FlowBell is designed to balance the app's core functionality—near real-time notification forwarding—with device energy efficiency. Since the app relies on background services to listen for and dispatch notifications, it implements a dynamic optimization logic that adjusts processing intensity based on the device's current power state.
This system monitors battery levels, charging status, and system-level power modes (such as Battery Saver and Doze mode) to adjust the batch size and delivery cadence of notification payloads. This ensures that while the device is in a critical power state, resource consumption is minimized without completely halting essential background operations unless absolutely necessary.
Sources: app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:10-14, README.md:46-46
Optimization Logic and Levels
The core of the optimization system is handled by the BatteryOptimizerUtils class, which categorizes the device into one of four OptimizationLevel states. These levels dictate the frequency of network requests and the size of the data batches sent to the user's webhook.
Optimization State Determination
The system uses several Android system services, including PowerManager and BatteryManager, to determine the current state. The following flow illustrates the decision-making process for assigning an optimization level:
The logic prioritizes system-level power-saving modes (Saver/Doze) over raw battery percentages to ensure compliance with Android's energy policies.
Sources: app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:111-131
Dynamic Processing Multipliers
The system applies "multipliers" and batch size limits based on the determined optimization level. These constants manage how much the processing interval is compressed or expanded.
| Optimization Level | Battery Threshold | Multiplier | Recommended Batch Size |
|---|---|---|---|
| NORMAL | > 35% or Charging | 0.33f | 50 items |
| LIGHT | < 35% | 0.27f | 50 items |
| MODERATE | < 20% | 0.20f | 25 items |
| AGGRESSIVE | < 10% or Saver | 0.13f | 10 items |
Sources: app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:18-32, app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:47-52
Component Interaction
The battery optimization data is used by the background delivery system to throttle operations. While the NotificationListenerService captures notifications, the actual delivery to webhooks is managed by a batching process (scheduled via WorkManager or internal timers).
Key Management Classes
- BatteryOptimizerUtils: The central utility for polling
BatteryManagerandPowerManager. It provides theBatteryStatedata class containing level, charging status, and recommended batch sizes. - App.kt: Handles the initialization of background processing components, including
WorkManager, which is used for persistent batch notification processing. - DataStoreManager: Stores user preferences regarding power-intensive features like "Auto Start Service" and "Debug Mode".
Sources: app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:65-84, app/src/main/kotlin/com/fathiraz/flowbell/presentation/app/App.kt:113-122, app/src/main/kotlin/com/fathiraz/flowbell/data/local/preferences/DataStoreManager.kt:125-135
Critical States and Safety Mechanisms
FlowBell implements safety checks to prevent the app from contributing to device shutdown during extreme low-power scenarios.
Notification Processing Halt
The function shouldProcessNotifications() returns false if the battery is below 10% (CRITICAL_BATTERY_LEVEL) and the device is not currently connected to a power source. This prevents the radio from being woken up for network transmissions when the battery is near exhaustion.
fun shouldProcessNotifications(): Boolean {
val batteryState = getCurrentBatteryState()
return when (batteryState.optimizationLevel) {
OptimizationLevel.NORMAL, OptimizationLevel.LIGHT, OptimizationLevel.MODERATE -> true
OptimizationLevel.AGGRESSIVE -> {
// Only skip if battery is critically low AND not charging
batteryState.level > CRITICAL_BATTERY_LEVEL || batteryState.isCharging
}
}
}
Sources: app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:156-166
Interval Management
The optimal processing interval is calculated between 2 to 5 seconds. Even in high-performance modes, the app enforces a minimum 2-second delay to avoid rapid-fire network requests that could lead to excessive battery drain and thermal issues.
Sources: app/src/main/kotlin/com/fathiraz/flowbell/core/utils/BatteryOptimizerUtils.kt:172-182
Summary
The Battery & Power Optimization module ensures FlowBell remains a sustainable background utility. By dynamically adjusting the batch size (from 50 down to 10) and the transmission cadence based on real-time power telemetry, the app maintains its function as a notification bridge while respecting the device's thermal and energy limits.