Clean up CheckForNewAppVersionTask
This commit is contained in:
parent
41061d0289
commit
a5a497c4ea
|
@ -17,8 +17,10 @@ import android.util.Log;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.app.NotificationManagerCompat;
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import com.grack.nanojson.JsonObject;
|
||||||
import org.json.JSONObject;
|
import com.grack.nanojson.JsonParser;
|
||||||
|
import com.grack.nanojson.JsonParserException;
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
||||||
import org.schabi.newpipe.report.ErrorActivity;
|
import org.schabi.newpipe.report.ErrorActivity;
|
||||||
import org.schabi.newpipe.report.UserAction;
|
import org.schabi.newpipe.report.UserAction;
|
||||||
|
@ -33,8 +35,6 @@ import java.security.cert.CertificateException;
|
||||||
import java.security.cert.CertificateFactory;
|
import java.security.cert.CertificateFactory;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AsyncTask to check if there is a newer version of the NewPipe 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
|
||||||
|
@ -43,14 +43,11 @@ import okhttp3.OkHttpClient;
|
||||||
public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
private static final boolean DEBUG = MainActivity.DEBUG;
|
private static final boolean DEBUG = MainActivity.DEBUG;
|
||||||
private static final String TAG = CheckForNewAppVersionTask.class.getSimpleName();
|
private static final String TAG = CheckForNewAppVersionTask.class.getSimpleName();
|
||||||
|
|
||||||
private static final Application APP = App.getApp();
|
private static final Application APP = App.getApp();
|
||||||
private static final String GITHUB_APK_SHA1
|
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";
|
= "B0:2E:90:7C:1C:D6:FC:57:C3:35:F0:88:D0:8F:50:5F:94:E4:D2:15";
|
||||||
private static final String NEWPIPE_API_URL = "https://newpipe.schabi.org/api/data.json";
|
private static final String NEWPIPE_API_URL = "https://newpipe.schabi.org/api/data.json";
|
||||||
private static final int TIMEOUT_PERIOD = 30;
|
|
||||||
|
|
||||||
private SharedPreferences mPrefs;
|
|
||||||
private OkHttpClient client;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to get the apk's SHA1 key. See https://stackoverflow.com/questions/9293019/#22506133.
|
* Method to get the apk's SHA1 key. See https://stackoverflow.com/questions/9293019/#22506133.
|
||||||
|
@ -58,31 +55,30 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
* @return String with the apk's SHA1 fingeprint in hexadecimal
|
* @return String with the apk's SHA1 fingeprint in hexadecimal
|
||||||
*/
|
*/
|
||||||
private static String getCertificateSHA1Fingerprint() {
|
private static String getCertificateSHA1Fingerprint() {
|
||||||
PackageManager pm = APP.getPackageManager();
|
final PackageManager pm = APP.getPackageManager();
|
||||||
String packageName = APP.getPackageName();
|
final String packageName = APP.getPackageName();
|
||||||
int flags = PackageManager.GET_SIGNATURES;
|
final int flags = PackageManager.GET_SIGNATURES;
|
||||||
PackageInfo packageInfo = null;
|
PackageInfo packageInfo = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
packageInfo = pm.getPackageInfo(packageName, flags);
|
packageInfo = pm.getPackageInfo(packageName, flags);
|
||||||
} catch (PackageManager.NameNotFoundException ex) {
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
ErrorActivity.reportError(APP, ex, null, null,
|
ErrorActivity.reportError(APP, e, null, null,
|
||||||
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
||||||
"Could not find package info", R.string.app_ui_crash));
|
"Could not find package info", R.string.app_ui_crash));
|
||||||
}
|
}
|
||||||
|
|
||||||
Signature[] signatures = packageInfo.signatures;
|
final Signature[] signatures = packageInfo.signatures;
|
||||||
byte[] cert = signatures[0].toByteArray();
|
final byte[] cert = signatures[0].toByteArray();
|
||||||
InputStream input = new ByteArrayInputStream(cert);
|
final InputStream input = new ByteArrayInputStream(cert);
|
||||||
|
|
||||||
CertificateFactory cf = null;
|
|
||||||
X509Certificate c = null;
|
X509Certificate c = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cf = CertificateFactory.getInstance("X509");
|
final CertificateFactory cf = CertificateFactory.getInstance("X509");
|
||||||
c = (X509Certificate) cf.generateCertificate(input);
|
c = (X509Certificate) cf.generateCertificate(input);
|
||||||
} catch (CertificateException ex) {
|
} catch (CertificateException e) {
|
||||||
ErrorActivity.reportError(APP, ex, null, null,
|
ErrorActivity.reportError(APP, e, null, null,
|
||||||
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
||||||
"Certificate error", R.string.app_ui_crash));
|
"Certificate error", R.string.app_ui_crash));
|
||||||
}
|
}
|
||||||
|
@ -91,14 +87,10 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance("SHA1");
|
MessageDigest md = MessageDigest.getInstance("SHA1");
|
||||||
byte[] publicKey = md.digest(c.getEncoded());
|
final byte[] publicKey = md.digest(c.getEncoded());
|
||||||
hexString = byte2HexFormatted(publicKey);
|
hexString = byte2HexFormatted(publicKey);
|
||||||
} catch (NoSuchAlgorithmException ex1) {
|
} catch (NoSuchAlgorithmException | CertificateEncodingException e) {
|
||||||
ErrorActivity.reportError(APP, ex1, null, null,
|
ErrorActivity.reportError(APP, e, null, null,
|
||||||
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
|
||||||
"Could not retrieve SHA1 key", R.string.app_ui_crash));
|
|
||||||
} catch (CertificateEncodingException ex2) {
|
|
||||||
ErrorActivity.reportError(APP, ex2, null, null,
|
|
||||||
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
||||||
"Could not retrieve SHA1 key", R.string.app_ui_crash));
|
"Could not retrieve SHA1 key", R.string.app_ui_crash));
|
||||||
}
|
}
|
||||||
|
@ -107,11 +99,11 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String byte2HexFormatted(final byte[] arr) {
|
private static String byte2HexFormatted(final byte[] arr) {
|
||||||
StringBuilder str = new StringBuilder(arr.length * 2);
|
final StringBuilder str = new StringBuilder(arr.length * 2);
|
||||||
|
|
||||||
for (int i = 0; i < arr.length; i++) {
|
for (int i = 0; i < arr.length; i++) {
|
||||||
String h = Integer.toHexString(arr[i]);
|
String h = Integer.toHexString(arr[i]);
|
||||||
int l = h.length();
|
final int l = h.length();
|
||||||
if (l == 1) {
|
if (l == 1) {
|
||||||
h = "0" + h;
|
h = "0" + h;
|
||||||
}
|
}
|
||||||
|
@ -132,11 +124,11 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreExecute() {
|
protected void onPreExecute() {
|
||||||
mPrefs = PreferenceManager.getDefaultSharedPreferences(APP);
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(APP);
|
||||||
|
|
||||||
// Check if user has enabled/ disabled update checking
|
// Check if user has enabled/disabled update checking
|
||||||
// and if the current apk is a github one or not.
|
// and if the current apk is a github one or not.
|
||||||
if (!mPrefs.getBoolean(APP.getString(R.string.update_app_key), true) || !isGithubApk()) {
|
if (!prefs.getBoolean(APP.getString(R.string.update_app_key), true) || !isGithubApk()) {
|
||||||
this.cancel(true);
|
this.cancel(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,10 +142,10 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
// Make a network request to get latest NewPipe data.
|
// Make a network request to get latest NewPipe data.
|
||||||
try {
|
try {
|
||||||
return DownloaderImpl.getInstance().get(NEWPIPE_API_URL).responseBody();
|
return DownloaderImpl.getInstance().get(NEWPIPE_API_URL).responseBody();
|
||||||
} catch (IOException | ReCaptchaException ex) {
|
} catch (IOException | ReCaptchaException e) {
|
||||||
// connectivity problems, do not alarm user and fail silently
|
// connectivity problems, do not alarm user and fail silently
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.w(TAG, Log.getStackTraceString(ex));
|
Log.w(TAG, Log.getStackTraceString(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,21 +158,19 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JSONObject mainObject = new JSONObject(response);
|
final JsonObject githubStableObject = JsonParser.object().from(response)
|
||||||
JSONObject flavoursObject = mainObject.getJSONObject("flavors");
|
.getObject("flavors").getObject("github").getObject("stable");
|
||||||
JSONObject githubObject = flavoursObject.getJSONObject("github");
|
|
||||||
JSONObject githubStableObject = githubObject.getJSONObject("stable");
|
|
||||||
|
|
||||||
String versionName = githubStableObject.getString("version");
|
final String versionName = githubStableObject.getString("version");
|
||||||
String versionCode = githubStableObject.getString("version_code");
|
final int versionCode = githubStableObject.getInt("version_code");
|
||||||
String apkLocationUrl = githubStableObject.getString("apk");
|
final String apkLocationUrl = githubStableObject.getString("apk");
|
||||||
|
|
||||||
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode);
|
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode);
|
||||||
|
|
||||||
} catch (JSONException ex) {
|
} catch (JsonParserException e) {
|
||||||
// connectivity problems, do not alarm user and fail silently
|
// connectivity problems, do not alarm user and fail silently
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.w(TAG, Log.getStackTraceString(ex));
|
Log.w(TAG, Log.getStackTraceString(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,17 +186,17 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
*/
|
*/
|
||||||
private void compareAppVersionAndShowNotification(final String versionName,
|
private void compareAppVersionAndShowNotification(final String versionName,
|
||||||
final String apkLocationUrl,
|
final String apkLocationUrl,
|
||||||
final String versionCode) {
|
final int versionCode) {
|
||||||
int notificationId = 2000;
|
int notificationId = 2000;
|
||||||
|
|
||||||
if (BuildConfig.VERSION_CODE < Integer.valueOf(versionCode)) {
|
if (BuildConfig.VERSION_CODE < versionCode) {
|
||||||
|
|
||||||
// A pending intent to open the apk location url in the browser.
|
// A pending intent to open the apk location url in the browser.
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
|
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
|
||||||
PendingIntent pendingIntent
|
final PendingIntent pendingIntent
|
||||||
= PendingIntent.getActivity(APP, 0, intent, 0);
|
= PendingIntent.getActivity(APP, 0, intent, 0);
|
||||||
|
|
||||||
NotificationCompat.Builder notificationBuilder = new NotificationCompat
|
final NotificationCompat.Builder notificationBuilder = new NotificationCompat
|
||||||
.Builder(APP, APP.getString(R.string.app_update_notification_channel_id))
|
.Builder(APP, APP.getString(R.string.app_update_notification_channel_id))
|
||||||
.setSmallIcon(R.drawable.ic_newpipe_update)
|
.setSmallIcon(R.drawable.ic_newpipe_update)
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||||
|
@ -216,13 +206,14 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
|
||||||
.setContentText(APP.getString(R.string.app_update_notification_content_text)
|
.setContentText(APP.getString(R.string.app_update_notification_content_text)
|
||||||
+ " " + versionName);
|
+ " " + versionName);
|
||||||
|
|
||||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(APP);
|
final NotificationManagerCompat notificationManager
|
||||||
|
= NotificationManagerCompat.from(APP);
|
||||||
notificationManager.notify(notificationId, notificationBuilder.build());
|
notificationManager.notify(notificationId, notificationBuilder.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isConnected() {
|
private boolean isConnected() {
|
||||||
ConnectivityManager cm =
|
final ConnectivityManager cm =
|
||||||
(ConnectivityManager) APP.getSystemService(Context.CONNECTIVITY_SERVICE);
|
(ConnectivityManager) APP.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
return cm.getActiveNetworkInfo() != null
|
return cm.getActiveNetworkInfo() != null
|
||||||
&& cm.getActiveNetworkInfo().isConnected();
|
&& cm.getActiveNetworkInfo().isConnected();
|
||||||
|
|
Loading…
Reference in New Issue