From b4f595eb75cf63e634ac273271ad5b2a9cc7a061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Mauduit?= Date: Wed, 7 Dec 2016 22:09:24 +0100 Subject: [PATCH] Make Downloader class a Singleton --- app/src/main/java/org/schabi/newpipe/App.java | 2 +- .../main/java/org/schabi/newpipe/Downloader.java | 15 +++++++++++++++ .../org/schabi/newpipe/report/ErrorActivity.java | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 7e93969bc..f9b95c457 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -62,7 +62,7 @@ public class App extends Application { } //init NewPipe - NewPipe.init(new Downloader()); + NewPipe.init(Downloader.getInstance()); // Initialize image loader ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build(); diff --git a/app/src/main/java/org/schabi/newpipe/Downloader.java b/app/src/main/java/org/schabi/newpipe/Downloader.java index 04098a4d7..1cc4fcdf2 100644 --- a/app/src/main/java/org/schabi/newpipe/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/Downloader.java @@ -36,6 +36,21 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"; + private static Downloader instance = null; + + private Downloader() {} + + public static Downloader getInstance() { + if(instance == null) { + synchronized (Downloader.class) { + if (instance == null) { + instance = new Downloader(); + } + } + } + return instance; + } + /**Download the text file at the supplied URL as in download(String), * but set the HTTP header field "Accept-Language" to the supplied string. * @param siteUrl the URL of the text file to return the contents of diff --git a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java index 49cfe0471..85151efb3 100644 --- a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java @@ -473,7 +473,7 @@ public class ErrorActivity extends AppCompatActivity { public void run() { String ipRange = "none"; try { - Downloader dl = new Downloader(); + Downloader dl = Downloader.getInstance(); String ip = dl.download("https://ifcfg.me/ip"); ipRange = Parser.matchGroup1("([0-9]*\\.[0-9]*\\.)[0-9]*\\.[0-9]*", ip)