Added compatibility package

This commit is contained in:
Sylvain Berfini 2023-11-21 15:52:11 +01:00
parent 0d31bfe3c3
commit ad35f85c3a
4 changed files with 155 additions and 12 deletions

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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")
}
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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")
}
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}
}
}
}

View file

@ -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...")
}