Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC - RNG Emulator #387

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions emulator-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
38 changes: 38 additions & 0 deletions emulator-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@file:Suppress("UnstableApiUsage")

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
id("com.squareup.wire") version "4.9.9"
}

group = "com.github.takahirom.roborazzi.emulator.client"
version = "1.0-SNAPSHOT"

dependencies {
api(libs.grpc.protobuf)
implementation(libs.grpc.kotlin.stub)
implementation(libs.wire.grpc.client)

implementation(project(":emulator-proto"))
protoPath(project(":emulator-proto"))
}

wire {
sourcePath {
srcProject(":emulator-proto")
}

kotlin {
rpcRole = "client"
singleMethodServices = false
rpcCallStyle = "suspending"
}
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs += "-Xcontext-receivers"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.takahirom.roborazzi.emulator

import com.android.emulator.control.EmulatorControllerClient
import com.android.emulator.control.ImageFormat
import com.squareup.wire.GrpcClient
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okio.FileSystem
import okio.Path.Companion.toPath
import roborazzi.emulator.ComposePreview
import roborazzi.emulator.RoborazziClient

suspend fun main() {
val port = 8080
val grpcClient = GrpcClient.Builder()
.client(
OkHttpClient.Builder()
.protocols(listOf(Protocol.H2_PRIOR_KNOWLEDGE))
.build())
.baseUrl("http://localhost:$port")
.build()

val roborazziService = grpcClient.create(RoborazziClient::class)
val emulatorControllerClient = grpcClient.create(EmulatorControllerClient::class)

val metadata = mapOf("instance" to "1")

println("--> getGps")
val gpsCall = emulatorControllerClient.getGps()
gpsCall.requestMetadata = metadata
println(gpsCall.execute(Unit))

println("--> launchComposePreview")
val previewCall = roborazziService.launchComposePreview()
previewCall.requestMetadata = metadata
previewCall.execute(ComposePreview(previewMethod = "com.github.takahirom.roborazzi.emulator.previews.SimplePreviewKt.SimplePreview"))

println("--> getScreenshot")
val screenshotCall = emulatorControllerClient.getScreenshot()
screenshotCall.requestMetadata = metadata
val image = screenshotCall.execute(ImageFormat())
println(image)
val path = "emulator-client/build/SimplePreview.png".toPath()
FileSystem.SYSTEM.write(path) {
write(image.image)
}
println("Wrote: $path")
}
1 change: 1 addition & 0 deletions emulator-proto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
18 changes: 18 additions & 0 deletions emulator-proto/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@file:Suppress("UnstableApiUsage")

plugins {
kotlin("jvm")
id("com.squareup.wire") version "4.9.9"
}

group = "com.github.takahirom.roborazzi.emulator.proto"
version = "1.0-SNAPSHOT"

dependencies {
api(libs.grpc.protobuf)
api(libs.wire.grpc.client)
}

wire {
protoLibrary = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.takahirom.roborazzi.emulator

import io.grpc.Context
import io.grpc.Metadata
import io.grpc.Metadata.ASCII_STRING_MARSHALLER

object GrpcMetadata {
val instanceMetadataKey = Metadata.Key.of("instance", ASCII_STRING_MARSHALLER)
val instanceContextKey = Context.key<String>("instance")
}
Loading
Loading