From 724823c193e9c3aaa630547fc9f5ae562ea33077 Mon Sep 17 00:00:00 2001 From: Abandoned Cart Date: Mon, 12 Jun 2023 22:17:15 -0400 Subject: [PATCH] android: Remove PiP reliance on fragment --- .../java/org/yuzu/yuzu_emu/NativeLibrary.kt | 5 ++ .../yuzu_emu/activities/EmulationActivity.kt | 9 +-- .../yuzu_emu/fragments/EmulationFragment.kt | 67 ++++++++----------- src/android/app/src/main/jni/native.cpp | 13 ++++ .../app/src/main/res/values/integers.xml | 38 +++++------ 5 files changed, 69 insertions(+), 63 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt index 4be9ade142..22f0a26468 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt @@ -282,6 +282,11 @@ object NativeLibrary { */ external fun isRunning(): Boolean + /** + * Returns true if emulation is paused. + */ + external fun isPaused(): Boolean + /** * Returns the performance stats for the current game */ 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 e2eab31055..36cc27a050 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 @@ -271,8 +271,7 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { val pictureInPictureActions : MutableList = mutableListOf() val pendingFlags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE - val isEmulationPaused = emulationFragment?.isEmulationStatePaused() ?: false - if (isEmulationPaused) { + if (NativeLibrary.isPaused()) { val playIcon = Icon.createWithResource(this@EmulationActivity, R.drawable.ic_pip_play) val playPendingIntent = PendingIntent.getBroadcast( this@EmulationActivity, R.drawable.ic_pip_play, Intent(actionPlay), pendingFlags @@ -303,9 +302,9 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { private var pictureInPictureReceiver = object : BroadcastReceiver() { override fun onReceive(context : Context?, intent : Intent) { if (intent.action == actionPlay) { - emulationFragment?.onPictureInPicturePlay() + if (NativeLibrary.isPaused()) NativeLibrary.unPauseEmulation() } else if (intent.action == actionPause) { - emulationFragment?.onPictureInPicturePause() + if (!NativeLibrary.isPaused()) NativeLibrary.pauseEmulation() } buildPictureInPictureParams() } @@ -320,12 +319,10 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { }.also { registerReceiver(pictureInPictureReceiver, it) } - emulationFragment?.onPictureInPictureEnter() } else { try { unregisterReceiver(pictureInPictureReceiver) } catch (ignored : Exception) { } - emulationFragment?.onPictureInPictureLeave() } } 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 5e37db46f1..2f1a16f00c 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 @@ -77,6 +77,14 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { emulationActivity = context NativeLibrary.setEmulationActivity(context) + lifecycleScope.launch(Dispatchers.Main) { + lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { + WindowInfoTracker.getOrCreate(context) + .windowLayoutInfo(context) + .collect { updateFoldableLayout(context, it) } + } + } + onReturnFromSettings = context.activityResultRegistry.register( "SettingsResult", ActivityResultContracts.StartActivityForResult() ) { @@ -198,14 +206,28 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - if (!isInFoldableLayout) { - if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { - binding.surfaceInputOverlay.setOrientation(InputOverlay.PORTRAIT) - } else { - binding.surfaceInputOverlay.setOrientation(InputOverlay.LANDSCAPE) + if (emulationActivity?.isInPictureInPictureMode == true) { + if (binding.drawerLayout.isOpen) { + binding.drawerLayout.close() + } + if (EmulationMenuSettings.showOverlay) { + binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.isVisible = false } + } + } else { + if (EmulationMenuSettings.showOverlay) { + binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.isVisible = true } + } + if (!isInFoldableLayout) { + if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { + binding.surfaceInputOverlay.setOrientation(InputOverlay.PORTRAIT) + } else { + binding.surfaceInputOverlay.setOrientation(InputOverlay.LANDSCAPE) + } + } + if (!binding.surfaceInputOverlay.isInEditMode) { + refreshInputOverlay() } } - if (!binding.surfaceInputOverlay.isInEditMode) refreshInputOverlay() } override fun onResume() { @@ -247,37 +269,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { super.onDetach() } - fun isEmulationStatePaused() : Boolean { - return this::emulationState.isInitialized && emulationState.isPaused - } - - fun onPictureInPictureEnter() { - if (binding.drawerLayout.isOpen) { - binding.drawerLayout.close() - } - if (EmulationMenuSettings.showOverlay) { - binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.isVisible = false } - } - } - - fun onPictureInPicturePause() { - if (!emulationState.isPaused) { - emulationState.pause() - } - } - - fun onPictureInPicturePlay() { - if (emulationState.isPaused) { - emulationState.run(false) - } - } - - fun onPictureInPictureLeave() { - if (EmulationMenuSettings.showOverlay) { - binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.isVisible = true } - } - } - private fun refreshInputOverlay() { binding.surfaceInputOverlay.refreshControls() } @@ -338,7 +329,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { private val Number.toPx get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics).toInt() - fun updateFoldableLayout(emulationActivity: EmulationActivity, newLayoutInfo: WindowLayoutInfo) { + private fun updateFoldableLayout(emulationActivity: EmulationActivity, newLayoutInfo: WindowLayoutInfo) { val isFolding = (newLayoutInfo.displayFeatures.find { it is FoldingFeature } as? FoldingFeature)?.let { if (it.isSeparating) { emulationActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED diff --git a/src/android/app/src/main/jni/native.cpp b/src/android/app/src/main/jni/native.cpp index 4091c23d18..f9617202b6 100644 --- a/src/android/app/src/main/jni/native.cpp +++ b/src/android/app/src/main/jni/native.cpp @@ -202,6 +202,11 @@ public: return m_is_running; } + bool IsPaused() const { + std::scoped_lock lock(m_mutex); + return m_is_running && m_is_paused; + } + const Core::PerfStatsResults& PerfStats() const { std::scoped_lock m_perf_stats_lock(m_perf_stats_mutex); return m_perf_stats; @@ -287,11 +292,13 @@ public: void PauseEmulation() { std::scoped_lock lock(m_mutex); m_system.Pause(); + m_is_paused = true; } void UnPauseEmulation() { std::scoped_lock lock(m_mutex); m_system.Run(); + m_is_paused = false; } void HaltEmulation() { @@ -473,6 +480,7 @@ private: std::shared_ptr m_vfs; Core::SystemResultStatus m_load_result{Core::SystemResultStatus::ErrorNotInitialized}; bool m_is_running{}; + bool m_is_paused{}; SoftwareKeyboard::AndroidKeyboard* m_software_keyboard{}; std::unique_ptr m_profile_manager; @@ -583,6 +591,11 @@ jboolean Java_org_yuzu_yuzu_1emu_NativeLibrary_isRunning([[maybe_unused]] JNIEnv return static_cast(EmulationSession::GetInstance().IsRunning()); } +jboolean Java_org_yuzu_yuzu_1emu_NativeLibrary_isPaused([[maybe_unused]] JNIEnv* env, + [[maybe_unused]] jclass clazz) { + return static_cast(EmulationSession::GetInstance().IsPaused()); +} + jboolean Java_org_yuzu_yuzu_1emu_NativeLibrary_isHandheldOnly([[maybe_unused]] JNIEnv* env, [[maybe_unused]] jclass clazz) { return EmulationSession::GetInstance().IsHandheldOnly(); diff --git a/src/android/app/src/main/res/values/integers.xml b/src/android/app/src/main/res/values/integers.xml index 04280fae28..2dbe881e35 100644 --- a/src/android/app/src/main/res/values/integers.xml +++ b/src/android/app/src/main/res/values/integers.xml @@ -59,43 +59,43 @@ 950 560 950 - 600 + 660 950 - 400 + 320 950 240 820 840 - 420 + 340 740 - 460 + 380 740 - 380 + 300 640 - 420 + 340 180 - 240 + 200 820 - 240 + 200 140 - 100 + 80 860 - 100 + 80 140 - 40 + 20 860 - 40 + 20 440 - 550 + 420 560 - 550 - 600 - 550 - 400 - 550 + 420 + 680 + 420 + 320 + 420 240 - 420 + 340