1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

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 // https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0-alpha01
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01' implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01'
implementation 'androidx.webkit:webkit:1.4.0' implementation 'androidx.webkit:webkit:1.4.0'
implementation 'androidx.work:work-runtime:2.7.1'
implementation 'com.google.android.material:material:1.4.0' implementation 'com.google.android.material:material:1.4.0'
/** Third-party libraries **/ /** Third-party libraries **/

View File

@ -381,10 +381,6 @@
<service <service
android:name=".RouterActivity$FetcherService" android:name=".RouterActivity$FetcherService"
android:exported="false" /> 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 --> <!-- opting out of sending metrics to Google in Android System WebView -->
<meta-data android:name="android.webkit.WebView.MetricsOptOut" android:value="true" /> <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); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
if (prefs.getBoolean(app.getString(R.string.update_app_key), true)) { 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. // and eventually searching for a new version.
// The service searching for a new NewPipe version must not be started in background. NewVersionWorker.enqueueNewVersionCheckingWork(app);
CheckForNewAppVersion.startNewVersionCheckService(app);
} }
} }

View File

@ -8,11 +8,14 @@ import android.net.Uri;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.JobIntentService;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.preference.PreferenceManager; 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.JsonObject;
import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParser;
@ -24,12 +27,16 @@ import org.schabi.newpipe.util.ReleaseVersionUtil;
import java.io.IOException; 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 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 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. * 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, * checks if all conditions for performing a version check are met,
* fetches the API endpoint {@link #NEWPIPE_API_URL} containing info * fetches the API endpoint {@link #NEWPIPE_API_URL} containing info
* about the latest NewPipe version * about the latest NewPipe version
@ -144,22 +151,25 @@ public final class CheckForNewAppVersion extends JobIntentService {
* <li>The app did not recently check for updates. * <li>The app did not recently check for updates.
* We do not want to make unnecessary connections and DOS our servers.</li> * We do not want to make unnecessary connections and DOS our servers.</li>
* </ul> * </ul>
* <b>Must not be executed</b> when the app is in background.
*/ */
public static void startNewVersionCheckService(final Context context) { public static void enqueueNewVersionCheckingWork(final Context context) {
enqueueWork(context, CheckForNewAppVersion.class, JOB_ID, final WorkRequest workRequest =
new Intent(context, CheckForNewAppVersion.class)); new OneTimeWorkRequest.Builder(NewVersionWorker.class).build();
WorkManager.getInstance(context).enqueue(workRequest);
} }
@NonNull
@Override @Override
protected void onHandleWork(@Nullable final Intent intent) { public Result doWork() {
try { try {
checkNewVersion(); checkNewVersion();
} catch (final IOException e) { } catch (final IOException e) {
Log.w(TAG, "Could not fetch NewPipe API: probably network problem", e); Log.w(TAG, "Could not fetch NewPipe API: probably network problem", e);
return Result.failure();
} catch (final ReCaptchaException e) { } catch (final ReCaptchaException e) {
Log.e(TAG, "ReCaptchaException should never happen here.", 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; package org.schabi.newpipe.settings;
import static org.schabi.newpipe.CheckForNewAppVersion.startNewVersionCheckService;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Toast; import android.widget.Toast;
import androidx.preference.Preference; import androidx.preference.Preference;
import org.schabi.newpipe.NewVersionWorker;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
public class UpdateSettingsFragment extends BasePreferenceFragment { 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. // Reset the expire time. This is necessary to check for an update immediately.
defaultPreferences.edit() defaultPreferences.edit()
.putLong(getString(R.string.update_expiry_key), 0).apply(); .putLong(getString(R.string.update_expiry_key), 0).apply();
startNewVersionCheckService(getContext()); NewVersionWorker.enqueueNewVersionCheckingWork(getContext());
} }
@Override @Override