From eab5ef59b9f60dc24732ad978ac57a9f584a0bc2 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 25 Oct 2020 15:17:36 +0530 Subject: [PATCH] Use RxJava instead of AsyncTask to handle updates. --- .../fdroid/nearby/WifiStateChangeService.java | 18 ++++-- .../java/org/fdroid/fdroid/UpdateService.java | 61 ++++++++----------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java b/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java index 2cbb14a2f..c3ddc3879 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java @@ -13,6 +13,10 @@ import android.os.Build; import android.text.TextUtils; import android.util.Log; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + import org.apache.commons.net.util.SubnetUtils; import org.fdroid.fdroid.BuildConfig; import org.fdroid.fdroid.FDroidApp; @@ -31,10 +35,8 @@ import java.security.cert.Certificate; import java.util.Enumeration; import java.util.Locale; -import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import cc.mvdan.accesspoint.WifiApControl; +import io.reactivex.rxjava3.disposables.CompositeDisposable; /** * Handle state changes to the device's wifi, storing the required bits. @@ -70,6 +72,8 @@ public class WifiStateChangeService extends IntentService { private static int previousWifiState = Integer.MIN_VALUE; private static int wifiState; + private final CompositeDisposable compositeDisposable = new CompositeDisposable(); + public WifiStateChangeService() { super("WifiStateChangeService"); } @@ -82,6 +86,12 @@ public class WifiStateChangeService extends IntentService { context.startService(intent); } + @Override + public void onDestroy() { + compositeDisposable.dispose(); + super.onDestroy(); + } + @Override protected void onHandleIntent(Intent intent) { android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST); @@ -109,7 +119,7 @@ public class WifiStateChangeService extends IntentService { } if (Build.VERSION.SDK_INT < 21 && wifiState == WifiManager.WIFI_STATE_ENABLED) { - UpdateService.scheduleIfStillOnWifi(this); + compositeDisposable.add(UpdateService.scheduleIfStillOnWifi(this).subscribe()); } } } diff --git a/app/src/main/java/org/fdroid/fdroid/UpdateService.java b/app/src/main/java/org/fdroid/fdroid/UpdateService.java index 1d0631289..78a2440b2 100644 --- a/app/src/main/java/org/fdroid/fdroid/UpdateService.java +++ b/app/src/main/java/org/fdroid/fdroid/UpdateService.java @@ -30,7 +30,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; -import android.os.AsyncTask; import android.os.Build; import android.os.Process; import android.os.SystemClock; @@ -38,6 +37,12 @@ import android.text.TextUtils; import android.util.Log; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.core.app.JobIntentService; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + import org.fdroid.fdroid.data.Apk; import org.fdroid.fdroid.data.ApkProvider; import org.fdroid.fdroid.data.App; @@ -51,15 +56,13 @@ import org.fdroid.fdroid.installer.InstallManagerService; import org.fdroid.fdroid.net.BluetoothDownloader; import org.fdroid.fdroid.net.ConnectivityMonitorService; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; -import androidx.annotation.NonNull; -import androidx.core.app.JobIntentService; -import androidx.core.app.NotificationCompat; -import androidx.core.content.ContextCompat; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; +import io.reactivex.rxjava3.core.Completable; +import io.reactivex.rxjava3.schedulers.Schedulers; public class UpdateService extends JobIntentService { @@ -215,42 +218,30 @@ public class UpdateService extends JobIntentService { * unlimited networks over metered networks for index updates and auto * downloads of app updates. Starting with {@code android-21}, this uses * {@link android.app.job.JobScheduler} instead. + * + * @return a {@link Completable} that schedules the update. If this process is already running, + * a {@code Completable} that completes immediately is returned. */ - public static void scheduleIfStillOnWifi(Context context) { + @NonNull + public static Completable scheduleIfStillOnWifi(Context context) { if (Build.VERSION.SDK_INT >= 21) { throw new IllegalStateException("This should never be used on android-21 or newer!"); } if (isScheduleIfStillOnWifiRunning || !Preferences.get().isBackgroundDownloadAllowed()) { - return; + return Completable.complete(); } isScheduleIfStillOnWifiRunning = true; - new StillOnWifiAsyncTask(context).execute(); - } - - private static final class StillOnWifiAsyncTask extends AsyncTask { - - private final WeakReference contextWeakReference; - - private StillOnWifiAsyncTask(Context context) { - this.contextWeakReference = new WeakReference<>(context); - } - - @Override - protected Void doInBackground(Void... voids) { - Context context = contextWeakReference.get(); - try { - Thread.sleep(120000); - if (Preferences.get().isBackgroundDownloadAllowed()) { - Utils.debugLog(TAG, "scheduling update because there is good internet"); - schedule(context); - } - } catch (Throwable e) { // NOPMD - Utils.debugLog(TAG, e.getMessage()); - } - isScheduleIfStillOnWifiRunning = false; - return null; - } + return Completable.timer(2, TimeUnit.MINUTES) + .andThen(Completable.fromAction(() -> { + if (Preferences.get().isBackgroundDownloadAllowed()) { + Utils.debugLog(TAG, "scheduling update because there is good internet"); + schedule(context); + } + isScheduleIfStillOnWifiRunning = false; + })) + .subscribeOn(Schedulers.computation()) + .observeOn(AndroidSchedulers.mainThread()); } public static void stopNow(Context context) {