[Android] Try to prevent NullPointerExceptions

Nicolas Pomepuy git at videolan.org
Tue Oct 26 06:45:23 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Oct 19 09:39:43 2021 +0200| [013b33cfff3cfb14acecc32ac448c92a8d806b74] | committer: Nicolas Pomepuy

Try to prevent NullPointerExceptions

> https://code.videolan.org/videolan/vlc-android/commit/013b33cfff3cfb14acecc32ac448c92a8d806b74
---

 .../src/org/videolan/vlc/PlaybackService.kt        | 45 ++++++++++++----------
 .../org/videolan/vlc/gui/helpers/ImageLoader.kt    |  9 ++++-
 2 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index bdb3d61aa..d86598f83 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -1265,28 +1265,33 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
         val queue = withContext(Dispatchers.Default) {
             ArrayList<MediaSessionCompat.QueueItem>(toIndex - fromIndex).also {
                 for ((position, media) in mediaList.subList(fromIndex, toIndex).withIndex()) {
-                    val title: String = media.nowPlaying ?: media.title
-                    val mediaId = MediaSessionBrowser.generateMediaId(media)
-                    val iconUri = when {
-                        isSchemeHttpOrHttps(media.artworkMrl) -> {
-                            //ArtworkProvider will cache remote images
-                            ArtworkProvider.buildUri(Uri.Builder()
-                                    .appendPath(ArtworkProvider.REMOTE)
-                                    .appendQueryParameter(ArtworkProvider.PATH, media.artworkMrl)
-                                    .build())
+                    try {
+                        val title: String = media.nowPlaying ?: media.title
+                        val mediaId = MediaSessionBrowser.generateMediaId(media)
+                        val iconUri = when {
+                            isSchemeHttpOrHttps(media.artworkMrl) -> {
+                                //ArtworkProvider will cache remote images
+                                ArtworkProvider.buildUri(Uri.Builder()
+                                        .appendPath(ArtworkProvider.REMOTE)
+                                        .appendQueryParameter(ArtworkProvider.PATH, media.artworkMrl)
+                                        .build())
+                            }
+                            ThumbnailsProvider.isMediaVideo(media) -> ArtworkProvider.buildMediaUri(media)
+                            else -> artworkMap[mediaId] ?: MediaSessionBrowser.DEFAULT_TRACK_ICON
                         }
-                        ThumbnailsProvider.isMediaVideo(media) -> ArtworkProvider.buildMediaUri(media)
-                        else -> artworkMap[mediaId] ?: MediaSessionBrowser.DEFAULT_TRACK_ICON
+                        val mediaDesc = MediaDescriptionCompat.Builder()
+                                .setTitle(title)
+                                .setSubtitle(MediaUtils.getMediaArtist(ctx, media))
+                                .setDescription(MediaUtils.getMediaAlbum(ctx, media))
+                                .setIconUri(iconUri)
+                                .setMediaUri(media.uri)
+                                .setMediaId(mediaId)
+                                .build()
+                        it.add(MediaSessionCompat.QueueItem(mediaDesc, (fromIndex + position).toLong()))
+                    } catch (e: NullPointerException) {
+                        Log.e("PlaybackService", e.message, e)
+                        VLCCrashHandler.saveLog(e)
                     }
-                    val mediaDesc = MediaDescriptionCompat.Builder()
-                            .setTitle(title)
-                            .setSubtitle(MediaUtils.getMediaArtist(ctx, media))
-                            .setDescription(MediaUtils.getMediaAlbum(ctx, media))
-                            .setIconUri(iconUri)
-                            .setMediaUri(media.uri)
-                            .setMediaId(mediaId)
-                            .build()
-                    it.add(MediaSessionCompat.QueueItem(mediaDesc, (fromIndex + position).toLong()))
                 }
             }
         }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
index e2b3c4473..93a9a1e9f 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
@@ -326,8 +326,13 @@ private suspend fun findInLibrary(item: MediaLibraryItem, isMedia: Boolean): Med
         val type = mw.type
         val isMediaFile = type == MediaWrapper.TYPE_AUDIO || type == MediaWrapper.TYPE_VIDEO
         val uri = mw.uri
-        if (!isMediaFile && !(type == MediaWrapper.TYPE_DIR && "upnp" == uri.scheme)) return item
-        if (isMediaFile && "file" == uri.scheme) return withContext(Dispatchers.IO) { sMedialibrary.getMedia(uri) }
+        val scheme = try {
+            uri.scheme
+        } catch (e: NullPointerException) {
+            ""
+        }
+        if (!isMediaFile && !(type == MediaWrapper.TYPE_DIR && "upnp" == scheme)) return item
+        if (isMediaFile && "file" == scheme) return withContext(Dispatchers.IO) { sMedialibrary.getMedia(uri) }
                 ?: item
     }
     return item



More information about the Android mailing list