Updated to OkHttp3

This commit is contained in:
Karim Abou Zeid 2016-01-10 20:52:24 +01:00
commit 95d26adbfa
11 changed files with 259 additions and 54 deletions

View file

@ -15,7 +15,7 @@ import com.kabouzeid.gramophone.util.Util;
import java.io.IOException;
import java.io.InputStream;
import retrofit.Response;
import retrofit2.Response;
/**
* @author Karim Abou Zeid (kabouzeid)
@ -30,7 +30,6 @@ 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;
@ -61,9 +60,8 @@ public class ArtistImageFetcher implements DataFetcher<InputStream> {
GlideUrl url = new GlideUrl(LastFMUtil.getLargestArtistImageUrl(lastFmArtist.getArtist().getImage()));
urlFetcher = urlLoader.getResourceFetcher(url, width, height);
inputStream = urlFetcher.loadData(priority);
return inputStream;
return urlFetcher.loadData(priority);
}
return null;
}

View file

@ -2,24 +2,28 @@ package com.kabouzeid.gramophone.glide.artistimage;
import android.content.Context;
import com.bumptech.glide.integration.okhttp.OkHttpUrlLoader;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
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.glide.okhttp.OkHttpUrlLoader;
import com.kabouzeid.gramophone.lastfm.rest.LastFMRestClient;
import com.squareup.okhttp.OkHttpClient;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
/**
* @author Karim Abou Zeid (kabouzeid)
*/
public class ArtistImageLoader implements StreamModelLoader<ArtistImage> {
// we need these very low values to make sure our artist image loading calls doesn't block the image loading queue
private static final int TIMEOUT = 500;
private Context context;
private LastFMRestClient lastFMClient;
private ModelLoader<GlideUrl, InputStream> urlLoader;
@ -40,14 +44,16 @@ public class ArtistImageLoader implements StreamModelLoader<ArtistImage> {
private OkHttpUrlLoader.Factory okHttpFactory;
public Factory(Context 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);
okHttpFactory = new OkHttpUrlLoader.Factory(new OkHttpClient.Builder()
.connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.build());
lastFMClient = new LastFMRestClient(LastFMRestClient.createDefaultOkHttpClientBuilder(context)
.connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
.build());
}
@Override

View file

@ -0,0 +1,31 @@
package com.kabouzeid.gramophone.glide.okhttp;
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.GlideModule;
import java.io.InputStream;
/**
* A {@link GlideModule} implementation to replace Glide's default
* {@link java.net.HttpURLConnection} based {@link com.bumptech.glide.load.model.ModelLoader}
* with an OkHttp based {@link com.bumptech.glide.load.model.ModelLoader}.
* <p/>
* <p> If you're using gradle, you can include this module simply by depending on the aar, the
* module will be merged in by manifest merger. For other build systems or for more more
* information, see {@link GlideModule}. </p>
*/
public class OkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Do nothing.
}
@Override
public void registerComponents(Context context, Glide glide) {
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}

View file

@ -0,0 +1,78 @@
package com.kabouzeid.gramophone.glide.okhttp;
import android.util.Log;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* Fetches an {@link InputStream} using the okhttp library.
*/
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private static final String TAG = "OkHttpFetcher";
private final Call.Factory client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) {
this.client = client;
this.url = url;
}
@Override
public InputStream loadData(Priority priority) throws Exception {
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
long contentLength = response.body().contentLength();
responseBody = response.body();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
} else {
Log.d(TAG, "OkHttp got error response: " + response.code() + ", " + response.message());
}
return stream;
}
@Override
public void cleanup() {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
// Ignored
}
if (responseBody != null) {
responseBody.close();
}
}
@Override
public String getId() {
return url.getCacheKey();
}
@Override
public void cancel() {
// TODO: call cancel on the client when this method is called on a background thread. See #257
}
}

View file

@ -0,0 +1,76 @@
package com.kabouzeid.gramophone.glide.okhttp;
import android.content.Context;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.OkHttpClient;
/**
* A simple model loader for fetching media over http/https using OkHttp.
*/
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
private final Call.Factory client;
public OkHttpUrlLoader(Call.Factory client) {
this.client = client;
}
@Override
public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) {
return new OkHttpStreamFetcher(client, model);
}
/**
* The default factory for {@link OkHttpUrlLoader}s.
*/
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
private static volatile Call.Factory internalClient;
private Call.Factory client;
private static Call.Factory getInternalClient() {
if (internalClient == null) {
synchronized (Factory.class) {
if (internalClient == null) {
internalClient = new OkHttpClient();
}
}
}
return internalClient;
}
/**
* Constructor for a new Factory that runs requests using a static singleton client.
*/
public Factory() {
this(getInternalClient());
}
/**
* Constructor for a new Factory that runs requests using given client.
*
* @param client this is typically an instance of {@code OkHttpClient}.
*/
public Factory(Call.Factory client) {
this.client = client;
}
@Override
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
return new OkHttpUrlLoader(client);
}
@Override
public void teardown() {
// Do nothing, this instance doesn't own the client.
}
}
}