Try not blocking main threads to start foreground service.

This commit is contained in:
Clark 2023-05-17 13:43:01 -04:00 committed by Greyson Parrelli
parent 99d3f9918f
commit 534c5c3c64
4 changed files with 16 additions and 5 deletions

View file

@ -277,7 +277,7 @@ public class VoiceNotePlaybackService extends MediaBrowserServiceCompat {
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
if (ongoing && !isForegroundService) {
try {
ForegroundServiceUtil.startWhenCapable(getApplicationContext(), new Intent(getApplicationContext(), VoiceNotePlaybackService.class));
ForegroundServiceUtil.start(getApplicationContext(), new Intent(getApplicationContext(), VoiceNotePlaybackService.class));
startForeground(notificationId, notification);
isForegroundService = true;
} catch (UnableToStartException e) {

View file

@ -30,7 +30,7 @@ import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor
object FcmFetchManager {
private val TAG = Log.tag(FcmFetchManager::class.java)
private const val MAX_BLOCKING_TIME_MS = 500L
private val EXECUTOR = SerialMonoLifoExecutor(SignalExecutors.UNBOUNDED)
@Volatile
@ -48,7 +48,7 @@ object FcmFetchManager {
try {
if (foreground) {
Log.i(TAG, "Starting in the foreground.")
ForegroundServiceUtil.startWhenCapableOrThrow(context, Intent(context, FcmFetchForegroundService::class.java))
ForegroundServiceUtil.startWhenCapableOrThrow(context, Intent(context, FcmFetchForegroundService::class.java), MAX_BLOCKING_TIME_MS)
startedForeground = true
} else {
Log.i(TAG, "Starting in the background.")

View file

@ -8,6 +8,7 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.SystemClock
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat
import org.signal.core.util.PendingIntentFlags
import org.signal.core.util.logging.Log
@ -73,6 +74,7 @@ object ForegroundServiceUtil {
@JvmOverloads
@JvmStatic
@Throws(UnableToStartException::class)
@WorkerThread
fun startWhenCapable(context: Context, intent: Intent, timeout: Long = DEFAULT_TIMEOUT) {
try {
start(context, intent)
@ -91,6 +93,7 @@ object ForegroundServiceUtil {
*/
@JvmOverloads
@JvmStatic
@WorkerThread
fun startWhenCapableOrThrow(context: Context, intent: Intent, timeout: Long = DEFAULT_TIMEOUT) {
try {
startWhenCapable(context, intent, timeout)

View file

@ -25,6 +25,7 @@ import org.thoughtcrime.securesms.jobmanager.JobTracker
import org.thoughtcrime.securesms.jobmanager.JobTracker.JobListener
import org.thoughtcrime.securesms.jobmanager.impl.BackoffUtil
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
import org.thoughtcrime.securesms.jobs.ForegroundServiceUtil
import org.thoughtcrime.securesms.jobs.ForegroundServiceUtil.startWhenCapable
import org.thoughtcrime.securesms.jobs.PushDecryptMessageJob
import org.thoughtcrime.securesms.jobs.PushProcessMessageJob
@ -94,9 +95,16 @@ class IncomingMessageObserver(private val context: Application) {
if (!SignalStore.account().fcmEnabled || SignalStore.internalValues().isWebsocketModeForced) {
try {
startWhenCapable(context, Intent(context, ForegroundService::class.java))
ForegroundServiceUtil.start(context, Intent(context, ForegroundService::class.java))
} catch (e: UnableToStartException) {
Log.w(TAG, "Unable to start foreground service for websocket!", e)
Log.w(TAG, "Unable to start foreground service for websocket. Deferring to background to try with blocking")
SignalExecutors.UNBOUNDED.execute {
try {
startWhenCapable(context, Intent(context, ForegroundService::class.java))
} catch (e: UnableToStartException) {
Log.w(TAG, "Unable to start foreground service for websocket!", e)
}
}
}
}