AndroidAutoSurfaceManager

Facade providing static access to AndroidAutoStateHolder.

This object exists solely for cross-module access from the androidauto module, which cannot use Hilt injection because its components (Session, SurfaceCallback) are created by the Android Auto framework, not our code.

All state and business logic is delegated to the injected AndroidAutoStateHolder. This facade provides a clean separation between:

  • State ownership: AndroidAutoStateHolder (injectable, testable)

  • Static access: AndroidAutoSurfaceManager (facade for system-created components)

Architecture

┌─────────────────────────────────────────────────────────────┐
│ App Module │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AndroidAutoStateHolder (Hilt @Singleton) │ │
│ │ - All state and business logic │ │
│ │ - Injected into MainActivity, ViewModels │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ │ initialize() at app startup │
│ ↓ │
└─────────────────────────────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────┐
│ Android Auto MSDK Module │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AndroidAutoSurfaceManager (object - THIS) │ │
│ │ - Thin facade providing static access │ │
│ │ - Delegates all operations to StateHolder │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
│ Static access
↓
┌─────────────────────────────────────────────────────────────┐
│ Android Auto Module │
│ - AndroidAutoSession.onCreateScreen() { getHolder() } │
│ - AndroidAutoSurfaceCallback { setLoadingState() } │
│ - AA Screens { phoneState.collect() } │
└─────────────────────────────────────────────────────────────┘

Initialization

The facade supports two initialization paths to handle different launch scenarios:

Path 1: Phone app launches first (normal)

The Hilt module calls getOrCreateStateHolder which creates a new holder. When AA connects later, the facade is already initialized.

Path 2: Android Auto launches first (cold start from car)

The AA framework creates Session before Hilt runs. The facade auto-creates a default AndroidAutoStateHolder on first access. When the phone app's Hilt module starts later, getOrCreateStateHolder returns the existing instance, ensuring the same holder is used everywhere.

@Module
@InstallIn(SingletonComponent::class)
object AndroidAutoModule {
@Provides
@Singleton
fun provideAndroidAutoStateHolder(): AndroidAutoStateHolder {
// Reuses existing holder if AA auto-created one, otherwise creates new
return AndroidAutoSurfaceManager.getOrCreateStateHolder()
}
}

Usage

Preferred: Direct holder access (new code should use this):

// Access focused sub-holders for type-safe, domain-scoped state management
val connectionState = AndroidAutoSurfaceManager.connectionState
connectionState.setAAConnected(true)

val screenState = AndroidAutoSurfaceManager.screenState
screenState.setLoadingState(AndroidAutoLoadingState.MAP_READY)

val mapState = AndroidAutoSurfaceManager.mapState
mapState.setCameraFollowingUser(false)

Legacy: Flat method API (still works, delegates to sub-holders internally):

AndroidAutoSurfaceManager.setAAConnected(true)
AndroidAutoSurfaceManager.setLoadingState(AndroidAutoLoadingState.MAP_READY)

In app module (prefer direct injection over facade):

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var stateHolder: AndroidAutoStateHolder

// Use stateHolder.connection / .screen / .map directly
stateHolder.connection.setPhoneState(PhoneAppState.READY)
}

Thread Safety

Thread safety is handled by the underlying AndroidAutoStateHolder. This facade only delegates calls without additional synchronization.

See also

Properties

Link copied to clipboard

StateFlow representing the current Android Auto screen state.

Link copied to clipboard

Direct access to the connection state sub-holder.

Link copied to clipboard

StateFlow of the day/night mode the user chose for the Android Auto map.

Link copied to clipboard

Flag indicating whether Android Auto is currently connected.

Link copied to clipboard

Flag indicating that Android Auto is in the process of disconnecting.

Link copied to clipboard

StateFlow tracking whether the map camera is following the user's GPS position.

Link copied to clipboard
val isCarDarkMode: StateFlow<Boolean>

StateFlow tracking the car head unit's current dark mode state.

Link copied to clipboard
val isSplitScreen: StateFlow<Boolean>

StateFlow tracking whether the Android Auto display is in split screen mode.

Link copied to clipboard

Direct access to the map state sub-holder.

Link copied to clipboard

