Skip to content

Commit

Permalink
use explicit FetchForegroundService
Browse files Browse the repository at this point in the history
this avoids potential issues with GenericForegroundService
which eg. may block app start.
  • Loading branch information
r10s committed Sep 21, 2024
1 parent f0da58b commit 01eaeee
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.service.GenericForegroundService;
import org.thoughtcrime.securesms.service.FetchForegroundService;
import org.thoughtcrime.securesms.service.NotificationController;
import org.thoughtcrime.securesms.util.Util;

public class FcmReceiveService extends FirebaseMessagingService {
private static final String TAG = FcmReceiveService.class.getSimpleName();
private static final Object INIT_LOCK = new Object();
private static final Object NOTIFICATION_CONTROLLER_LOCK = new Object();

private static boolean initialized;
private static volatile boolean triedRegistering;
private static volatile String prefixedToken;
private static NotificationController notificationController;

public static void register(Context context) {
if (Build.VERSION.SDK_INT < 19) {
Expand Down Expand Up @@ -101,23 +98,13 @@ public static String getToken() {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Log.i(TAG, "FCM push notification received");
synchronized (NOTIFICATION_CONTROLLER_LOCK) {
notificationController = GenericForegroundService.startForegroundTask(this, getString(R.string.connectivity_updating));
if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { // we should complete within 20 seconds
notificationController.close();
notificationController = null;
}
FetchForegroundService.start(this);
if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { // we should complete within 20 seconds
FetchForegroundService.stop(this);
}
Log.i(TAG, "background fetch done");
}

public static void backgroundFetchDone() {
synchronized (NOTIFICATION_CONTROLLER_LOCK) {
notificationController.close();
notificationController = null;
}
}

@Override
public void onDeletedMessages() {
Log.i(TAG, "FCM push notifications dropped");
Expand Down
4 changes: 4 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@
android:name=".service.GenericForegroundService"
android:foregroundServiceType="dataSync" />

<service
android:name=".service.FetchForegroundService"
android:foregroundServiceType="dataSync" />

<service
android:name=".service.IPCAddAccountsService"
android:foregroundServiceType="dataSync"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -179,7 +180,7 @@ public long handleEvent(@NonNull DcEvent event) {
break;

case DcContext.DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE:
FcmReceiveService.backgroundFetchDone();
FetchForegroundService.stop(context);
break;

case DcContext.DC_EVENT_IMEX_PROGRESS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private PendingIntent getMarkAsReadIntent(ChatData chatData, int msgId, boolean
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


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ synchronized void replaceProgress(int id, int progressMax, int progress, boolean
}

@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,
Expand Down

0 comments on commit 01eaeee

Please sign in to comment.