From 79d75d14e0c7b165e7b13a79a8db993342aa2e8c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 25 Sep 2023 09:22:29 +0200 Subject: [PATCH] catch rare instances of foreground service not allowed to start --- .../services/XmppConnectionService.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 79d34e19926824255e26cea25da5be74371fd9da..72a56fbec0fb62f30cd839bcf00ca7e8fa582459 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1580,12 +1580,12 @@ public class XmppConnectionService extends Service { if (showOngoing) { notification = this.mNotificationService.getOngoingCallNotification(ongoing); id = NotificationService.ONGOING_CALL_NOTIFICATION_ID; - startForeground(id, notification); + startForegroundOrCatch(id, notification); mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID); } else { notification = this.mNotificationService.createForegroundNotification(); id = NotificationService.FOREGROUND_NOTIFICATION_ID; - startForeground(id, notification); + startForegroundOrCatch(id, notification); } if (!mForceForegroundService.get()) { @@ -1605,6 +1605,14 @@ public class XmppConnectionService extends Service { Log.d(Config.LOGTAG, "ForegroundService: " + (status ? "on" : "off")); } + private void startForegroundOrCatch(final int id, final Notification notification) { + try { + startForeground(id, notification); + } catch (final IllegalStateException e) { + Log.e(Config.LOGTAG,"Could not start foreground service", e); + } + } + public boolean foregroundNotificationNeedsUpdatingWhenErrorStateChanges() { return !mForceForegroundService.get() && ongoingCall.get() == null && Compatibility.keepForegroundService(this) && hasEnabledAccounts(); }