package com.failure.vanta.home import android.app.AlarmManager import android.content.ComponentName import android.content.Context import android.content.pm.ActivityInfo import android.content.pm.ApplicationInfo import android.content.pm.LauncherActivityInfo import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.LinearGradient import android.graphics.Paint import android.graphics.RadialGradient import android.graphics.Shader import android.media.AudioManager import android.media.session.MediaSession import android.os.Process import android.os.SystemClock import android.os.UserHandle import androidx.compose.runtime.Composable import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.MutableState import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.asImageBitmap import androidx.activity.ComponentActivity import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.swipeDown import androidx.compose.ui.test.swipeLeft import androidx.compose.ui.test.swipeUp import androidx.test.core.app.ApplicationProvider import com.failure.vanta.core.VantaAuras import com.failure.vanta.core.VantaTheme import com.failure.vanta.home.cover.CoverHome import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import org.robolectric.annotation.GraphicsMode import java.io.File import java.util.Calendar /** * Preview renders (tools/preview/build.sh), not a regression gate: the real Compose * code with a HomeState filled by hand, so the cover and home can be looked at on Zo. */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(sdk = [35]) class HomeShots { @get:Rule val rule = createAndroidComposeRule() private val ctx: Context get() = ApplicationProvider.getApplicationContext() private val me: UserHandle = Process.myUserHandle() private fun HomeState.put(name: String, v: Any?) { val f = HomeState::class.java.getDeclaredField("$name\$delegate").apply { isAccessible = true } @Suppress("UNCHECKED_CAST") (f.get(this) as MutableState).value = v } private fun at(h: Int, m: Int): Long = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, h); set(Calendar.MINUTE, m); set(Calendar.SECOND, 12) }.timeInMillis private fun art(): Bitmap { val b = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888) val c = Canvas(b) c.drawPaint(Paint().apply { shader = LinearGradient(0f, 0f, 256f, 256f, 0xFF7A5CFF.toInt(), 0xFF05080A.toInt(), Shader.TileMode.CLAMP) }) c.drawCircle(170f, 90f, 80f, Paint().apply { shader = RadialGradient(170f, 90f, 80f, 0xFF35E0FF.toInt(), 0x0035E0FF, Shader.TileMode.CLAMP) }) return b } private fun entry(label: String, pkg: String): AppEntry? = try { val ai = ActivityInfo().apply { name = "$pkg.Main"; packageName = pkg; nonLocalizedLabel = label applicationInfo = ApplicationInfo().apply { packageName = pkg; nonLocalizedLabel = label; uid = Process.myUid() } } // Hidden constructors differ by release: fill every parameter by its type. fun arg(t: Class<*>): Any? = when { t == Context::class.java -> ctx t == ActivityInfo::class.java -> ai t == UserHandle::class.java -> me t.name == "android.content.pm.LauncherActivityInfoInternal" -> t.declaredConstructors.maxBy { it.parameterCount } .apply { isAccessible = true }.let { c -> c.newInstance(*c.parameterTypes.map { arg(it) }.toTypedArray()) } t == Boolean::class.javaPrimitiveType -> false t == Int::class.javaPrimitiveType -> 0 t == Long::class.javaPrimitiveType -> 0L t == Float::class.javaPrimitiveType -> 0f else -> null } val ctor = LauncherActivityInfo::class.java.declaredConstructors.maxBy { it.parameterCount }.apply { isAccessible = true } val info = ctor.newInstance(*ctor.parameterTypes.map { arg(it) }.toTypedArray()) as LauncherActivityInfo AppEntry(label, ComponentName(pkg, "$pkg.Main"), me, info, "$pkg/.Main@0") } catch (t: Throwable) { System.err.println("preview: no LauncherActivityInfo for $pkg: $t") null } private fun state(panelFirst: String = "clock", prefs: HomePrefs = HomePrefs()): HomeState { val s = HomeState(ctx) val now = at(21, 35) s.put("now", now) s.put("battery", Battery(76, true)) s.put("alarm", AlarmManager.AlarmClockInfo(at(7, 0) + 24 * 3_600_000L, null)) s.put("media", MediaInfo( "Midnight Circuit", "Kaito Arakawa", art().asImageBitmap(), true, MediaSession(ctx, "preview").controller, position = 83_000, positionAt = SystemClock.elapsedRealtime(), duration = 214_000, )) s.put("notifApps", listOf( NotifApp("com.android.messaging", me, 3), NotifApp("com.android.email", me, 1), NotifApp("com.android.calendar", me, 1), )) s.put("notifs", listOf( NotifEntry("a", "com.android.messaging", me, "Mika", "the box finished syncing, ready when you are", now - 120_000, true, null), NotifEntry("b", "com.android.messaging", me, "Rin", "dinner at 8?", now - 30 * 60_000, true, null), NotifEntry("c", "com.android.email", me, "Vanta build", "GSI passed its boot gates", now - 2 * 3_600_000, true, null), NotifEntry("d", "com.android.calendar", me, "Standup", "Tomorrow 09:30", now - 5 * 3_600_000, false, null), )) s.put("dnd", true) s.put("ringer", AudioManager.RINGER_MODE_VIBRATE) val apps = listOfNotNull( entry("Camera", "org.lineageos.aperture"), entry("Messages", "com.android.messaging"), entry("Music", "org.lineageos.twelve"), entry("Maps", "net.osmand"), entry("Browser", "org.lineageos.jelly"), entry("Clock", "com.android.deskclock"), entry("Calendar", "com.android.calendar"), entry("Settings", "com.android.settings"), ) s.put("appList", apps) s.put("coverApps", apps) s.put("dock", apps.take(prefs.dockSlots).map { it as AppEntry? }) val ids = prefs.coverPanels s.put("prefs", prefs.copy(coverPanels = listOf(panelFirst) + ids.filter { it != panelFirst })) return s } private fun shot(name: String, content: @Composable () -> Unit) { rule.mainClock.autoAdvance = false // Transparent window: tools/preview/compose.py puts the Liquid wallpaper behind and the Rail on top, // the way the phone layers them (wallpaper service, this window, SystemUI's navigation handle). rule.runOnUiThread { rule.activity.window.setBackgroundDrawable(android.graphics.drawable.ColorDrawable(0)) } rule.setContent { VantaTheme(aura = VantaAuras.default) { content() } } rule.mainClock.advanceTimeBy(2_500) grab(File(System.getProperty("vanta.shots") ?: "build", "$name.png")) } // The paused clock (aurora and seconds never settle) means no frame for captureToImage // to wait on, so draw the window ourselves. private fun grab(f: File) { val v = rule.activity.window.decorView val bmp = Bitmap.createBitmap(v.width, v.height, Bitmap.Config.ARGB_8888) rule.runOnUiThread { v.draw(Canvas(bmp)) } f.parentFile?.mkdirs() f.outputStream().use { bmp.compress(Bitmap.CompressFormat.PNG, 100, it) } } /** [frames] frames at 30 fps into shots/frames//; [at] runs gestures at given frame numbers. */ private fun record(name: String, frames: Int, at: Map Unit>, content: @Composable () -> Unit) { rule.mainClock.autoAdvance = false rule.runOnUiThread { rule.activity.window.setBackgroundDrawable(android.graphics.drawable.ColorDrawable(0)) } rule.setContent { VantaTheme(aura = VantaAuras.default) { content() } } val dir = File(System.getProperty("vanta.shots") ?: "build", "frames/$name") dir.deleteRecursively() for (i in 0 until frames) { at[i]?.invoke() rule.mainClock.advanceTimeBy(33) grab(File(dir, "%04d.png".format(i))) } } private fun swipe(dir: String) = rule.onRoot().performTouchInput { when (dir) { "left" -> swipeLeft(startX = right * 0.85f, endX = left + width * 0.15f, durationMillis = 260) "up" -> swipeUp(startY = bottom * 0.8f, endY = top + height * 0.25f, durationMillis = 260) else -> swipeDown(startY = top + height * 0.2f, endY = bottom * 0.85f, durationMillis = 260) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun videoCover() = state("clock", HomePrefs(coverSeconds = true)).let { s -> record("cover", 330, mapOf(60 to { swipe("left") }, 125 to { swipe("left") }, 190 to { swipe("left") }, 255 to { swipe("left") })) { CoverHome(s) } } @Test @Config(qualifiers = "w412dp-h1006dp-xxhdpi") fun videoHome() = state(prefs = HomePrefs(drawerKeyboard = false)).let { s -> record("home", 210, mapOf(75 to { swipe("up") }, 150 to { swipe("down") })) { HomeScreen(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverClockSplit() = state("clock").let { s -> shot("cover-clock-split") { CoverHome(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverClockOrbit() = state("clock", HomePrefs(coverClock = "orbit", coverSeconds = true, coverBackground = "aurora")).let { s -> shot("cover-clock-orbit") { CoverHome(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverClockStack() = state("clock", HomePrefs(coverClock = "stack", coverBackground = "black")).let { s -> shot("cover-clock-stack") { CoverHome(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverNotifications() = state("notifications").let { s -> shot("cover-notifications") { CoverHome(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverMedia() = state("media").let { s -> shot("cover-media") { CoverHome(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverControls() = state("controls").let { s -> shot("cover-controls") { CoverHome(s) } } @Test @Config(qualifiers = "w250dp-h260dp-xhdpi") fun coverApps() = state("apps").let { s -> shot("cover-apps") { CoverHome(s) } } @Test @Config(qualifiers = "w412dp-h1006dp-xxhdpi") fun homeHero() = state().let { s -> shot("home-hero") { HomeScreen(s) } } @Test @Config(qualifiers = "w412dp-h1006dp-xxhdpi") fun homeSplit() = state(prefs = HomePrefs(clockStyle = "split", greeting = true, dockSlots = 5, dockLabels = true)).let { s -> shot("home-split") { HomeScreen(s) } } @Test @Config(qualifiers = "w412dp-h1006dp-xxhdpi") fun homeMinimal() = state(prefs = HomePrefs(clockStyle = "minimal", cards = listOf("clock", "media", "notifications", "glance"))).let { s -> shot("home-minimal") { HomeScreen(s) } } }