diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAppStage.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAppStage.kt index 9680eb9..897f8ae 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAppStage.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAppStage.kt @@ -178,7 +178,9 @@ private fun AppSurface( modifier.onGloballyPositioned { bounds = it.boundsInWindow() }, ) { val epoch by connection.surfaceEpoch.collectAsState() - androidx.compose.runtime.key(constraints.maxWidth, constraints.maxHeight, epoch) { + // A new size for map apps takes a new surface, as a new size of screen does. + val mapSize by CarSettings.mapAppSize.collectAsState() + androidx.compose.runtime.key(constraints.maxWidth, constraints.maxHeight, epoch, mapSize) { SurfaceHost(connection, frame, onSize = { size = it }, Modifier.fillMaxSize()) } } @@ -243,7 +245,7 @@ private fun SurfaceHost( ) { GearslipLog.i("stage: surface ready at ${width}x$height") onSize(width to height) - connection.attachSurface(holder.surface, width, height, frame.densityDpi) + connection.attachSurface(holder.surface, width, height, CarSettings.mapAppDensity(frame.densityDpi, height)) } override fun surfaceDestroyed(holder: SurfaceHolder) { diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarSettings.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarSettings.kt index 4f80dcc..ee7a4e1 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarSettings.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarSettings.kt @@ -68,6 +68,7 @@ object CarSettings { private const val KEY_SCALE = "car_scale" private const val KEY_DENSITY = "car_density" + private const val KEY_MAP_SIZE = "car_map_size" private const val KEY_OPEN = "car_open_on_connect" private const val KEY_NIGHT = "car_night_mode" private const val KEY_PIPE_AUDIO = "car_pipe_audio" @@ -127,6 +128,7 @@ private const val KEY_CLUSTER_VIDEO = "cluster_video_enabled" private val scaleFlow = MutableStateFlow(1f) private val carDensityFlow = MutableStateFlow(DENSITY_FROM_CAR) + private val mapSizeFlow = MutableStateFlow(MAP_SIZE_AUTO) private val openFlow = MutableStateFlow(HOME) private val nightFlow = MutableStateFlow(NightMode.AUTO) private val pipeAudioFlow = MutableStateFlow(true) @@ -362,6 +364,26 @@ private const val KEY_CLUSTER_VIDEO = "cluster_video_enabled" /** The density to draw at, for a car or preview that would otherwise use [carDpi]. */ fun effectiveDensity(carDpi: Int) = carDensityFlow.value.takeIf { it != DENSITY_FROM_CAR } ?: carDpi + + /** [mapAppSize] when Gearslip picks: smaller on a short screen, as drawn elsewhere. */ + const val MAP_SIZE_AUTO = 0 + val MAP_SIZES = listOf(100, 90, 80, 70, 60) + private const val MAP_MIN_HEIGHT_DP = 320 + + /** How big map apps draw their own buttons and text: a percent of the car UI's density, or [MAP_SIZE_AUTO]. */ + val mapAppSize: StateFlow = mapSizeFlow + + /** + * The density to lend a map app a [heightPx]-tall surface at, when the car UI draws at [dpi]. + * Automatic shrinks it only until the map is [MAP_MIN_HEIGHT_DP] tall, and no further than + * the smallest size: a single-DIN SOOEMAKA left Google Maps 226dp, and its own buttons and + * search bar filled most of it while Gearslip's nav bar looked right. + */ + fun mapAppDensity(dpi: Int, heightPx: Int): Int { + val percent = mapSizeFlow.value + if (percent != MAP_SIZE_AUTO) return dpi * percent / 100 + return (heightPx * 160 / MAP_MIN_HEIGHT_DP).coerceIn(dpi * MAP_SIZES.last() / 100, dpi) + } val openOnConnect: StateFlow = openFlow val nightMode: StateFlow = nightFlow @@ -489,6 +511,7 @@ private const val KEY_CLUSTER_VIDEO = "cluster_video_enabled" app = context.applicationContext scaleFlow.value = AppSettings.getString(app, KEY_SCALE, "1.0").toFloatOrNull() ?: 1f carDensityFlow.value = AppSettings.getString(app, KEY_DENSITY, "").toIntOrNull()?.takeIf { it in DENSITIES || it in CUSTOM_DENSITIES } ?: DENSITY_FROM_CAR + mapSizeFlow.value = AppSettings.getString(app, KEY_MAP_SIZE, "").toIntOrNull()?.takeIf { it in MAP_SIZES } ?: MAP_SIZE_AUTO openFlow.value = AppSettings.getString(app, KEY_OPEN, HOME) lastNavFlow.value = AppSettings.getString(app, KEY_LAST_NAV, "").ifEmpty { null } lastMediaFlow.value = AppSettings.getString(app, KEY_LAST_MEDIA, "").ifEmpty { null } @@ -572,6 +595,11 @@ private const val KEY_CLUSTER_VIDEO = "cluster_video_enabled" AppSettings.putString(app, KEY_DENSITY, dpi.toString()) } + fun setMapAppSize(percent: Int) { + mapSizeFlow.value = percent + AppSettings.putString(app, KEY_MAP_SIZE, percent.toString()) + } + fun setOpenOnConnect(id: String) { openFlow.value = id AppSettings.putString(app, KEY_OPEN, id) diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/ui/HomeCarSettings.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/ui/HomeCarSettings.kt index bc3f2b3..daef862 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/ui/HomeCarSettings.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/ui/HomeCarSettings.kt @@ -94,6 +94,11 @@ private fun densityChoices(current: Int): List> { (DENSITY_CUSTOM to stringResource(R.string.choice_density_custom)) } +@Composable +private fun mapSizeChoices(): List> = + listOf(CarSettings.MAP_SIZE_AUTO to stringResource(R.string.choice_map_size_auto)) + + CarSettings.MAP_SIZES.map { it to stringResource(R.string.choice_map_size_percent, it) } + /** Types in any density in [CarSettings.CUSTOM_DENSITIES] (issue #82). */ @Composable private fun CustomDensityDialog(current: Int, onDismiss: () -> Unit, onSave: (Int) -> Unit) { @@ -354,6 +359,7 @@ internal fun CarGroupScreen(group: CarGroup, onBack: () -> Unit) { private fun GroupRows(group: CarGroup) { val scale by CarSettings.scale.collectAsState() val density by CarSettings.carDensity.collectAsState() + val mapSize by CarSettings.mapAppSize.collectAsState() val open by CarSettings.openOnConnect.collectAsState() val night by CarSettings.nightMode.collectAsState() val units by CarSettings.units.collectAsState() @@ -403,6 +409,11 @@ private fun GroupRows(group: CarGroup) { } } SettingsDivider() + Choice( + stringResource(R.string.car_map_size), mapSizeChoices(), mapSize, CarSettings::setMapAppSize, + note = stringResource(R.string.car_map_size_note), + ) + SettingsDivider() Choice(stringResource(R.string.car_app_theme), themeChoices(), theme, CarSettings::setAppTheme) SettingsDivider() if (theme != AppTheme.LIGHT) { diff --git a/gearslip/src/main/res/values/strings.xml b/gearslip/src/main/res/values/strings.xml index d1eacc3..b2c7d19 100644 --- a/gearslip/src/main/res/values/strings.xml +++ b/gearslip/src/main/res/values/strings.xml @@ -208,6 +208,10 @@ From car %1$d DPI Custom… + Map app size + The buttons and text that map apps draw themselves. Automatic shrinks them on a short screen and leaves others alone. + Automatic + %1$d%% App theme Dark mode Buttons diff --git a/gearslip/src/test/java/app/seb3thehacker/gearslip/car/MapAppSizeTest.kt b/gearslip/src/test/java/app/seb3thehacker/gearslip/car/MapAppSizeTest.kt new file mode 100644 index 0000000..544bd5f --- /dev/null +++ b/gearslip/src/test/java/app/seb3thehacker/gearslip/car/MapAppSizeTest.kt @@ -0,0 +1,48 @@ +package app.seb3thehacker.gearslip.car + +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test + +class MapAppSizeTest { + private val context = InMemoryContext() + + @Before + fun startOnAutomatic() { + CarSettings.init(context) + CarSettings.setMapAppSize(CarSettings.MAP_SIZE_AUTO) + } + + @Test + fun automaticLeavesAnOrdinaryScreenAlone() { + // 800x480 at 160 with the nav bar over it: the map is 424 tall. + assertEquals(160, CarSettings.mapAppDensity(160, 424)) + assertEquals(240, CarSettings.mapAppDensity(240, 650)) + } + + @Test + fun automaticShrinksAShortScreenUntilTheMapIs320dpTall() { + // The SOOEMAKA's map, squashed and above the nav bar: 240 px at 170 dpi. + assertEquals(120, CarSettings.mapAppDensity(170, 240)) + } + + @Test + fun automaticStopsAtTheSmallestSize() { + assertEquals(102, CarSettings.mapAppDensity(170, 100)) + } + + @Test + fun aChosenSizeIsAPercentOfTheCarUi() { + for (percent in CarSettings.MAP_SIZES) { + CarSettings.setMapAppSize(percent) + assertEquals(200 * percent / 100, CarSettings.mapAppDensity(200, 240)) + } + } + + @Test + fun theChoiceOutlivesARestart() { + CarSettings.setMapAppSize(70) + CarSettings.init(context) + assertEquals(70, CarSettings.mapAppSize.value) + } +}