CarClient

class CarClient(context: Context, coroutineScope: CoroutineScope)

Safe client for the Car service.

This class keeps track of the Car service health as long as the coroutineScope is active.

As soon as the Car service becomes available, CarClient will execute all the actions specified with onConnected. After a Car service crash, onConnected actions will be executed again. Every time the service crashes, and also when CarClient is stopped, it will execute all the actions specified with onDisconnected. If the service is not available at all, for example if running under Build.VERSION_CODES.R or with a non-automotive Android version, the client will execute all the actions specified with onFailed. Additional one-time actions can be executed with postOnCar which guarantees they are either run with a valid Car service connection, or not executed at all.

Unless otherwise noted, callbacks are executed in the main thread. When the coroutineScope is destroyed, CarClient must be disposed of.

Requires at least Android Build.VERSION_CODES.R, otherwise the client will directly call the onFailed actions then stay dormant.

Constructors

Link copied to clipboard
constructor(context: Context, coroutineScope: CoroutineScope)

Functions

Link copied to clipboard
fun onConnected(block: suspend Car.() -> Unit): CarClient

Executes block every time the Car service is connected or reconnected.

Link copied to clipboard
fun onDisconnected(block: suspend Car?.() -> Unit): CarClient

Executes block every time the Car service is disconnected.

Link copied to clipboard
fun onFailed(block: () -> Unit): CarClient

Executes block when the Car service fails.

Link copied to clipboard
fun postOnCar(block: suspend Car.() -> Unit)

Executes block asynchronously with a connected Car service.