Merge pull request #12364 from JosJuice/android-emulation-settings-reload

Android: Fix crash after process recreation
This commit is contained in:
Mai
2023-12-08 21:24:05 -05:00
committed by GitHub
2 changed files with 33 additions and 18 deletions

View File

@ -56,6 +56,7 @@ import org.dolphinemu.dolphinemu.overlay.InputOverlayPointer
import org.dolphinemu.dolphinemu.ui.main.MainPresenter import org.dolphinemu.dolphinemu.ui.main.MainPresenter
import org.dolphinemu.dolphinemu.ui.main.ThemeProvider import org.dolphinemu.dolphinemu.ui.main.ThemeProvider
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper import org.dolphinemu.dolphinemu.utils.FileBrowserHelper
import org.dolphinemu.dolphinemu.utils.ThemeHelper import org.dolphinemu.dolphinemu.utils.ThemeHelper
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -109,8 +110,6 @@ class EmulationActivity : AppCompatActivity(), ThemeProvider {
settings = Settings() settings = Settings()
settings.loadSettings() settings.loadSettings()
updateOrientation()
// Set these options now so that the SurfaceView the game renders into is the right size. // Set these options now so that the SurfaceView the game renders into is the right size.
enableFullscreenImmersive() enableFullscreenImmersive()
@ -203,22 +202,20 @@ class EmulationActivity : AppCompatActivity(), ThemeProvider {
super.onResume() super.onResume()
// Only android 9+ support this feature. // If the whole app process was recreated, directory initialization might not be done yet.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { // If that's the case, we can't read settings, so skip reading the settings for now and do
val attributes = window.attributes // it once onTitleChanged runs instead.
if (DirectoryInitialization.areDolphinDirectoriesReady()) {
attributes.layoutInDisplayCutoutMode = updateDisplaySettings();
if (BooleanSetting.MAIN_EXPAND_TO_CUTOUT_AREA.boolean) { } else {
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES // If the process was recreated and DolphinApplication.onStart didn't think it should
} else { // start directory initialization, we have to start it, otherwise emulation will never
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER // start. Technically it would be nicer to ask the user for write permission first,
} // but because this problem can only happen in very convoluted situations, this code is
// going to get essentially no testing, so let's go with the simplest possible fix.
window.attributes = attributes DirectoryInitialization.start(this);
} }
updateOrientation()
DolphinSensorEventListener.setDeviceRotation(windowManager.defaultDisplay.rotation) DolphinSensorEventListener.setDeviceRotation(windowManager.defaultDisplay.rotation)
} }
@ -238,6 +235,8 @@ class EmulationActivity : AppCompatActivity(), ThemeProvider {
title = NativeLibrary.GetCurrentTitleDescription() title = NativeLibrary.GetCurrentTitleDescription()
emulationFragment?.refreshInputOverlay() emulationFragment?.refreshInputOverlay()
updateDisplaySettings()
} catch (_: IllegalStateException) { } catch (_: IllegalStateException) {
// Most likely the core delivered an onTitleChanged while emulation was shutting down. // Most likely the core delivered an onTitleChanged while emulation was shutting down.
// Let's just ignore it, since we're about to shut down anyway. // Let's just ignore it, since we're about to shut down anyway.
@ -338,7 +337,20 @@ class EmulationActivity : AppCompatActivity(), ThemeProvider {
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
} }
private fun updateOrientation() { private fun updateDisplaySettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val attributes = window.attributes
attributes.layoutInDisplayCutoutMode =
if (BooleanSetting.MAIN_EXPAND_TO_CUTOUT_AREA.boolean) {
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
} else {
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
}
window.attributes = attributes
}
requestedOrientation = IntSetting.MAIN_EMULATION_ORIENTATION.int requestedOrientation = IntSetting.MAIN_EMULATION_ORIENTATION.int
} }

View File

@ -16,6 +16,7 @@ import org.dolphinemu.dolphinemu.databinding.FragmentEmulationBinding
import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting
import org.dolphinemu.dolphinemu.features.settings.model.Settings import org.dolphinemu.dolphinemu.features.settings.model.Settings
import org.dolphinemu.dolphinemu.overlay.InputOverlay import org.dolphinemu.dolphinemu.overlay.InputOverlay
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner
import org.dolphinemu.dolphinemu.utils.Log import org.dolphinemu.dolphinemu.utils.Log
import java.io.File import java.io.File
@ -100,7 +101,9 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
if (NativeLibrary.IsGameMetadataValid()) if (NativeLibrary.IsGameMetadataValid())
inputOverlay?.refreshControls() inputOverlay?.refreshControls()
run(emulationActivity!!.isActivityRecreated) AfterDirectoryInitializationRunner().runWithLifecycle(this) {
run(emulationActivity!!.isActivityRecreated)
}
} }
override fun onPause() { override fun onPause() {