Skip to content

Commit 03f11f3

Browse files
author
v.lazin
committed
[SDK-#] add Android Auto example
1 parent e558651 commit 03f11f3

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ dependencies {
149149
implementation libs.kotlin.reflect
150150

151151
implementation libs.androidx.appcompat
152+
implementation libs.androidx.car.app
152153
implementation libs.androidx.constraintlayout
153154
implementation libs.androidx.core.ktx
154155
implementation libs.androidx.fragment

app/src/main/AndroidManifest.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@
7474
android:name=".SearchActivity"
7575
android:theme="@style/AppTheme"
7676
android:windowSoftInputMode="adjustNothing" />
77+
78+
<meta-data
79+
android:name="com.google.android.gms.car.application"
80+
android:resource="@xml/automotive_app_desc" />
81+
<meta-data
82+
android:name="androidx.car.app.minCarApiLevel"
83+
android:value="1" />
84+
<service android:name=".car.MapService"
85+
android:exported="true">
86+
<intent-filter>
87+
<action android:name="androidx.car.app.CarAppService" />
88+
<category android:name="androidx.car.app.category.POI" />
89+
</intent-filter>
90+
</service>
7791
</application>
7892

7993
</manifest>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.dgis.sdk.demo.car
2+
3+
import androidx.car.app.CarContext
4+
import androidx.car.app.Screen
5+
import androidx.car.app.model.Action
6+
import androidx.car.app.model.ActionStrip
7+
import androidx.car.app.model.Template
8+
import androidx.car.app.navigation.model.NavigationTemplate
9+
10+
class MainScreen(carContext: CarContext) : Screen(carContext) {
11+
override fun onGetTemplate(): Template {
12+
return NavigationTemplate.Builder()
13+
.setActionStrip(
14+
ActionStrip.Builder()
15+
.addAction(Action.BACK)
16+
.build()
17+
)
18+
.setMapActionStrip(
19+
ActionStrip.Builder()
20+
.addAction(Action.PAN)
21+
.build()
22+
)
23+
.build()
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.dgis.sdk.demo.car
2+
3+
import androidx.car.app.CarAppService
4+
import androidx.car.app.Session
5+
import androidx.car.app.validation.HostValidator
6+
7+
class MapService : CarAppService() {
8+
override fun createHostValidator(): HostValidator {
9+
// Avoid using ALLOW_ALL_HOSTS_VALIDATOR in production as it is insecure.
10+
// Refer to the Android Auto documentation for proper security practices.
11+
return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
12+
}
13+
14+
override fun onCreateSession(): Session {
15+
return MapSession()
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.dgis.sdk.demo.car
2+
3+
import android.content.Intent
4+
import androidx.car.app.AppManager
5+
import androidx.car.app.CarToast
6+
import androidx.car.app.Screen
7+
import ru.dgis.sdk.androidauto.AndroidAutoMapSession
8+
import ru.dgis.sdk.map.Map
9+
import ru.dgis.sdk.map.MapOptions
10+
import ru.dgis.sdk.map.ScreenPoint
11+
12+
class MapSession : AndroidAutoMapSession(MapOptions()) {
13+
private var map: Map? = null
14+
15+
private fun showToast(message: String) {
16+
carContext.getCarService(AppManager::class.java)
17+
.showToast(message, CarToast.LENGTH_SHORT)
18+
}
19+
20+
override fun onCreateScreen(intent: Intent): Screen {
21+
return MainScreen(carContext)
22+
}
23+
24+
override fun onMapReady(map: Map) {
25+
this.map = map
26+
}
27+
28+
override fun onMapReadyException(exception: Exception) {
29+
exception.message?.let(::showToast)
30+
}
31+
32+
override fun onMapClicked(x: Float, y: Float) {
33+
map?.getRenderedObjects(centerPoint = ScreenPoint(x = x, y = y))?.onResult {
34+
val objectInfo = it.firstOrNull()
35+
showToast("$objectInfo")
36+
}
37+
}
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<automotiveApp>
3+
<uses name="template" />
4+
</automotiveApp>

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[versions]
2+
car-app = "1.4.0"
23
kotlin = "1.9.24"
34
agp = "8.5.1"
45
google-services = "4.3.15"
@@ -35,6 +36,7 @@ ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
3536

3637
[libraries]
3738
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
39+
androidx-car-app = { module = "androidx.car.app:app", version.ref = "car-app" }
3840
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
3941
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "core" }
4042
androidx-fragment = { module = "androidx.fragment:fragment-ktx", version.ref = "fragment" }

0 commit comments

Comments
 (0)