Add fallback to ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS if ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS fails.
This commit is contained in:
parent
fc5c811d76
commit
3d60bb81ee
1 changed files with 17 additions and 3 deletions
|
|
@ -57,14 +57,28 @@ class AndroidBatteryOptimization @Inject constructor(
|
||||||
|
|
||||||
@SuppressLint("BatteryLife")
|
@SuppressLint("BatteryLife")
|
||||||
override fun requestDisablingBatteryOptimization(): Boolean {
|
override fun requestDisablingBatteryOptimization(): Boolean {
|
||||||
|
val ignoreBatteryOptimizationsResult = launchAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, withData = true)
|
||||||
|
if (ignoreBatteryOptimizationsResult) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Open settings as a fallback if the first attempt fails
|
||||||
|
return launchAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS, withData = false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchAction(
|
||||||
|
action: String,
|
||||||
|
withData: Boolean,
|
||||||
|
): Boolean {
|
||||||
val intent = Intent()
|
val intent = Intent()
|
||||||
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
intent.action = action
|
||||||
intent.data = ("package:" + context.packageName).toUri()
|
if (withData) {
|
||||||
|
intent.data = ("package:" + context.packageName).toUri()
|
||||||
|
}
|
||||||
return try {
|
return try {
|
||||||
externalIntentLauncher.launch(intent)
|
externalIntentLauncher.launch(intent)
|
||||||
true
|
true
|
||||||
} catch (exception: ActivityNotFoundException) {
|
} catch (exception: ActivityNotFoundException) {
|
||||||
Timber.w(exception, "Cannot request ignoring battery optimizations.")
|
Timber.w(exception, "Cannot launch intent with action $action.")
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue