SearchProvider
Interface for providing search functionality to Android Auto.
This interface abstracts the search functionality, allowing different apps to provide their own implementations while sharing the same Android Auto integration code. It follows the same pattern as MapController.
Purpose
Abstraction: Decouples Android Auto module from specific search implementations
Reusability: Allows the Android Auto module to be used by different apps
MSDK Integration: Designed to be migrated into the MSDK in the future
Implementation Notes
The implementation is responsible for:
Calling the geocoder API for autocomplete suggestions
Filtering favorites matching the query
Converting results to SearchResultItem (clean type with no app dependencies)
Handling search debouncing (recommended: 500ms delay)
Managing the search state via searchState StateFlow
Example Implementation
class SearchProviderImpl(
private val autocompleteUseCase: AutocompleteSearchUseCase,
private val favoritesUseCase: FavoriteAddressesUseCases,
private val getLastKnownLocation: GetLastKnownLocation
) : SearchProvider {
private val _searchState = MutableStateFlow(SearchProviderState())
override val searchState: StateFlow<SearchProviderState> = _searchState.asStateFlow()
private var searchJob: Job? = null
override fun search(query: String) {
searchJob?.cancel()
if (query.isBlank()) {
_searchState.value = SearchProviderState()
return
}
_searchState.value = _searchState.value.copy(query = query, isLoading = true)
searchJob = scope.launch {
delay(500L) // Debounce
performSearch(query)
}
}
override fun clearSearch() {
searchJob?.cancel()
_searchState.value = SearchProviderState()
}
}