The Apps page can show a photo for light and dark mode, or the weather outside drawn live

This commit is contained in:
Seb3thehacker committed 2026-11-24 12:10:35 -08:00
1 parent 12fee1e311
commit e44028dc81
5 files changed
+421

No files matched your search

@@ -45,6 +45,7 @@ import kotlinx.coroutines.flow.asStateFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
@@ -344,6 +345,8 @@ fun CarLauncher() {
onDispose { if (CarKeys.knobTaker === taker) CarKeys.knobTaker = null }
}
Box(Modifier.fillMaxSize()) {
AppsWallpaper()
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 108.dp),
state = gridState,
@@ -368,6 +371,50 @@ fun CarLauncher() {
}
}
}
}
/**
* Behind the Apps page's icons: the driver's photo for the car's light or dark mode, or the
* weather outside, drawn live. Nothing when the setting is off. A photo gets a light veil in the
* screen's own background colour, so section headings stay readable on a busy picture.
*/
@Composable
private fun AppsWallpaper() {
val context = LocalContext.current
remember(context) { CarWallpaper.init(context) }
val mode by CarWallpaper.mode.collectAsState()
val background = MaterialTheme.colorScheme.background
val dark = background.luminance() < 0.5f
when (mode) {
CarWallpaper.Mode.OFF -> Unit
CarWallpaper.Mode.PHOTO -> {
val version by CarWallpaper.photos.collectAsState()
val photo by androidx.compose.runtime.produceState<ImageBitmap?>(null, dark, version) {
value = withContext(Dispatchers.IO) {
// The other mode's photo stands in when only one has been picked.
val file = CarWallpaper.file(context, dark).takeIf { it.isFile } ?: CarWallpaper.file(context, !dark)
if (file.isFile) android.graphics.BitmapFactory.decodeFile(file.path)?.asImageBitmap() else null
}
}
photo?.let {
Image(it, contentDescription = null, modifier = Modifier.fillMaxSize(), contentScale = androidx.compose.ui.layout.ContentScale.Crop)
Box(Modifier.fillMaxSize().background(background.copy(alpha = 0.3f)))
}
}
CarWallpaper.Mode.WEATHER -> {
val weather by Weather.state.collectAsState()
LaunchedEffect(Unit) { Weather.refresh(context) }
val data = (weather as? WeatherState.Ready)?.data
val hour = remember { java.util.Calendar.getInstance().get(java.util.Calendar.HOUR_OF_DAY) }
WeatherScene(
scene = data?.let { skySceneOf(it.code) } ?: SkyScene.CLEAR,
isDay = data?.isDay ?: (hour in 6..18),
dark = dark,
modifier = Modifier.fillMaxSize(),
)
}
}
}
/**
* Every phone app, for the launcher's Phone apps section (issue #124). Empty until Shizuku is
@@ -0,0 +1,73 @@
package app.seb3thehacker.gearslip.car
import android.content.Context
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.net.Uri
import app.seb3thehacker.gearslip.AppSettings
import app.seb3thehacker.gearslip.GearslipLog
import java.io.File
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
/**
* What the car's Apps page draws behind its icons: nothing, a photo from the phone (one for the
* car's light mode and one for its dark mode), or the weather outside, drawn live.
*
* Photos are copied into Gearslip's own files, scaled down to what a car screen can show, so the
* original can be moved or deleted from the gallery without the car losing it.
*/
object CarWallpaper {
enum class Mode { OFF, PHOTO, WEATHER }
private const val KEY_MODE = "car_wallpaper"
private const val LIGHT = "wallpaper-light.jpg"
private const val DARK = "wallpaper-dark.jpg"
/** Wide enough for the biggest car screens Gearslip has seen, without keeping a 50 MP photo. */
private const val MAX_SIDE = 1920
private val modeFlow = MutableStateFlow(Mode.OFF)
val mode: StateFlow<Mode> = modeFlow
/** Bumped whenever a photo changes, so whatever shows it reloads. */
private val photosFlow = MutableStateFlow(0)
val photos: StateFlow<Int> = photosFlow
@Volatile private var loaded = false
fun init(context: Context) {
if (loaded) return
loaded = true
modeFlow.value = runCatching { Mode.valueOf(AppSettings.getString(context, KEY_MODE, Mode.OFF.name)) }.getOrDefault(Mode.OFF)
}
fun setMode(context: Context, mode: Mode) {
modeFlow.value = mode
AppSettings.putString(context, KEY_MODE, mode.name)
}
fun file(context: Context, dark: Boolean): File = File(context.filesDir, if (dark) DARK else LIGHT)
fun hasPhoto(context: Context, dark: Boolean): Boolean = file(context, dark).isFile
/** Copies the picked photo in, scaled down. Runs off the main thread; false when it couldn't be read. */
fun savePhoto(context: Context, uri: Uri, dark: Boolean): Boolean = runCatching {
val source = ImageDecoder.createSource(context.contentResolver, uri)
val bitmap = ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
val (w, h) = info.size.width to info.size.height
val scale = minOf(1f, MAX_SIDE.toFloat() / maxOf(w, h))
decoder.setTargetSize((w * scale).toInt().coerceAtLeast(1), (h * scale).toInt().coerceAtLeast(1))
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
}
file(context, dark).outputStream().use { bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it) }
photosFlow.value++
true
}.onFailure { GearslipLog.e("could not save the wallpaper photo", it) }.getOrDefault(false)
fun removePhoto(context: Context, dark: Boolean) {
file(context, dark).delete()
photosFlow.value++
}
}
@@ -0,0 +1,231 @@
package app.seb3thehacker.gearslip.car
import androidx.compose.foundation.Canvas
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.lerp
import kotlin.math.PI
import kotlin.math.sin
import kotlin.random.Random
/** What the sky is doing, from Open-Meteo's WMO weather code. */
enum class SkyScene { CLEAR, PARTLY_CLOUDY, OVERCAST, FOG, DRIZZLE, RAIN, SNOW, STORM }
fun skySceneOf(code: Int): SkyScene = when (code) {
0, 1 -> SkyScene.CLEAR
2 -> SkyScene.PARTLY_CLOUDY
3 -> SkyScene.OVERCAST
45, 48 -> SkyScene.FOG
in 51..57 -> SkyScene.DRIZZLE
in 61..67, in 80..82 -> SkyScene.RAIN
in 71..77, 85, 86 -> SkyScene.SNOW
in 95..99 -> SkyScene.STORM
else -> SkyScene.PARTLY_CLOUDY
}
/**
* The weather outside, drawn live behind the Apps page: a sky for the time of day and the
* conditions, with sun rays, stars, clouds, rain, snow, fog or lightning moving over it.
*
* Every particle's place is worked out from the clock and its own fixed seed, so nothing is
* stored or allocated per frame, and only the drawing reruns each frame, not the composition.
* [dark] follows the car's dark mode: the same scene, dimmer, so it doesn't glare at night.
*/
@Composable
fun WeatherScene(scene: SkyScene, isDay: Boolean, dark: Boolean, modifier: Modifier = Modifier) {
val time = remember { mutableFloatStateOf(0f) }
LaunchedEffect(Unit) {
val start = withFrameNanos { it }
while (true) withFrameNanos { time.floatValue = (it - start) / 1_000_000_000f }
}
val seeds = remember { Seeds() }
Canvas(modifier) {
val t = time.floatValue
drawSky(scene, isDay, dark)
when (scene) {
SkyScene.CLEAR -> if (isDay) drawSun(t, 1f) else drawNight(t, seeds, 1f, dark)
SkyScene.PARTLY_CLOUDY -> {
if (isDay) drawSun(t, 0.7f) else drawNight(t, seeds, 0.6f, dark)
drawClouds(t, seeds, count = 4, opacity = cloudAlpha(isDay, dark) * 0.8f)
}
SkyScene.OVERCAST -> drawClouds(t, seeds, count = 7, opacity = cloudAlpha(isDay, dark))
SkyScene.FOG -> drawFog(t, isDay, dark)
SkyScene.DRIZZLE -> {
drawClouds(t, seeds, count = 6, opacity = cloudAlpha(isDay, dark))
drawRain(t, seeds, count = 45, speed = 0.55f, length = 0.025f, dark = dark)
}
SkyScene.RAIN -> {
drawClouds(t, seeds, count = 7, opacity = cloudAlpha(isDay, dark))
drawRain(t, seeds, count = 110, speed = 1f, length = 0.05f, dark = dark)
}
SkyScene.SNOW -> {
drawClouds(t, seeds, count = 5, opacity = cloudAlpha(isDay, dark) * 0.8f)
drawSnow(t, seeds)
}
SkyScene.STORM -> {
drawClouds(t, seeds, count = 8, opacity = cloudAlpha(isDay, dark) * 1.2f)
drawRain(t, seeds, count = 140, speed = 1.3f, length = 0.06f, dark = dark)
drawLightning(t)
}
}
}
}
/** How far dark mode takes the sky toward black. */
private const val SKY_DIM = 0.62f
/** Fixed random starting points for every particle, made once. */
private class Seeds {
private val random = Random(124)
val x = FloatArray(MAX) { random.nextFloat() }
val y = FloatArray(MAX) { random.nextFloat() }
val speed = FloatArray(MAX) { 0.6f + random.nextFloat() * 0.8f }
val size = FloatArray(MAX) { random.nextFloat() }
val phase = FloatArray(MAX) { random.nextFloat() * 2f * PI.toFloat() }
companion object { const val MAX = 160 }
}
/** The fractional part, always in 0..1, so particles wrap around the screen. */
private fun wrap(v: Float): Float = v - kotlin.math.floor(v)
private fun cloudAlpha(isDay: Boolean, dark: Boolean): Float = when {
dark -> 0.10f
isDay -> 0.35f
else -> 0.14f
}
private fun DrawScope.drawSky(scene: SkyScene, isDay: Boolean, dark: Boolean) {
val (top, bottom) = when {
!isDay -> Color(0xFF0B1230) to Color(0xFF1E2A4A)
scene == SkyScene.CLEAR -> Color(0xFF2F74D0) to Color(0xFF8CC4F5)
scene == SkyScene.PARTLY_CLOUDY -> Color(0xFF3F7CC4) to Color(0xFF9BC2E6)
scene == SkyScene.STORM -> Color(0xFF2C3440) to Color(0xFF4A5563)
scene == SkyScene.SNOW || scene == SkyScene.FOG -> Color(0xFF8E9AA8) to Color(0xFFC9D1DA)
else -> Color(0xFF5B6E84) to Color(0xFF93A3B5)
}
// Dark mode keeps the scene's colours but takes them most of the way to black.
val dim = if (dark) SKY_DIM else 0f
drawRect(Brush.verticalGradient(listOf(lerp(top, Color.Black, dim), lerp(bottom, Color.Black, dim))))
}
private fun DrawScope.drawSun(t: Float, strength: Float) {
val center = Offset(size.width * 0.82f, size.height * 0.2f)
val radius = size.minDimension * 0.11f
drawCircle(
Brush.radialGradient(listOf(Color(0x66FFE9A8), Color(0x00FFE9A8)), center, radius * 4.5f),
radius * 4.5f, center, alpha = strength,
)
// Long soft rays, turning about once a minute.
rotate(t * 6f, center) {
for (i in 0 until 12) {
rotate(i * 30f, center) {
drawLine(
Color(0x22FFF4D0), center, Offset(center.x, center.y - radius * 4f),
strokeWidth = radius * 0.5f, cap = StrokeCap.Round, alpha = strength,
)
}
}
}
drawCircle(Color(0xFFFFE59A), radius, center, alpha = strength)
}
private fun DrawScope.drawNight(t: Float, seeds: Seeds, strength: Float, dark: Boolean) {
for (i in 0 until 70) {
val twinkle = 0.45f + 0.55f * ((sin(t * (0.6f + seeds.speed[i]) + seeds.phase[i]) + 1f) / 2f)
drawCircle(
Color.White,
radius = 0.6f + seeds.size[i] * 1.6f,
center = Offset(seeds.x[i] * size.width, seeds.y[i] * size.height * 0.75f),
alpha = twinkle * 0.8f * strength,
)
}
// A crescent: a pale disc with the sky's colour taken out of one side.
val moon = Offset(size.width * 0.8f, size.height * 0.2f)
val r = size.minDimension * 0.07f
drawCircle(Color(0x33E8ECFF), r * 3f, moon, alpha = strength)
drawCircle(Color(0xFFEDEFF7), r, moon, alpha = strength)
drawCircle(lerp(Color(0xFF14204A), Color.Black, if (dark) SKY_DIM else 0f), r * 0.92f, Offset(moon.x + r * 0.45f, moon.y - r * 0.2f), alpha = strength)
}
private fun DrawScope.drawClouds(t: Float, seeds: Seeds, count: Int, opacity: Float) {
for (i in 0 until count) {
val drift = t * 0.008f * seeds.speed[i]
val w = size.width * (0.35f + seeds.size[i] * 0.3f)
val x = wrap(seeds.x[i] + drift) * (size.width + w) - w / 2
val y = size.height * (0.08f + seeds.y[i] * 0.55f)
drawCloud(Offset(x, y), w, opacity.coerceAtMost(1f))
}
}
/** A cloud from overlapping circles, flatter at the bottom. */
private fun DrawScope.drawCloud(at: Offset, width: Float, alpha: Float) {
val c = Color.White
val r = width * 0.22f
drawCircle(c, r, Offset(at.x - width * 0.22f, at.y + r * 0.2f), alpha = alpha)
drawCircle(c, r * 1.35f, Offset(at.x, at.y - r * 0.15f), alpha = alpha)
drawCircle(c, r * 1.05f, Offset(at.x + width * 0.24f, at.y + r * 0.1f), alpha = alpha)
drawRect(c, Offset(at.x - width * 0.22f, at.y + r * 0.2f), Size(width * 0.46f, r * 1.0f), alpha = alpha)
}
private fun DrawScope.drawRain(t: Float, seeds: Seeds, count: Int, speed: Float, length: Float, dark: Boolean) {
val slant = size.height * 0.12f
val color = if (dark) Color(0x88B8C8E0) else Color(0xAAE6F0FF)
for (i in 0 until count.coerceAtMost(Seeds.MAX)) {
val fall = wrap(seeds.y[i] + t * 0.9f * speed * seeds.speed[i])
val y = fall * (size.height * (1f + length)) - size.height * length
val x = wrap(seeds.x[i] + fall * slant / size.width) * size.width
val len = size.height * length * (0.6f + seeds.size[i] * 0.6f)
drawLine(color, Offset(x, y), Offset(x - slant * len / size.height, y + len), strokeWidth = 1.6f, cap = StrokeCap.Round)
}
}
private fun DrawScope.drawSnow(t: Float, seeds: Seeds) {
for (i in 0 until 90) {
val fall = wrap(seeds.y[i] + t * 0.06f * seeds.speed[i])
val sway = sin(t * 0.8f * seeds.speed[i] + seeds.phase[i]) * size.width * 0.015f
val x = wrap(seeds.x[i]) * size.width + sway
drawCircle(Color.White, 1.5f + seeds.size[i] * 3f, Offset(x, fall * size.height), alpha = 0.55f + seeds.size[i] * 0.35f)
}
}
private fun DrawScope.drawFog(t: Float, isDay: Boolean, dark: Boolean) {
val alpha = when {
dark -> 0.08f
isDay -> 0.28f
else -> 0.12f
}
for (i in 0 until 6) {
val y = size.height * (0.15f + i * 0.14f)
val drift = sin(t * 0.05f + i) * size.width * 0.08f
drawRect(
Brush.horizontalGradient(
listOf(Color.Transparent, Color.White, Color.White, Color.Transparent),
startX = -size.width * 0.2f + drift, endX = size.width * 1.2f + drift,
),
Offset(0f, y), Size(size.width, size.height * 0.07f), alpha = alpha,
)
}
}
/** A flash every eight seconds or so, sharp and then fading, with a weaker second flicker. */
private fun DrawScope.drawLightning(t: Float) {
val cycle = t % 8f
val flash = when {
cycle < 0.12f -> 1f - cycle / 0.12f
cycle in 0.25f..0.33f -> 0.5f * (1f - (cycle - 0.25f) / 0.08f)
else -> 0f
}
if (flash > 0f) drawRect(Color.White, alpha = flash * 0.35f)
}
@@ -323,6 +323,8 @@ private fun GroupRows(group: CarGroup) {
Choice(stringResource(R.string.car_map_day_night), nightChoices(), night, CarSettings::setNightMode)
SettingsDivider()
Choice(stringResource(R.string.car_units), unitChoices(), units, CarSettings::setUnits)
SettingsDivider()
WallpaperRows()
}
CarGroup.NAV_BAR -> {
Choice(stringResource(R.string.car_nav_bar), navBarChoices(), navBar, CarSettings::setNavBarMode)
@@ -637,3 +639,61 @@ internal fun CarAudioSection() {
)
}
}
/** The Apps page background: off, the driver's photos, or the weather drawn live. */
@Composable
private fun WallpaperRows() {
val context = androidx.compose.ui.platform.LocalContext.current
androidx.compose.runtime.remember(context) { app.seb3thehacker.gearslip.car.CarWallpaper.init(context) }
val mode by app.seb3thehacker.gearslip.car.CarWallpaper.mode.collectAsState()
Choice(
stringResource(R.string.car_wallpaper),
listOf(
app.seb3thehacker.gearslip.car.CarWallpaper.Mode.OFF to stringResource(R.string.choice_wallpaper_off),
app.seb3thehacker.gearslip.car.CarWallpaper.Mode.PHOTO to stringResource(R.string.choice_wallpaper_photo),
app.seb3thehacker.gearslip.car.CarWallpaper.Mode.WEATHER to stringResource(R.string.choice_wallpaper_weather),
),
mode,
{ app.seb3thehacker.gearslip.car.CarWallpaper.setMode(context, it) },
note = stringResource(R.string.car_wallpaper_note),
)
if (mode == app.seb3thehacker.gearslip.car.CarWallpaper.Mode.PHOTO) {
SettingsDivider()
WallpaperPhotoRow(dark = false)
SettingsDivider()
WallpaperPhotoRow(dark = true)
}
}
@Composable
private fun WallpaperPhotoRow(dark: Boolean) {
val context = androidx.compose.ui.platform.LocalContext.current
val scope = androidx.compose.runtime.rememberCoroutineScope()
val version by app.seb3thehacker.gearslip.car.CarWallpaper.photos.collectAsState()
val chosen = androidx.compose.runtime.remember(version, dark) { app.seb3thehacker.gearslip.car.CarWallpaper.hasPhoto(context, dark) }
val pick = androidx.activity.compose.rememberLauncherForActivityResult(
androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia(),
) { uri ->
if (uri != null) scope.launch(kotlinx.coroutines.Dispatchers.IO) {
app.seb3thehacker.gearslip.car.CarWallpaper.savePhoto(context, uri, dark)
}
}
SettingsRow(
stringResource(if (dark) R.string.car_wallpaper_dark else R.string.car_wallpaper_light),
Modifier.clickable {
pick.launch(
androidx.activity.result.PickVisualMediaRequest(
androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageOnly,
),
)
},
subtitle = stringResource(if (chosen) R.string.car_wallpaper_chosen else R.string.car_wallpaper_none),
trailing = if (chosen) {
{
androidx.compose.material3.TextButton(onClick = { app.seb3thehacker.gearslip.car.CarWallpaper.removePhoto(context, dark) }) {
Text(stringResource(R.string.car_wallpaper_remove))
}
}
} else null,
)
}
+10
View File
@@ -737,6 +737,16 @@
<string name="web_plain_http_stop">Don\'t warn for this site</string>
<string name="web_plain_http_hide">Hide warning</string>
<string name="launcher_phone_apps">Phone apps</string>
<string name="car_wallpaper">Apps page background</string>
<string name="car_wallpaper_note">Live weather shows the sky outside, with sun, clouds, rain, snow, fog or stars as they happen.</string>
<string name="choice_wallpaper_off">Off</string>
<string name="choice_wallpaper_photo">Photo</string>
<string name="choice_wallpaper_weather">Live weather</string>
<string name="car_wallpaper_light">Photo for light mode</string>
<string name="car_wallpaper_dark">Photo for dark mode</string>
<string name="car_wallpaper_none">None yet. Tap to pick one.</string>
<string name="car_wallpaper_chosen">Tap to change it.</string>
<string name="car_wallpaper_remove">Remove</string>
<string name="native_app_failed">This app couldn\'t open on the car screen. Check that Shizuku is running, then try again.</string>
<string name="shizuku_set_up">Set up with Shizuku</string>
<string name="shizuku_working">Setting up…</string>