Reduced the artist image loading timeout, this should fix album images not showing up when being on slow network.

This commit is contained in:
Karim Abou Zeid 2016-01-05 17:52:10 +01:00
commit abebf06a27
4 changed files with 43 additions and 13 deletions

View file

@ -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<InputStream> {
private final int height;
private volatile boolean isCancelled;
private DataFetcher<InputStream> urlFetcher;
private InputStream inputStream;
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height) {
this.context = context;
@ -45,12 +50,21 @@ public class ArtistImageFetcher implements DataFetcher<InputStream> {
@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<LastFmArtist> 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<InputStream> {
}
}
@DebugLog
@Override
public void cancel() {
isCancelled = true;
if (urlFetcher != null) {
urlFetcher.cancel();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}

View file

@ -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<ArtistImage> {
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

View file

@ -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));

View file

@ -265,21 +265,19 @@ public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implement
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.listener(new RequestListener<ArtistImage, BitmapPaletteWrapper>() {
@Override
public boolean onException(Exception e, ArtistImage model, Target<BitmapPaletteWrapper> target, boolean isFirstResource) {
toastUpdatedArtistImageIfDownloadWasForced();
public boolean onException(@Nullable Exception e, ArtistImage model, Target<BitmapPaletteWrapper> 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<BitmapPaletteWrapper> 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) {