Use RxJava instead of AsyncTask to check for new app versions.

This commit is contained in:
Isira Seneviratne 2020-10-12 06:25:35 +05:30
parent 7dd317e530
commit ef5084036c
3 changed files with 120 additions and 100 deletions

View file

@ -36,6 +36,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.CompositeException; import io.reactivex.exceptions.CompositeException;
import io.reactivex.exceptions.MissingBackpressureException; import io.reactivex.exceptions.MissingBackpressureException;
import io.reactivex.exceptions.OnErrorNotImplementedException; import io.reactivex.exceptions.OnErrorNotImplementedException;
@ -65,6 +66,9 @@ public class App extends MultiDexApplication {
protected static final String TAG = App.class.toString(); protected static final String TAG = App.class.toString();
private static App app; private static App app;
private Disposable disposable = null;
@NonNull
public static App getApp() { public static App getApp() {
return app; return app;
} }
@ -100,7 +104,15 @@ public class App extends MultiDexApplication {
configureRxJavaErrorHandler(); configureRxJavaErrorHandler();
// Check for new version // Check for new version
new CheckForNewAppVersionTask().execute(); disposable = CheckForNewAppVersion.checkNewVersion(this);
}
@Override
public void onTerminate() {
if (disposable != null) {
disposable.dispose();
}
super.onTerminate();
} }
protected Downloader getDownloader() { protected Downloader getDownloader() {

View file

@ -9,9 +9,9 @@ import android.content.pm.PackageManager;
import android.content.pm.Signature; import android.content.pm.Signature;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -35,16 +35,18 @@ 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 io.reactivex.Observable;
* AsyncTask to check if there is a newer version of the NewPipe github apk available or not. import io.reactivex.android.schedulers.AndroidSchedulers;
* If there is a newer version we show a notification, informing the user. On tapping import io.reactivex.disposables.Disposable;
* the notification, the user will be directed to the download link. import io.reactivex.disposables.Disposables;
*/ import io.reactivex.schedulers.Schedulers;
public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
private static final boolean DEBUG = MainActivity.DEBUG; public final class CheckForNewAppVersion {
private static final String TAG = CheckForNewAppVersionTask.class.getSimpleName(); private CheckForNewAppVersion() { }
private static final boolean DEBUG = MainActivity.DEBUG;
private static final String TAG = CheckForNewAppVersion.class.getSimpleName();
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";
@ -52,18 +54,19 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
/** /**
* 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.
* *
* @param application The application
* @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(@NonNull final Application application) {
final PackageManager pm = APP.getPackageManager(); final PackageManager pm = application.getPackageManager();
final String packageName = APP.getPackageName(); final String packageName = application.getPackageName();
final 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 (final PackageManager.NameNotFoundException e) { } catch (final PackageManager.NameNotFoundException e) {
ErrorActivity.reportError(APP, e, null, null, ErrorActivity.reportError(application, 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));
} }
@ -78,7 +81,7 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
final CertificateFactory cf = CertificateFactory.getInstance("X509"); final CertificateFactory cf = CertificateFactory.getInstance("X509");
c = (X509Certificate) cf.generateCertificate(input); c = (X509Certificate) cf.generateCertificate(input);
} catch (final CertificateException e) { } catch (final CertificateException e) {
ErrorActivity.reportError(APP, e, null, null, ErrorActivity.reportError(application, 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));
} }
@ -90,7 +93,7 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
final byte[] publicKey = md.digest(c.getEncoded()); final byte[] publicKey = md.digest(c.getEncoded());
hexString = byte2HexFormatted(publicKey); hexString = byte2HexFormatted(publicKey);
} catch (NoSuchAlgorithmException | CertificateEncodingException e) { } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
ErrorActivity.reportError(APP, e, null, null, ErrorActivity.reportError(application, e, 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));
} }
@ -118,104 +121,108 @@ public class CheckForNewAppVersionTask extends AsyncTask<Void, Void, String> {
return str.toString(); return str.toString();
} }
public static boolean isGithubApk() { /**
return getCertificateSHA1Fingerprint().equals(GITHUB_APK_SHA1); * Method to compare the current and latest available app version.
* If a newer version is available, we show the update notification.
*
* @param application The application
* @param versionName Name of new version
* @param apkLocationUrl Url with the new apk
* @param versionCode Code of new version
*/
private static void compareAppVersionAndShowNotification(@NonNull final Application application,
final String versionName,
final String apkLocationUrl,
final int versionCode) {
final int notificationId = 2000;
if (BuildConfig.VERSION_CODE < versionCode) {
// A pending intent to open the apk location url in the browser.
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
final PendingIntent pendingIntent
= PendingIntent.getActivity(application, 0, intent, 0);
final String channelId = application
.getString(R.string.app_update_notification_channel_id);
final NotificationCompat.Builder notificationBuilder
= new NotificationCompat.Builder(application, channelId)
.setSmallIcon(R.drawable.ic_newpipe_update)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setContentTitle(application
.getString(R.string.app_update_notification_content_title))
.setContentText(application
.getString(R.string.app_update_notification_content_text)
+ " " + versionName);
final NotificationManagerCompat notificationManager
= NotificationManagerCompat.from(application);
notificationManager.notify(notificationId, notificationBuilder.build());
}
} }
@Override private static boolean isConnected(@NonNull final App app) {
protected void onPreExecute() { final ConnectivityManager cm = ContextCompat.getSystemService(app,
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(APP); ConnectivityManager.class);
return cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isConnected();
}
public static boolean isGithubApk(@NonNull final App app) {
return getCertificateSHA1Fingerprint(app).equals(GITHUB_APK_SHA1);
}
@NonNull
public static Disposable checkNewVersion(@NonNull final App 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 (!prefs.getBoolean(APP.getString(R.string.update_app_key), true) || !isGithubApk()) { if (!prefs.getBoolean(app.getString(R.string.update_app_key), true)
this.cancel(true); || !isGithubApk(app)) {
} return Disposables.empty();
}
@Override
protected String doInBackground(final Void... voids) {
if (isCancelled() || !isConnected()) {
return null;
} }
// Make a network request to get latest NewPipe data. return Observable.fromCallable(() -> {
try { if (!isConnected(app)) {
return DownloaderImpl.getInstance().get(NEWPIPE_API_URL).responseBody(); return null;
} catch (IOException | ReCaptchaException e) {
// connectivity problems, do not alarm user and fail silently
if (DEBUG) {
Log.w(TAG, Log.getStackTraceString(e));
} }
}
return null;
}
@Override
protected void onPostExecute(final String response) {
// Parse the json from the response.
if (response != null) {
// Make a network request to get latest NewPipe data.
try { try {
final JsonObject githubStableObject = JsonParser.object().from(response) return DownloaderImpl.getInstance().get(NEWPIPE_API_URL).responseBody();
.getObject("flavors").getObject("github").getObject("stable"); } catch (IOException | ReCaptchaException e) {
final String versionName = githubStableObject.getString("version");
final int versionCode = githubStableObject.getInt("version_code");
final String apkLocationUrl = githubStableObject.getString("apk");
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode);
} catch (final 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(e)); Log.w(TAG, Log.getStackTraceString(e));
} }
} }
}
}
/** return null;
* Method to compare the current and latest available app version. })
* If a newer version is available, we show the update notification. .subscribeOn(Schedulers.io())
* .observeOn(AndroidSchedulers.mainThread())
* @param versionName Name of new version .subscribe(response -> {
* @param apkLocationUrl Url with the new apk // Parse the json from the response.
* @param versionCode Code of new version if (response != null) {
*/ try {
private void compareAppVersionAndShowNotification(final String versionName, final JsonObject githubStableObject = JsonParser.object().from(response)
final String apkLocationUrl, .getObject("flavors").getObject("github").getObject("stable");
final int versionCode) {
final int notificationId = 2000;
if (BuildConfig.VERSION_CODE < versionCode) { final String versionName = githubStableObject.getString("version");
final int versionCode = githubStableObject.getInt("version_code");
final String apkLocationUrl = githubStableObject.getString("apk");
// A pending intent to open the apk location url in the browser. compareAppVersionAndShowNotification(app, versionName, apkLocationUrl,
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl)); versionCode);
final PendingIntent pendingIntent } catch (final JsonParserException e) {
= PendingIntent.getActivity(APP, 0, intent, 0); // connectivity problems, do not alarm user and fail silently
if (DEBUG) {
final NotificationCompat.Builder notificationBuilder = new NotificationCompat Log.w(TAG, Log.getStackTraceString(e));
.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);
final NotificationManagerCompat notificationManager
= NotificationManagerCompat.from(APP);
notificationManager.notify(notificationId, notificationBuilder.build());
}
}
private boolean isConnected() {
final ConnectivityManager cm = ContextCompat.getSystemService(APP,
ConnectivityManager.class);
return cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isConnected();
} }
} }

View file

@ -4,7 +4,8 @@ import android.os.Bundle;
import androidx.preference.Preference; import androidx.preference.Preference;
import org.schabi.newpipe.CheckForNewAppVersionTask; import org.schabi.newpipe.App;
import org.schabi.newpipe.CheckForNewAppVersion;
import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
@ -15,7 +16,7 @@ public class MainSettingsFragment extends BasePreferenceFragment {
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.main_settings); addPreferencesFromResource(R.xml.main_settings);
if (!CheckForNewAppVersionTask.isGithubApk()) { if (!CheckForNewAppVersion.isGithubApk(App.getApp())) {
final Preference update = findPreference(getString(R.string.update_pref_screen_key)); final Preference update = findPreference(getString(R.string.update_pref_screen_key));
getPreferenceScreen().removePreference(update); getPreferenceScreen().removePreference(update);