[Android] Fix crash when ActivityManager.runningAppProcesses is empty

Nicolas Pomepuy git at videolan.org
Wed Jun 5 11:20:42 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu May 16 09:36:50 2024 +0200| [3488af995293bb9541aff9446d5974b94f7098fc] | committer: Nicolas Pomepuy

Fix crash when ActivityManager.runningAppProcesses is empty

> https://code.videolan.org/videolan/vlc-android/commit/3488af995293bb9541aff9446d5974b94f7098fc
---

 .../tools/src/main/java/org/videolan/tools/KotlinExtensions.kt      | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt b/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
index ff3d62eae1..f406d0338f 100644
--- a/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
+++ b/application/tools/src/main/java/org/videolan/tools/KotlinExtensions.kt
@@ -122,7 +122,11 @@ suspend fun Context.awaitAppIsForegroung(): Boolean {
     return activityManager.isAppForeground()
 }
 
-private fun ActivityManager.isAppForeground() = runningAppProcesses[0].importance <= RunningAppProcessInfo.IMPORTANCE_FOREGROUND
+private fun ActivityManager.isAppForeground() = try {
+    runningAppProcesses[0].importance <= RunningAppProcessInfo.IMPORTANCE_FOREGROUND
+} catch (e: Exception) {
+    false
+}
 
 @OptIn(ExperimentalContracts::class)
 fun String?.isValidUrl(): Boolean {



More information about the Android mailing list