Callback interface for phone app to receive events from Android Auto.

Link copied to clipboard

StateFlow representing what AA screen the phone's current state maps to.

Link copied to clipboard
val phoneState: StateFlow<PhoneAppState>

StateFlow representing the current phone app state.

Link copied to clipboard

Callback interface for phone app to send commands to Android Auto.

Link copied to clipboard

Direct access to the screen state sub-holder.

Link copied to clipboard

StateFlow providing the horizontal center of the visible area as a fraction of the full MapView surface width.

Functions

Link copied to clipboard
fun clear()

Clears the holder reference (for cleanup).

Link copied to clipboard

Clears the current guidance session state.

Link copied to clipboard

Gets the current AA screen state.

Link copied to clipboard

Gets the current guidance destination title.

Link copied to clipboard

Gets the AndroidAutoSurfaceHolder instance.

Link copied to clipboard

Gets the current loading state.

Link copied to clipboard

Returns the existing state holder, or creates and registers a new one.

Link copied to clipboard

Gets the phone's equivalent AA screen state.

Link copied to clipboard

Gets the current phone app state.

Link copied to clipboard

Initializes the facade with the injected state holder.

Link copied to clipboard

Gets whether the current guidance session is in demo mode.

Link copied to clipboard

Checks if holder has been set.

Link copied to clipboard

Returns whether the facade has been initialized with a state holder.

Link copied to clipboard

Checks if the phone app is closing.

Link copied to clipboard

Requests the Android Auto MapView to be re-attached to Navigation.

Link copied to clipboard
fun reset()

Resets the facade state holder reference.

Link copied to clipboard

Resets the AA screen state to DISCONNECTED.

Link copied to clipboard

Resets the loading state to IDLE.

Link copied to clipboard

Resets the phone state to UNKNOWN.

Link copied to clipboard
fun setAAConnected(connected: Boolean)

Sets the Android Auto connection state.

Link copied to clipboard
fun setAADisconnecting(disconnecting: Boolean)

Sets the Android Auto disconnecting flag.

Link copied to clipboard

Sets the current Android Auto screen state.

Link copied to clipboard

Sets the camera following state.

Link copied to clipboard
fun setCarDarkMode(isDark: Boolean)

Sets the car's dark mode state.

Link copied to clipboard
fun setCurrentGuidanceState(destinationTitle: String, isDemoMode: Boolean)

Sets the current guidance session state.

Link copied to clipboard

Sets the day/night mode the user chose for the Android Auto map.

Link copied to clipboard

Sets the AndroidAutoSurfaceHolder instance.

Link copied to clipboard

Sets the current loading state and notifies the listener.

Link copied to clipboard
fun setOnCloseCallback(callback: () -> Unit?)

Sets the callback to be invoked when the phone app is closing.

Link copied to clipboard

Registers the phone app callback to receive events from Android Auto.

Link copied to clipboard

Sets the phone app closing flag and triggers immediate Android Auto close.

Link copied to clipboard

Sets the phone's equivalent AA screen state.

Link copied to clipboard

Sets the current phone app state.

Link copied to clipboard

Registers the callback for phone-to-AA commands.

Link copied to clipboard
fun setReattachMapViewCallback(callback: () -> Unit?)

Sets the callback for MapView re-attachment.

Link copied to clipboard
fun setSplitScreen(splitScreen: Boolean)

Sets the split screen state.

Link copied to clipboard

Registers a listener to be notified of loading state changes.

Link copied to clipboard

Sets the horizontal center of the visible area as a fraction of the full surface.

Link copied to clipboard
fun setZoomHandler(handler: (Boolean) -> Unit?)

Sets the callback for handling zoom operations on the AA MapView.

Link copied to clipboard
fun setZoomToRectHandler(handler: (GeoRect) -> Unit?)

Sets the callback for fitting the AA MapView camera to a geographic bounding box.

Link copied to clipboard
fun zoomIn()

Zooms in on the Android Auto MapView at the current camera position.

Link copied to clipboard
fun zoomOut()

Zooms out on the Android Auto MapView at the current camera position.

Link copied to clipboard
fun zoomToRect(rect: GeoRect)

Fits the AA MapView camera to a geographic bounding box within the visible screen area.