diff --git a/app/src/main/java/org/linphone/compatibility/Api28Compatibility.kt b/app/src/main/java/org/linphone/compatibility/Api28Compatibility.kt new file mode 100644 index 000000000..7897de9ee --- /dev/null +++ b/app/src/main/java/org/linphone/compatibility/Api28Compatibility.kt @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2010-2023 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.compatibility + +import android.app.Notification +import android.app.Service +import org.linphone.core.tools.Log + +class Api28Compatibility { + companion object { + private const val TAG = "[API 28 Compatibility]" + + fun startServiceForeground(service: Service, id: Int, notification: Notification) { + try { + service.startForeground( + id, + notification + ) + } catch (e: Exception) { + Log.e("$TAG Can't start service as foreground! $e") + } + } + } +} diff --git a/app/src/main/java/org/linphone/compatibility/Api29Compatibility.kt b/app/src/main/java/org/linphone/compatibility/Api29Compatibility.kt new file mode 100644 index 000000000..e0b654d84 --- /dev/null +++ b/app/src/main/java/org/linphone/compatibility/Api29Compatibility.kt @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2010-2023 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.compatibility + +import android.app.Notification +import android.app.Service +import android.os.Build +import androidx.annotation.RequiresApi +import org.linphone.core.tools.Log + +@RequiresApi(Build.VERSION_CODES.Q) +class Api29Compatibility { + companion object { + private const val TAG = "[API 29 Compatibility]" + + fun startServiceForeground( + service: Service, + id: Int, + notification: Notification, + foregroundServiceType: Int + ) { + try { + service.startForeground( + id, + notification, + foregroundServiceType + ) + } catch (e: Exception) { + Log.e("$TAG Can't start service as foreground! $e") + } + } + } +} diff --git a/app/src/main/java/org/linphone/compatibility/Compatibility.kt b/app/src/main/java/org/linphone/compatibility/Compatibility.kt new file mode 100644 index 000000000..5cc6739ff --- /dev/null +++ b/app/src/main/java/org/linphone/compatibility/Compatibility.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2010-2023 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.compatibility + +import android.annotation.SuppressLint +import android.app.Notification +import android.app.Service +import org.linphone.mediastream.Version + +@SuppressLint("NewApi") +class Compatibility { + companion object { + private const val TAG = "[Compatibility]" + + + const val FOREGROUND_SERVICE_TYPE_PHONE_CALL = 4 // Matches ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL + const val FOREGROUND_SERVICE_TYPE_CAMERA = 64// Matches ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA + const val FOREGROUND_SERVICE_TYPE_MICROPHONE = 128 // ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE + + fun startServiceForeground( + service: Service, + id: Int, + notification: Notification, + foregroundServiceType: Int + ) { + if (Version.sdkAboveOrEqual(Version.API29_ANDROID_10)) { + Api29Compatibility.startServiceForeground( + service, + id, + notification, + foregroundServiceType + ) + } else { + Api28Compatibility.startServiceForeground(service, id, notification) + } + } + } +} diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 1d54eed7e..6ee6bea90 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -51,6 +51,7 @@ import kotlinx.coroutines.launch import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R +import org.linphone.compatibility.Compatibility import org.linphone.contacts.AvatarGenerator import org.linphone.contacts.getAvatarBitmap import org.linphone.contacts.getPerson @@ -487,18 +488,14 @@ class NotificationsManager @MainThread constructor(private val context: Context) val service = coreService if (service != null) { Log.i("$TAG Service found, starting it as foreground using notification") - // TODO FIXME: API LEVEL, add compatibility - try { - service.startForeground( - notifiable.notificationId, - notification.notification, - ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL - or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE - or ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA - ) - } catch (e: Exception) { - Log.e("$TAG Can't start service as foreground! $e") - } + Compatibility.startServiceForeground( + service, + notifiable.notificationId, + notification.notification, + Compatibility.FOREGROUND_SERVICE_TYPE_PHONE_CALL + or Compatibility.FOREGROUND_SERVICE_TYPE_MICROPHONE + or Compatibility.FOREGROUND_SERVICE_TYPE_CAMERA + ) } else { Log.w("$TAG Core Foreground Service hasn't started yet...") }