use explicit FetchForegroundService

this avoids potential issues with GenericForegroundService
which eg. may block app start.
This commit is contained in:
B. Petersen
2024-09-21 23:39:34 +02:00
committed by bjoern
parent 3a4c02c8cd
commit ba3ea172e5
6 changed files with 73 additions and 22 deletions
@@ -11,7 +11,7 @@ import com.b44t.messenger.DcEvent;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.notifications.FcmReceiveService;
import org.thoughtcrime.securesms.service.FetchForegroundService;
import org.thoughtcrime.securesms.util.Util;
import java.util.ArrayList;
@@ -179,7 +179,7 @@ public class DcEventCenter {
break;
case DcContext.DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE:
FcmReceiveService.backgroundFetchDone();
FetchForegroundService.stop(context);
break;
case DcContext.DC_EVENT_IMEX_PROGRESS:
@@ -168,6 +168,7 @@ public class NotificationCenter {
public static final int ID_PERMANENT = 1;
public static final int ID_MSG_SUMMARY = 2;
public static final int ID_GENERIC = 3;
public static final int ID_FETCH = 4;
public static final int ID_MSG_OFFSET = 0; // msgId is added - as msgId start at 10, there are no conflicts with lower numbers
@@ -0,0 +1,61 @@
package org.thoughtcrime.securesms.service;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.notifications.NotificationCenter;
public final class FetchForegroundService extends Service {
private static final Object SERVICE_LOCK = new Object();
private static Intent service;
public static void start(Context context) {
GenericForegroundService.createFgNotificationChannel(context);
synchronized (SERVICE_LOCK) {
if (service == null) {
service = new Intent(context, FetchForegroundService.class);
ContextCompat.startForegroundService(context, service);
}
}
}
public static void stop(Context context) {
synchronized (SERVICE_LOCK) {
if (service != null) {
context.stopService(service);
service = null;
}
}
}
@Override
public void onCreate() {
super.onCreate();
Notification notification = new NotificationCompat.Builder(this, NotificationCenter.CH_GENERIC)
.setContentTitle(getString(R.string.connectivity_updating))
.setSmallIcon(R.drawable.notification_permanent)
.build();
startForeground(NotificationCenter.ID_FETCH, notification);
}
@Override
public void onDestroy() {
stopForeground(true);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
@@ -185,7 +185,7 @@ public final class GenericForegroundService extends Service {
}
@TargetApi(Build.VERSION_CODES.O)
static private void createFgNotificationChannel(Context context) {
static public void createFgNotificationChannel(Context context) {
if(!CHANNEL_CREATED.get() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CHANNEL_CREATED.set(true);
NotificationChannel channel = new NotificationChannel(NotificationCenter.CH_GENERIC,