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