From 0f9c5b8d6a127258d8f4c21b6c3b12fe1d3998f3 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Mon, 29 May 2023 03:16:04 -0400 Subject: [PATCH] android: Re-enable service notification --- .../yuzu_emu/activities/EmulationActivity.kt | 22 ++++++++----------- .../yuzu_emu/fragments/EmulationFragment.kt | 15 +++++++------ .../org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 4 ++-- .../yuzu/yuzu_emu/utils/ForegroundService.kt | 12 ++++++++-- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt index ea4071ba7..37caa1b83 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt @@ -31,18 +31,16 @@ import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.fragments.EmulationFragment import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.utils.ControllerMappingHelper +import org.yuzu.yuzu_emu.utils.ForegroundService import org.yuzu.yuzu_emu.utils.InputHandler import org.yuzu.yuzu_emu.utils.NfcReader import org.yuzu.yuzu_emu.utils.SerializableHelper.parcelable import org.yuzu.yuzu_emu.utils.ThemeHelper import kotlin.math.roundToInt -open class EmulationActivity : AppCompatActivity(), SensorEventListener { +class EmulationActivity : AppCompatActivity(), SensorEventListener { private var controllerMappingHelper: ControllerMappingHelper? = null - // TODO(bunnei): Disable notifications until we support app suspension. - //private Intent foregroundService; - var isActivityRecreated = false private var menuVisible = false private var emulationFragment: EmulationFragment? = null @@ -57,8 +55,7 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener { private lateinit var game: Game override fun onDestroy() { - // TODO(bunnei): Disable notifications until we support app suspension. - //stopService(foregroundService); + stopForegroundService(this) super.onDestroy() } @@ -100,9 +97,8 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener { inputHandler.initialize() // Start a foreground service to prevent the app from getting killed in the background - // TODO(bunnei): Disable notifications until we support app suspension. - //foregroundService = new Intent(EmulationActivity.this, ForegroundService.class); - //startForegroundService(foregroundService); + val startIntent = Intent(this, ForegroundService::class.java) + startForegroundService(startIntent) } override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { @@ -324,7 +320,6 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener { companion object { const val EXTRA_SELECTED_GAME = "SelectedGame" - private const val EMULATION_RUNNING_NOTIFICATION = 0x1000 fun launch(activity: AppCompatActivity, game: Game) { val launcher = Intent(activity, EmulationActivity::class.java) @@ -332,9 +327,10 @@ open class EmulationActivity : AppCompatActivity(), SensorEventListener { activity.startActivity(launcher) } - fun tryDismissRunningNotification(activity: Activity?) { - // TODO(bunnei): Disable notifications until we support app suspension. - //NotificationManagerCompat.from(activity).cancel(EMULATION_RUNNING_NOTIFICATION); + fun stopForegroundService(activity: Activity) { + val startIntent = Intent(activity, ForegroundService::class.java) + startIntent.action = ForegroundService.ACTION_STOP + activity.startForegroundService(startIntent) } private fun areCoordinatesOutside(view: View?, x: Float, y: Float): Boolean { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt index a31414a0e..ce3f2639a 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt @@ -123,8 +123,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } R.id.menu_exit -> { - requireActivity().finish() emulationState.stop() + requireActivity().finish() true } @@ -364,7 +364,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } } - private class EmulationState(private val mGamePath: String?) { + private class EmulationState(private val gamePath: String) { private var state: State private var surface: Surface? = null private var runWhenSurfaceIsValid = false @@ -391,8 +391,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { fun stop() { if (state != State.STOPPED) { Log.debug("[EmulationFragment] Stopping emulation.") - state = State.STOPPED NativeLibrary.stopEmulation() + state = State.STOPPED } else { Log.warning("[EmulationFragment] Stop called while already stopped.") } @@ -402,12 +402,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { @Synchronized fun pause() { if (state != State.PAUSED) { - state = State.PAUSED Log.debug("[EmulationFragment] Pausing emulation.") // Release the surface before pausing, since emulation has to be running for that. NativeLibrary.surfaceDestroyed() NativeLibrary.pauseEmulation() + + state = State.PAUSED } else { Log.warning("[EmulationFragment] Pause called while already paused.") } @@ -464,11 +465,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { when (state) { State.STOPPED -> { NativeLibrary.surfaceChanged(surface) - val mEmulationThread = Thread({ + val emulationThread = Thread({ Log.debug("[EmulationFragment] Starting emulation thread.") - NativeLibrary.run(mGamePath) + NativeLibrary.run(gamePath) }, "NativeEmulation") - mEmulationThread.start() + emulationThread.start() } State.PAUSED -> { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt index 961b5b822..b1329db74 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt @@ -119,7 +119,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider { } // Dismiss previous notifications (should not happen unless a crash occurred) - EmulationActivity.tryDismissRunningNotification(this) + EmulationActivity.stopForegroundService(this) setInsets() } @@ -221,7 +221,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider { } override fun onDestroy() { - EmulationActivity.tryDismissRunningNotification(this) + EmulationActivity.stopForegroundService(this) super.onDestroy() } 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 index 7e33ff044..626123966 100644 --- 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 @@ -18,13 +18,16 @@ import org.yuzu.yuzu_emu.activities.EmulationActivity */ class ForegroundService : Service() { companion object { - private const val EMULATION_RUNNING_NOTIFICATION = 0x1000 + 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, + this, + 0, Intent(this, EmulationActivity::class.java), PendingIntent.FLAG_IMMUTABLE ) @@ -50,6 +53,11 @@ class ForegroundService : Service() { } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { + if (intent.action == ACTION_STOP) { + NotificationManagerCompat.from(this).cancel(EMULATION_RUNNING_NOTIFICATION) + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelfResult(startId) + } return START_STICKY }