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:
connection (AAConnectionState): AA connection lifecycle, phone state, bidirectional callbacks, phone app closing coordination
screen (AAScreenStateHolder): AA screen state, loading/initialization lifecycle with guard logic, guidance session state for mid-session sync
map (AAMapState): Map camera state, zoom handlers, dark mode, split screen, MapView reattachment
AndroidAutoSurfaceHolder (via setHolder/getHolder): MapController, MapViewDelegate, providers (search, guidance, quick access, settings), route display state
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:
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.
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:
AAConnectionState: AtomicBoolean, StateFlow, @Volatile
AAScreenStateHolder: @Volatile, StateFlow
AAMapState: StateFlow, @Volatile
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
Properties
StateFlow representing the current Android Auto screen state.
Connection state sub-holder managing AA connection lifecycle, phone state, and callbacks.
StateFlow of the day/night mode the user chose for the Android Auto map.
Flag indicating whether Android Auto is currently connected.
Flag indicating that Android Auto is in the process of disconnecting.
StateFlow tracking whether the map camera is following the user's GPS position.
StateFlow tracking the car head unit's current dark mode state.
StateFlow tracking whether the Android Auto display is in split screen mode.
Map state sub-holder managing camera, zoom, dark mode, split screen, and reattachment.
Callback interface for phone app to receive events from Android Auto.
StateFlow representing what AA screen the phone's current state maps to.
StateFlow representing the current phone app state.
Callback interface for phone app to send commands to Android Auto.
Screen state sub-holder managing AA screen state, loading lifecycle, and guidance session.
StateFlow providing the horizontal center of the visible area as a fraction.
Functions
Clears the current guidance session state.
Gets the current AA screen state.
Gets the current guidance destination title.
Gets the AndroidAutoSurfaceHolder instance.
Gets the current loading state.
Gets the phone's equivalent AA screen state.
Gets the current phone app state.
Handles a zoom operation by delegating to the registered zoom handler.
Fits the AA MapView camera to a geographic bounding box within the visible screen area.
Gets whether the current guidance session is in demo mode.
Checks if holder has been set.
Checks if the phone app is closing.
Requests the Android Auto MapView to be re-attached to Navigation.
Resets the AA screen state to DISCONNECTED.
Resets the loading state to IDLE.
Resets the phone state to UNKNOWN.
Sets the Android Auto connection state.
Sets the Android Auto disconnecting flag.
Sets the current Android Auto screen state.
Sets the camera following state.
Sets the car's dark mode state.
Sets the current guidance session state.
Sets the day/night mode the user chose for the Android Auto map.
Sets the AndroidAutoSurfaceHolder instance.
Sets the current loading state and notifies the listener.
Sets the callback to be invoked when the phone app is closing.
Registers the phone app callback to receive events from Android Auto.
Sets the phone app closing flag and triggers immediate Android Auto close.
Sets the phone's equivalent AA screen state.
Sets the current phone app state.
Registers the callback for phone-to-AA commands.
Sets the callback for MapView re-attachment.
Sets the split screen state.
Registers a listener to be notified of loading state changes.
Sets the horizontal center of the visible area as a fraction of the full surface.
Sets the callback for handling zoom operations on the AA MapView.
Sets the callback for fitting the AA MapView camera to a geographic bounding box.