Godot Engine – Multi-platform 2D and 3D game engine
[Android XR] Fix issue with transitioning from Full space (FSM) to Home space (HSM) when running the XR editor
Fredia Huya-Kouadiofe05ac5eebfc
diff --git a/platform/android/java/editor/src/android/java/org/godotengine/editor/GodotEditor.kt b/platform/android/java/editor/src/android/java/org/godotengine/editor/GodotEditor.ktindex ebbf5049627..d6e723fc06e 100644--- a/platform/android/java/editor/src/android/java/org/godotengine/editor/GodotEditor.kt+++ b/platform/android/java/editor/src/android/java/org/godotengine/editor/GodotEditor.kt@@ -67,23 +67,20 @@ open class GodotEditor : BaseGodotEditor() {}private val handler = Handler()+ private val windowIdToPanelEntity = HashMap<Int, ActivityPanelEntity>()- private var pendingFullSpaceRequest = false- private val pendingSpatialContainerLaunches = LinkedList<Pair<EditorWindowInfo, Intent>>()-+ private val pendingNewInstanceLaunches = LinkedList<Triple<EditorWindowInfo, Intent, ActivityOptions?>>()private val spatialCapabilitiesChangedListener = Consumer<Set<SpatialCapability>> {- if (canEmbedActivity()) {- pendingFullSpaceRequest = false-+ if (!isInFullSpace()) {+ // TODO: There's an OS bug that prevents properly resuming home space when launching new Activity+ // instances while in full space. As a workaround, we launch those instances in home space.+ // This logic should be removed when the OS bug is fixed.// Launch any queued spatial containers.- Log.v(TAG, "Launching pending spatial container launches..")- for ((first, second) in pendingSpatialContainerLaunches) {- startSpatialContainerActivity(first, second)+ Log.v(TAG, "Launching pending new instances..")+ for ((first, second, third) in pendingNewInstanceLaunches) {+ dispatchNewInstance(first, second, third)}- pendingSpatialContainerLaunches.clear()- } else {- // Already in HSM, so remove any request to transition.- handler.removeCallbacks(requestHomeSpaceRunnable)+ pendingNewInstanceLaunches.clear()}}@@ -102,44 +99,24 @@ open class GodotEditor : BaseGodotEditor() {}private val requestHomeSpaceRunnable: Runnable = Runnable {- val scene = session?.scene ?: return@Runnable-handler.removeCallbacks(requestHomeSpaceRunnable)- if (canEmbedActivity()) {- scene.requestHomeSpace()+ if (isInFullSpace()) {+ Log.v(TAG, "Requesting home space")+ session?.scene?.requestHomeSpace()}}- private fun canEmbedActivity(): Boolean {- val scene = session?.scene ?: return false- return scene.spatialCapabilities.contains(SpatialCapability.EMBED_ACTIVITY)+ private val requestFullSpaceRunnable: Runnable = Runnable {+ handler.removeCallbacks(requestFullSpaceRunnable)+ if (!isInFullSpace()) {+ Log.v(TAG, "Requesting full space")+ session?.scene?.requestFullSpace()+ }}- private fun launchSpatialContainerUsingSceneCore(editorWindowInfo: EditorWindowInfo, newInstance: Intent, activityOptions: ActivityOptions?): Boolean {- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {- try {- if (session != null) {- val scene = session!!.scene-- handler.removeCallbacks(requestHomeSpaceRunnable)- if (canEmbedActivity()) {- Log.v(TAG, "Launching spatial container in FSM")- startSpatialContainerActivity(editorWindowInfo, newInstance)- } else {- pendingSpatialContainerLaunches.push(Pair(editorWindowInfo, newInstance))- if (!pendingFullSpaceRequest) {- Log.v(TAG, "Requesting full space")- scene.requestFullSpace()- pendingFullSpaceRequest = true- }- }- return true- }- } catch (e: Exception) {- Log.e(TAG, "Unable to launch spatial container", e)- }- }- return false+ private fun isInFullSpace(): Boolean {+ val scene = session?.scene ?: return false+ return scene.spatialCapabilities.contains(SpatialCapability.EMBED_ACTIVITY)}private fun resizePanelEntity(resizeEvent: ResizeEvent) {@@ -149,9 +126,9 @@ open class GodotEditor : BaseGodotEditor() {}}- private fun startSpatialContainerActivity(editorWindowInfo: EditorWindowInfo, newInstance: Intent) {+ private fun startSpatialContainerActivity(editorWindowInfo: EditorWindowInfo, newInstance: Intent): Boolean {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {- val scene = session!!.scene+ val scene = session?.scene ?: return falseval mainPanelWidth = scene.mainPanelEntity.size.widthval mainPanelHeight = scene.mainPanelEntity.size.height@@ -174,23 +151,23 @@ open class GodotEditor : BaseGodotEditor() {scene.activitySpace)+ windowIdToPanelEntity[editorWindowInfo.windowId] = activityPanel+// We remove the 'NEW_TASK' flag as launching in a new task prevents embedding.newInstance.removeFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+ Log.v(TAG, "Launching spatial container: $newInstance")activityPanel.startActivity(newInstance)+ return true}+ return false}- private fun exitFullSpaceUsingSceneCore() {- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {- handler.postDelayed(requestHomeSpaceRunnable, 250L)+ private fun exitFullSpace() {+ handler.postDelayed(requestHomeSpaceRunnable, 50L)+ }- session?.scene?.activitySpace?.children?.forEach { child ->- if (child is ActivityPanelEntity) {- Log.d(TAG, "Removing entity $child from the scene graph.")- child.parent = null- }- }- }+ private fun enterFullSpace() {+ handler.postDelayed(requestFullSpaceRunnable, 50L)}override fun getExcludedPermissions(): MutableSet<String> {@@ -219,21 +196,39 @@ open class GodotEditor : BaseGodotEditor() {newInstance: Intent,activityOptions: ActivityOptions?) {- if (isSpatialContainerRunGameInfo(editorWindowInfo) && launchSpatialContainerUsingSceneCore(editorWindowInfo, newInstance, activityOptions)) {- return+ // TODO: There's an OS bug that prevents properly resuming home space when launching new Activity+ // instances while in full space. As a workaround, we launch those instances in home space.+ // This logic should be removed when the OS bug is fixed.+ if (!isInFullSpace()) {+ if (isSpatialContainerRunGameInfo(editorWindowInfo) && startSpatialContainerActivity(editorWindowInfo, newInstance)) {+ return+ }+ super.dispatchNewInstance(editorWindowInfo, newInstance, activityOptions)+ } else {+ pendingNewInstanceLaunches.push(Triple(editorWindowInfo, newInstance, activityOptions))+ exitFullSpace()+ }+ }++ override fun onEditorConnected(editorId: Int) {+ super.onEditorConnected(editorId)+ if (isSpatialContainerRunGameInfoWindowId(editorId)) {+ enterFullSpace()}- super.dispatchNewInstance(editorWindowInfo, newInstance, activityOptions)}override fun onEditorDisconnected(editorId: Int) {if (isSpatialContainerRunGameInfoWindowId(editorId)) {- val spatialContainersRunning =- (SPATIAL_CONTAINER_RUN_GAME_INFO_0.windowId != editorId && editorMessageDispatcher.hasEditorConnection(SPATIAL_CONTAINER_RUN_GAME_INFO_0)) ||- (SPATIAL_CONTAINER_RUN_GAME_INFO_1.windowId != editorId && editorMessageDispatcher.hasEditorConnection(SPATIAL_CONTAINER_RUN_GAME_INFO_1))+ // Unparent the ActivityPanelEntity.+ windowIdToPanelEntity.remove(editorId)?.apply {+ Log.d(TAG, "Removing entity $this from the scene graph.")+ parent = null+ }++ val spatialContainersRunning = windowIdToPanelEntity.isNotEmpty()if (!spatialContainersRunning) {// No spatial container is running, time to exit FSM.- Log.v(TAG, "Exiting FSM.")- exitFullSpaceUsingSceneCore()+ exitFullSpace()}}super.onEditorDisconnected(editorId)