From 0cbfae0188b0bd4d9b1e6336e84844335a13b256 Mon Sep 17 00:00:00 2001 From: Abestanis Date: Sun, 14 Jul 2024 02:24:10 +0200 Subject: [PATCH] Use getContentUri on newer Android versions --- .../sweyer_plugin/handlers/FetchHandler.kt | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/sweyer_plugin/android/src/main/kotlin/com/nt4f04und/sweyer/sweyer_plugin/handlers/FetchHandler.kt b/sweyer_plugin/android/src/main/kotlin/com/nt4f04und/sweyer/sweyer_plugin/handlers/FetchHandler.kt index 1103004a8..ed9e58f00 100644 --- a/sweyer_plugin/android/src/main/kotlin/com/nt4f04und/sweyer/sweyer_plugin/handlers/FetchHandler.kt +++ b/sweyer_plugin/android/src/main/kotlin/com/nt4f04und/sweyer/sweyer_plugin/handlers/FetchHandler.kt @@ -32,7 +32,12 @@ object FetchHandler { } fun retrieveSongs(resolver: ContentResolver): ArrayList> { - return executeQuery(resolver, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songsSelection, buildMap { + val contentUrl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL) + } else { + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + } + return executeQuery(resolver, contentUrl, songsSelection, buildMap { put(MediaStore.Audio.Media._ID, "id" to { cursor, index -> cursor.getInt(index) }) put(MediaStore.Audio.Media.ALBUM, "album" to { cursor, index -> cursor.getString(index) }) put(MediaStore.Audio.Media.ALBUM_ID, "albumId" to { cursor, index -> cursor.getInt(index) }) @@ -115,10 +120,15 @@ object FetchHandler { } else { "artist_id" } + val contentUrl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Audio.Albums.getContentUri(MediaStore.VOLUME_EXTERNAL) + } else { + MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI + } try { return executeQuery( resolver, - MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, + contentUrl, MediaStore.Audio.Albums.ALBUM + " IS NOT NULL", mapOf( MediaStore.Audio.Albums._ID to Pair("id") { cursor, index -> cursor.getInt(index) }, @@ -138,7 +148,7 @@ object FetchHandler { } catch (ignored: Throwable) { return executeQuery( resolver, - MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, + contentUrl, MediaStore.Audio.Albums.ALBUM + " IS NOT NULL", mapOf( MediaStore.Audio.Albums._ID to Pair("id") { cursor, index -> cursor.getInt(index) }, @@ -159,9 +169,14 @@ object FetchHandler { fun retrievePlaylists(resolver: ContentResolver): ArrayList> { val memberProjection = arrayOf(MediaStore.Audio.Playlists.Members.AUDIO_ID) + val contentUrl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Audio.Playlists.getContentUri(MediaStore.VOLUME_EXTERNAL) + } else { + MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI + } return executeQuery( resolver, - MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, + contentUrl, MediaStore.Audio.Playlists.NAME + " IS NOT NULL", mapOf( MediaStore.Audio.Playlists._ID to Pair("id") { cursor, index -> cursor.getLong(index) }, @@ -191,9 +206,14 @@ object FetchHandler { } fun retrieveArtists(resolver: ContentResolver): ArrayList> { + val contentUrl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Audio.Artists.getContentUri(MediaStore.VOLUME_EXTERNAL) + } else { + MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI + } return executeQuery( resolver, - MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, + contentUrl, MediaStore.Audio.Artists.ARTIST + " IS NOT NULL", mapOf( MediaStore.Audio.Artists._ID to Pair("id") { cursor, index -> cursor.getInt(index) }, @@ -210,9 +230,14 @@ object FetchHandler { fun retrieveGenres(resolver: ContentResolver): ArrayList> { val memberProjection = arrayOf(MediaStore.Audio.Genres.Members._ID) + val contentUrl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Audio.Genres.getContentUri(MediaStore.VOLUME_EXTERNAL) + } else { + MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI + } return executeQuery( resolver, - MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, + contentUrl, MediaStore.Audio.Genres.NAME + " IS NOT NULL", mapOf( MediaStore.Audio.Genres._ID to Pair("id") { cursor, index -> cursor.getInt(index) },