From 6565d4b2f09fc4d2a66e543502257268bee57ce0 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sat, 20 Jan 2018 09:13:17 +0100 Subject: [PATCH] Prepares proxy for pictures --- .../client/Glide/CustomStreamFetcher.java | 10 ++++++--- .../mastodon/client/Glide/HttpsUrlLoader.java | 22 ++++++++++++++----- .../client/Glide/TLSLibraryGlideModule.java | 5 +++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/CustomStreamFetcher.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/CustomStreamFetcher.java index bd3b8739b..c62a81a4f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/CustomStreamFetcher.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/CustomStreamFetcher.java @@ -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 { private GlideUrl url; + private WeakReference contextWeakReference; - CustomStreamFetcher(GlideUrl url) { + CustomStreamFetcher(Context context, GlideUrl url) { + this.contextWeakReference = new WeakReference<>(context); this.url = url; } @Override - public void loadData(Priority priority, DataCallback callback) { - callback.onDataReady(new HttpsConnection().getPicture(url.toStringUrl())); + public void loadData(@NonNull Priority priority, @NonNull DataCallback callback) { + callback.onDataReady(new HttpsConnection(this.contextWeakReference.get()).getPicture(url.toStringUrl())); } @Override diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/HttpsUrlLoader.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/HttpsUrlLoader.java index dee392f0d..7318986d6 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/HttpsUrlLoader.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/HttpsUrlLoader.java @@ -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 { + private static WeakReference contextWeakReference; - HttpsUrlLoader() {} + HttpsUrlLoader(Context context) { + contextWeakReference = new WeakReference<>(context);} @Nullable @Override - public LoadData buildLoadData(GlideUrl glideUrl, int width, int height, Options options) { - return new LoadData<>(glideUrl, new CustomStreamFetcher(glideUrl)); + public LoadData 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 { + Factory(Context context){ + contextWeakReference = new WeakReference<>(context); + } + @NonNull @Override - public ModelLoader build(MultiModelLoaderFactory multiFactory) { - return new HttpsUrlLoader(); + public ModelLoader build(@NonNull MultiModelLoaderFactory multiFactory) { + return new HttpsUrlLoader(contextWeakReference.get()); } @Override public void teardown() { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/TLSLibraryGlideModule.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/TLSLibraryGlideModule.java index 72960e688..93760e525 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/TLSLibraryGlideModule.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Glide/TLSLibraryGlideModule.java @@ -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