Skip to content
Snippets Groups Projects
Commit e708da8a authored by Nicolas Pomepuy's avatar Nicolas Pomepuy Committed by Duncan McNamara
Browse files

Prevent ForegroundServiceStartNotAllowedException

Fixes #2784
parent 98366949
No related branches found
No related tags found
1 merge request!1676Backport 3.5.x: !1672, !1674
package org.videolan.resources.util
import android.app.ForegroundServiceStartNotAllowedException
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.os.Parcelable
import android.os.Handler
import android.os.Looper
import androidx.core.content.ContextCompat
......@@ -90,7 +99,13 @@ fun Context.launchForeground(intent: Intent, block: () -> Unit = {}) {
val ctx = this@launchForeground
AppScope.launch(Dispatchers.Main) {
intent.putExtra("foreground", true)
ContextCompat.startForegroundService(ctx, intent)
try {
ContextCompat.startForegroundService(ctx, intent)
} catch (e: Exception) {
if (SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
Log.w("MediaParsingService", "ForegroundServiceStartNotAllowedException caught!")
}
}
block()
}
}
\ No newline at end of file
......@@ -24,6 +24,7 @@ package org.videolan.vlc
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.ForegroundServiceStartNotAllowedException
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
......@@ -200,7 +201,13 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
@TargetApi(Build.VERSION_CODES.O)
private fun forceForeground() {
val notification = NotificationHelper.createScanNotification(applicationContext, getString(R.string.loading_medialibrary), scanPaused, -1, -1)
startForeground(43, notification)
try {
startForeground(43, notification)
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
Log.w("MediaParsingService", "ForegroundServiceStartNotAllowedException caught!")
}
}
}
private fun discoverStorage(path: String) {
......@@ -376,7 +383,13 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
if (lastNotificationTime != -1L) {
try {
val notification = NotificationHelper.createScanNotification(applicationContext, progressText, scanPaused, scheduled, done)
startForeground(43, notification)
try {
startForeground(43, notification)
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
Log.w("MediaParsingService", "ForegroundServiceStartNotAllowedException caught!")
}
}
} catch (ignored: IllegalArgumentException) {}
progressText
} else ""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment