From 4165d848d8d8956c4fa14002920ec17729551b34 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sun, 2 Aug 2015 17:51:33 +0200 Subject: [PATCH] Respect the mobile download settings --- .../core/glide/ApOkHttpUrlLoader.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java b/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java index 33290723b..433ba8db4 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java @@ -21,18 +21,20 @@ import java.net.HttpURLConnection; import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.service.download.HttpDownloader; import de.danoeh.antennapod.core.storage.DBReader; +import de.danoeh.antennapod.core.util.NetworkUtils; /** * @see com.bumptech.glide.integration.okhttp.OkHttpUrlLoader */ public class ApOkHttpUrlLoader implements ModelLoader { - private static final String TAG = "ApOkHttpUrlLoader"; + private static final String TAG = ApOkHttpUrlLoader.class.getSimpleName(); /** * The default factory for {@link ApOkHttpUrlLoader}s. */ public static class Factory implements ModelLoaderFactory { + private static volatile OkHttpClient internalClient; private OkHttpClient client; @@ -41,6 +43,7 @@ public class ApOkHttpUrlLoader implements ModelLoader { synchronized (Factory.class) { if (internalClient == null) { internalClient = new OkHttpClient(); + internalClient.interceptors().add(new NetworkAllowanceInterceptor()); internalClient.interceptors().add(new BasicAuthenticationInterceptor()); } } @@ -84,6 +87,19 @@ public class ApOkHttpUrlLoader implements ModelLoader { return new OkHttpStreamFetcher(client, model); } + private static class NetworkAllowanceInterceptor implements Interceptor { + + @Override + public Response intercept(Chain chain) throws IOException { + if (NetworkUtils.isDownloadAllowed()) { + return chain.proceed(chain.request()); + } else { + return null; + } + } + + } + private static class BasicAuthenticationInterceptor implements Interceptor { @Override