1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-08-05 02:20:38 +00:00
NewPipe/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java

158 lines
5.5 KiB
Java
Raw Normal View History

2018-08-11 14:06:23 +00:00
package org.schabi.newpipe;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
2018-08-11 14:06:23 +00:00
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
2018-08-11 14:06:23 +00:00
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;
/**
2018-08-12 13:04:20 +00:00
* AsyncTask to check if there is a newer version of the NewPipe github apk available or not.
2018-08-11 14:06:23 +00:00
* If there is a newer version we show a notification, informing the user. On tapping
2018-08-12 13:04:20 +00:00
* the notification, the user will be directed to the download link.
2018-08-11 14:06:23 +00:00
*/
2018-08-12 13:04:20 +00:00
public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
2018-08-11 14:06:23 +00:00
2018-08-12 15:27:30 +00:00
private Application app = App.getContext();
2018-08-11 14:06:23 +00:00
private String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json";
private int timeoutPeriod = 10000;
2018-08-12 13:04:20 +00:00
@Override
protected void onPreExecute() {
// Continue with version check only if the build variant is of type "github".
2018-08-12 15:27:30 +00:00
if (!BuildConfig.FLAVOR.equals(app.getString(R.string.app_flavor_github))) {
2018-08-12 13:04:20 +00:00
this.cancel(true);
}
}
2018-08-11 14:06:23 +00:00
@Override
protected String doInBackground(Void... voids) {
2018-08-12 13:04:20 +00:00
// Make a network request to get latest NewPipe data.
2018-08-11 14:06:23 +00:00
2018-08-12 13:04:20 +00:00
String response;
2018-08-11 14:06:23 +00:00
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:
2018-08-12 15:27:30 +00:00
BufferedReader bufferedReader = new BufferedReader(
2018-08-11 14:06:23 +00:00
new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
2018-08-12 13:04:20 +00:00
stringBuilder.append(line);
stringBuilder.append("\n");
2018-08-11 14:06:23 +00:00
}
bufferedReader.close();
2018-08-12 13:04:20 +00:00
response = stringBuilder.toString();
2018-08-11 14:06:23 +00:00
2018-08-12 13:04:20 +00:00
return response;
2018-08-11 14:06:23 +00:00
}
} 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
2018-08-12 13:04:20 +00:00
protected void onPostExecute(String response) {
2018-08-11 14:06:23 +00:00
2018-08-12 13:04:20 +00:00
// Parse the json from the response.
if (response != null) {
2018-08-11 14:06:23 +00:00
try {
2018-08-12 13:04:20 +00:00
JSONObject mainObject = new JSONObject(response);
2018-08-11 14:06:23 +00:00
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();
}
}
}
/**
2018-08-12 13:04:20 +00:00
* 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
*/
2018-08-11 14:06:23 +00:00
private void compareAppVersionAndShowNotification(String versionName, String apkLocationUrl) {
int NOTIFICATION_ID = 2000;
if (!BuildConfig.VERSION_NAME.equals(versionName.replace("v", ""))) {
// A pending intent to open the apk location url in the browser.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
PendingIntent pendingIntent
= PendingIntent.getActivity(app, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat
.Builder(app, app.getString(R.string.app_update_notification_channel_id))
.setSmallIcon(R.drawable.ic_newpipe_update)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setContentTitle(app.getString(R.string.app_update_notification_content_title))
.setContentText(app.getString(R.string.app_update_notification_content_text)
+ " " + versionName);
2018-08-11 14:06:23 +00:00
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
2018-08-11 14:06:23 +00:00
}
}
}