Added folding devices support

This commit is contained in:
skriand 2023-04-21 21:44:29 +03:00
parent d3d36c6fa0
commit 37d162a9aa
3 changed files with 54 additions and 5 deletions

View File

@ -172,6 +172,7 @@ dependencies {
implementation 'com.google.android.material:material:1.8.0' implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.documentfile:documentfile:1.0.1' implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.window:window:1.0.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1" implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
implementation 'androidx.fragment:fragment-ktx:1.5.5' implementation 'androidx.fragment:fragment-ktx:1.5.5'

View File

@ -13,12 +13,14 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.content.res.AssetManager import android.content.res.AssetManager
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.PointF import android.graphics.PointF
import android.graphics.drawable.Icon import android.graphics.drawable.Icon
import android.hardware.display.DisplayManager import android.hardware.display.DisplayManager
import android.os.* import android.os.*
import android.util.DisplayMetrics
import android.util.Log import android.util.Log
import android.util.Rational import android.util.Rational
import android.view.* import android.view.*
@ -30,6 +32,12 @@ import androidx.core.view.isGone
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.core.view.updateMargins import androidx.core.view.updateMargins
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoTracker
import androidx.window.layout.WindowLayoutInfo
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.BuildConfig import emu.skyline.BuildConfig
@ -47,12 +55,15 @@ import emu.skyline.settings.NativeSettings
import emu.skyline.utils.ByteBufferSerializable import emu.skyline.utils.ByteBufferSerializable
import emu.skyline.utils.GpuDriverHelper import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.serializable import emu.skyline.utils.serializable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.ByteOrder import java.nio.ByteOrder
import java.util.concurrent.FutureTask import java.util.concurrent.FutureTask
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
private const val ActionPause = "${BuildConfig.APPLICATION_ID}.ACTION_EMULATOR_PAUSE" private const val ActionPause = "${BuildConfig.APPLICATION_ID}.ACTION_EMULATOR_PAUSE"
private const val ActionMute = "${BuildConfig.APPLICATION_ID}.ACTION_EMULATOR_MUTE" private const val ActionMute = "${BuildConfig.APPLICATION_ID}.ACTION_EMULATOR_MUTE"
@ -263,7 +274,6 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
populateAppItem() populateAppItem()
emulationSettings = EmulationSettings.forEmulation(item.titleId ?: item.key()) emulationSettings = EmulationSettings.forEmulation(item.titleId ?: item.key())
requestedOrientation = emulationSettings.orientation
window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
inputHandler = InputHandler(inputManager, emulationSettings) inputHandler = InputHandler(inputManager, emulationSettings)
setContentView(binding.root) setContentView(binding.root)
@ -305,6 +315,16 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
} }
) )
lifecycleScope.launch(Dispatchers.Main) {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
WindowInfoTracker.getOrCreate(this@EmulationActivity)
.windowLayoutInfo(this@EmulationActivity)
.collect { newLayoutInfo ->
updateCurrentLayout(newLayoutInfo)
}
}
}
if (emulationSettings.perfStats) { if (emulationSettings.perfStats) {
if (emulationSettings.disableFrameThrottling) if (emulationSettings.disableFrameThrottling)
binding.perfStats.setTextColor(getColor(R.color.colorPerfStatsSecondary)) binding.perfStats.setTextColor(getColor(R.color.colorPerfStatsSecondary))
@ -480,6 +500,28 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
} }
} }
/**
* Updating the layout depending on type and state of device
*/
private fun updateCurrentLayout(newLayoutInfo: WindowLayoutInfo) {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
binding.onScreenGameView.minimumHeight = displayMetrics.heightPixels
requestedOrientation = emulationSettings.orientation
for (displayFeature in newLayoutInfo.displayFeatures) {
val foldFeature = displayFeature as? FoldingFeature
foldFeature?.let {
if (it.isSeparating) {
//Folding feature separates the display area into two distinct sections
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
if (foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL) {
binding.onScreenGameView.minimumHeight = displayFeature.bounds.top
}
}
}
}
}
/** /**
* Stop the currently executing ROM and replace it with the one specified in the new intent * Stop the currently executing ROM and replace it with the one specified in the new intent
*/ */

View File

@ -9,11 +9,17 @@
tools:context=".EmulationActivity" tools:context=".EmulationActivity"
tools:ignore="RtlHardcoded"> tools:ignore="RtlHardcoded">
<emu.skyline.views.FixedRatioSurfaceView <FrameLayout
android:id="@+id/game_view" android:id="@+id/on_screen_game_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content">
android:layout_gravity="center" />
<emu.skyline.views.FixedRatioSurfaceView
android:id="@+id/game_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
<emu.skyline.input.onscreen.OnScreenControllerView <emu.skyline.input.onscreen.OnScreenControllerView
android:id="@+id/on_screen_controller_view" android:id="@+id/on_screen_controller_view"