Do not throw an ISE when we cannot start a foreground service from calling.
This commit is contained in:
parent
d3f779cea9
commit
a870fe9e1a
2 changed files with 22 additions and 6 deletions
|
@ -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.
|
* 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].
|
* However, it is always possible that the attempt will fail, so always handle the [UnableToStartException].
|
||||||
|
|
|
@ -80,22 +80,22 @@ public final class WebRtcCallService extends Service implements SignalAudioManag
|
||||||
.putExtra(EXTRA_RECIPIENT_ID, recipientId)
|
.putExtra(EXTRA_RECIPIENT_ID, recipientId)
|
||||||
.putExtra(EXTRA_IS_VIDEO_CALL, isVideoCall);
|
.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) {
|
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) {
|
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) {
|
public static void stop(@NonNull Context context) {
|
||||||
Intent intent = new Intent(context, WebRtcCallService.class);
|
Intent intent = new Intent(context, WebRtcCallService.class);
|
||||||
intent.setAction(ACTION_STOP);
|
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) {
|
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 intent = new Intent(context, WebRtcCallService.class);
|
||||||
intent.setAction(ACTION_SEND_AUDIO_COMMAND)
|
intent.setAction(ACTION_SEND_AUDIO_COMMAND)
|
||||||
.putExtra(EXTRA_AUDIO_COMMAND, 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) {
|
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)
|
intent.setAction(ACTION_CHANGE_POWER_BUTTON)
|
||||||
.putExtra(EXTRA_ENABLED, register);
|
.putExtra(EXTRA_ENABLED, register);
|
||||||
|
|
||||||
ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT);
|
ForegroundServiceUtil.tryToStartWhenCapable(context, intent, FOREGROUND_SERVICE_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue