From b394a6b93740bfe7c32958cde14d7241baa5e81d Mon Sep 17 00:00:00 2001 From: Abandoned Cart Date: Sat, 3 Jun 2023 15:16:25 -0400 Subject: [PATCH 1/2] android: Replace deprecated and Java code --- .../yuzu_emu/activities/EmulationActivity.kt | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 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 81474b8246..1382ae29f8 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 @@ -5,7 +5,6 @@ package org.yuzu.yuzu_emu.activities import android.app.Activity import android.content.Context -import android.content.DialogInterface import android.content.Intent import android.content.res.Configuration import android.graphics.Rect @@ -13,22 +12,22 @@ import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager +import android.hardware.display.DisplayManager import android.os.Bundle +import android.view.Display import android.view.InputDevice import android.view.KeyEvent import android.view.MotionEvent import android.view.Surface import android.view.View -import android.view.WindowManager import android.view.inputmethod.InputMethodManager import androidx.appcompat.app.AppCompatActivity -import androidx.preference.PreferenceManager -import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.google.android.material.slider.Slider.OnChangeListener +import androidx.core.content.getSystemService +import androidx.core.view.WindowCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.WindowInsetsControllerCompat import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.databinding.DialogSliderBinding -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 @@ -44,7 +43,6 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { private var controllerMappingHelper: ControllerMappingHelper? = null var isActivityRecreated = false - private var menuVisible = false private var emulationFragment: EmulationFragment? = null private lateinit var nfcReader: NfcReader private lateinit var inputHandler: InputHandler @@ -241,20 +239,20 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { override fun onAccuracyChanged(sensor: Sensor, i: Int) {} private fun getAdjustedRotation():Int { - val rotation = windowManager.defaultDisplay.rotation; + val rotation = getSystemService()!!.getDisplay(Display.DEFAULT_DISPLAY).rotation val config: Configuration = resources.configuration if ((config.screenLayout and Configuration.SCREENLAYOUT_LONG_YES) != 0 || (config.screenLayout and Configuration.SCREENLAYOUT_LONG_NO) == 0) { - return rotation; + return rotation } when (rotation) { - Surface.ROTATION_0 -> return Surface.ROTATION_90; - Surface.ROTATION_90 -> return Surface.ROTATION_0; - Surface.ROTATION_180 -> return Surface.ROTATION_270; - Surface.ROTATION_270 -> return Surface.ROTATION_180; + Surface.ROTATION_0 -> return Surface.ROTATION_90 + Surface.ROTATION_90 -> return Surface.ROTATION_0 + Surface.ROTATION_180 -> return Surface.ROTATION_270 + Surface.ROTATION_270 -> return Surface.ROTATION_180 } - return rotation; + return rotation } private fun restoreState(savedInstanceState: Bundle) { @@ -262,18 +260,13 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { } private fun enableFullscreenImmersive() { - window.attributes.layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES + WindowCompat.setDecorFitsSystemWindows(window, false) - window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) - - // It would be nice to use IMMERSIVE_STICKY, but that doesn't show the toolbar. - window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or - View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or - View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or - View.SYSTEM_UI_FLAG_FULLSCREEN or - View.SYSTEM_UI_FLAG_IMMERSIVE + WindowInsetsControllerCompat(window, window.decorView).let { controller -> + controller.hide(WindowInsetsCompat.Type.systemBars()) + controller.systemBarsBehavior = + WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + } } private fun startMotionSensorListener() { From cfa8bec5b9e65f147c472d09ae6ea2bfab243259 Mon Sep 17 00:00:00 2001 From: Abandoned Cart Date: Sat, 3 Jun 2023 22:57:28 -0400 Subject: [PATCH 2/2] android: Add support for split foldable view --- .../yuzu_emu/activities/EmulationActivity.kt | 14 ++++++++ .../yuzu_emu/fragments/EmulationFragment.kt | 33 +++++++++++++++++++ .../main/res/layout/fragment_emulation.xml | 9 ++++- 3 files changed, 55 insertions(+), 1 deletion(-) 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 1382ae29f8..901a3978d8 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 @@ -26,6 +26,12 @@ import androidx.core.content.getSystemService import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsControllerCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import androidx.window.layout.WindowInfoTracker +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.fragments.EmulationFragment @@ -96,6 +102,14 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { inputHandler = InputHandler() inputHandler.initialize() + lifecycleScope.launch(Dispatchers.Main) { + lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { + WindowInfoTracker.getOrCreate(this@EmulationActivity) + .windowLayoutInfo(this@EmulationActivity) + .collect { emulationFragment?.updateCurrentLayout(this@EmulationActivity, it) } + } + } + // Start a foreground service to prevent the app from getting killed in the background val startIntent = Intent(this, ForegroundService::class.java) startForegroundService(startIntent) 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 e00f02f86d..1a9843fcc0 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 @@ -8,10 +8,13 @@ import android.app.AlertDialog import android.content.Context import android.content.DialogInterface import android.content.SharedPreferences +import android.content.pm.ActivityInfo +import android.content.res.Resources import android.graphics.Color import android.os.Bundle import android.os.Handler import android.os.Looper +import android.util.TypedValue import android.view.* import android.widget.TextView import androidx.activity.OnBackPressedCallback @@ -20,8 +23,11 @@ import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.preference.PreferenceManager +import androidx.window.layout.FoldingFeature +import androidx.window.layout.WindowLayoutInfo import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.slider.Slider import org.yuzu.yuzu_emu.NativeLibrary @@ -211,6 +217,33 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } } + private val Number.toPx get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics).toInt() + + fun updateCurrentLayout(emulationActivity: EmulationActivity, newLayoutInfo: WindowLayoutInfo) { + val isFolding = (newLayoutInfo.displayFeatures.find { it is FoldingFeature } as? FoldingFeature)?.let { + if (it.isSeparating) { + emulationActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + if (it.orientation == FoldingFeature.Orientation.HORIZONTAL) { + binding.surfaceEmulation.layoutParams.height = it.bounds.top + binding.inGameMenu.layoutParams.height = it.bounds.bottom + binding.overlayContainer.layoutParams.height = it.bounds.bottom - 48.toPx + binding.overlayContainer.updatePadding(0, 0, 0, 24.toPx) + } + } + it.isSeparating + } ?: false + if (!isFolding) { + binding.surfaceEmulation.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT + binding.inGameMenu.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT + binding.overlayContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT + binding.overlayContainer.updatePadding(0, 0, 0, 0) + emulationActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE + } + binding.surfaceInputOverlay.requestLayout() + binding.inGameMenu.requestLayout() + binding.overlayContainer.requestLayout() + } + override fun surfaceCreated(holder: SurfaceHolder) { // We purposely don't do anything here. // All work is done in surfaceChanged, which we are guaranteed to get even for surface creation. diff --git a/src/android/app/src/main/res/layout/fragment_emulation.xml b/src/android/app/src/main/res/layout/fragment_emulation.xml index 74ec512afc..940dbd4bf7 100644 --- a/src/android/app/src/main/res/layout/fragment_emulation.xml +++ b/src/android/app/src/main/res/layout/fragment_emulation.xml @@ -20,6 +20,12 @@ android:focusable="false" android:focusableInTouchMode="false" /> + + + @@ -55,7 +62,7 @@ android:id="@+id/in_game_menu" android:layout_width="wrap_content" android:layout_height="match_parent" - android:layout_gravity="start" + android:layout_gravity="start|bottom" app:headerLayout="@layout/header_in_game" app:menu="@menu/menu_in_game" />