diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/base/ActivityExtensions.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/base/ActivityExtensions.kt new file mode 100644 index 00000000000..83d7f1b7115 --- /dev/null +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/base/ActivityExtensions.kt @@ -0,0 +1,20 @@ +package de.rki.coronawarnapp.ui.base + +import android.app.Activity +import android.content.Intent +import de.rki.coronawarnapp.exception.ExceptionCategory +import de.rki.coronawarnapp.exception.ExternalActionException +import de.rki.coronawarnapp.exception.reporting.report + +fun Activity.startActivitySafely( + intent: Intent, + handler: (Exception) -> Unit = { + ExternalActionException(it).report(ExceptionCategory.UI) + } +) { + try { + startActivity(intent) + } catch (exception: Exception) { + handler.invoke(exception) + } +} diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/main/MainActivity.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/main/MainActivity.kt index bfd78ff91cf..c4bbd54c91d 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/main/MainActivity.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/main/MainActivity.kt @@ -15,6 +15,7 @@ import de.rki.coronawarnapp.R import de.rki.coronawarnapp.http.playbook.BackgroundNoise import de.rki.coronawarnapp.nearby.InternalExposureNotificationClient import de.rki.coronawarnapp.storage.LocalData +import de.rki.coronawarnapp.ui.base.startActivitySafely import de.rki.coronawarnapp.ui.viewmodel.SettingsViewModel import de.rki.coronawarnapp.util.BackgroundPrioritization import de.rki.coronawarnapp.util.ConnectivityHelper @@ -130,7 +131,7 @@ class MainActivity : AppCompatActivity() { R.string.onboarding_energy_optimized_dialog_button_negative, false, { // go to battery optimization - powerManagement.disableBatteryOptimizations() + startActivitySafely(powerManagement.disableBatteryOptimizationsIntent) }, { // keep battery optimization enabled showManualCheckingRequiredDialog() diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/settings/SettingsBackgroundPriorityFragment.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/settings/SettingsBackgroundPriorityFragment.kt index 3b3223f2839..aa8218a169d 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/settings/SettingsBackgroundPriorityFragment.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/settings/SettingsBackgroundPriorityFragment.kt @@ -8,6 +8,7 @@ import android.view.accessibility.AccessibilityEvent import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import de.rki.coronawarnapp.databinding.FragmentSettingsBackgroundPriorityBinding +import de.rki.coronawarnapp.ui.base.startActivitySafely import de.rki.coronawarnapp.ui.main.MainActivity import de.rki.coronawarnapp.ui.viewmodel.SettingsViewModel import de.rki.coronawarnapp.ui.viewmodel.TracingViewModel @@ -66,8 +67,9 @@ class SettingsBackgroundPriorityFragment : Fragment() { val isPriorityEnabled = settingsViewModel.isBackgroundPriorityEnabled.value == true if (!isPriorityEnabled) { - val activity = requireActivity() as MainActivity - activity.powerManagement.disableBatteryOptimizations() + (requireActivity() as MainActivity).apply { + startActivitySafely(powerManagement.disableBatteryOptimizationsIntent) + } } } } diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/ExternalActionHelper.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/ExternalActionHelper.kt index 5a2e9b6c046..e200dadb090 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/ExternalActionHelper.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/ExternalActionHelper.kt @@ -155,17 +155,4 @@ object ExternalActionHelper { ) } } - - fun toBatterySaverSettings(context: Context) { - try { - val intent = Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS) - context.startActivity(intent) - } catch (exception: Exception) { - // catch generic exception on settings navigation - // most likely due to device / rom specific intent issue - ExternalActionException(exception).report( - ExceptionCategory.UI - ) - } - } } diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/DefaultPowerManagement.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/DefaultPowerManagement.kt index 140b1e0e9d6..12475b33358 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/DefaultPowerManagement.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/DefaultPowerManagement.kt @@ -5,9 +5,6 @@ import android.content.Intent import android.net.Uri import android.os.PowerManager import android.provider.Settings -import de.rki.coronawarnapp.exception.ExceptionCategory -import de.rki.coronawarnapp.exception.ExternalActionException -import de.rki.coronawarnapp.exception.reporting.report import javax.inject.Inject class DefaultPowerManagement @Inject constructor( @@ -24,20 +21,9 @@ class DefaultPowerManagement @Inject constructor( override val toBatteryOptimizationSettingsIntent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) - override fun disableBatteryOptimizations() { - try { - context.startActivity( - Intent( - Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, - Uri.parse("package:${context.packageName}") - ) - ) - } catch (exception: Exception) { - // catch generic exception on settings navigation - // most likely due to device / rom specific intent issue - ExternalActionException(exception).report( - ExceptionCategory.UI - ) - } - } + override val disableBatteryOptimizationsIntent = + Intent( + Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, + Uri.parse("package:${context.packageName}") + ) } diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/PowerManagement.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/PowerManagement.kt index 1e92d27413a..d0cd90a3025 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/PowerManagement.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/util/device/PowerManagement.kt @@ -11,5 +11,5 @@ interface PowerManagement { val toBatteryOptimizationSettingsIntent: Intent - fun disableBatteryOptimizations() + val disableBatteryOptimizationsIntent: Intent }