Prevent crash on Android 12 if we aren't allowed to start a Service as foreground

This commit is contained in:
Sylvain Berfini 2022-02-24 11:03:21 +01:00
parent 2748a34c25
commit aba0af378a
5 changed files with 37 additions and 11 deletions

View file

@ -21,8 +21,7 @@ package org.linphone.compatibility
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.annotation.TargetApi import android.annotation.TargetApi
import android.app.Activity import android.app.*
import android.app.PendingIntent
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
@ -248,6 +247,10 @@ class Api21Compatibility {
context.startService(intent) context.startService(intent)
} }
fun startForegroundService(service: Service, notifId: Int, notif: Notification?) {
service.startForeground(notifId, notif)
}
fun hideAndroidSystemUI(hide: Boolean, window: Window) { fun hideAndroidSystemUI(hide: Boolean, window: Window) {
val decorView = window.decorView val decorView = window.decorView
val uiOptions = if (hide) { val uiOptions = if (hide) {

View file

@ -20,12 +20,9 @@
package org.linphone.compatibility package org.linphone.compatibility
import android.annotation.TargetApi import android.annotation.TargetApi
import android.app.Activity import android.app.*
import android.app.Notification
import android.app.PendingIntent
import android.app.Person
import android.app.PictureInPictureParams
import android.content.Context import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
@ -150,6 +147,22 @@ class Api31Compatibility {
return builder.build() 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")
}
}
fun enableAutoEnterPiP(activity: Activity, enable: Boolean) { fun enableAutoEnterPiP(activity: Activity, enable: Boolean) {
val supportsPip = activity.packageManager val supportsPip = activity.packageManager
.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) .hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)

View file

@ -22,6 +22,7 @@ package org.linphone.compatibility
import android.app.Activity import android.app.Activity
import android.app.Notification import android.app.Notification
import android.app.PendingIntent import android.app.PendingIntent
import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
@ -203,13 +204,23 @@ class Compatibility {
} }
fun startForegroundService(context: Context, intent: Intent) { 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) Api26Compatibility.startForegroundService(context, intent)
} else { } else {
Api21Compatibility.startForegroundService(context, intent) 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 */ /* Call */
fun canDrawOverlay(context: Context): Boolean { fun canDrawOverlay(context: Context): Boolean {

View file

@ -23,7 +23,6 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R import org.linphone.R
import org.linphone.compatibility.Compatibility import org.linphone.compatibility.Compatibility
@ -56,6 +55,6 @@ class BootReceiver : BroadcastReceiver() {
val serviceIntent = Intent(Intent.ACTION_MAIN).setClass(context, CoreService::class.java) val serviceIntent = Intent(Intent.ACTION_MAIN).setClass(context, CoreService::class.java)
serviceIntent.putExtra("StartForeground", true) serviceIntent.putExtra("StartForeground", true)
ContextCompat.startForegroundService(context, serviceIntent) Compatibility.startForegroundService(context, serviceIntent)
} }
} }

View file

@ -338,7 +338,7 @@ class NotificationsManager(private val context: Context) {
} }
currentForegroundServiceNotificationId = SERVICE_NOTIF_ID currentForegroundServiceNotificationId = SERVICE_NOTIF_ID
Log.i("[Notifications Manager] Starting service as foreground [$currentForegroundServiceNotificationId]") Log.i("[Notifications Manager] Starting service as foreground [$currentForegroundServiceNotificationId]")
coreService.startForeground(currentForegroundServiceNotificationId, serviceNotification) Compatibility.startForegroundService(coreService, currentForegroundServiceNotificationId, serviceNotification)
service = coreService service = coreService
} }