Add battery optimization badge to Service menu item

Show a theme-colored badge on the Service settings when battery
optimization is not ignored. Also shorten button text to "Ignore".
This commit is contained in:
世界
2025-12-18 19:26:08 +08:00
parent ec6543ccfb
commit 7b39c1dd5a
2 changed files with 30 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
package io.nekohasekai.sfa.compose.screen.settings package io.nekohasekai.sfa.compose.screen.settings
import android.os.Build
import android.os.PowerManager
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@@ -31,8 +33,12 @@ import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@@ -53,6 +59,15 @@ fun SettingsScreen(navController: NavController) {
val context = LocalContext.current val context = LocalContext.current
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val hasUpdate by UpdateState.hasUpdate val hasUpdate by UpdateState.hasUpdate
var isBatteryOptimizationIgnored by remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val pm = context.getSystemService(PowerManager::class.java)
isBatteryOptimizationIgnored =
pm?.isIgnoringBatteryOptimizations(context.packageName) == true
}
}
Column( Column(
modifier = modifier =
@@ -140,6 +155,11 @@ fun SettingsScreen(navController: NavController) {
tint = MaterialTheme.colorScheme.primary, tint = MaterialTheme.colorScheme.primary,
) )
}, },
trailingContent = {
if (!isBatteryOptimizationIgnored) {
Badge(containerColor = MaterialTheme.colorScheme.primary)
}
},
modifier = Modifier.clickable { navController.navigate("settings/service") }, modifier = Modifier.clickable { navController.navigate("settings/service") },
colors = colors =
ListItemDefaults.colors( ListItemDefaults.colors(
@@ -299,17 +319,16 @@ fun SettingsScreen(navController: NavController) {
} }
} }
// Debug
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(R.string.title_debug),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(horizontal = 32.dp, vertical = 8.dp),
)
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
// Debug
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(R.string.title_debug),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(horizontal = 32.dp, vertical = 8.dp),
)
Card( Card(
modifier = modifier =
Modifier Modifier

View File

@@ -143,7 +143,7 @@
<string name="background_permission">Background permission</string> <string name="background_permission">Background permission</string>
<string name="background_permission_description">Apply for the necessary permissions in order for the VPN to function properly.\n\nIf you are using a device made by a Chinese company, the card may not disappear after the permission is granted.</string> <string name="background_permission_description">Apply for the necessary permissions in order for the VPN to function properly.\n\nIf you are using a device made by a Chinese company, the card may not disappear after the permission is granted.</string>
<string name="read_more">Read More</string> <string name="read_more">Read More</string>
<string name="request_background_permission">Ignore Battery Optimizations</string> <string name="request_background_permission">Ignore</string>
<string name="import_remote_profile">Import remote profile</string> <string name="import_remote_profile">Import remote profile</string>
<string name="import_remote_profile_message">Are you sure to import remote profile %1$s? You will connect to %2$s to download the configuration.</string> <string name="import_remote_profile_message">Are you sure to import remote profile %1$s? You will connect to %2$s to download the configuration.</string>
<string name="profile_override">Profile Override</string> <string name="profile_override">Profile Override</string>