Do not throw an ISE when we cannot start a foreground service from calling.

This commit is contained in:
Alex Hart 2023-07-06 16:12:09 -03:00 committed by GitHub
parent d3f779cea9
commit a870fe9e1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View file

@ -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].

View file

@ -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