Skip to content

Commit

Permalink
Allow custom Volley requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Feb 7, 2015
1 parent 52ec8e3 commit 3dcad68
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bumptech.glide.integration.volley;

import com.android.volley.Request;
import com.android.volley.Request.Priority;

import java.io.InputStream;

/**
* Used to construct a custom Volley request, such as for authentication header decoration.
*/
public interface VolleyRequestFactory {

/**
* Returns a Volley request for the given image url. The given future should be set as a
* listener or called when the request completes.
*/
Request<byte[]> create(String url, VolleyRequestFuture<InputStream> future, Priority priority);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@
* A DataFetcher backed by volley for fetching images via http.
*/
public class VolleyStreamFetcher implements DataFetcher<InputStream> {
public static final VolleyRequestFactory DEFAULT_REQUEST_FACTORY = new VolleyRequestFactory() {

@Override
public Request<byte[]> create(
String url, VolleyRequestFuture<InputStream> future,
Request.Priority priority) {
return new GlideRequest(url, future, priority);
}

};

private final RequestQueue requestQueue;
private final VolleyRequestFactory requestFactory;
private final GlideUrl url;
private VolleyRequestFuture<InputStream> requestFuture;

Expand All @@ -27,8 +39,14 @@ public VolleyStreamFetcher(RequestQueue requestQueue, GlideUrl url) {

public VolleyStreamFetcher(RequestQueue requestQueue, GlideUrl url,
VolleyRequestFuture<InputStream> requestFuture) {
this(requestQueue, url, requestFuture, DEFAULT_REQUEST_FACTORY);
}

public VolleyStreamFetcher(RequestQueue requestQueue, GlideUrl url,
VolleyRequestFuture<InputStream> requestFuture, VolleyRequestFactory requestFactory) {
this.requestQueue = requestQueue;
this.url = url;
this.requestFactory = requestFactory;
this.requestFuture = requestFuture;
if (requestFuture == null) {
this.requestFuture = VolleyRequestFuture.newFuture();
Expand All @@ -39,7 +57,8 @@ public VolleyStreamFetcher(RequestQueue requestQueue, GlideUrl url,
public InputStream loadData(Priority priority) throws Exception {
// Make sure the string url safely encodes non ascii characters.
String stringUrl = url.toURL().toString();
GlideRequest request = new GlideRequest(stringUrl, requestFuture, glideToVolleyPriority(priority));
Request<byte[]> request = requestFactory.create(
stringUrl, requestFuture, glideToVolleyPriority(priority));

requestFuture.setRequest(requestQueue.add(request));

Expand Down Expand Up @@ -80,7 +99,7 @@ private static Request.Priority glideToVolleyPriority(Priority priority) {

private static class GlideRequest extends Request<byte[]> {
private final VolleyRequestFuture<InputStream> future;
private Priority priority;
private final Priority priority;

public GlideRequest(String url, VolleyRequestFuture<InputStream> future, Priority priority) {
super(Method.GET, url, future);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class VolleyUrlLoader implements ModelLoader<GlideUrl, InputStream> {
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
private static RequestQueue internalQueue;
private RequestQueue requestQueue;
private final VolleyRequestFactory requestFactory;

private static RequestQueue getInternalQueue(Context context) {
if (internalQueue == null) {
Expand All @@ -46,12 +47,21 @@ public Factory(Context context) {
* Constructor for a new Factory that runs requests using the given {@link RequestQueue}.
*/
public Factory(RequestQueue requestQueue) {
this(requestQueue, VolleyStreamFetcher.DEFAULT_REQUEST_FACTORY);
}

/**
* Constructor for a new Factory with a custom Volley request factory that runs requests
* using the given {@link RequestQueue}.
*/
public Factory(RequestQueue requestQueue, VolleyRequestFactory requestFactory) {
this.requestFactory = requestFactory;
this.requestQueue = requestQueue;
}

@Override
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
return new VolleyUrlLoader(requestQueue);
return new VolleyUrlLoader(requestQueue, requestFactory);
}

@Override
Expand All @@ -61,13 +71,20 @@ public void teardown() {
}

private final RequestQueue requestQueue;
private final VolleyRequestFactory requestFactory;

public VolleyUrlLoader(RequestQueue requestQueue) {
this(requestQueue, VolleyStreamFetcher.DEFAULT_REQUEST_FACTORY);
}

public VolleyUrlLoader(RequestQueue requestQueue, VolleyRequestFactory requestFactory) {
this.requestQueue = requestQueue;
this.requestFactory = requestFactory;
}

@Override
public DataFetcher<InputStream> getResourceFetcher(GlideUrl url, int width, int height) {
return new VolleyStreamFetcher(requestQueue, url, new VolleyRequestFuture<InputStream>());
return new VolleyStreamFetcher(
requestQueue, url, new VolleyRequestFuture<InputStream>(), requestFactory);
}
}

0 comments on commit 3dcad68

Please sign in to comment.