From a870fe9e1ac0821cfbc78021f67963a06d2afd1c Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Thu, 6 Jul 2023 16:12:09 -0300 Subject: [PATCH] Do not throw an ISE when we cannot start a foreground service from calling. --- .../securesms/jobs/ForegroundServiceUtil.kt | 16 ++++++++++++++++ .../service/webrtc/WebRtcCallService.java | 12 ++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/ForegroundServiceUtil.kt b/app/src/main/java/org/thoughtcrime/securesms/jobs/ForegroundServiceUtil.kt index 7c5445bd99..f9cadd24ca 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/ForegroundServiceUtil.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/ForegroundServiceUtil.kt @@ -102,6 +102,22 @@ object ForegroundServiceUtil { } } + /** + * Identical to [startWhenCapable], but we swallow the error. + * + * @param timeout The maximum time you're willing to wait to create the conditions for a foreground service to start. + */ + @JvmOverloads + @JvmStatic + @WorkerThread + fun tryToStartWhenCapable(context: Context, intent: Intent, timeout: Long = DEFAULT_TIMEOUT) { + return try { + startWhenCapable(context, intent, timeout) + } catch (e: UnableToStartException) { + Log.w(TAG, "Failed to start foreground service", e) + } + } + /** * Does its best to start a foreground service with your task name, including possibly blocking and waiting until we are able to. * However, it is always possible that the attempt will fail, so always handle the [UnableToStartException]. diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/WebRtcCallService.java b/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/WebRtcCallService.java index 1ada00f78f..8937f412a9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/WebRtcCallService.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/WebRtcCallService.java @@ -80,22 +80,22 @@ public final class WebRtcCallService extends Service implements SignalAudioManag .putExtra(EXTRA_RECIPIENT_ID, recipientId) .putExtra(EXTRA_IS_VIDEO_CALL, isVideoCall); - ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT); + ForegroundServiceUtil.tryToStartWhenCapable(context, intent, FOREGROUND_SERVICE_TIMEOUT); } public static void denyCall(@NonNull Context context) { - ForegroundServiceUtil.startWhenCapableOrThrow(context, denyCallIntent(context), FOREGROUND_SERVICE_TIMEOUT); + ForegroundServiceUtil.tryToStartWhenCapable(context, denyCallIntent(context), FOREGROUND_SERVICE_TIMEOUT); } public static void hangup(@NonNull Context context) { - ForegroundServiceUtil.startWhenCapableOrThrow(context, hangupIntent(context), FOREGROUND_SERVICE_TIMEOUT); + ForegroundServiceUtil.tryToStartWhenCapable(context, hangupIntent(context), FOREGROUND_SERVICE_TIMEOUT); } public static void stop(@NonNull Context context) { Intent intent = new Intent(context, WebRtcCallService.class); intent.setAction(ACTION_STOP); - ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT); + ForegroundServiceUtil.tryToStartWhenCapable(context, intent, FOREGROUND_SERVICE_TIMEOUT); } public static @NonNull Intent denyCallIntent(@NonNull Context context) { @@ -110,7 +110,7 @@ public final class WebRtcCallService extends Service implements SignalAudioManag Intent intent = new Intent(context, WebRtcCallService.class); intent.setAction(ACTION_SEND_AUDIO_COMMAND) .putExtra(EXTRA_AUDIO_COMMAND, command); - ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT); + ForegroundServiceUtil.tryToStartWhenCapable(context, intent, FOREGROUND_SERVICE_TIMEOUT); } public static void changePowerButtonReceiver(@NonNull Context context, boolean register) { @@ -118,7 +118,7 @@ public final class WebRtcCallService extends Service implements SignalAudioManag intent.setAction(ACTION_CHANGE_POWER_BUTTON) .putExtra(EXTRA_ENABLED, register); - ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT); + ForegroundServiceUtil.tryToStartWhenCapable(context, intent, FOREGROUND_SERVICE_TIMEOUT); } @Override