Always use new foreground service utils.

This commit is contained in:
Greyson Parrelli
2022-11-29 14:40:29 -05:00
committed by Cody Henthorne
parent 7b13550086
commit 23804046c6
19 changed files with 350 additions and 126 deletions

View File

@@ -1,6 +1,5 @@
package org.thoughtcrime.securesms.service;
import android.app.ForegroundServiceStartNotAllowedException;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
@@ -19,6 +18,8 @@ import org.signal.core.util.PendingIntentFlags;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.MainActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.jobs.ForegroundServiceUtil;
import org.thoughtcrime.securesms.jobs.UnableToStartException;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.whispersystems.signalservice.api.util.Preconditions;
@@ -173,20 +174,7 @@ public final class GenericForegroundService extends Service {
Log.i(TAG, String.format(Locale.US, "Starting foreground service (%s) id=%d", task, id));
if (Build.VERSION.SDK_INT < 31) {
ContextCompat.startForegroundService(context, intent);
} else {
try {
ContextCompat.startForegroundService(context, intent);
} catch (IllegalStateException e) {
if (e instanceof ForegroundServiceStartNotAllowedException) {
Log.e(TAG, "Unable to start foreground service", e);
throw new UnableToStartException(e);
} else {
throw e;
}
}
}
ForegroundServiceUtil.start(context, intent);
return new NotificationController(context, id);
}
@@ -197,7 +185,7 @@ public final class GenericForegroundService extends Service {
intent.putExtra(EXTRA_ID, id);
Log.i(TAG, String.format(Locale.US, "Stopping foreground service id=%d", id));
ContextCompat.startForegroundService(context, intent);
ForegroundServiceUtil.startWhenCapableOrThrow(context, intent);
}
synchronized void replaceTitle(int id, @NonNull String title) {
@@ -325,10 +313,4 @@ public final class GenericForegroundService extends Service {
return GenericForegroundService.this;
}
}
public static final class UnableToStartException extends Exception {
public UnableToStartException(Throwable cause) {
super(cause);
}
}
}

View File

@@ -17,11 +17,11 @@ import android.telephony.TelephonyManager;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import org.signal.core.util.ThreadUtil;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.ForegroundServiceUtil;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.util.TelephonyUtil;
@@ -58,6 +58,7 @@ public final class WebRtcCallService extends Service implements SignalAudioManag
private static final int INVALID_NOTIFICATION_ID = -1;
private static final long REQUEST_WEBSOCKET_STAY_OPEN_DELAY = TimeUnit.MINUTES.toMillis(1);
private static final long FOREGROUND_SERVICE_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
private final WebSocketKeepAliveTask webSocketKeepAliveTask = new WebSocketKeepAliveTask();
@@ -78,22 +79,22 @@ public final class WebRtcCallService extends Service implements SignalAudioManag
.putExtra(EXTRA_UPDATE_TYPE, type)
.putExtra(EXTRA_RECIPIENT_ID, recipientId);
ContextCompat.startForegroundService(context, intent);
ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT);
}
public static void denyCall(@NonNull Context context) {
ContextCompat.startForegroundService(context, denyCallIntent(context));
ForegroundServiceUtil.startWhenCapableOrThrow(context, denyCallIntent(context), FOREGROUND_SERVICE_TIMEOUT);
}
public static void hangup(@NonNull Context context) {
ContextCompat.startForegroundService(context, hangupIntent(context));
ForegroundServiceUtil.startWhenCapableOrThrow(context, hangupIntent(context), FOREGROUND_SERVICE_TIMEOUT);
}
public static void stop(@NonNull Context context) {
Intent intent = new Intent(context, WebRtcCallService.class);
intent.setAction(ACTION_STOP);
ContextCompat.startForegroundService(context, intent);
ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT);
}
public static @NonNull Intent denyCallIntent(@NonNull Context context) {
@@ -108,7 +109,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);
ContextCompat.startForegroundService(context, intent);
ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT);
}
public static void changePowerButtonReceiver(@NonNull Context context, boolean register) {
@@ -116,7 +117,7 @@ public final class WebRtcCallService extends Service implements SignalAudioManag
intent.setAction(ACTION_CHANGE_POWER_BUTTON)
.putExtra(EXTRA_ENABLED, register);
ContextCompat.startForegroundService(context, intent);
ForegroundServiceUtil.startWhenCapableOrThrow(context, intent, FOREGROUND_SERVICE_TIMEOUT);
}
@Override