move backgroundFetch() to FetchForegroundService

This commit is contained in:
B. Petersen
2024-09-22 12:37:01 +02:00
committed by bjoern
parent 1bc40837fe
commit 9fc4bfb970
2 changed files with 18 additions and 8 deletions
@@ -5,15 +5,20 @@ import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.notifications.FcmReceiveService;
import org.thoughtcrime.securesms.notifications.NotificationCenter;
import org.thoughtcrime.securesms.util.Util;
public final class FetchForegroundService extends Service {
private static final String TAG = FcmReceiveService.class.getSimpleName();
private static final Object SERVICE_LOCK = new Object();
private static Intent service;
@@ -38,6 +43,7 @@ public final class FetchForegroundService extends Service {
@Override
public void onCreate() {
Log.i(TAG, "Creating fetch service");
super.onCreate();
Notification notification = new NotificationCompat.Builder(this, NotificationCenter.CH_GENERIC)
@@ -46,6 +52,18 @@ public final class FetchForegroundService extends Service {
.build();
startForeground(NotificationCenter.ID_FETCH, notification);
// Start explicit fetch only after we marked ourselves as requiring foreground;
// this may help we on getting network and time adequately
// Fetch is started in background to not block the UI.
// We then run not longer than the max. of 20 seconds,
// see https://firebase.google.com/docs/cloud-messaging/android/receive .
Util.runOnAnyBackgroundThread(() -> {
Log.i(TAG, "Starting fetch");
if (!ApplicationContext.dcAccounts.backgroundFetch(19)) {
FetchForegroundService.stop(this);
}
});
}
@Override