diff --git a/app/build.gradle b/app/build.gradle index a028fe9..caa4cda 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9432f40..21c46dd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -78,6 +78,21 @@ android:name=".DownloadTerritoriesActivity" android:theme="@style/AppTheme" android:windowSoftInputMode="adjustNothing" /> + + + + + + + + + diff --git a/app/src/main/java/ru/dgis/sdk/demo/car/MainScreen.kt b/app/src/main/java/ru/dgis/sdk/demo/car/MainScreen.kt new file mode 100644 index 0000000..9f6e02c --- /dev/null +++ b/app/src/main/java/ru/dgis/sdk/demo/car/MainScreen.kt @@ -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() + } +} diff --git a/app/src/main/java/ru/dgis/sdk/demo/car/MapService.kt b/app/src/main/java/ru/dgis/sdk/demo/car/MapService.kt new file mode 100644 index 0000000..666442b --- /dev/null +++ b/app/src/main/java/ru/dgis/sdk/demo/car/MapService.kt @@ -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() + } +} diff --git a/app/src/main/java/ru/dgis/sdk/demo/car/MapSession.kt b/app/src/main/java/ru/dgis/sdk/demo/car/MapSession.kt new file mode 100644 index 0000000..ec9c190 --- /dev/null +++ b/app/src/main/java/ru/dgis/sdk/demo/car/MapSession.kt @@ -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") + } + } +} diff --git a/app/src/main/res/xml/automotive_app_desc.xml b/app/src/main/res/xml/automotive_app_desc.xml new file mode 100644 index 0000000..8dc476f --- /dev/null +++ b/app/src/main/res/xml/automotive_app_desc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index de9a4f8..499ffc4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,4 +1,5 @@ [versions] +car-app = "1.4.0" kotlin = "1.9.24" agp = "8.5.1" google-services = "4.3.15" @@ -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" }