From 5e2aa51627846afbf86fd8c34e94c4b09628cd5b Mon Sep 17 00:00:00 2001 From: Kartikey Kushwaha Date: Sat, 11 Aug 2018 19:36:23 +0530 Subject: [PATCH] Moved the asynctask to its own class. --- .../schabi/newpipe/FetchAppVersionTask.java | 118 ++++++++++++++++++ .../java/org/schabi/newpipe/MainActivity.java | 109 +--------------- 2 files changed, 119 insertions(+), 108 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java diff --git a/app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java b/app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java new file mode 100644 index 000000000..2f98fe1ad --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/FetchAppVersionTask.java @@ -0,0 +1,118 @@ +package org.schabi.newpipe; + +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * AsyncTask to check if there is a newer version of the 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. + */ +public class FetchAppVersionTask extends AsyncTask { + + private String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json"; + private int timeoutPeriod = 10000; + + @Override + protected String doInBackground(Void... voids) { + + String output; + + HttpURLConnection connection = null; + + try { + + URL url = new URL(newPipeApiUrl); + + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(timeoutPeriod); + connection.setReadTimeout(timeoutPeriod); + connection.setRequestProperty("Content-length", "0"); + connection.setUseCaches(false); + connection.setAllowUserInteraction(false); + connection.connect(); + + int responseStatus = connection.getResponseCode(); + + switch (responseStatus) { + + case 200: + case 201: + BufferedReader bufferedReader + = new BufferedReader( + new InputStreamReader(connection.getInputStream())); + + StringBuilder stringBuilder = new StringBuilder(); + + String line; + + while ((line = bufferedReader.readLine()) != null) { + stringBuilder.append(line + "\n"); + } + + bufferedReader.close(); + output = stringBuilder.toString(); + + return output; + } + } catch (MalformedURLException ex) { + ex.printStackTrace(); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + if (connection != null) { + try { + connection.disconnect(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + + return null; + } + + @Override + protected void onPostExecute(String output) { + + if (output != null) { + + Log.i("output---", output); + + try { + JSONObject mainObject = new JSONObject(output); + JSONObject flavoursObject = mainObject.getJSONObject("flavors"); + JSONObject githubObject = flavoursObject.getJSONObject("github"); + JSONObject githubStableObject = githubObject.getJSONObject("stable"); + + String versionName = githubStableObject.getString("version"); + // String versionCode = githubStableObject.getString("version_code"); + String apkLocationUrl = githubStableObject.getString("apk"); + + compareAppVersionAndShowNotification(versionName, apkLocationUrl); + + } catch (JSONException ex) { + ex.printStackTrace(); + } + } + } + + private void compareAppVersionAndShowNotification(String versionName, String apkLocationUrl) { + + if (!BuildConfig.VERSION_NAME.equals(versionName)) { + + + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 4dba91706..1554dddc0 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -24,7 +24,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -48,8 +47,6 @@ import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; -import org.json.JSONException; -import org.json.JSONObject; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.fragments.BackPressable; @@ -64,13 +61,6 @@ import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.ThemeHelper; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; - public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); @@ -91,7 +81,7 @@ public class MainActivity extends AppCompatActivity { ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this)); if (BuildConfig.FLAVOR.equals("github")) { - new versionCheckTask().execute(); + new FetchAppVersionTask().execute(); } super.onCreate(savedInstanceState); @@ -423,101 +413,4 @@ public class MainActivity extends AppCompatActivity { NavigationHelper.gotoMainFragment(getSupportFragmentManager()); } } - - /** - * AsyncTask to check if there is a newer version of the 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. - */ - private static class versionCheckTask extends AsyncTask { - - String newPipeApiUrl = "https://api.myjson.com/bins/19gx44"; - int timeoutPeriod = 10000; - - @Override - protected String doInBackground(Void... voids) { - - String output; - - HttpURLConnection connection = null; - - try { - - URL url = new URL(newPipeApiUrl); - - connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - connection.setConnectTimeout(timeoutPeriod); - connection.setReadTimeout(timeoutPeriod); - connection.setRequestProperty("Content-length", "0"); - connection.setUseCaches(false); - connection.setAllowUserInteraction(false); - connection.connect(); - - int responseStatus = connection.getResponseCode(); - - switch (responseStatus) { - - case 200: - case 201: - BufferedReader bufferedReader - = new BufferedReader( - new InputStreamReader(connection.getInputStream())); - - StringBuilder stringBuilder = new StringBuilder(); - - String line; - - while ((line = bufferedReader.readLine()) != null) { - stringBuilder.append(line + "\n"); - } - - bufferedReader.close(); - output = stringBuilder.toString(); - - return output; - } - } catch (MalformedURLException ex) { - ex.printStackTrace(); - } catch (IOException ex) { - ex.printStackTrace(); - } finally { - if (connection != null) { - try { - connection.disconnect(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - } - - return null; - } - - @Override - protected void onPostExecute(String output) { - - if (output != null) { - - Log.i("output---", output); - - try { - JSONObject mainObject = new JSONObject(output); - JSONObject flavoursObject = mainObject.getJSONObject("flavors"); - JSONObject githubObject = flavoursObject.getJSONObject("github"); - JSONObject githubStableObject = githubObject.getJSONObject("stable"); - - String version = githubStableObject.getString("version"); - // String versionCode = githubStableObject.getString("version_code"); - String apkLocationUrl = githubStableObject.getString("apk"); - - Log.i("jsonConvert---", version + " " + apkLocationUrl); - } catch (JSONException ex) { - ex.printStackTrace(); - } - } - } - } - - }