From 86ddb51a87879cf5df49d7acf8212b9c4e980acb Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 26 Sep 2025 05:01:33 +0200 Subject: [PATCH] [android] Implement foreground notification service (#480) A notification is shown when emulation is active. This, in theory should help preventing Eden for getting destroyed in the background. It is also a nice Q.O.L feature to have. Credits go to t895 for the initial implementation. This was back-ported from older official Citra builds, although with crashes which i fixed myself. Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/480 Reviewed-by: crueter Co-authored-by: nyx Co-committed-by: nyx --- src/android/app/src/main/AndroidManifest.xml | 6 ++ .../java/org/yuzu/yuzu_emu/YuzuApplication.kt | 12 +++ .../yuzu_emu/activities/EmulationActivity.kt | 19 +++++ .../org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 9 +++ .../yuzu/yuzu_emu/utils/ForegroundService.kt | 79 +++++++++++++++++++ .../app/src/main/res/values/strings.xml | 5 ++ 6 files changed, 130 insertions(+) create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt diff --git a/src/android/app/src/main/AndroidManifest.xml b/src/android/app/src/main/AndroidManifest.xml index d31deaa355..45c5dfef8c 100644 --- a/src/android/app/src/main/AndroidManifest.xml +++ b/src/android/app/src/main/AndroidManifest.xml @@ -27,6 +27,8 @@ SPDX-License-Identifier: GPL-3.0-or-later + + @@ -93,6 +95,10 @@ SPDX-License-Identifier: GPL-3.0-or-later + + + + if (result != null) { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt new file mode 100644 index 0000000000..c181656d99 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ForegroundService.kt @@ -0,0 +1,79 @@ +// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +// Copyright 2023 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +package org.yuzu.yuzu_emu.utils + +import android.app.PendingIntent +import android.app.Service +import android.content.Intent +import android.os.IBinder +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.activities.EmulationActivity + +/** + * A service that shows a permanent notification in the background to avoid the app getting + * cleared from memory by the system. + */ +class ForegroundService : Service() { + companion object { + const val EMULATION_RUNNING_NOTIFICATION = 0x1000 + + const val ACTION_STOP = "stop" + } + + private fun showRunningNotification() { + // Intent is used to resume emulation if the notification is clicked + val contentIntent = PendingIntent.getActivity( + this, + 0, + Intent(this, EmulationActivity::class.java), + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + ) + val builder = + NotificationCompat.Builder(this, getString(R.string.app_notification_channel_id)) + .setSmallIcon(R.drawable.ic_stat_notification_logo) + .setContentTitle(getString(R.string.app_name)) + .setContentText(getString(R.string.app_notification_running)) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setOngoing(true) + .setVibrate(null) + .setSound(null) + .setContentIntent(contentIntent) + startForeground(EMULATION_RUNNING_NOTIFICATION, builder.build()) + } + + override fun onBind(intent: Intent): IBinder? { + return null + } + + override fun onCreate() { + showRunningNotification() + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent?.action == ACTION_STOP) { + try { + NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION) + stopForeground(STOP_FOREGROUND_REMOVE) + } catch (e: Exception) { + Log.error("Failed to stop foreground service") + } + stopSelfResult(startId) + return START_NOT_STICKY + } + + if (intent != null) { + showRunningNotification() + } + return START_STICKY + } + + override fun onDestroy() = + NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION) +} diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index 1545576ea8..48ea3ed99b 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -8,6 +8,11 @@ noticesAndErrors Shows notifications when something goes wrong. Notification permission not granted! + Eden + Eden + Eden Switch emulator notifications + Eden is Running + (Enhanced)