[Android] Add genre to Android Auto search results and try-catch block to Artwork Provider

Robert Stone git at videolan.org
Tue Jul 12 07:56:38 UTC 2022


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Mon Jul 11 23:59:17 2022 -0700| [54ff5ef91ec0fce99a3b2c4ba4ac48875a4f7756] | committer: Robert Stone

Add genre to Android Auto search results and try-catch block to Artwork Provider

> https://code.videolan.org/videolan/vlc-android/commit/54ff5ef91ec0fce99a3b2c4ba4ac48875a4f7756
---

 .../src/org/videolan/vlc/ArtworkProvider.kt        | 26 ++++++++++++++++------
 .../org/videolan/vlc/media/MediaSessionBrowser.kt  |  3 ++-
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt b/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt
index 5e2fb1134..8c4884b19 100644
--- a/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt
@@ -27,6 +27,7 @@ import android.graphics.BitmapFactory
 import android.graphics.Canvas
 import android.net.Uri
 import android.os.Binder
+import android.os.Build
 import android.os.Bundle
 import android.os.ParcelFileDescriptor
 import android.util.Log
@@ -48,17 +49,13 @@ import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
 import org.videolan.vlc.media.MediaSessionBrowser
 import org.videolan.vlc.util.AccessControl
 import org.videolan.vlc.util.ThumbnailsProvider
-import java.io.ByteArrayOutputStream
-import java.io.File
-import java.io.FileNotFoundException
-import java.io.FileOutputStream
+import java.io.*
 import java.nio.ByteBuffer
 import java.security.SecureRandom
 import java.text.DecimalFormat
 import java.text.SimpleDateFormat
 import java.util.*
 import java.util.zip.CRC32
-import kotlin.collections.ArrayList
 import kotlin.math.max
 
 private const val TAG = "VLC/ArtworkProvider"
@@ -383,7 +380,11 @@ class ArtworkProvider : ContentProvider() {
         return super.openPipeHelper(Uri.EMPTY, MIME_TYPE_IMAGE_WEBP, null, bitmap
         ) { pfd: ParcelFileDescriptor, _: Uri, _: String, _: Bundle?, bmp: Bitmap? ->
             /* Compression is performed on an AsyncTask thread within openPipeHelper() */
-            bmp?.compress(CompressFormat.WEBP, 100, FileOutputStream(pfd.fileDescriptor))
+            try {
+                bmp?.let { FileOutputStream(pfd.fileDescriptor).use { bmp.compress(CompressFormat.WEBP, 100, it) } }
+            } catch (e: IOException) {
+                logError(e)
+            }
         }
     }
 
@@ -393,10 +394,21 @@ class ArtworkProvider : ContentProvider() {
     private fun getPFDFromByteArray(byteArray: ByteArray?): ParcelFileDescriptor {
         return super.openPipeHelper(Uri.EMPTY, MIME_TYPE_IMAGE_WEBP, null, byteArray
         ) { pfd: ParcelFileDescriptor, _: Uri, _: String, _: Bundle?, bArray: ByteArray? ->
-            if (bArray != null) FileOutputStream(pfd.fileDescriptor).write(bArray)
+            try {
+                bArray?.let { FileOutputStream(pfd.fileDescriptor).use { it.write(bArray) } }
+            } catch (e: IOException) {
+                logError(e)
+            }
         }
     }
 
+    private fun logError(e: Exception) {
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
+            Log.e(TAG, "Could not transfer cover art", e)
+        else
+            Log.e(TAG, "Could not transfer cover art to caller: $callingPackage", e)
+    }
+
     private val dateFormatter by lazy {
         object : ThreadLocal<SimpleDateFormat>() {
             override fun initialValue() = SimpleDateFormat("hhmmss.SSS", Locale.getDefault())
diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
index a01943060..9e6cb4504 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
@@ -480,7 +480,7 @@ class MediaSessionBrowser : ExtensionManagerActivity {
 
         /**
          * The search method is passed a simple query string absent metadata indicating
-         * the user's intent to load a playlist, album, artist, or song. This is slightly different
+         * the user's intent to load a playlist, genre, artist, album, or song. This is slightly different
          * than PlaybackService.onPlayFromSearch (which is also invoked by voice search) and allows
          * the user to navigate to other content via on-screen menus.
          */
@@ -491,6 +491,7 @@ class MediaSessionBrowser : ExtensionManagerActivity {
             val searchAggregate = Medialibrary.getInstance().search(query, false)
             val searchMediaId = ID_SEARCH.toUri().buildUpon().appendQueryParameter("query", query).toString()
             results.addAll(buildMediaItems(context, ID_PLAYLIST, searchAggregate.playlists, res.getString(R.string.playlists)))
+            results.addAll(buildMediaItems(context, ID_GENRE, searchAggregate.genres, res.getString(R.string.genres)))
             results.addAll(buildMediaItems(context, ID_ARTIST, searchAggregate.artists, res.getString(R.string.artists)))
             results.addAll(buildMediaItems(context, ID_ALBUM, searchAggregate.albums, res.getString(R.string.albums)))
             results.addAll(buildMediaItems(context, searchMediaId, searchAggregate.tracks, res.getString(R.string.tracks)))



More information about the Android mailing list