diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt index fcc6314a4..571a23a97 100644 --- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt +++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt @@ -83,24 +83,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec private var discoverTriggered = false internal val sb = StringBuilder() - private val exceptionHandler = if (BuildConfig.BETA) AbstractMedialibrary.MedialibraryExceptionHandler { context, errMsg, _ -> - val intent = Intent(applicationContext, SendCrashActivity::class.java).apply { - putExtra(CRASH_ML_CTX, context) - putExtra(CRASH_ML_MSG, errMsg) - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - } - // Lock the Medialibrary thread during DB extraction. - runBlocking { - SendCrashActivity.job = Job() - try { - startActivity(intent) - SendCrashActivity.job?.join() - } catch (e: Exception) { - SendCrashActivity.job = null - } - } - } else null - private val notificationActor by lazy { actor(capacity = Channel.UNLIMITED) { for (update in channel) when (update) { @@ -419,6 +401,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec val context = this@MediaParsingService var shouldInit = !dbExists() val initCode = medialibrary.init(context) + setExceptionHandler() medialibrary.exceptionHandler = exceptionHandler if (initCode != AbstractMedialibrary.ML_INIT_ALREADY_INITIALIZED) { shouldInit = shouldInit or (initCode == AbstractMedialibrary.ML_INIT_DB_RESET) or (initCode == AbstractMedialibrary.ML_INIT_DB_CORRUPTED) @@ -469,6 +452,32 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec val progress = MutableLiveData() val newStorages = MutableLiveData>() val preselectedStorages = mutableListOf() + var exceptionHandler : AbstractMedialibrary.MedialibraryExceptionHandler? = null + } + + private fun setExceptionHandler() { + if (exceptionHandler !== null) return + exceptionHandler = if (BuildConfig.BETA) AbstractMedialibrary.MedialibraryExceptionHandler { context, errMsg, _ -> + val intent = Intent(applicationContext, SendCrashActivity::class.java).apply { + putExtra(CRASH_ML_CTX, context) + putExtra(CRASH_ML_MSG, errMsg) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + Log.wtf(TAG, "medialibrary reported unhandled exception: -----------------") + // Lock the Medialibrary thread during DB extraction. + runBlocking { + SendCrashActivity.job = Job() + try { + startActivity(intent) + SendCrashActivity.job?.join() + } catch (e: Exception) { + SendCrashActivity.job = null + } + } + } else if (BuildConfig.DEBUG) AbstractMedialibrary.MedialibraryExceptionHandler { context, errMsg, _ -> + throw IllegalStateException("$context:\n$errMsg") + } else null + } }