From a2373b817a06156527abb9686a61a22b0f404b5f Mon Sep 17 00:00:00 2001 From: wb9688 Date: Thu, 22 Feb 2018 13:25:56 +0100 Subject: [PATCH] Use OkHttp with UIL --- app/src/main/java/org/schabi/newpipe/App.java | 1 + .../java/org/schabi/newpipe/Downloader.java | 15 ++++++++++- .../org/schabi/newpipe/ImageDownloader.java | 25 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/schabi/newpipe/ImageDownloader.java diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index ee119dbc8..e39207d39 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -168,6 +168,7 @@ public class App extends Application { return new ImageLoaderConfiguration.Builder(this) .memoryCache(new LRULimitedMemoryCache(memoryCacheSizeMb * 1024 * 1024)) .diskCacheSize(diskCacheSizeMb * 1024 * 1024) + .imageDownloader(new ImageDownloader(getApplicationContext())) .build(); } diff --git a/app/src/main/java/org/schabi/newpipe/Downloader.java b/app/src/main/java/org/schabi/newpipe/Downloader.java index a143c5cb7..d9537c6b6 100644 --- a/app/src/main/java/org/schabi/newpipe/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/Downloader.java @@ -6,6 +6,7 @@ import android.text.TextUtils; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import java.io.IOException; +import java.io.InputStream; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -98,6 +99,18 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { */ @Override public String download(String siteUrl, Map customProperties) throws IOException, ReCaptchaException { + return getBody(siteUrl, customProperties).string(); + } + + public InputStream stream(String siteUrl) throws IOException { + try { + return getBody(siteUrl, Collections.emptyMap()).byteStream(); + } catch (ReCaptchaException e) { + throw new IOException(e.getMessage(), e.getCause()); + } + } + + private ResponseBody getBody(String siteUrl, Map customProperties) throws IOException, ReCaptchaException { final Request.Builder requestBuilder = new Request.Builder() .method("GET", null).url(siteUrl) .addHeader("User-Agent", USER_AGENT); @@ -123,7 +136,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { return null; } - return body.string(); + return body; } /** diff --git a/app/src/main/java/org/schabi/newpipe/ImageDownloader.java b/app/src/main/java/org/schabi/newpipe/ImageDownloader.java new file mode 100644 index 000000000..5ea067d00 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/ImageDownloader.java @@ -0,0 +1,25 @@ +package org.schabi.newpipe; + +import android.content.Context; + +import com.nostra13.universalimageloader.core.download.BaseImageDownloader; + +import org.schabi.newpipe.extractor.NewPipe; + +import java.io.IOException; +import java.io.InputStream; + +public class ImageDownloader extends BaseImageDownloader { + public ImageDownloader(Context context) { + super(context); + } + + public ImageDownloader(Context context, int connectTimeout, int readTimeout) { + super(context, connectTimeout, readTimeout); + } + + protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException { + Downloader downloader = (Downloader) NewPipe.getDownloader(); + return downloader.stream(imageUri); + } +}