From ebb645a349a7059ca7a9147957d45bd1fc129407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 29 Dec 2024 20:52:46 +0100 Subject: [PATCH 1/2] Use `ServiceCompat` for server service on Android Follow current best practice and simplify code. --- .../java/org/ddnet/client/ServerService.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/scripts/android/files/java/org/ddnet/client/ServerService.java b/scripts/android/files/java/org/ddnet/client/ServerService.java index ba297bb786f..00b42e903ac 100644 --- a/scripts/android/files/java/org/ddnet/client/ServerService.java +++ b/scripts/android/files/java/org/ddnet/client/ServerService.java @@ -2,8 +2,9 @@ import java.io.File; -import androidx.core.app.RemoteInput; import androidx.core.app.NotificationCompat; +import androidx.core.app.RemoteInput; +import androidx.core.app.ServiceCompat; import android.app.*; import android.content.*; @@ -63,19 +64,12 @@ public void onCreate() { createNotificationChannel(); - Notification notification = createRunningNotification(); - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground( - NOTIFICATION_ID, - notification, - ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST - ); - } else { - startForeground( - NOTIFICATION_ID, - notification - ); - } + ServiceCompat.startForeground( + this, + NOTIFICATION_ID, + createRunningNotification(), + ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST + ); thread = new NativeServerThread(this); thread.start(); From aa73de255d5c9268e3aca327228b1bc66e754d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 30 Dec 2024 16:01:51 +0100 Subject: [PATCH 2/2] Fix server not starting on Android 12 and older versions Only request the notification permission on Android 13 and newer, as on older Android versions it was not a permission but a part of the notification system. The `SDLActivity` does not use the recommended `ContextCompat` and `ActivityCompat` for permissions so requesting the unknown permission fails. --- src/android/android_main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/android/android_main.cpp b/src/android/android_main.cpp index 37790e689ce..00571b3c400 100644 --- a/src/android/android_main.cpp +++ b/src/android/android_main.cpp @@ -241,8 +241,9 @@ bool StartAndroidServer() { // We need the notification-permission to show a notification for the foreground service. // We use SDL for this instead of doing it on the Java side because this function blocks - // until the user made a choice, which is easier to handle. - if(!SDL_AndroidRequestPermission("android.permission.POST_NOTIFICATIONS")) + // until the user made a choice, which is easier to handle. Only Android 13 (API 33) and + // newer support requesting this permission at runtime. + if(SDL_GetAndroidSDKVersion() >= 33 && !SDL_AndroidRequestPermission("android.permission.POST_NOTIFICATIONS")) { return false; }