From 1fdfedc43eb4cb031d529f3f3c20b0642247d5c2 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 26 Sep 2023 18:28:40 -0400 Subject: [PATCH 1/5] android: Close activity with toast if emulation has no game --- .../yuzu_emu/fragments/EmulationFragment.kt | 26 ++++++++++++++++--- .../app/src/main/res/values/strings.xml | 1 + 2 files changed, 23 insertions(+), 4 deletions(-) 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 750638bc9f..aa9cea4427 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 @@ -17,6 +17,7 @@ import android.os.Handler import android.os.Looper import android.view.* import android.widget.TextView +import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.PopupMenu import androidx.core.content.res.ResourcesCompat @@ -53,6 +54,7 @@ import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.EmulationViewModel import org.yuzu.yuzu_emu.overlay.InputOverlay import org.yuzu.yuzu_emu.utils.* +import java.lang.NullPointerException class EmulationFragment : Fragment(), SurfaceHolder.Callback { private lateinit var preferences: SharedPreferences @@ -104,10 +106,21 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { null } } - game = if (args.game != null) { - args.game!! - } else { - intentGame ?: error("[EmulationFragment] No bootable game present!") + + try { + game = if (args.game != null) { + args.game!! + } else { + intentGame!! + } + } catch (e: NullPointerException) { + Toast.makeText( + requireContext(), + R.string.no_game_present, + Toast.LENGTH_SHORT + ).show() + requireActivity().finish() + return } // So this fragment doesn't restart on configuration changes; i.e. rotation. @@ -131,6 +144,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { // This is using the correct scope, lint is just acting up @SuppressLint("UnsafeRepeatOnLifecycleDetector") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + if (requireActivity().isFinishing) { + return + } + binding.surfaceEmulation.holder.addCallback(this) binding.showFpsText.setTextColor(Color.YELLOW) binding.doneControlConfig.setOnClickListener { stopConfiguringControls() } diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index 574290479e..0347614010 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -294,6 +294,7 @@ Turning off this setting will significantly reduce emulation performance! For the best experience, it is recommended that you leave this setting enabled. Device RAM: %1$s\nRecommended: %2$s %1$s %2$s + No bootable game present! Japan From 6a425e95cb5feffb4938df7cf155397e8f4ac85e Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 26 Sep 2023 18:50:58 -0400 Subject: [PATCH 2/5] android: Don't wait for post to update input overlay visibility --- .../org/yuzu/yuzu_emu/fragments/EmulationFragment.kt | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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 aa9cea4427..39b2a961b4 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 @@ -310,19 +310,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { binding.drawerLayout.close() } if (EmulationMenuSettings.showOverlay) { - binding.surfaceInputOverlay.post { - binding.surfaceInputOverlay.visibility = View.INVISIBLE - } + binding.surfaceInputOverlay.visibility = View.INVISIBLE } } else { if (EmulationMenuSettings.showOverlay && emulationViewModel.emulationStarted.value) { - binding.surfaceInputOverlay.post { - binding.surfaceInputOverlay.visibility = View.VISIBLE - } + binding.surfaceInputOverlay.visibility = View.VISIBLE } else { - binding.surfaceInputOverlay.post { - binding.surfaceInputOverlay.visibility = View.INVISIBLE - } + binding.surfaceInputOverlay.visibility = View.INVISIBLE } if (!isInFoldableLayout) { if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { From ec388622ffd1d0083cb946998ceb79f25dc1b4b8 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 26 Sep 2023 18:54:34 -0400 Subject: [PATCH 3/5] android: Don't update views if binding is null in onConfigurationChanged --- .../java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt | 4 ++++ 1 file changed, 4 insertions(+) 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 39b2a961b4..e6ad2aa77a 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 @@ -304,6 +304,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) + if (_binding == null) { + return + } + updateScreenLayout() if (emulationActivity?.isInPictureInPictureMode == true) { if (binding.drawerLayout.isOpen) { From d70f18b87b03cf9ab908f2294a49a0f57684e230 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 26 Sep 2023 19:58:16 -0400 Subject: [PATCH 4/5] android: Prevent setup fragment crash in background Sometimes during onSaveInstanceState, the SetupFragment would crash the app in the background if we tried to store the state of a view. --- .../main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt index fbb2f6e18d..6bb6a44641 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt @@ -295,8 +295,10 @@ class SetupFragment : Fragment() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putBoolean(KEY_NEXT_VISIBILITY, binding.buttonNext.isVisible) - outState.putBoolean(KEY_BACK_VISIBILITY, binding.buttonBack.isVisible) + if (_binding != null) { + outState.putBoolean(KEY_NEXT_VISIBILITY, binding.buttonNext.isVisible) + outState.putBoolean(KEY_BACK_VISIBILITY, binding.buttonBack.isVisible) + } outState.putBooleanArray(KEY_HAS_BEEN_WARNED, hasBeenWarned) } From 22284fc5041b7ad307b50811cd11e6e4d5dbc926 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 26 Sep 2023 20:13:12 -0400 Subject: [PATCH 5/5] android: Prevent crash when trying to change pages in setup fragment Sometimes when we want to change the current setup page, the current view isn't available and we try to alter the current view. This adds a guard to prevent that issue. --- .../java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt index 6bb6a44641..c66bb635ab 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SetupFragment.kt @@ -355,11 +355,15 @@ class SetupFragment : Fragment() { } fun pageForward() { - binding.viewPager2.currentItem = binding.viewPager2.currentItem + 1 + if (_binding != null) { + binding.viewPager2.currentItem += 1 + } } fun pageBackward() { - binding.viewPager2.currentItem = binding.viewPager2.currentItem - 1 + if (_binding != null) { + binding.viewPager2.currentItem -= 1 + } } fun setPageWarned(page: Int) {