Prepares proxy for pictures
This commit is contained in:
parent
72b15d3893
commit
6565d4b2f0
|
@ -1,5 +1,6 @@
|
|||
package fr.gouv.etalab.mastodon.client.Glide;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.Priority;
|
||||
|
@ -8,6 +9,7 @@ import com.bumptech.glide.load.data.DataFetcher;
|
|||
import com.bumptech.glide.load.model.GlideUrl;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.HttpsConnection;
|
||||
|
||||
|
@ -19,14 +21,16 @@ import fr.gouv.etalab.mastodon.client.HttpsConnection;
|
|||
public class CustomStreamFetcher implements DataFetcher<InputStream> {
|
||||
|
||||
private GlideUrl url;
|
||||
private WeakReference<Context> contextWeakReference;
|
||||
|
||||
CustomStreamFetcher(GlideUrl url) {
|
||||
CustomStreamFetcher(Context context, GlideUrl url) {
|
||||
this.contextWeakReference = new WeakReference<>(context);
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
|
||||
callback.onDataReady(new HttpsConnection().getPicture(url.toStringUrl()));
|
||||
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
|
||||
callback.onDataReady(new HttpsConnection(this.contextWeakReference.get()).getPicture(url.toStringUrl()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package fr.gouv.etalab.mastodon.client.Glide;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.bumptech.glide.load.Options;
|
||||
|
@ -9,6 +11,8 @@ import com.bumptech.glide.load.model.ModelLoaderFactory;
|
|||
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -18,26 +22,32 @@ import java.io.InputStream;
|
|||
|
||||
public class HttpsUrlLoader implements ModelLoader<GlideUrl, InputStream> {
|
||||
|
||||
private static WeakReference<Context> contextWeakReference;
|
||||
|
||||
HttpsUrlLoader() {}
|
||||
HttpsUrlLoader(Context context) {
|
||||
contextWeakReference = new WeakReference<>(context);}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public LoadData<InputStream> buildLoadData(GlideUrl glideUrl, int width, int height, Options options) {
|
||||
return new LoadData<>(glideUrl, new CustomStreamFetcher(glideUrl));
|
||||
public LoadData<InputStream> buildLoadData(@NonNull GlideUrl glideUrl, int width, int height, @NonNull Options options) {
|
||||
return new LoadData<>(glideUrl, new CustomStreamFetcher(contextWeakReference.get(), glideUrl));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handles(GlideUrl glideUrl) {
|
||||
public boolean handles(@NonNull GlideUrl glideUrl) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
|
||||
|
||||
Factory(Context context){
|
||||
contextWeakReference = new WeakReference<>(context);
|
||||
}
|
||||
@NonNull
|
||||
@Override
|
||||
public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
|
||||
return new HttpsUrlLoader();
|
||||
public ModelLoader<GlideUrl, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
|
||||
return new HttpsUrlLoader(contextWeakReference.get());
|
||||
}
|
||||
@Override
|
||||
public void teardown() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package fr.gouv.etalab.mastodon.client.Glide;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.GlideBuilder;
|
||||
|
@ -20,8 +21,8 @@ import java.io.InputStream;
|
|||
public final class TLSLibraryGlideModule extends AppGlideModule {
|
||||
|
||||
@Override
|
||||
public void registerComponents(Context context, Glide glide, Registry registry) {
|
||||
registry.replace(GlideUrl.class, InputStream.class, new HttpsUrlLoader.Factory());
|
||||
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
|
||||
registry.replace(GlideUrl.class, InputStream.class, new HttpsUrlLoader.Factory(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue