From 2854fb5f6cfdff37f9d38dea5a75fb01f6a9bfed Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 7 Mar 2020 15:51:46 -0500 Subject: [PATCH] Add shortcut to disabling battery optimization --- app/src/main/AndroidManifest.xml | 1 + .../ui/setting/SettingsAdvancedController.kt | 28 +++++++++++++++++++ app/src/main/res/values/strings.xml | 3 ++ 3 files changed, 32 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8a038bdd1b..91e613917c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ + diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt index bc707af09e..0a63a3161e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt @@ -1,7 +1,14 @@ package eu.kanade.tachiyomi.ui.setting +import android.annotation.SuppressLint import android.app.Dialog +import android.content.Context.POWER_SERVICE +import android.content.Intent +import android.net.Uri +import android.os.Build import android.os.Bundle +import android.os.PowerManager +import android.provider.Settings import android.view.View import androidx.preference.PreferenceScreen import com.afollestad.materialdialogs.MaterialDialog @@ -35,6 +42,7 @@ class SettingsAdvancedController : SettingsController() { private val db: DatabaseHelper by injectLazy() + @SuppressLint("BatteryLife") override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { titleRes = R.string.pref_category_advanced @@ -81,6 +89,26 @@ class SettingsAdvancedController : SettingsController() { onClick { LibraryUpdateService.start(context, target = Target.TRACKING) } } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + preference { + titleRes = R.string.pref_disable_battery_optimization + summaryRes = R.string.pref_disable_battery_optimization_summary + + onClick { + val packageName: String = context.packageName + val pm = context.getSystemService(POWER_SERVICE) as PowerManager? + if (!pm!!.isIgnoringBatteryOptimizations(packageName)) { + val intent = Intent().apply { + action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS + data = Uri.parse("package:$packageName") + } + startActivity(intent) + } else { + context.toast(R.string.battery_optimization_disabled) + } + } + } + } } private fun clearChapterCache() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a163c9ebd2..232a3cee0a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -324,6 +324,9 @@ Updates covers, genres, description and manga status information Refresh tracking metadata Updates status, score and last chapter read from the tracking services + Disable battery optimization + Helps with background library updates and backups + Battery optimization is already disabled Version