Skip to content
Snippets Groups Projects
Commit 095506fd authored by Nicolas Pomepuy's avatar Nicolas Pomepuy
Browse files

Prevent ForegroundServiceStartNotAllowedException

Fixes #2784
parent 94a9ea07
No related branches found
No related tags found
1 merge request!1672Prevent ForegroundServiceStartNotAllowedException
Pipeline #309887 passed with stage
in 7 minutes and 53 seconds
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
......@@ -94,7 +97,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()
}
}
......
......@@ -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
......@@ -201,7 +202,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) {
......@@ -377,7 +384,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