Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature sdk # android auto #222

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@
android:name=".DownloadTerritoriesActivity"
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"
tools:ignore="ExportedService">
<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
Loading