Revert "Fix android termination bug (#1354)"

This reverts commit 70f9379eefc84b7651e3aababcce33987e073ed0.
This commit is contained in:
OpenSauce04 2025-09-05 20:22:24 +01:00
parent 1830290e55
commit e0078b2407
2 changed files with 21 additions and 32 deletions

View File

@ -85,7 +85,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
private val preferences: SharedPreferences private val preferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext) get() = PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext)
private var emulationState: EmulationState? = null private lateinit var emulationState: EmulationState
private var perfStatsUpdater: Runnable? = null private var perfStatsUpdater: Runnable? = null
private lateinit var emulationActivity: EmulationActivity private lateinit var emulationActivity: EmulationActivity
@ -106,10 +106,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
if (context is EmulationActivity) { if (context is EmulationActivity) {
emulationActivity = context emulationActivity = context
NativeLibrary.setEmulationActivity(context) NativeLibrary.setEmulationActivity(context)
EmulationLifecycleUtil.addPauseResumeHook(hook = { togglePause() })
if (emulationState != null) {
EmulationLifecycleUtil.addShutdownHook(hook = { emulationState!!.stop() })
}
} else { } else {
throw IllegalStateException("EmulationFragment must have EmulationActivity parent") throw IllegalStateException("EmulationFragment must have EmulationActivity parent")
} }
@ -160,7 +156,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
emulationState = EmulationState(game.path) emulationState = EmulationState(game.path)
emulationActivity = requireActivity() as EmulationActivity emulationActivity = requireActivity() as EmulationActivity
screenAdjustmentUtil = ScreenAdjustmentUtil(requireContext(), requireActivity().windowManager, settingsViewModel.settings) screenAdjustmentUtil = ScreenAdjustmentUtil(requireContext(), requireActivity().windowManager, settingsViewModel.settings)
EmulationLifecycleUtil.addShutdownHook(hook = { emulationState!!.stop() }) EmulationLifecycleUtil.addShutdownHook(hook = { emulationState.stop() })
EmulationLifecycleUtil.addPauseResumeHook(hook = { togglePause() })
} }
override fun onCreateView( override fun onCreateView(
@ -259,8 +256,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
binding.inGameMenu.setNavigationItemSelectedListener { binding.inGameMenu.setNavigationItemSelectedListener {
when (it.itemId) { when (it.itemId) {
R.id.menu_emulation_pause -> { R.id.menu_emulation_pause -> {
if (emulationState!!.isPaused) { if (emulationState.isPaused) {
emulationState!!.unpause() emulationState.unpause()
it.title = resources.getString(R.string.pause_emulation) it.title = resources.getString(R.string.pause_emulation)
it.icon = ResourcesCompat.getDrawable( it.icon = ResourcesCompat.getDrawable(
resources, resources,
@ -268,7 +265,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
requireContext().theme requireContext().theme
) )
} else { } else {
emulationState!!.pause() emulationState.pause()
it.title = resources.getString(R.string.resume_emulation) it.title = resources.getString(R.string.resume_emulation)
it.icon = ResourcesCompat.getDrawable( it.icon = ResourcesCompat.getDrawable(
resources, resources,
@ -358,7 +355,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
} }
R.id.menu_exit -> { R.id.menu_exit -> {
emulationState!!.pause() emulationState.pause()
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.emulation_close_game) .setTitle(R.string.emulation_close_game)
.setMessage(R.string.emulation_close_game_message) .setMessage(R.string.emulation_close_game_message)
@ -366,9 +363,9 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
EmulationLifecycleUtil.closeGame() EmulationLifecycleUtil.closeGame()
} }
.setNegativeButton(android.R.string.cancel) { _: DialogInterface?, _: Int -> .setNegativeButton(android.R.string.cancel) { _: DialogInterface?, _: Int ->
emulationState!!.unpause() emulationState.unpause()
} }
.setOnCancelListener { emulationState!!.unpause() } .setOnCancelListener { emulationState.unpause() }
.show() .show()
true true
} }
@ -462,10 +459,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
} }
private fun togglePause() { private fun togglePause() {
if (emulationState!!.isPaused) { if (emulationState.isPaused) {
emulationState!!.unpause() emulationState.unpause()
} else { } else {
emulationState!!.pause() emulationState.pause()
} }
} }
@ -473,7 +470,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
super.onResume() super.onResume()
Choreographer.getInstance().postFrameCallback(this) Choreographer.getInstance().postFrameCallback(this)
if (NativeLibrary.isRunning()) { if (NativeLibrary.isRunning()) {
emulationState!!.pause() emulationState.pause()
// If the overlay is enabled, we need to update the position if changed // If the overlay is enabled, we need to update the position if changed
val position = IntSetting.PERFORMANCE_OVERLAY_POSITION.int val position = IntSetting.PERFORMANCE_OVERLAY_POSITION.int
@ -491,7 +488,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
} }
if (DirectoryInitialization.areCitraDirectoriesReady()) { if (DirectoryInitialization.areCitraDirectoriesReady()) {
emulationState!!.run(emulationActivity.isActivityRecreated) emulationState.run(emulationActivity.isActivityRecreated)
} else { } else {
setupCitraDirectoriesThenStartEmulation() setupCitraDirectoriesThenStartEmulation()
} }
@ -499,7 +496,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
override fun onPause() { override fun onPause() {
if (NativeLibrary.isRunning()) { if (NativeLibrary.isRunning()) {
emulationState!!.pause() emulationState.pause()
} }
Choreographer.getInstance().removeFrameCallback(this) Choreographer.getInstance().removeFrameCallback(this)
super.onPause() super.onPause()
@ -515,7 +512,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
if (directoryInitializationState === if (directoryInitializationState ===
DirectoryInitializationState.CITRA_DIRECTORIES_INITIALIZED DirectoryInitializationState.CITRA_DIRECTORIES_INITIALIZED
) { ) {
emulationState!!.run(emulationActivity.isActivityRecreated) emulationState.run(emulationActivity.isActivityRecreated)
} else if (directoryInitializationState === } else if (directoryInitializationState ===
DirectoryInitializationState.EXTERNAL_STORAGE_PERMISSION_NEEDED DirectoryInitializationState.EXTERNAL_STORAGE_PERMISSION_NEEDED
) { ) {
@ -1351,11 +1348,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height) Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height)
emulationState!!.newSurface(holder.surface) emulationState.newSurface(holder.surface)
} }
override fun surfaceDestroyed(holder: SurfaceHolder) { override fun surfaceDestroyed(holder: SurfaceHolder) {
emulationState!!.clearSurface() emulationState.clearSurface()
} }
override fun doFrame(frameTimeNanos: Long) { override fun doFrame(frameTimeNanos: Long) {

View File

@ -1,4 +1,4 @@
// Copyright Citra Emulator Project / Azahar Emulator Project // Copyright 2023 Citra Emulator Project
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
@ -18,19 +18,11 @@ object EmulationLifecycleUtil {
} }
fun addShutdownHook(hook: Runnable) { fun addShutdownHook(hook: Runnable) {
if (shutdownHooks.contains(hook)) { shutdownHooks.add(hook)
Log.warning("[EmulationLifecycleUtil] Tried to add shutdown hook that already existed. Skipping.")
} else {
shutdownHooks.add(hook)
}
} }
fun addPauseResumeHook(hook: Runnable) { fun addPauseResumeHook(hook: Runnable) {
if (pauseResumeHooks.contains(hook)) { pauseResumeHooks.add(hook)
Log.warning("[EmulationLifecycleUtil] Tried to add pause resume hook that already existed. Skipping.")
} else {
pauseResumeHooks.add(hook)
}
} }
fun clear() { fun clear() {