|
| 1 | +# Navigation for Android Auto |
| 2 | + |
| 3 | +This guide explains how to enable and integrate Android Auto with the Flutter Navigation SDK. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Android device |
| 8 | +- Android Auto test device or Android Automotive OS emulator |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +Refer to the [Android for Cars developer documentation](https://developer.android.com/training/cars) to understand how the Android Auto works and to complete the initial setup. Key steps include: |
| 13 | + |
| 14 | +- Installing Android for Cars App Library. |
| 15 | +- Configuring your app's manifest file to include Android Auto. |
| 16 | +- Declaring a minimum car-app level in your manifest. |
| 17 | +- Creating 'CarAppService' and session |
| 18 | + |
| 19 | +For all the steps above, you can refer to the Android example application for guidance. |
| 20 | + |
| 21 | +### Screen for Android Auto |
| 22 | + |
| 23 | +Once your project is configured accordingly, and you are ready to build the screen for Android Auto, you can leverage the `AndroidAutoBaseScreen` provided by the SDK. This base class simplifies the setup by handling initialization, teardown, and rendering the map on the Android Auto display. |
| 24 | + |
| 25 | +Please refer to the `SampleAndroidAutoScreen.kt` file in the Android example app for guidance. |
| 26 | + |
| 27 | +To customize the Android Auto experience, override the `onGetTemplate` method in your custom AndroidAutoScreen class, providing your own `Template`: |
| 28 | + |
| 29 | +```kotlin |
| 30 | +override fun onGetTemplate(): Template { |
| 31 | + /** ... */ |
| 32 | + @SuppressLint("MissingPermission") |
| 33 | + val navigationTemplateBuilder = |
| 34 | + NavigationTemplate.Builder() |
| 35 | + .setActionStrip( |
| 36 | + ActionStrip.Builder() |
| 37 | + .addAction( |
| 38 | + Action.Builder() |
| 39 | + .setTitle("Re-center") |
| 40 | + .setOnClickListener { |
| 41 | + if (mGoogleMap == null) return@setOnClickListener |
| 42 | + mGoogleMap!!.followMyLocation(GoogleMap.CameraPerspective.TILTED) |
| 43 | + } |
| 44 | + .build()) |
| 45 | + .addAction( |
| 46 | + Action.Builder() |
| 47 | + .setTitle("Custom event") |
| 48 | + .setOnClickListener { |
| 49 | + sendCustomNavigationAutoEvent("CustomAndroidAutoEvent", mapOf("sampleDataKey" to "sampleDataContent")) |
| 50 | + } |
| 51 | + .build()) |
| 52 | + .build()) |
| 53 | + .setMapActionStrip(ActionStrip.Builder().addAction(Action.PAN).build()) |
| 54 | + /** ... */ |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +For advanced customization, you can bypass the base class and implement your own screen by inheriting `Screen`. You can use the provided `AndroidAutoBaseScreen` base class as a reference on how to do that. |
| 59 | + |
| 60 | +### Flutter Setup |
| 61 | + |
| 62 | +On the Flutter side, you can use the `GoogleMapsAutoViewController` to interface with the CarPlay instance. The `GoogleMapsAutoViewController` allows you to call map functions on the CarPlay map view, and you can manage listeners using the provided functions. |
| 63 | + |
| 64 | +```dart |
| 65 | +final GoogleMapsAutoViewController _autoViewController = |
| 66 | + GoogleMapsAutoViewController(); |
| 67 | +
|
| 68 | +_autoViewController.listenForCustomNavigationAutoEvents((event) { |
| 69 | + showMessage("Received event: ${event.event}"); |
| 70 | +}); |
| 71 | +
|
| 72 | +Future<void> _setMapTypeForAutoToSatellite() async { |
| 73 | + await _autoViewController.setMapType(mapType: MapType.satellite); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +For a more detailed example, refer to the `lib/pages/navigation.dart` file in the Flutter example application. |
| 78 | + |
| 79 | +## Example Project |
| 80 | + |
| 81 | +For a fully functional Android Auto implementation, check out the [Android Studio example app](./example/android/). |
0 commit comments