hey! I'm developing a Kotlin app por Wear OS 5.
Now, I have a view with buttons, and I wanna change some of them.
On MyApp.kt, I have the following code
onSleepingButtonPressed = {
context ->
val contentResolver = context.contentResolver
code
try {
// Get current brightness
val currentTimeout = Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
Toast.makeText(
context,
"Actual Timeout: $currentTimeout",
Toast.LENGTH_SHORT
).show()
Settings.System.putInt(
contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
when (currentTimeout) {
5000 -> 15000
15000 -> 30000
30000 -> 5000
else -> 5000 // fallback if value is unexpected
}
)
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}
},
and on SettingsScreen.kt, the following
fun SettingsScreen(context: Context,
uiViewModel: VerisureViewModel,
onRefreshMQTTButtonPressed : () -> Unit,
onMQTTButtonPressed: () -> Unit,
onMQTTEnabledToggled: () -> Unit,
onVibrationButtonPressed : () -> Unit,
onBrigthnessButtonPressed : (Context) -> Unit,
onBrigthnessForceButtonPressed : (Context) -> Unit,
onPermissionButtonPressed : () -> Unit,
onSleepingButtonPressed : (Context) -> Unit,
onManagerButtonPressed : () -> Unit,
onFacerButtonPressed : () -> Unit,
)
Could you change the button to an Inline Slidder? So evey time it's changed, the system setting is changed. When the Inline Slider first appears, it should read the actual value of the setting. Because of that, you should also check if permission for reading that variable (in case it is needed) is already granted, and in case it isn't, ask for it. If it is denied, you should gray out the inline slider in case it is a common and doable thing. If it's not, just hide it. It is really really really important that you use for this androidx.wear.compose.material
Assistant
I'll help you implement an Inline Slider for your Wear OS app that controls the screen timeout setting. Here's how to modify your code:
kotlin
User
The slider should offer just int values
Assistant
I'll modify the InlineSlider to only offer integer values for the timeout settings:
// In SettingsScreen.kt
import androidx.wear.compose.material.InlineSlider
import androidx.wear.compose.material.InlineSliderDefaults
import androidx.wear.compose.material.Text
import android.provider.Settings
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.ui.Modifier
import android.Manifest
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
fun SettingsScreen(context: Context,
uiViewModel: VerisureViewModel,
onRefreshMQTTButtonPressed : () -> Unit,
onMQTTButtonPressed: () -> Unit,
onMQTTEnabledToggled: () -> Unit,
onVibrationButtonPressed : () -> Unit,
onBrigthnessButtonPressed : (Context) -> Unit,
onBrigthnessForceButtonPressed : (Context) -> Unit,
onPermissionButtonPressed : () -> Unit,
onSleepingButtonPressed : (Context) -> Unit,
onManagerButtonPressed : () -> Unit,
onFacerButtonPressed : () -> Unit,
) {
// Check for WRITE_SETTINGS permission
val hasWriteSettingsPermission = Settings.System.canWrite(context)
// Create permission launcher
val permissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
// Handle permission result if needed
}
// Get current timeout value and convert to slider position
var currentTimeoutValue by remember { mutableStateOf(0) }
// Define timeout options in milliseconds
val timeoutOptions = listOf(5000, 15000, 30000)
// Initialize the timeout value if permission is granted
if (hasWriteSettingsPermission) {
try {
val contentResolver = context.contentResolver
val timeout = Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
// Find the closest matching option
currentTimeoutValue = when (timeout) {
5000 -> 0
15000 -> 1
30000 -> 2
else -> 0 // Default to first option
}
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}
}
// Function to update the timeout setting
val updateTimeout = { position: Int ->
if (hasWriteSettingsPermission) {
val newTimeout = timeoutOptions[position]
try {
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
newTimeout
)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
// Replace the Chip with InlineSlider
item {
if (hasWriteSettingsPermission) {
InlineSlider(
modifier = Modifier.fillMaxWidth(),
value = currentTimeoutValue,
onValueChange = {
currentTimeoutValue = it
updateTimeout(it)
},
valueRange = 0f..2f,
steps = 1,
enabled = true,
colors = InlineSliderDefaults.colors(),
startIcon = {
Text("Sleep")
},
endIcon = {
Text(
when (currentTimeoutValue) {
0 -> "5s"
1 -> "15s"
2 -> "30s"
else -> "5s"
}
)
}
)
} else {
// Show button to request permission instead
Chip(
modifier = Modifier.fillMaxWidth(),
enabled = true,
onClick = {
// Launch intent to Settings app for WRITE_SETTINGS permission
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
intent.data = Uri.parse("package:" + context.packageName)
context.startActivity(intent)
},
label = {
Text("Enable screen timeout control")
}
)
}
}
}
And you'll need to update your MyApp.kt to handle the new approach:
kotlin
// In MyApp.kt
// You can remove or modify the onSleepingButtonPressed lambda since we're now
// handling the timeout changes directly in the SettingsScreen
// Make sure to add the WRITE_SETTINGS permission to your AndroidManifest.xml:
// <uses-permission android:name="android.permission.WRITE_SETTINGS" />
Would you like me to explain or break down the code?
// In SettingsScreen.kt
import androidx.wear.compose.material.InlineSlider
import androidx.wear.compose.material.InlineSliderDefaults
import androidx.wear.compose.material.Text
import android.provider.Settings
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.foundation.layout.fillMaxWidth
import android.content.Intent
import android.net.Uri
fun SettingsScreen(context: Context,
uiViewModel: VerisureViewModel,
onRefreshMQTTButtonPressed : () -> Unit,
onMQTTButtonPressed: () -> Unit,
onMQTTEnabledToggled: () -> Unit,
onVibrationButtonPressed : () -> Unit,
onBrigthnessButtonPressed : (Context) -> Unit,
onBrigthnessForceButtonPressed : (Context) -> Unit,
onPermissionButtonPressed : () -> Unit,
onSleepingButtonPressed : (Context) -> Unit,
onManagerButtonPressed : () -> Unit,
onFacerButtonPressed : () -> Unit,
) {
// Check for WRITE_SETTINGS permission
val hasWriteSettingsPermission = Settings.System.canWrite(context)
// Get current timeout value and convert to slider position
var currentTimeoutPosition by remember { mutableStateOf(0) }
// Define timeout options in milliseconds - these are the exact integer values
val timeoutOptions = listOf(5000, 15000, 30000)
// Initialize the timeout value if permission is granted
if (hasWriteSettingsPermission) {
try {
val contentResolver = context.contentResolver
val timeout = Settings.System.getInt(
contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
// Find the closest matching option
currentTimeoutPosition = when (timeout) {
5000 -> 0
15000 -> 1
30000 -> 2
else -> 0 // Default to first option
}
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}
}
// Function to update the timeout setting
val updateTimeout = { position: Int ->
if (hasWriteSettingsPermission) {
val newTimeout = timeoutOptions[position]
try {
Settings.System.putInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT,
newTimeout
)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
// Replace the Chip with InlineSlider
item {
if (hasWriteSettingsPermission) {
InlineSlider(
modifier = Modifier.fillMaxWidth(),
value = currentTimeoutPosition,
onValueChange = {
// Only accept integer values
val newPosition = it.toInt()
currentTimeoutPosition = newPosition
updateTimeout(newPosition)
},
valueRange = 0f..2f,
steps = 2, // 2 steps for 3 positions (0, 1, 2)
enabled = true,
colors = InlineSliderDefaults.colors(),
startIcon = {
Text("Sleep")
},
endIcon = {
// Display the actual timeout value in seconds
Text(
when (currentTimeoutPosition) {
0 -> "5s"
1 -> "15s"
2 -> "30s"
else -> "5s"
}
)
}
)
} else {
// Show button to request permission instead
Chip(
modifier = Modifier.fillMaxWidth(),
enabled = true,
onClick = {
// Launch intent to Settings app for WRITE_SETTINGS permission
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
intent.data = Uri.parse("package:" + context.packageName)
context.startActivity(intent)
},
label = {
Text("Enable screen timeout control")
}
)
}
}
}
Would you like me to explain or break down the code?