mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
use explicit FetchForegroundService
this avoids potential issues with GenericForegroundService which eg. may block app start.
This commit is contained in:
@@ -17,20 +17,15 @@ import com.google.firebase.messaging.RemoteMessage;
|
||||
|
||||
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.NotificationController;
|
||||
import org.thoughtcrime.securesms.service.FetchForegroundService;
|
||||
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) {
|
||||
@@ -101,23 +96,13 @@ public class FcmReceiveService extends FirebaseMessagingService {
|
||||
@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");
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user