1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-19 00:04:56 +00:00

Code refactored and added comments.

This commit is contained in:
Kartikey Kushwaha 2018-08-12 18:34:20 +05:30
parent 12b93d6637
commit af42e32ae6
3 changed files with 26 additions and 18 deletions

View File

@ -101,9 +101,7 @@ public class App extends Application {
configureRxJavaErrorHandler(); configureRxJavaErrorHandler();
// Check for new version // Check for new version
if (BuildConfig.FLAVOR.equals("github")) { new CheckForNewAppVersionTask().execute();
new FetchAppVersionTask().execute();
}
} }
protected Downloader getDownloader() { protected Downloader getDownloader() {

View File

@ -19,20 +19,29 @@ import java.net.MalformedURLException;
import java.net.URL; 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 * 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<Void, Void, String> { public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
private String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json"; private String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json";
private int timeoutPeriod = 10000; 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 @Override
protected String doInBackground(Void... voids) { protected String doInBackground(Void... voids) {
String output; // Make a network request to get latest NewPipe data.
String response;
HttpURLConnection connection = null; HttpURLConnection connection = null;
try { try {
@ -63,13 +72,14 @@ public class FetchAppVersionTask extends AsyncTask<Void, Void, String> {
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n"); stringBuilder.append(line);
stringBuilder.append("\n");
} }
bufferedReader.close(); bufferedReader.close();
output = stringBuilder.toString(); response = stringBuilder.toString();
return output; return response;
} }
} catch (MalformedURLException ex) { } catch (MalformedURLException ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -89,12 +99,13 @@ public class FetchAppVersionTask extends AsyncTask<Void, Void, String> {
} }
@Override @Override
protected void onPostExecute(String output) { protected void onPostExecute(String response) {
if (output != null) { // Parse the json from the response.
if (response != null) {
try { try {
JSONObject mainObject = new JSONObject(output); JSONObject mainObject = new JSONObject(response);
JSONObject flavoursObject = mainObject.getJSONObject("flavors"); JSONObject flavoursObject = mainObject.getJSONObject("flavors");
JSONObject githubObject = flavoursObject.getJSONObject("github"); JSONObject githubObject = flavoursObject.getJSONObject("github");
JSONObject githubStableObject = githubObject.getJSONObject("stable"); JSONObject githubStableObject = githubObject.getJSONObject("stable");
@ -112,7 +123,8 @@ public class FetchAppVersionTask extends AsyncTask<Void, Void, String> {
} }
/** /**
* 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 versionName
* @param apkLocationUrl * @param apkLocationUrl
*/ */

View File

@ -58,7 +58,7 @@ import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.SubtitleView; import com.google.android.exoplayer2.ui.SubtitleView;
import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.BuildConfig;
import org.schabi.newpipe.FetchAppVersionTask; import org.schabi.newpipe.CheckForNewAppVersionTask;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.VideoStream; 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); hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
// Check for new version // Check for new version
if (BuildConfig.FLAVOR.equals("github")) { new CheckForNewAppVersionTask().execute();
new FetchAppVersionTask().execute();
}
} }
@Override @Override