@@ -321,6 +321,8 @@ class MainActivity : AppCompatActivity() {
321321 checkAllPermissions()
322322 // Force a full widget update every time the app is opened
323323 updateWidget()
324+ // Update battery optimization switch state
325+ updateBatteryOptimizationSwitch()
324326 }
325327
326328 override fun onRequestPermissionsResult (requestCode : Int , permissions : Array <out String >, grantResults : IntArray ) {
@@ -860,6 +862,7 @@ class MainActivity : AppCompatActivity() {
860862 setupStepsSection()
861863 setupScreenTimeSection()
862864 setupKeepAliveSection()
865+ setupBatteryOptimizationSection()
863866 setupEventsAndTasksSections()
864867 setupThemeSection(colorOptions)
865868
@@ -1244,6 +1247,57 @@ class MainActivity : AppCompatActivity() {
12441247 }
12451248 }
12461249 }
1250+ private fun setupBatteryOptimizationSection () {
1251+ val row = findViewById<View >(R .id.row_battery_optimization_toggle)
1252+ val tvTitle = row.findViewById<TextView >(R .id.row_label)
1253+ tvTitle.text = " Disable Battery Optimization"
1254+ updateBatteryOptimizationSwitch()
1255+ }
1256+ private fun updateBatteryOptimizationSwitch () {
1257+ val row = findViewById<View >(R .id.row_battery_optimization_toggle) ? : return
1258+ val switchOpt = row.findViewById<SwitchMaterial >(R .id.row_switch) ? : return
1259+ val powerManager = getSystemService(Context .POWER_SERVICE ) as android.os.PowerManager
1260+ val isIgnoring = if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .M ) {
1261+ powerManager.isIgnoringBatteryOptimizations(packageName)
1262+ } else true
1263+
1264+ switchOpt.setOnCheckedChangeListener(null )
1265+ switchOpt.isChecked = isIgnoring
1266+ switchOpt.setOnCheckedChangeListener { _, isChecked ->
1267+ if (isChecked) {
1268+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .M ) {
1269+ val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
1270+ if (! isCurrentlyIgnoring) {
1271+ try {
1272+ val intent = Intent (android.provider.Settings .ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS ).apply {
1273+ data = android.net.Uri .parse(" package:$packageName " )
1274+ }
1275+ startActivity(intent)
1276+ } catch (e: Exception ) {
1277+ try {
1278+ val intent = Intent (android.provider.Settings .ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS )
1279+ startActivity(intent)
1280+ } catch (ex: Exception ) {
1281+ ex.printStackTrace()
1282+ }
1283+ }
1284+ }
1285+ }
1286+ } else {
1287+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .M ) {
1288+ val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
1289+ if (isCurrentlyIgnoring) {
1290+ try {
1291+ val intent = Intent (android.provider.Settings .ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS )
1292+ startActivity(intent)
1293+ } catch (e: Exception ) {
1294+ e.printStackTrace()
1295+ }
1296+ }
1297+ }
1298+ }
1299+ }
1300+ }
12471301 private fun setupEventsAndTasksSections () {
12481302 // Events
12491303 val eventsSwitch = bindFoldedSection(
0 commit comments