[Android] Improve ML exception handler

Geoffrey Métais git at videolan.org
Thu Oct 17 16:46:44 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct 17 16:45:45 2019 +0200| [5fb016eab9fc846eb2d3437e02d09272ac4f47ee] | committer: Geoffrey Métais

Improve ML exception handler

- Throw an esception in debug build
- Add a log line to help debugging
- Move it to the companion object for safety

> https://code.videolan.org/videolan/vlc-android/commit/5fb016eab9fc846eb2d3437e02d09272ac4f47ee
---

 .../src/org/videolan/vlc/MediaParsingService.kt    | 45 +++++++++++++---------
 1 file changed, 27 insertions(+), 18 deletions(-)

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<Notification>(capacity = Channel.UNLIMITED) {
             for (update in channel) when (update) {
@@ -419,6 +401,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
                 val context = this at 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<ScanProgress>()
         val newStorages = MutableLiveData<MutableList<String>>()
         val preselectedStorages = mutableListOf<String>()
+        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
+
     }
 }
 



More information about the Android mailing list