kotlinsurfaceandroid-auto

How to draw content to an AndroidAuto Surface


I am developing a simple Kotlin app for AndroidAuto, it is a navigation app without routes that uses a custom georeferenced image as map. Im already familiar with Screens and running the service on the Desktop Head Unit, i also have managed to insert a NavigationTemplate to one of my Screens, the problem is that i don't know how to insert any content on this surface beyond buttons. It would be nice if someone knew a way to plot a map or anything else on this surface.

class MapScreen(carContext: CarContext) : Screen(carContext), SurfaceCallback{

    init {
        Log.d("HelloMapScreen", "Register surface callback")
        carContext.getCarService(AppManager::class.java).setSurfaceCallback(this)
    }
    override fun onGetTemplate(): Template {

        return NavigationTemplate.Builder()
            .setActionStrip(ActionStrip.Builder()
                .addAction(Action.BACK)
                .addAction(Action.APP_ICON)
                .addAction(Action.PAN)
                .addAction(Action.Builder().setTitle("SomeButton")
                    .build())
                .build()
            )
            .setMapActionStrip(ActionStrip.Builder()
                .addAction(Action.PAN)
                .build()
            )
            .build()
    }

    override fun onSurfaceAvailable(surfaceContainer: SurfaceContainer) {
        Log.d("HelloMapScreen", "Received a surface.")
        Log.d("Dpi", surfaceContainer.dpi.toString())
    }
}

This is my screen kotlin code, it renderizes a empty map layout on the emulator with no content (just the buttons, that disappear as if i was driving) i know the surface is ready to draw because the dpi is being returned on the logcat, but from this point i don't know how to proceed to insert a map or anything else on this template. Any help would be great.


Solution

  • Check out the navigation sample app code available on GitHub (or for a more up-to-date version, see the AndroidX version of it). The Open Street Maps repo's Android Auto directory may also be of interest.