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();
// Check for new version
if (BuildConfig.FLAVOR.equals("github")) {
new FetchAppVersionTask().execute();
}
new CheckForNewAppVersionTask().execute();
}
protected Downloader getDownloader() {

View File

@ -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<Void, Void, String> {
public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
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<Void, Void, String> {
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<Void, Void, String> {
}
@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<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 apkLocationUrl
*/

View File

@ -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