From 523f9c74098252ad40fab6c543c064f225a94380 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Mon, 13 Jun 2022 10:17:43 -0400 Subject: [PATCH] Be more resistent to android disallowing service starts. --- .../securesms/gcm/FcmFetchManager.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchManager.kt b/app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchManager.kt index d8b0a2b947..483768b313 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/gcm/FcmFetchManager.kt @@ -10,6 +10,7 @@ import org.thoughtcrime.securesms.dependencies.ApplicationDependencies import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob import org.thoughtcrime.securesms.messages.RestStrategy import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor +import java.lang.IllegalStateException /** * Our goals with FCM processing are as follows: @@ -42,22 +43,26 @@ object FcmFetchManager { @JvmStatic fun enqueue(context: Context, foreground: Boolean) { synchronized(this) { - if (foreground) { - Log.i(TAG, "Starting in the foreground.") - ContextCompat.startForegroundService(context, Intent(context, FcmFetchForegroundService::class.java)) - startedForeground = true - } else { - Log.i(TAG, "Starting in the background.") - context.startService(Intent(context, FcmFetchBackgroundService::class.java)) - } + try { + if (foreground) { + Log.i(TAG, "Starting in the foreground.") + ContextCompat.startForegroundService(context, Intent(context, FcmFetchForegroundService::class.java)) + startedForeground = true + } else { + Log.i(TAG, "Starting in the background.") + context.startService(Intent(context, FcmFetchBackgroundService::class.java)) + } - val performedReplace = EXECUTOR.enqueue { fetch(context) } + val performedReplace = EXECUTOR.enqueue { fetch(context) } - if (performedReplace) { - Log.i(TAG, "Already have one running and one enqueued. Ignoring.") - } else { - activeCount++ - Log.i(TAG, "Incrementing active count to $activeCount") + if (performedReplace) { + Log.i(TAG, "Already have one running and one enqueued. Ignoring.") + } else { + activeCount++ + Log.i(TAG, "Incrementing active count to $activeCount") + } + } catch (e: IllegalStateException) { + Log.w(TAG, "Failed to start service!", e) } } }