feat(battery): add battery optimization management for background reliability

Add battery optimization permission and management system to ensure app runs reliably in background:
- Add REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission
- Create BatteryOptimizationManager to handle battery optimization checks and requests
- Add battery optimization UI screens and flow in onboarding
- Integrate battery optimization checks into MainActivity flow
This commit is contained in:
Faded
2025-07-14 14:23:59 +05:00
parent 013f0c00cf
commit fe57c5684e
6 changed files with 628 additions and 6 deletions
@@ -4,6 +4,7 @@ import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.PowerManager
import android.util.Log
import androidx.core.content.ContextCompat
@@ -86,6 +87,31 @@ class PermissionManager(private val context: Context) {
return getRequiredPermissions().all { isPermissionGranted(it) }
}
/**
* Check if battery optimization is disabled for this app
*/
fun isBatteryOptimizationDisabled(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
powerManager.isIgnoringBatteryOptimizations(context.packageName)
} catch (e: Exception) {
Log.e(TAG, "Error checking battery optimization status", e)
false
}
} else {
// Battery optimization doesn't exist on Android < 6.0
true
}
}
/**
* Check if battery optimization is supported on this device
*/
fun isBatteryOptimizationSupported(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}
/**
* Get the list of permissions that are missing
*/
@@ -152,6 +178,19 @@ class PermissionManager(private val context: Context) {
)
}
// Battery optimization category (if applicable)
if (isBatteryOptimizationSupported()) {
categories.add(
PermissionCategory(
type = PermissionType.BATTERY_OPTIMIZATION,
description = "Disable battery optimization to ensure bitchat runs reliably in the background and maintains mesh network connections",
permissions = listOf("BATTERY_OPTIMIZATION"), // Custom identifier
isGranted = isBatteryOptimizationDisabled(),
systemDescription = "Allow bitchat to run without battery restrictions"
)
)
}
return categories
}
@@ -208,5 +247,6 @@ enum class PermissionType(val nameValue: String) {
NEARBY_DEVICES("Nearby Devices"),
PRECISE_LOCATION("Precise Location"),
NOTIFICATIONS("Notifications"),
BATTERY_OPTIMIZATION("Battery Optimization"),
OTHER("Other")
}