1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-07-12 14:54:24 +00:00
NewPipe/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java

243 lines
8.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.content.SharedPreferences;
2018-10-14 13:46:28 +00:00
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.net.Uri;
2018-08-11 14:06:23 +00:00
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
2018-08-11 14:06:23 +00:00
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
2018-10-14 13:46:28 +00:00
import java.io.ByteArrayInputStream;
2018-08-11 14:06:23 +00:00
import java.io.IOException;
2018-10-14 13:46:28 +00:00
import java.io.InputStream;
2018-08-11 14:06:23 +00:00
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
2018-10-14 13:46:28 +00:00
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
2018-08-11 14:06:23 +00:00
/**
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
private static final Application app = App.getContext();
private static final String GITHUB_APK_SHA1 = "B0:2E:90:7C:1C:D6:FC:57:C3:35:F0:88:D0:8F:50:5F:94:E4:D2:15";
private static final String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json";
private static final int timeoutPeriod = 10000;
2018-08-11 14:06:23 +00:00
private SharedPreferences mPrefs;
2018-08-12 13:04:20 +00:00
@Override
protected void onPreExecute() {
mPrefs = PreferenceManager.getDefaultSharedPreferences(app);
2018-10-14 13:46:28 +00:00
// Check if user has enabled/ disabled update checking
// and if the current apk is a github one or not.
if (!mPrefs.getBoolean(app.getString(R.string.update_app_key), true)
|| !isGithubApk()) {
2018-08-16 20:16:33 +00:00
this.cancel(true);
2018-08-12 13:04:20 +00:00
}
}
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.
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 (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");
2018-08-16 19:23:42 +00:00
String versionCode = githubStableObject.getString("version_code");
2018-08-11 14:06:23 +00:00
String apkLocationUrl = githubStableObject.getString("apk");
2018-08-16 19:23:42 +00:00
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode);
2018-08-11 14:06:23 +00:00
} 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-16 19:23:42 +00:00
private void compareAppVersionAndShowNotification(String versionName,
String apkLocationUrl,
String versionCode) {
2018-08-11 14:06:23 +00:00
int NOTIFICATION_ID = 2000;
2018-08-16 19:23:42 +00:00
if (BuildConfig.VERSION_CODE < Integer.valueOf(versionCode)) {
// 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
}
}
2018-10-14 13:46:28 +00:00
/**
* Method to get the apk's SHA1 key.
* https://stackoverflow.com/questions/9293019/get-certificate-fingerprint-from-android-app#22506133
*/
private static String getCertificateSHA1Fingerprint() {
2018-10-14 13:46:28 +00:00
PackageManager pm = app.getPackageManager();
String packageName = app.getPackageName();
int flags = PackageManager.GET_SIGNATURES;
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(packageName, flags);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Signature[] signatures = packageInfo.signatures;
byte[] cert = signatures[0].toByteArray();
InputStream input = new ByteArrayInputStream(cert);
2018-10-22 17:42:25 +00:00
CertificateFactory cf = null;
2018-10-14 13:46:28 +00:00
X509Certificate c = null;
try {
2018-10-22 17:42:25 +00:00
cf = CertificateFactory.getInstance("X509");
2018-10-14 13:46:28 +00:00
c = (X509Certificate) cf.generateCertificate(input);
} catch (CertificateException e) {
e.printStackTrace();
}
String hexString = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] publicKey = md.digest(c.getEncoded());
hexString = byte2HexFormatted(publicKey);
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (CertificateEncodingException e) {
e.printStackTrace();
}
return hexString;
}
private static String byte2HexFormatted(byte[] arr) {
StringBuilder str = new StringBuilder(arr.length * 2);
for (int i = 0; i < arr.length; i++) {
String h = Integer.toHexString(arr[i]);
int l = h.length();
if (l == 1) h = "0" + h;
if (l > 2) h = h.substring(l - 2, l);
str.append(h.toUpperCase());
if (i < (arr.length - 1)) str.append(':');
}
return str.toString();
}
public static boolean isGithubApk() {
return getCertificateSHA1Fingerprint().equals(GITHUB_APK_SHA1);
}
2018-08-11 14:06:23 +00:00
}