diff --git a/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageFetcher.java b/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageFetcher.java index fa695559..1ac89bb6 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageFetcher.java +++ b/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageFetcher.java @@ -12,8 +12,12 @@ import com.kabouzeid.gramophone.util.LastFMUtil; import com.kabouzeid.gramophone.util.MusicUtil; import com.kabouzeid.gramophone.util.Util; +import java.io.IOException; import java.io.InputStream; +import hugo.weaving.DebugLog; +import retrofit.Response; + /** * @author Karim Abou Zeid (kabouzeid) */ @@ -27,6 +31,7 @@ public class ArtistImageFetcher implements DataFetcher { private final int height; private volatile boolean isCancelled; private DataFetcher urlFetcher; + private InputStream inputStream; public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader urlLoader, int width, int height) { this.context = context; @@ -45,12 +50,21 @@ public class ArtistImageFetcher implements DataFetcher { @Override public InputStream loadData(Priority priority) throws Exception { if (!MusicUtil.isArtistNameUnknown(model.artistName) && Util.isAllowedToAutoDownload(context)) { - LastFmArtist lastFmArtist = lastFMRestClient.getApiService().getArtistInfo(model.artistName, model.skipOkHttpCache ? "no-cache" : null).execute().body(); + Response response = lastFMRestClient.getApiService().getArtistInfo(model.artistName, model.skipOkHttpCache ? "no-cache" : null).execute(); + + if (!response.isSuccess()) { + throw new IOException("Request failed with code: " + response.code()); + } + + LastFmArtist lastFmArtist = response.body(); if (isCancelled) return null; - urlFetcher = urlLoader.getResourceFetcher(new GlideUrl(LastFMUtil.getLargestArtistImageUrl(lastFmArtist.getArtist().getImage())), width, height); - return urlFetcher.loadData(priority); + GlideUrl url = new GlideUrl(LastFMUtil.getLargestArtistImageUrl(lastFmArtist.getArtist().getImage())); + urlFetcher = urlLoader.getResourceFetcher(url, width, height); + inputStream = urlFetcher.loadData(priority); + + return inputStream; } return null; } @@ -62,11 +76,19 @@ public class ArtistImageFetcher implements DataFetcher { } } + @DebugLog @Override public void cancel() { isCancelled = true; if (urlFetcher != null) { urlFetcher.cancel(); } + if (inputStream != null) { + try { + inputStream.close(); + } catch (Throwable t) { + t.printStackTrace(); + } + } } } diff --git a/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageLoader.java b/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageLoader.java index fe08678a..815c9381 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageLoader.java +++ b/app/src/main/java/com/kabouzeid/gramophone/glide/artistimage/ArtistImageLoader.java @@ -10,8 +10,10 @@ import com.bumptech.glide.load.model.ModelLoader; import com.bumptech.glide.load.model.ModelLoaderFactory; import com.bumptech.glide.load.model.stream.StreamModelLoader; import com.kabouzeid.gramophone.lastfm.rest.LastFMRestClient; +import com.squareup.okhttp.OkHttpClient; import java.io.InputStream; +import java.util.concurrent.TimeUnit; /** * @author Karim Abou Zeid (kabouzeid) @@ -38,8 +40,14 @@ public class ArtistImageLoader implements StreamModelLoader { private OkHttpUrlLoader.Factory okHttpFactory; public Factory(Context context) { - okHttpFactory = new OkHttpUrlLoader.Factory(); - lastFMClient = new LastFMRestClient(context); + // we need these very low values to make sure our artist image loading calls doesn't block the image loading queue + OkHttpClient okHttpClient = new OkHttpClient(); + okHttpClient.setConnectTimeout(500, TimeUnit.MILLISECONDS); + okHttpClient.setReadTimeout(500, TimeUnit.MILLISECONDS); + okHttpClient.setWriteTimeout(500, TimeUnit.MILLISECONDS); + + okHttpFactory = new OkHttpUrlLoader.Factory(okHttpClient); + lastFMClient = new LastFMRestClient(context, okHttpClient); } @Override diff --git a/app/src/main/java/com/kabouzeid/gramophone/lastfm/rest/LastFMRestClient.java b/app/src/main/java/com/kabouzeid/gramophone/lastfm/rest/LastFMRestClient.java index 85110f5e..e1718087 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/lastfm/rest/LastFMRestClient.java +++ b/app/src/main/java/com/kabouzeid/gramophone/lastfm/rest/LastFMRestClient.java @@ -25,8 +25,10 @@ public class LastFMRestClient { private LastFMService apiService; public LastFMRestClient(@NonNull Context context) { - OkHttpClient okHttpClient = new OkHttpClient(); + this(context, new OkHttpClient()); + } + public LastFMRestClient(@NonNull Context context, @NonNull OkHttpClient okHttpClient) { File cacheDir = new File(context.getCacheDir().getAbsolutePath(), "/okhttp-lastfm/"); if (cacheDir.mkdirs() || cacheDir.isDirectory()) { okHttpClient.setCache(new Cache(cacheDir, 1024 * 1024 * 10)); diff --git a/app/src/main/java/com/kabouzeid/gramophone/ui/activities/ArtistDetailActivity.java b/app/src/main/java/com/kabouzeid/gramophone/ui/activities/ArtistDetailActivity.java index d2d08250..22babb44 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/ui/activities/ArtistDetailActivity.java +++ b/app/src/main/java/com/kabouzeid/gramophone/ui/activities/ArtistDetailActivity.java @@ -265,21 +265,19 @@ public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implement .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .listener(new RequestListener() { @Override - public boolean onException(Exception e, ArtistImage model, Target target, boolean isFirstResource) { - toastUpdatedArtistImageIfDownloadWasForced(); + public boolean onException(@Nullable Exception e, ArtistImage model, Target target, boolean isFirstResource) { + if (forceDownload) { + Toast.makeText(ArtistDetailActivity.this, e != null ? e.getClass().getSimpleName() : "Error", Toast.LENGTH_SHORT).show(); + } return false; } @Override public boolean onResourceReady(BitmapPaletteWrapper resource, ArtistImage model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { - toastUpdatedArtistImageIfDownloadWasForced(); - return false; - } - - private void toastUpdatedArtistImageIfDownloadWasForced() { if (forceDownload) { Toast.makeText(ArtistDetailActivity.this, getString(R.string.updated_artist_image), Toast.LENGTH_SHORT).show(); } + return false; } }) .into(new PhonographColoredTarget(artistImage) {