Add preference to disable battery optimization

This commit is contained in:
Shinokuni 2024-07-24 18:39:12 +02:00
parent 63867d3acd
commit 67b29d7590
4 changed files with 53 additions and 1 deletions

View File

@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:name=".ReadropsApp"

View File

@ -1,5 +1,11 @@
package com.readrops.app.more.preferences
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
@ -9,10 +15,14 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
@ -21,12 +31,14 @@ import cafe.adriel.voyager.koin.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import com.readrops.app.R
import com.readrops.app.more.preferences.components.BasePreference
import com.readrops.app.more.preferences.components.ListPreferenceWidget
import com.readrops.app.more.preferences.components.PreferenceHeader
import com.readrops.app.more.preferences.components.SwitchPreferenceWidget
import com.readrops.app.sync.SyncWorker
import com.readrops.app.util.components.AndroidScreen
import com.readrops.app.util.components.CenteredProgressIndicator
import kotlinx.coroutines.launch
class PreferencesScreen : AndroidScreen() {
@ -38,6 +50,9 @@ class PreferencesScreen : AndroidScreen() {
val context = LocalContext.current
val screenModel = getScreenModel<PreferencesScreenModel>()
val coroutineScope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
val state by screenModel.state.collectAsStateWithLifecycle()
Scaffold(
@ -55,7 +70,8 @@ class PreferencesScreen : AndroidScreen() {
}
}
)
}
},
snackbarHost = { SnackbarHost(snackbarHostState) }
) { paddingValues ->
Box(
modifier = Modifier.padding(paddingValues)
@ -64,6 +80,7 @@ class PreferencesScreen : AndroidScreen() {
is PreferencesScreenState.Loading -> {
CenteredProgressIndicator()
}
else -> {
val loadedState = (state as PreferencesScreenState.Loaded)
@ -99,6 +116,34 @@ class PreferencesScreen : AndroidScreen() {
onValueChange = { SyncWorker.startPeriodically(context, it) }
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
BasePreference(
title = stringResource(R.string.disable_battery_optimization),
subtitle = stringResource(R.string.disable_battery_optimization_subtitle),
onClick = {
val powerManager =
context.getSystemService("power") as PowerManager
val packageName = context.packageName
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
@SuppressLint("BatteryLife")
val intent = Intent().apply {
action =
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
data = Uri.parse("package:$packageName")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
} else {
coroutineScope.launch {
snackbarHostState.showSnackbar(context.getString(R.string.battery_optimization_already_disabled))
}
}
}
)
}
PreferenceHeader(text = stringResource(id = R.string.timeline))
ListPreferenceWidget(

View File

@ -185,4 +185,7 @@
<string name="large">Large</string>
<string name="item_size">Taille des items</string>
<string name="background_sync_already_running">Une synchronisation en arrière-plan est déjà en cours</string>
<string name="disable_battery_optimization">Désactiver l\'optimisation de la batterie</string>
<string name="disable_battery_optimization_subtitle">Peut aider à éviter que le système n\'empêche la synchronisation d\'arrière-plan</string>
<string name="battery_optimization_already_disabled">L\'optimisation de la batterie est déjà optimisée pour cette appli</string>
</resources>

View File

@ -194,4 +194,7 @@
<string name="large">Large</string>
<string name="item_size">Item size</string>
<string name="background_sync_already_running">A background synchronization is already running</string>
<string name="disable_battery_optimization">Disable battery optimization</string>
<string name="disable_battery_optimization_subtitle">Can help with background synchronization not being killed by the system</string>
<string name="battery_optimization_already_disabled">Battery optimization already disabled for this app</string>
</resources>