Migrate CheckForNewAppVersion to JobIntentService

This commit is contained in:
TacoTheDank 2022-03-03 13:24:12 -05:00
parent 0f175de599
commit 81fef1be19
4 changed files with 12 additions and 13 deletions

View File

@ -383,7 +383,8 @@
android:exported="false" />
<service
android:name=".CheckForNewAppVersion"
android:exported="false" />
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

@ -1,7 +1,7 @@
package org.schabi.newpipe;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
@ -9,6 +9,7 @@ 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;
@ -23,14 +24,12 @@ import org.schabi.newpipe.util.ReleaseVersionUtil;
import java.io.IOException;
public final class CheckForNewAppVersion extends IntentService {
public CheckForNewAppVersion() {
super("CheckForNewAppVersion");
}
public final class CheckForNewAppVersion extends JobIntentService {
private static final boolean DEBUG = MainActivity.DEBUG;
private static final String TAG = CheckForNewAppVersion.class.getSimpleName();
private static final String NEWPIPE_API_URL = "https://newpipe.net/api/data.json";
private static final int JOB_ID = -17000;
/**
* Method to compare the current and latest available app version.
@ -147,14 +146,13 @@ public final class CheckForNewAppVersion extends IntentService {
* </ul>
* <b>Must not be executed</b> when the app is in background.
*/
public static void startNewVersionCheckService() {
final Intent intent = new Intent(App.getApp().getApplicationContext(),
CheckForNewAppVersion.class);
App.getApp().startService(intent);
public static void startNewVersionCheckService(final Context context) {
enqueueWork(context, CheckForNewAppVersion.class, JOB_ID,
new Intent(context, CheckForNewAppVersion.class));
}
@Override
protected void onHandleIntent(@Nullable final Intent intent) {
protected void onHandleWork(@Nullable final Intent intent) {
try {
checkNewVersion();
} catch (final IOException e) {

View File

@ -176,7 +176,7 @@ public class MainActivity extends AppCompatActivity {
// Start the service 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();
CheckForNewAppVersion.startNewVersionCheckService(app);
}
}

View File

@ -33,7 +33,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();
startNewVersionCheckService(getContext());
}
@Override