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