From d2f3bfb2e3a26be43832abdfb7cd8fb87d63e07c Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 18 Jun 2022 13:11:14 +0300 Subject: [PATCH] Add ignore battery optimization option --- .idea/icon.svg | 287 ++++++++++++++++++ app/src/main/AndroidManifest.xml | 1 + .../settings/TrackerSettingsFragment.kt | 45 +++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/pref_tracker.xml | 11 +- 5 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 .idea/icon.svg diff --git a/.idea/icon.svg b/.idea/icon.svg new file mode 100644 index 000000000..61006f3ad --- /dev/null +++ b/.idea/icon.svg @@ -0,0 +1,287 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9cb967580..e1688abcf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ + (KEY_IGNORE_DOZE)?.run { + isVisible = isDozeIgnoreAvailable(context) + } updateCategoriesSummary() updateNotificationsSummary() } @@ -95,6 +106,10 @@ class TrackerSettingsFragment : startActivity(CategoriesActivity.newIntent(preference.context)) true } + KEY_IGNORE_DOZE -> { + startIgnoreDoseActivity(preference.context) + true + } else -> super.onPreferenceTreeClick(preference) } } @@ -122,4 +137,34 @@ class TrackerSettingsFragment : pref.summary = getString(R.string.enabled_d_of_d, count[0], count[1]) } } + + @SuppressLint("BatteryLife") + private fun startIgnoreDoseActivity(context: Context) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + Snackbar.make(listView, R.string.operation_not_supported, Snackbar.LENGTH_SHORT).show() + return + } + val packageName = context.packageName + val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager + if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { + try { + val intent = Intent( + Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, + "package:$packageName".toUri(), + ) + startActivity(intent) + } catch (e: ActivityNotFoundException) { + Snackbar.make(listView, R.string.operation_not_supported, Snackbar.LENGTH_SHORT).show() + } + } + } + + private fun isDozeIgnoreAvailable(context: Context): Boolean { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return false + } + val packageName = context.packageName + val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager + return !powerManager.isIgnoringBatteryOptimizations(packageName) + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 254f66818..369a5daaf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -296,4 +296,6 @@ Default mode Autodetect reader mode Automatically detect if manga is webtoon + Disable battery optimization + Helps with background updates checks \ No newline at end of file diff --git a/app/src/main/res/xml/pref_tracker.xml b/app/src/main/res/xml/pref_tracker.xml index 4087b503e..10824fc59 100644 --- a/app/src/main/res/xml/pref_tracker.xml +++ b/app/src/main/res/xml/pref_tracker.xml @@ -22,11 +22,20 @@ android:title="@string/favourites_categories" /> + +