From 2af13a4e6ca6391137348c46ec230f0e2c0b047e Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Tue, 6 Oct 2020 09:40:16 +0530 Subject: [PATCH] Artist fallback Fallback to fetch Album cover for missing artists --- .../retromusic/glide/ArtistGlideRequest.java | 2 +- .../glide/artistimage/ArtistImageLoader.kt | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/glide/ArtistGlideRequest.java b/app/src/main/java/code/name/monkey/retromusic/glide/ArtistGlideRequest.java index 4d9d2124..c11ffcc2 100644 --- a/app/src/main/java/code/name/monkey/retromusic/glide/ArtistGlideRequest.java +++ b/app/src/main/java/code/name/monkey/retromusic/glide/ArtistGlideRequest.java @@ -62,7 +62,7 @@ public class ArtistGlideRequest { boolean hasCustomImage = CustomArtistImageUtil.Companion.getInstance(App.Companion.getContext()) .hasCustomArtistImage(artist); if (noCustomImage || !hasCustomImage) { - return requestManager.load(new ArtistImage(artist.getName())); + return requestManager.load(new ArtistImage(artist)); } else { return requestManager.load(CustomArtistImageUtil.getFile(artist)); } diff --git a/app/src/main/java/code/name/monkey/retromusic/glide/artistimage/ArtistImageLoader.kt b/app/src/main/java/code/name/monkey/retromusic/glide/artistimage/ArtistImageLoader.kt index 43d23120..9b67cb62 100644 --- a/app/src/main/java/code/name/monkey/retromusic/glide/artistimage/ArtistImageLoader.kt +++ b/app/src/main/java/code/name/monkey/retromusic/glide/artistimage/ArtistImageLoader.kt @@ -15,6 +15,7 @@ package code.name.monkey.retromusic.glide.artistimage import android.content.Context +import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Data import code.name.monkey.retromusic.network.DeezerService import code.name.monkey.retromusic.util.MusicUtil @@ -34,7 +35,7 @@ import java.io.IOException import java.io.InputStream import java.util.concurrent.TimeUnit -class ArtistImage(val artistName: String) +class ArtistImage(val artist: Artist) class ArtistImageFetcher( private val context: Context, @@ -53,7 +54,7 @@ class ArtistImageFetcher( } override fun getId(): String { - return model.artistName + return model.artist.name } override fun cancel() { @@ -62,10 +63,10 @@ class ArtistImageFetcher( } override fun loadData(priority: Priority?): InputStream? { - if (!MusicUtil.isArtistNameUnknown(model.artistName) && + if (!MusicUtil.isArtistNameUnknown(model.artist.name) && PreferenceUtil.isAllowedToDownloadMetadata() ) { - val artists = model.artistName.split(",") + val artists = model.artist.name.split(",") val response = deezerService.getArtistImage(artists[0]).execute() if (!response.isSuccessful) { @@ -85,13 +86,21 @@ class ArtistImageFetcher( val glideUrl = GlideUrl(imageUrl) urlFetcher = urlLoader.getResourceFetcher(glideUrl, width, height) urlFetcher?.loadData(priority) - } else null + } else { + getFallbackAlbumImage() + } } catch (e: Exception) { - null + getFallbackAlbumImage() } } else return null } + private fun getFallbackAlbumImage(): InputStream? { + val imageUri = MusicUtil.getMediaStoreAlbumCoverUri(model.artist.safeGetFirstAlbum().id) + return context.contentResolver.openInputStream(imageUri) + } + + private fun getHighestQuality(imageUrl: Data): String { return when { imageUrl.pictureXl.isNotEmpty() -> imageUrl.pictureXl