Skip to content

Commit

Permalink
[SDK-#] add Android Auto example
Browse files Browse the repository at this point in the history
  • Loading branch information
v.lazin committed Aug 16, 2024
1 parent e558651 commit 03f11f3
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ dependencies {
implementation libs.kotlin.reflect

implementation libs.androidx.appcompat
implementation libs.androidx.car.app
implementation libs.androidx.constraintlayout
implementation libs.androidx.core.ktx
implementation libs.androidx.fragment
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@
android:name=".SearchActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustNothing" />

<meta-data
android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc" />
<meta-data
android:name="androidx.car.app.minCarApiLevel"
android:value="1" />
<service android:name=".car.MapService"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService" />
<category android:name="androidx.car.app.category.POI" />
</intent-filter>
</service>
</application>

</manifest>
25 changes: 25 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/car/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.dgis.sdk.demo.car

import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.ActionStrip
import androidx.car.app.model.Template
import androidx.car.app.navigation.model.NavigationTemplate

class MainScreen(carContext: CarContext) : Screen(carContext) {
override fun onGetTemplate(): Template {
return NavigationTemplate.Builder()
.setActionStrip(
ActionStrip.Builder()
.addAction(Action.BACK)
.build()
)
.setMapActionStrip(
ActionStrip.Builder()
.addAction(Action.PAN)
.build()
)
.build()
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/car/MapService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.dgis.sdk.demo.car

import androidx.car.app.CarAppService
import androidx.car.app.Session
import androidx.car.app.validation.HostValidator

class MapService : CarAppService() {
override fun createHostValidator(): HostValidator {
// Avoid using ALLOW_ALL_HOSTS_VALIDATOR in production as it is insecure.
// Refer to the Android Auto documentation for proper security practices.
return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
}

override fun onCreateSession(): Session {
return MapSession()
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/car/MapSession.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.dgis.sdk.demo.car

import android.content.Intent
import androidx.car.app.AppManager
import androidx.car.app.CarToast
import androidx.car.app.Screen
import ru.dgis.sdk.androidauto.AndroidAutoMapSession
import ru.dgis.sdk.map.Map
import ru.dgis.sdk.map.MapOptions
import ru.dgis.sdk.map.ScreenPoint

class MapSession : AndroidAutoMapSession(MapOptions()) {
private var map: Map? = null

private fun showToast(message: String) {
carContext.getCarService(AppManager::class.java)
.showToast(message, CarToast.LENGTH_SHORT)
}

override fun onCreateScreen(intent: Intent): Screen {
return MainScreen(carContext)
}

override fun onMapReady(map: Map) {
this.map = map
}

override fun onMapReadyException(exception: Exception) {
exception.message?.let(::showToast)
}

override fun onMapClicked(x: Float, y: Float) {
map?.getRenderedObjects(centerPoint = ScreenPoint(x = x, y = y))?.onResult {
val objectInfo = it.firstOrNull()
showToast("$objectInfo")
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/xml/automotive_app_desc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<automotiveApp>
<uses name="template" />
</automotiveApp>
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[versions]
car-app = "1.4.0"
kotlin = "1.9.24"
agp = "8.5.1"
google-services = "4.3.15"
Expand Down Expand Up @@ -35,6 +36,7 @@ ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }

[libraries]
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-car-app = { module = "androidx.car.app:app", version.ref = "car-app" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "core" }
androidx-fragment = { module = "androidx.fragment:fragment-ktx", version.ref = "fragment" }
Expand Down

0 comments on commit 03f11f3

Please sign in to comment.