diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index aa2ccc2..3e78743 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,9 +12,10 @@ + + - ("transient.app_version")?.summary = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})" + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + findPreference("system")?.isEnabled = false + findPreference("system.disable_battery_optimizations")?.summary = + getString(R.string.battery_optimizations_not_supported_on_this_device) + } else { + findPreference("system.disable_battery_optimizations")?.summary = + if (isIgnoringBatteryOptimizations()) getString(R.string.disabled) else getString(R.string.enabled) + } } override fun onDisplayPreferenceDialog(preference: Preference) { @@ -62,6 +79,45 @@ class SettingsFragment : PreferenceFragmentCompat() { super.onDisplayPreferenceDialog(preference) } + override fun onPreferenceTreeClick(preference: Preference): Boolean { + if (preference.key == "system.disable_battery_optimizations") { + requestIgnoreBatteryOptimizations() + return true + } + + return super.onPreferenceTreeClick(preference) + } + + private fun requestIgnoreBatteryOptimizations() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + Toast.makeText( + requireContext(), + getString(R.string.battery_optimizations_not_supported_on_this_device), + Toast.LENGTH_SHORT + ).show() + return + } + + if (isIgnoringBatteryOptimizations()) { + Toast.makeText( + requireContext(), + getString(R.string.battery_optimizations_already_disabled), + Toast.LENGTH_SHORT + ).show() + return + } + + val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) + intent.data = Uri.parse("package:${requireContext().packageName}") + startActivity(intent) + } + + @RequiresApi(Build.VERSION_CODES.M) + private fun isIgnoringBatteryOptimizations(): Boolean { + val powerManager = requireContext().getSystemService(Context.POWER_SERVICE) as PowerManager + return powerManager.isIgnoringBatteryOptimizations(requireContext().packageName) + } + companion object { fun newInstance() = SettingsFragment() } diff --git a/app/src/main/res/drawable-notnight/ic_battery_optimizations.xml b/app/src/main/res/drawable-notnight/ic_battery_optimizations.xml new file mode 100644 index 0000000..a848225 --- /dev/null +++ b/app/src/main/res/drawable-notnight/ic_battery_optimizations.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_battery_optimizations.xml b/app/src/main/res/drawable/ic_battery_optimizations.xml new file mode 100644 index 0000000..c3e5e7e --- /dev/null +++ b/app/src/main/res/drawable/ic_battery_optimizations.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27f1b64..e965de9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -59,4 +59,11 @@ Password: Server address: To apply the changes, restart the app using the button below. + System + can affect battery life + Disable battery optimizations + Disabled + Enabled + Battery optimizations not supported on this device + Battery optimizations already disabled \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 8bf4142..5dd637e 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -62,6 +62,16 @@ app:useSimpleSummaryProvider="true" /> + + + +