-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.dylanvann.fastimage" | ||
> | ||
> | ||
<application> | ||
<meta-data | ||
android:name="com.dylanvann.fastimage.OkHttpProgressGlideModule" | ||
android:value="GlideModule" | ||
/> | ||
<meta-data | ||
android:name="com.bumptech.glide.integration.okhttp.OkHttpGlideModule" | ||
tools:node="remove" | ||
android:value="GlideModule" | ||
/> | ||
</application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
android/src/main/java/com/dylanvann/fastimage/OkHttpProgressGlideModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package com.dylanvann.fastimage; | ||
|
||
import android.content.Context; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.GlideBuilder; | ||
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.module.GlideModule; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import okio.Buffer; | ||
import okio.BufferedSource; | ||
import okio.ForwardingSource; | ||
import okio.Okio; | ||
import okio.Source; | ||
|
||
public class OkHttpProgressGlideModule implements GlideModule { | ||
|
||
@Override | ||
public void applyOptions(Context context, GlideBuilder builder) { } | ||
|
||
@Override | ||
public void registerComponents(Context context, Glide glide) { | ||
OkHttpClient client = new OkHttpClient | ||
.Builder() | ||
.addInterceptor(createInterceptor(new DispatchingProgressListener())) | ||
.build(); | ||
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client)); | ||
} | ||
|
||
private static Interceptor createInterceptor(final ResponseProgressListener listener) { | ||
return new Interceptor() { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
Response response = chain.proceed(request); | ||
final String key = request.url().toString(); | ||
return response | ||
.newBuilder() | ||
.body(new OkHttpProgressResponseBody(key, response.body(), listener)) | ||
.build(); | ||
} | ||
}; | ||
} | ||
|
||
public static void forget(String url) { | ||
DispatchingProgressListener.forget(url); | ||
} | ||
|
||
public static void expect(String url, UIProgressListener listener) { | ||
DispatchingProgressListener.expect(url, listener); | ||
} | ||
|
||
private interface ResponseProgressListener { | ||
void update(String key, long bytesRead, long contentLength); | ||
} | ||
|
||
private static class DispatchingProgressListener implements ResponseProgressListener { | ||
private static final Map<String, UIProgressListener> LISTENERS = new HashMap<>(); | ||
private static final Map<String, Long> PROGRESSES = new HashMap<>(); | ||
|
||
private final Handler handler; | ||
|
||
DispatchingProgressListener() { | ||
this.handler = new Handler(Looper.getMainLooper()); | ||
} | ||
|
||
static void forget(String url) { | ||
LISTENERS.remove(url); | ||
PROGRESSES.remove(url); | ||
} | ||
|
||
static void expect(String url, UIProgressListener listener) { | ||
LISTENERS.put(url, listener); | ||
} | ||
|
||
@Override | ||
public void update(String key, final long bytesRead, final long contentLength) { | ||
final UIProgressListener listener = LISTENERS.get(key); | ||
if (listener == null) { | ||
return; | ||
} | ||
if (contentLength <= bytesRead) { | ||
forget(key); | ||
} | ||
if (needsDispatch(key, bytesRead, contentLength, listener.getGranularityPercentage())) { | ||
handler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
listener.onProgress(bytesRead, contentLength); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private boolean needsDispatch(String key, long current, long total, float granularity) { | ||
if (granularity == 0 || current == 0 || total == current) { | ||
return true; | ||
} | ||
float percent = 100f * current / total; | ||
long currentProgress = (long) (percent / granularity); | ||
Long lastProgress = PROGRESSES.get(key); | ||
if (lastProgress == null || currentProgress != lastProgress) { | ||
PROGRESSES.put(key, currentProgress); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
private static class OkHttpProgressResponseBody extends ResponseBody { | ||
private final String key; | ||
private final ResponseBody responseBody; | ||
private final ResponseProgressListener progressListener; | ||
private BufferedSource bufferedSource; | ||
|
||
OkHttpProgressResponseBody( | ||
String key, | ||
ResponseBody responseBody, | ||
ResponseProgressListener progressListener | ||
) { | ||
this.key = key; | ||
this.responseBody = responseBody; | ||
this.progressListener = progressListener; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return responseBody.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() { | ||
return responseBody.contentLength(); | ||
} | ||
|
||
@Override | ||
public BufferedSource source() { | ||
if (bufferedSource == null) { | ||
bufferedSource = Okio.buffer(source(responseBody.source())); | ||
} | ||
return bufferedSource; | ||
} | ||
|
||
private Source source(Source source) { | ||
return new ForwardingSource(source) { | ||
long totalBytesRead = 0L; | ||
|
||
@Override | ||
public long read(Buffer sink, long byteCount) throws IOException { | ||
long bytesRead = super.read(sink, byteCount); | ||
long fullLength = responseBody.contentLength(); | ||
if (bytesRead == -1) { | ||
// this source is exhausted | ||
totalBytesRead = fullLength; | ||
} else { | ||
totalBytesRead += bytesRead; | ||
} | ||
progressListener.update(key, totalBytesRead, fullLength); | ||
return bytesRead; | ||
} | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.