From af42e32ae6ef75cc1ae143694f79640ddf45e6de Mon Sep 17 00:00:00 2001 From: Kartikey Kushwaha Date: Sun, 12 Aug 2018 18:34:20 +0530 Subject: [PATCH] Code refactored and added comments. --- app/src/main/java/org/schabi/newpipe/App.java | 4 +-- ...sk.java => CheckForNewAppVersionTask.java} | 34 +++++++++++++------ .../newpipe/player/PopupVideoPlayer.java | 6 ++-- 3 files changed, 26 insertions(+), 18 deletions(-) rename app/src/main/java/org/schabi/newpipe/{FetchAppVersionTask.java => CheckForNewAppVersionTask.java} (80%) diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 0a218d061..fe64d95fb 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -101,9 +101,7 @@ public class App extends Application { configureRxJavaErrorHandler(); // Check for new version - if (BuildConfig.FLAVOR.equals("github")) { - new FetchAppVersionTask().execute(); - } + new CheckForNewAppVersionTask().execute(); } protected Downloader getDownloader() { diff --git a/app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java b/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java similarity index 80% rename from app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java rename to app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java index 3130dc655..2be4712a7 100644 --- a/app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java +++ b/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java @@ -19,20 +19,29 @@ import java.net.MalformedURLException; import java.net.URL; /** - * AsyncTask to check if there is a newer version of the github apk available or not. + * AsyncTask to check if there is a newer version of the NewPipe github apk available or not. * If there is a newer version we show a notification, informing the user. On tapping - * the notification, the user will be directed to download link. + * the notification, the user will be directed to the download link. */ -public class FetchAppVersionTask extends AsyncTask { +public class CheckForNewAppVersionTask extends AsyncTask { private String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json"; private int timeoutPeriod = 10000; + @Override + protected void onPreExecute() { + // Continue with version check only if the build variant is of type "github". + if (!BuildConfig.FLAVOR.equals("github")) { + this.cancel(true); + } + } + @Override protected String doInBackground(Void... voids) { - String output; + // Make a network request to get latest NewPipe data. + String response; HttpURLConnection connection = null; try { @@ -63,13 +72,14 @@ public class FetchAppVersionTask extends AsyncTask { String line; while ((line = bufferedReader.readLine()) != null) { - stringBuilder.append(line + "\n"); + stringBuilder.append(line); + stringBuilder.append("\n"); } bufferedReader.close(); - output = stringBuilder.toString(); + response = stringBuilder.toString(); - return output; + return response; } } catch (MalformedURLException ex) { ex.printStackTrace(); @@ -89,12 +99,13 @@ public class FetchAppVersionTask extends AsyncTask { } @Override - protected void onPostExecute(String output) { + protected void onPostExecute(String response) { - if (output != null) { + // Parse the json from the response. + if (response != null) { try { - JSONObject mainObject = new JSONObject(output); + JSONObject mainObject = new JSONObject(response); JSONObject flavoursObject = mainObject.getJSONObject("flavors"); JSONObject githubObject = flavoursObject.getJSONObject("github"); JSONObject githubStableObject = githubObject.getJSONObject("stable"); @@ -112,7 +123,8 @@ public class FetchAppVersionTask extends AsyncTask { } /** - * Method to compare + * Method to compare the current and latest available app version. + * If a newer version is available, we show the update notification. * @param versionName * @param apkLocationUrl */ diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index a4c91cd30..2c233f219 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -58,7 +58,7 @@ import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.SubtitleView; import org.schabi.newpipe.BuildConfig; -import org.schabi.newpipe.FetchAppVersionTask; +import org.schabi.newpipe.CheckForNewAppVersionTask; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.VideoStream; @@ -666,9 +666,7 @@ public final class PopupVideoPlayer extends Service { hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME); // Check for new version - if (BuildConfig.FLAVOR.equals("github")) { - new FetchAppVersionTask().execute(); - } + new CheckForNewAppVersionTask().execute(); } @Override