Dont block main thread when we try to stop FCM fetch service.

This commit is contained in:
Clark
2023-06-07 14:12:35 -04:00
committed by GitHub
parent b0c4bb04e7
commit bf048e2a75
2 changed files with 16 additions and 1 deletions

View File

@@ -47,6 +47,7 @@ class FcmFetchForegroundService : Service() {
return if (intent != null && intent.getBooleanExtra(KEY_STOP_SELF, false)) {
stopForeground(true)
stopSelf()
FcmFetchManager.onForegroundFetchServiceStop()
START_NOT_STICKY
} else {
START_STICKY

View File

@@ -39,6 +39,9 @@ object FcmFetchManager {
@Volatile
private var startedForeground = false
@Volatile
private var stoppingForeground = false
/**
* @return True if a service was successfully started, otherwise false.
*/
@@ -89,6 +92,7 @@ object FcmFetchManager {
if (startedForeground) {
try {
context.startService(FcmFetchForegroundService.buildStopIntent(context))
stoppingForeground = true
} catch (e: IllegalStateException) {
Log.w(TAG, "Failed to stop the foreground notification!", e)
}
@@ -102,7 +106,10 @@ object FcmFetchManager {
@JvmStatic
fun tryLegacyFallback(context: Context) {
synchronized(this) {
if (startedForeground) {
if (startedForeground || stoppingForeground) {
if (stoppingForeground) {
Log.i(TAG, "Legacy fallback: foreground service is stopping, but trying to run in background anyways.")
}
val performedReplace = EXECUTOR.enqueue { fetch(context) }
if (performedReplace) {
@@ -134,4 +141,11 @@ object FcmFetchManager {
}
}
}
fun onForegroundFetchServiceStop() {
synchronized(this) {
stoppingForeground = false
Log.i(TAG, "Foreground service stopped successfully")
}
}
}