From be6c184061f9cac8ec3f62b5b11cce8f2db9a62c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 22 Jun 2020 23:31:13 +0200 Subject: [PATCH] try to catch startup error --- .../persistentNotification/DummyService.kt | 13 +++++++++++-- .../androidaps/services/LocationService.kt | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.kt index 8a4524d9e2..82bc322824 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.persistentNotification +import android.app.Notification import android.app.Service import android.content.Intent import android.os.IBinder @@ -32,7 +33,11 @@ class DummyService : DaggerService() { super.onCreate() // TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service? // As onCreate() is not called every time a service is started, copied to onStartCommand(). - startForeground(notificationHolder.notificationID, notificationHolder.notification) + try { + startForeground(notificationHolder.notificationID, notificationHolder.notification) + } catch (e: Exception) { + startForeground(4711, Notification()) + } disposable.add(rxBus .toObservable(EventAppExit::class.java) .observeOn(Schedulers.io()) @@ -52,7 +57,11 @@ class DummyService : DaggerService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) - startForeground(notificationHolder.notificationID, notificationHolder.notification) + try { + startForeground(notificationHolder.notificationID, notificationHolder.notification) + } catch (e: Exception) { + startForeground(4711, Notification()) + } return Service.START_STICKY } } diff --git a/app/src/main/java/info/nightscout/androidaps/services/LocationService.kt b/app/src/main/java/info/nightscout/androidaps/services/LocationService.kt index 35374b47d7..4af050da21 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/LocationService.kt +++ b/app/src/main/java/info/nightscout/androidaps/services/LocationService.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.services import android.Manifest +import android.app.Notification import android.app.Service import android.content.Context import android.content.Intent @@ -73,17 +74,27 @@ class LocationService : DaggerService() { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) - startForeground(notificationHolder.notificationID, notificationHolder.notification) + try { + startForeground(notificationHolder.notificationID, notificationHolder.notification) + } catch (e: Exception) { + startForeground(4711, Notification()) + } return Service.START_STICKY } override fun onCreate() { super.onCreate() - startForeground(notificationHolder.notificationID, notificationHolder.notification) + try { + startForeground(notificationHolder.notificationID, notificationHolder.notification) + } catch (e: Exception) { + startForeground(4711, Notification()) + } // Get last location once until we get regular update - LocationServices.getFusedLocationProviderClient(this).lastLocation.addOnSuccessListener { - lastLocationDataContainer.lastLocation = it + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + LocationServices.getFusedLocationProviderClient(this).lastLocation.addOnSuccessListener { + lastLocationDataContainer.lastLocation = it + } } initializeLocationManager()