Don't start disallowed foreground service on API 31+.

This commit is contained in:
Greyson Parrelli 2022-09-24 17:48:15 -04:00 committed by Cody Henthorne
parent 4f803c695b
commit 457c3c0526

View file

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.messages;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import androidx.annotation.NonNull;
@ -18,6 +19,7 @@ import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.WakeLockUtil;
import java.io.Closeable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@ -60,7 +62,7 @@ public class BackgroundMessageRetriever {
}
synchronized (this) {
try (DelayedNotificationController controller = GenericForegroundService.startForegroundTaskDelayed(context, context.getString(R.string.BackgroundMessageRetriever_checking_for_messages), showNotificationAfterMs, R.drawable.ic_signal_refresh)) {
try (NoExceptionCloseable unused = startDelayedForegroundServiceIfPossible(context, showNotificationAfterMs)) {
PowerManager.WakeLock wakeLock = null;
try {
@ -87,6 +89,14 @@ public class BackgroundMessageRetriever {
}
}
private NoExceptionCloseable startDelayedForegroundServiceIfPossible(@NonNull Context context, long showNotificationAfterMs) {
if (Build.VERSION.SDK_INT < 31) {
return (NoExceptionCloseable) GenericForegroundService.startForegroundTaskDelayed(context, context.getString(R.string.BackgroundMessageRetriever_checking_for_messages), showNotificationAfterMs, R.drawable.ic_signal_refresh);
} else {
return () -> {};
}
}
private boolean executeBackgroundRetrieval(@NonNull Context context, long startTime, @NonNull MessageRetrievalStrategy[] strategies) {
boolean success = false;
@ -129,4 +139,9 @@ public class BackgroundMessageRetriever {
private static String logSuffix(long startTime) {
return " (" + (System.currentTimeMillis() - startTime) + " ms elapsed)";
}
private interface NoExceptionCloseable extends AutoCloseable {
@Override
void close();
}
}