Be more resistent to android disallowing service starts.

This commit is contained in:
Greyson Parrelli 2022-06-13 10:17:43 -04:00
parent d5d7c73ebf
commit 523f9c7409

View file

@ -10,6 +10,7 @@ import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob
import org.thoughtcrime.securesms.messages.RestStrategy import org.thoughtcrime.securesms.messages.RestStrategy
import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor
import java.lang.IllegalStateException
/** /**
* Our goals with FCM processing are as follows: * Our goals with FCM processing are as follows:
@ -42,22 +43,26 @@ object FcmFetchManager {
@JvmStatic @JvmStatic
fun enqueue(context: Context, foreground: Boolean) { fun enqueue(context: Context, foreground: Boolean) {
synchronized(this) { synchronized(this) {
if (foreground) { try {
Log.i(TAG, "Starting in the foreground.") if (foreground) {
ContextCompat.startForegroundService(context, Intent(context, FcmFetchForegroundService::class.java)) Log.i(TAG, "Starting in the foreground.")
startedForeground = true ContextCompat.startForegroundService(context, Intent(context, FcmFetchForegroundService::class.java))
} else { startedForeground = true
Log.i(TAG, "Starting in the background.") } else {
context.startService(Intent(context, FcmFetchBackgroundService::class.java)) 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) { if (performedReplace) {
Log.i(TAG, "Already have one running and one enqueued. Ignoring.") Log.i(TAG, "Already have one running and one enqueued. Ignoring.")
} else { } else {
activeCount++ activeCount++
Log.i(TAG, "Incrementing active count to $activeCount") Log.i(TAG, "Incrementing active count to $activeCount")
}
} catch (e: IllegalStateException) {
Log.w(TAG, "Failed to start service!", e)
} }
} }
} }