kotlin Copy code
import android.content.Context
import android.provider.Settings
import android.widget.Toast
import androidx.compose.runtime.*
import androidx.wear.compose.material.*
import kotlin.math.roundToInt
@Composable
fun SettingsScreen(
context: Context,
/* ... other parameters ... */
) {
val timeoutValues = listOf(5_000, 15_000, 30_000)
var currentTimeout by remember { mutableStateOf<Int?>(null) }
val hasPermission = remember { Settings.System.canWrite(context) }
LaunchedEffect(Unit) {
if (hasPermission) {
currentTimeout = Settings.System.getInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
5_000 // Default if not found
)
}
}
item {
if (hasPermission && currentTimeout != null) {
val currentIndex = timeoutValues.indexOf(currentTimeout).coerceAtLeast(0)
InlineSlider(
value = currentIndex.toFloat(),
onValueChange = { newValue ->
val selectedIndex = newValue.roundToInt()
val newTimeout = timeoutValues[selectedIndex]
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
newTimeout
)
currentTimeout = newTimeout
Toast.makeText(
context,
"Timeout: ${newTimeout / 1_000}s",
Toast.LENGTH_SHORT
).show()
},
valueRange = 0f..(timeoutValues.size - 1).toFloat(),
steps = timeoutValues.size - 1,
increaseIcon = {
Icon(
imageVector = InlineSliderDefaults.IncreaseIcon,
contentDescription = "Increase"
)
},
decreaseIcon = {
Icon(
imageVector = InlineSliderDefaults.DecreaseIcon,
contentDescription = "Decrease"
)
},
enabled = true
)
} else {
// Disabled slider when no permission
InlineSlider(
value = 0f,
onValueChange = {},
valueRange = 0f..0f,
increaseIcon = {
Icon(
imageVector = InlineSliderDefaults.IncreaseIcon,
contentDescription = null
)
},
decreaseIcon = {
Icon(
imageVector = InlineSliderDefaults.DecreaseIcon,
contentDescription = null
)
},
enabled = false
)
}
}
}