From 71f141f3f8236f2f19e558685f4391c982051cb4 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Thu, 3 Mar 2022 13:26:57 -0500 Subject: [PATCH] Migrate CheckForNewAppVersion to Worker (and rename it) --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 4 --- .../java/org/schabi/newpipe/MainActivity.java | 5 ++- ...wAppVersion.java => NewVersionWorker.java} | 34 ++++++++++++------- .../settings/UpdateSettingsFragment.java | 5 ++- 5 files changed, 27 insertions(+), 22 deletions(-) rename app/src/main/java/org/schabi/newpipe/{CheckForNewAppVersion.java => NewVersionWorker.java} (87%) diff --git a/app/build.gradle b/app/build.gradle index d78b7e730..35cdde5f4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -220,6 +220,7 @@ dependencies { // https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0-alpha01 implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01' implementation 'androidx.webkit:webkit:1.4.0' + implementation 'androidx.work:work-runtime:2.7.1' implementation 'com.google.android.material:material:1.4.0' /** Third-party libraries **/ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2f215a768..f9c99819c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -381,10 +381,6 @@ - diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 6f1bea376..b208d8443 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -173,10 +173,9 @@ public class MainActivity extends AppCompatActivity { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app); if (prefs.getBoolean(app.getString(R.string.update_app_key), true)) { - // Start the service which is checking all conditions + // Start the worker which is checking all conditions // and eventually searching for a new version. - // The service searching for a new NewPipe version must not be started in background. - CheckForNewAppVersion.startNewVersionCheckService(app); + NewVersionWorker.enqueueNewVersionCheckingWork(app); } } diff --git a/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java b/app/src/main/java/org/schabi/newpipe/NewVersionWorker.java similarity index 87% rename from app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java rename to app/src/main/java/org/schabi/newpipe/NewVersionWorker.java index 4ed4d1b1b..00405a899 100644 --- a/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java +++ b/app/src/main/java/org/schabi/newpipe/NewVersionWorker.java @@ -8,11 +8,14 @@ import android.net.Uri; import android.util.Log; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.app.JobIntentService; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.preference.PreferenceManager; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkManager; +import androidx.work.WorkRequest; +import androidx.work.Worker; +import androidx.work.WorkerParameters; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; @@ -24,12 +27,16 @@ import org.schabi.newpipe.util.ReleaseVersionUtil; import java.io.IOException; -public final class CheckForNewAppVersion extends JobIntentService { +public final class NewVersionWorker extends Worker { private static final boolean DEBUG = MainActivity.DEBUG; - private static final String TAG = CheckForNewAppVersion.class.getSimpleName(); + private static final String TAG = NewVersionWorker.class.getSimpleName(); private static final String NEWPIPE_API_URL = "https://newpipe.net/api/data.json"; - private static final int JOB_ID = -17000; + + public NewVersionWorker(@NonNull final Context context, + @NonNull final WorkerParameters workerParams) { + super(context, workerParams); + } /** * Method to compare the current and latest available app version. @@ -130,7 +137,7 @@ public final class CheckForNewAppVersion extends JobIntentService { } /** - * Start a new service which + * Start a new worker which * checks if all conditions for performing a version check are met, * fetches the API endpoint {@link #NEWPIPE_API_URL} containing info * about the latest NewPipe version @@ -144,22 +151,25 @@ public final class CheckForNewAppVersion extends JobIntentService { *
  • The app did not recently check for updates. * We do not want to make unnecessary connections and DOS our servers.
  • * - * Must not be executed when the app is in background. */ - public static void startNewVersionCheckService(final Context context) { - enqueueWork(context, CheckForNewAppVersion.class, JOB_ID, - new Intent(context, CheckForNewAppVersion.class)); + public static void enqueueNewVersionCheckingWork(final Context context) { + final WorkRequest workRequest = + new OneTimeWorkRequest.Builder(NewVersionWorker.class).build(); + WorkManager.getInstance(context).enqueue(workRequest); } + @NonNull @Override - protected void onHandleWork(@Nullable final Intent intent) { + public Result doWork() { try { checkNewVersion(); } catch (final IOException e) { Log.w(TAG, "Could not fetch NewPipe API: probably network problem", e); + return Result.failure(); } catch (final ReCaptchaException e) { Log.e(TAG, "ReCaptchaException should never happen here.", e); + return Result.failure(); } - + return Result.success(); } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java index 46582cb24..1043e88c2 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java @@ -1,12 +1,11 @@ package org.schabi.newpipe.settings; -import static org.schabi.newpipe.CheckForNewAppVersion.startNewVersionCheckService; - import android.os.Bundle; import android.widget.Toast; import androidx.preference.Preference; +import org.schabi.newpipe.NewVersionWorker; import org.schabi.newpipe.R; public class UpdateSettingsFragment extends BasePreferenceFragment { @@ -33,7 +32,7 @@ public class UpdateSettingsFragment extends BasePreferenceFragment { // Reset the expire time. This is necessary to check for an update immediately. defaultPreferences.edit() .putLong(getString(R.string.update_expiry_key), 0).apply(); - startNewVersionCheckService(getContext()); + NewVersionWorker.enqueueNewVersionCheckingWork(getContext()); } @Override