AndroidAutoStateHolder

Injectable state holder for Android Auto cross-module communication.

This class orchestrates all Android Auto state through three focused sub-holders and the shared AndroidAutoSurfaceHolder:

Architecture

This class is the "real implementation" behind AndroidAutoSurfaceManager. The pattern is:

┌─────────────────────────────────────────────────────────────┐
│ App Module │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AndroidAutoStateHolder (Hilt @Singleton) │ │
│ │ - Orchestrates sub-holders + surface holder │ │
│ │ - Injected into MainActivity, ViewModels │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ │ Registered at app startup │
│ ↓ │
└─────────────────────────────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────┐
│ Android Auto MSDK Module │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AndroidAutoSurfaceManager (object - Facade) │ │
│ │ - Thin facade providing static access │ │
│ │ - Delegates all operations to StateHolder │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
│ Static access (system-created components)
↓
┌─────────────────────────────────────────────────────────────┐
│ Android Auto Module │
│ - AndroidAutoSession (created by system, can't use DI) │
│ - AndroidAutoSurfaceCallback (created by system) │
│ - AA Screens (MainScreen, SearchScreen, etc.) │
└─────────────────────────────────────────────────────────────┘

Cross-Holder Wiring

The sub-holders are mostly independent, with two cross-cutting concerns wired via lambdas:

  1. Phone state → loading state: When the phone reports PhoneAppState.ONBOARDING, AAConnectionState needs to set loading state to AndroidAutoLoadingState.ONBOARDING_REQUIRED on AAScreenStateHolder. Wired via AAConnectionState.onPhoneStateRequiresLoadingUpdate.

  2. Screen state → phone callback: When the AA screen state changes, AAScreenStateHolder needs to notify PhoneAppCallback.onAAScreenStateChanged. The callback is owned by AAConnectionState. Wired via AAScreenStateHolder.onAAScreenStateChanged.

Both lambdas capture this references that are only resolved at invocation time (never during construction), so the initialization order is safe.

Thread Safety

Thread safety is handled by the individual sub-holders:

This orchestrator adds no additional synchronization.

Usage

In app module (MainActivity):

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
androidAutoStateHolder.setPhoneAppCallback(phoneAppCallback)
}
}

In androidauto module (via facade):

class AndroidAutoSession : Session() {
override fun onCreateScreen(intent: Intent): Screen {
val holder = AndroidAutoSurfaceManager.getHolder()
}
}

See also

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

StateFlow representing the current Android Auto screen state.

Link copied to clipboard

Connection state sub-holder managing AA connection lifecycle, phone state, and callbacks.

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

Map state sub-holder managing camera, zoom, dark mode, split screen, and reattachment.

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

Screen state sub-holder managing AA screen state, loading lifecycle, and guidance session.

Link copied to clipboard

StateFlow providing the horizontal center of the visible area as a fraction.

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

Gets the phone's equivalent AA screen state.

Link copied to clipboard

Gets the current phone app state.

Link copied to clipboard
fun handleZoom(zoomIn: Boolean)

Handles a zoom operation by delegating to the registered zoom handler.

Link copied to clipboard

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

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

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

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.