Add shortcut to disabling battery optimization

This commit is contained in:
arkon 2020-04-17 00:35:55 -04:00 committed by Jay
parent 0af41005f8
commit a4178f5d28
3 changed files with 35 additions and 2 deletions

View File

@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" /> <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

View File

@ -1,7 +1,14 @@
package eu.kanade.tachiyomi.ui.setting package eu.kanade.tachiyomi.ui.setting
import android.annotation.SuppressLint
import android.app.Dialog import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.widget.Toast import android.widget.Toast
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
@ -36,6 +43,7 @@ class SettingsAdvancedController : SettingsController() {
private val db: DatabaseHelper by injectLazy() private val db: DatabaseHelper by injectLazy()
@SuppressLint("BatteryLife")
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
titleRes = R.string.advanced titleRes = R.string.advanced
@ -83,6 +91,26 @@ class SettingsAdvancedController : SettingsController() {
onClick { cleanupDownloads() } onClick { cleanupDownloads() }
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager?
if (pm != null) preference {
titleRes = R.string.disable_battery_optimization
summaryRes = R.string.disable_if_issues_with_updating
onClick {
val packageName: String = context.packageName
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 cleanupDownloads() { private fun cleanupDownloads() {
@ -141,10 +169,10 @@ class SettingsAdvancedController : SettingsController() {
override fun onCreateDialog(savedViewState: Bundle?): Dialog { override fun onCreateDialog(savedViewState: Bundle?): Dialog {
return MaterialDialog(activity!!) return MaterialDialog(activity!!)
.message(R.string.clear_database_confirmation) .message(R.string.clear_database_confirmation)
.positiveButton(android.R.string.yes) { .positiveButton(android.R.string.ok) {
(targetController as? SettingsAdvancedController)?.clearDatabase() (targetController as? SettingsAdvancedController)?.clearDatabase()
} }
.negativeButton(android.R.string.no) .negativeButton(android.R.string.cancel)
} }
} }

View File

@ -510,6 +510,10 @@
and read chapter folders</string> and read chapter folders</string>
<string name="starting_cleanup">Starting cleanup</string> <string name="starting_cleanup">Starting cleanup</string>
<string name="no_folders_to_cleanup">No folders to cleanup</string> <string name="no_folders_to_cleanup">No folders to cleanup</string>
<string name="disable_battery_optimization">Disable battery optimization</string>
<string name="disable_if_issues_with_updating">Disable this if you are experiencing issues
with updating or restoring your library</string>
<string name="battery_optimization_disabled">Battery optimization is already disabled</string>
<plurals name="cleanup_done"> <plurals name="cleanup_done">
<item quantity="one">Cleanup done. Removed %d folder</item> <item quantity="one">Cleanup done. Removed %d folder</item>
<item quantity="other">Cleanup done. Removed %d folders</item> <item quantity="other">Cleanup done. Removed %d folders</item>