From 34ca235d7b2ff1427b45e6de887a0adf97a7ed4e Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 14 Feb 2022 10:10:08 +0100 Subject: [PATCH] Prevent crash when background mode is enabled & service notification channel is disabled --- .../java/org/linphone/core/BootReceiver.kt | 24 ++++++++++++++----- .../notifications/NotificationsManager.kt | 6 +++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/linphone/core/BootReceiver.kt b/app/src/main/java/org/linphone/core/BootReceiver.kt index 6888da9e9..39b49c544 100644 --- a/app/src/main/java/org/linphone/core/BootReceiver.kt +++ b/app/src/main/java/org/linphone/core/BootReceiver.kt @@ -22,8 +22,11 @@ package org.linphone.core import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat import org.linphone.LinphoneApplication.Companion.corePreferences +import org.linphone.R +import org.linphone.compatibility.Compatibility import org.linphone.core.tools.Log class BootReceiver : BroadcastReceiver() { @@ -32,18 +35,27 @@ class BootReceiver : BroadcastReceiver() { val autoStart = corePreferences.autoStart Log.i("[Boot Receiver] Device is starting, autoStart is $autoStart") if (autoStart) { - val serviceIntent = Intent(Intent.ACTION_MAIN).setClass(context, CoreService::class.java) - serviceIntent.putExtra("StartForeground", true) - ContextCompat.startForegroundService(context, serviceIntent) + startService(context) } } else if (intent.action.equals(Intent.ACTION_MY_PACKAGE_REPLACED, ignoreCase = true)) { val autoStart = corePreferences.autoStart Log.i("[Boot Receiver] App has been updated, autoStart is $autoStart") if (autoStart) { - val serviceIntent = Intent(Intent.ACTION_MAIN).setClass(context, CoreService::class.java) - serviceIntent.putExtra("StartForeground", true) - ContextCompat.startForegroundService(context, serviceIntent) + startService(context) } } } + + private fun startService(context: Context) { + val serviceChannel = context.getString(R.string.notification_channel_service_id) + val notificationManager = NotificationManagerCompat.from(context) + if (Compatibility.getChannelImportance(notificationManager, serviceChannel) == NotificationManagerCompat.IMPORTANCE_NONE) { + Log.w("[Boot Receiver] Service channel is disabled!") + return + } + + val serviceIntent = Intent(Intent.ACTION_MAIN).setClass(context, CoreService::class.java) + serviceIntent.putExtra("StartForeground", true) + ContextCompat.startForegroundService(context, serviceIntent) + } } diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 8a415fa9d..903b4d81f 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -279,6 +279,12 @@ class NotificationsManager(private val context: Context) { /* Service related */ fun startForeground() { + val serviceChannel = context.getString(R.string.notification_channel_service_id) + if (Compatibility.getChannelImportance(notificationManager, serviceChannel) == NotificationManagerCompat.IMPORTANCE_NONE) { + Log.w("[Notifications Manager] Service channel is disabled!") + return + } + val coreService = service if (coreService != null) { startForeground(coreService, useAutoStartDescription = false)