mirror of
https://gitlab.linphone.org/BC/public/linphone-android.git
synced 2026-01-17 19:38:08 +00:00
Prevent crash on Android 12 if we aren't allowed to start a Service as foreground
This commit is contained in:
parent
ea2defdd20
commit
fdd9e8848a
5 changed files with 38 additions and 9 deletions
|
|
@ -21,8 +21,7 @@ package org.linphone.compatibility
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.annotation.TargetApi
|
||||
import android.app.Activity
|
||||
import android.app.PendingIntent
|
||||
import android.app.*
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
|
|
@ -245,5 +244,9 @@ class Api21Compatibility {
|
|||
fun startForegroundService(context: Context, intent: Intent) {
|
||||
context.startService(intent)
|
||||
}
|
||||
|
||||
fun startForegroundService(service: Service, notifId: Int, notif: Notification?) {
|
||||
service.startForeground(notifId, notif)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,16 +20,16 @@
|
|||
package org.linphone.compatibility
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.app.Notification
|
||||
import android.app.PendingIntent
|
||||
import android.app.Person
|
||||
import android.app.*
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.core.content.ContextCompat
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.LinphoneApplication.Companion.corePreferences
|
||||
import org.linphone.R
|
||||
import org.linphone.contact.Contact
|
||||
import org.linphone.core.Call
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.notifications.Notifiable
|
||||
import org.linphone.notifications.NotificationsManager
|
||||
import org.linphone.utils.ImageUtils
|
||||
|
|
@ -139,5 +139,21 @@ class Api31Compatibility {
|
|||
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
fun startForegroundService(context: Context, intent: Intent) {
|
||||
try {
|
||||
Compatibility.startForegroundService(context, intent)
|
||||
} catch (fssnae: ForegroundServiceStartNotAllowedException) {
|
||||
Log.e("[Api31 Compatibility] Can't start service as foreground! $fssnae")
|
||||
}
|
||||
}
|
||||
|
||||
fun startForegroundService(service: Service, notifId: Int, notif: Notification?) {
|
||||
try {
|
||||
service.startForeground(notifId, notif)
|
||||
} catch (fssnae: ForegroundServiceStartNotAllowedException) {
|
||||
Log.e("[Api31 Compatibility] Can't start service as foreground! $fssnae")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ package org.linphone.compatibility
|
|||
import android.app.Activity
|
||||
import android.app.Notification
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
|
|
@ -202,13 +203,23 @@ class Compatibility {
|
|||
}
|
||||
|
||||
fun startForegroundService(context: Context, intent: Intent) {
|
||||
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
|
||||
if (Version.sdkAboveOrEqual(Version.API31_ANDROID_12)) {
|
||||
Api31Compatibility.startForegroundService(context, intent)
|
||||
} else if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
|
||||
Api26Compatibility.startForegroundService(context, intent)
|
||||
} else {
|
||||
Api21Compatibility.startForegroundService(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
fun startForegroundService(service: Service, notifId: Int, notif: Notification?) {
|
||||
if (Version.sdkAboveOrEqual(Version.API31_ANDROID_12)) {
|
||||
Api31Compatibility.startForegroundService(service, notifId, notif)
|
||||
} else {
|
||||
Api21Compatibility.startForegroundService(service, notifId, notif)
|
||||
}
|
||||
}
|
||||
|
||||
/* Call */
|
||||
|
||||
fun canDrawOverlay(context: Context): Boolean {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ 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
|
||||
|
|
@ -56,6 +55,6 @@ class BootReceiver : BroadcastReceiver() {
|
|||
|
||||
val serviceIntent = Intent(Intent.ACTION_MAIN).setClass(context, CoreService::class.java)
|
||||
serviceIntent.putExtra("StartForeground", true)
|
||||
ContextCompat.startForegroundService(context, serviceIntent)
|
||||
Compatibility.startForegroundService(context, serviceIntent)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -340,7 +340,7 @@ class NotificationsManager(private val context: Context) {
|
|||
}
|
||||
currentForegroundServiceNotificationId = SERVICE_NOTIF_ID
|
||||
Log.i("[Notifications Manager] Starting service as foreground [$currentForegroundServiceNotificationId]")
|
||||
coreService.startForeground(currentForegroundServiceNotificationId, serviceNotification)
|
||||
Compatibility.startForegroundService(coreService, currentForegroundServiceNotificationId, serviceNotification)
|
||||
service = coreService
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue