From 0417ade8027e86f76cf83658de199cfccea18d6c Mon Sep 17 00:00:00 2001 From: johan12345 Date: Sat, 23 Sep 2023 18:23:39 +0200 Subject: [PATCH] Android Auto: fix crash on Android 14 due to missing permission --- app/src/main/AndroidManifest.xml | 1 + .../java/net/vonforst/evmap/auto/CarAppService.kt | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fa5110f75..578bbc6e9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + diff --git a/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt b/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt index c420c79ed..4cc8892c9 100644 --- a/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt +++ b/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt @@ -45,13 +45,15 @@ interface LocationAwareScreen { class CarAppService : androidx.car.app.CarAppService() { private val CHANNEL_ID = "car_location" private val NOTIFICATION_ID = 1000 + private var foregroundStarted = false - override fun onCreate() { - super.onCreate() - + fun ensureForegroundService() { // we want to run as a foreground service to make sure we can use location - createNotificationChannel() - startForeground(NOTIFICATION_ID, getNotification()) + if (!foregroundStarted) { + createNotificationChannel() + startForeground(NOTIFICATION_ID, getNotification()) + foregroundStarted = true + } } private fun createNotificationChannel() { @@ -222,6 +224,7 @@ class EVMapSession(val cas: CarAppService) : Session(), DefaultLifecycleObserver @SuppressLint("MissingPermission") fun requestLocationUpdates() { if (!locationPermissionGranted()) return + cas.ensureForegroundService() Log.i(TAG, "Requesting location updates") requestCarHardwareLocationUpdates() requestPhoneLocationUpdates()