Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Fixed: cannot activate prioritised background activity on some devices (EXPOSUREAPP-2506) #1116

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface PowerManagement {

val toBatteryOptimizationSettingsIntent: Intent

fun disableBatteryOptimizations()
val disableBatteryOptimizationsIntent: Intent
}