Migrate CheckForNewAppVersion to Worker (and rename it)

This commit is contained in:
TacoTheDank 2022-03-03 13:26:57 -05:00
parent 81fef1be19
commit 71f141f3f8
5 changed files with 27 additions and 22 deletions

View file

@ -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 **/

View file

@ -381,10 +381,6 @@
<service
android:name=".RouterActivity$FetcherService"
android:exported="false" />
<service
android:name=".CheckForNewAppVersion"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
<!-- opting out of sending metrics to Google in Android System WebView -->
<meta-data android:name="android.webkit.WebView.MetricsOptOut" android:value="true" />

View file

@ -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);
}
}

View file

@ -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 {
* <li>The app did not recently check for updates.
* We do not want to make unnecessary connections and DOS our servers.</li>
* </ul>
* <b>Must not be executed</b> 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();
}
}

View file

@ -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