From 5cf0ba88e32cecefcaa73b9e326c948412da86e0 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Thu, 15 Jun 2023 21:11:40 -0700 Subject: [PATCH] Add parameters for the Godot Activity starting intent to allow restarting or force-quitting the engine Follow-up code cleanup for https://github.com/godotengine/godot/pull/78130 --- platform/android/java/app/AndroidManifest.xml | 2 +- .../org/godotengine/editor/GodotEditor.kt | 27 ++++------------- .../godotengine/godot/FullScreenGodotApp.java | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/platform/android/java/app/AndroidManifest.xml b/platform/android/java/app/AndroidManifest.xml index b4e3a41c029..d8dffd244be 100644 --- a/platform/android/java/app/AndroidManifest.xml +++ b/platform/android/java/app/AndroidManifest.xml @@ -66,7 +66,7 @@ android:name=".GodotApp" android:label="@string/godot_project_name_string" android:theme="@style/GodotAppSplashTheme" - android:launchMode="singleTask" + android:launchMode="singleInstance" android:excludeFromRecents="false" android:exported="true" android:screenOrientation="landscape" diff --git a/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.kt b/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.kt index ec1e0f61a85..329d6d1833b 100644 --- a/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.kt +++ b/platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.kt @@ -62,7 +62,6 @@ open class GodotEditor : FullScreenGodotApp() { private const val WAIT_FOR_DEBUGGER = false - private const val EXTRA_FORCE_QUIT = "force_quit_requested" private const val EXTRA_COMMAND_LINE_PARAMS = "command_line_params" private const val EDITOR_ID = 777 @@ -96,7 +95,9 @@ open class GodotEditor : FullScreenGodotApp() { // requested on demand based on use-cases. PermissionsUtil.requestManifestPermissions(this, setOf(Manifest.permission.RECORD_AUDIO)) - handleIntentParams(intent) + val params = intent.getStringArrayExtra(EXTRA_COMMAND_LINE_PARAMS) + Log.d(TAG, "Received parameters ${params.contentToString()}") + updateCommandLineParams(params) if (BuildConfig.BUILD_TYPE == "dev" && WAIT_FOR_DEBUGGER) { Debug.waitForDebugger() @@ -105,25 +106,6 @@ open class GodotEditor : FullScreenGodotApp() { super.onCreate(savedInstanceState) } - override fun onNewIntent(newIntent: Intent) { - intent = newIntent - handleIntentParams(newIntent) - super.onNewIntent(newIntent) - } - - private fun handleIntentParams(receivedIntent: Intent) { - val forceQuitRequested = receivedIntent.getBooleanExtra(EXTRA_FORCE_QUIT, false) - if (forceQuitRequested) { - Log.d(TAG, "Force quit requested, terminating..") - ProcessPhoenix.forceQuit(this) - return - } - - val params = receivedIntent.getStringArrayExtra(EXTRA_COMMAND_LINE_PARAMS) - Log.d(TAG, "Received parameters ${params.contentToString()}") - updateCommandLineParams(params) - } - override fun onGodotSetupCompleted() { super.onGodotSetupCompleted() val longPressEnabled = enableLongPressGestures() @@ -154,7 +136,7 @@ open class GodotEditor : FullScreenGodotApp() { private fun updateCommandLineParams(args: Array?) { // Update the list of command line params with the new args commandLineParams.clear() - if (args != null && args.isNotEmpty()) { + if (!args.isNullOrEmpty()) { commandLineParams.addAll(listOf(*args)) } if (BuildConfig.BUILD_TYPE == "dev") { @@ -201,6 +183,7 @@ open class GodotEditor : FullScreenGodotApp() { ProcessPhoenix.triggerRebirth(this, newInstance) } else { Log.d(TAG, "Starting $targetClass with parameters ${args.contentToString()}") + newInstance.putExtra(EXTRA_NEW_LAUNCH, true) startActivity(newInstance) } return instanceId diff --git a/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java b/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java index 65032d6a68f..714d218f346 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java +++ b/platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java @@ -51,6 +51,9 @@ import androidx.fragment.app.FragmentActivity; public abstract class FullScreenGodotApp extends FragmentActivity implements GodotHost { private static final String TAG = FullScreenGodotApp.class.getSimpleName(); + protected static final String EXTRA_FORCE_QUIT = "force_quit_requested"; + protected static final String EXTRA_NEW_LAUNCH = "new_launch_requested"; + @Nullable private Godot godotFragment; @@ -59,6 +62,8 @@ public abstract class FullScreenGodotApp extends FragmentActivity implements God super.onCreate(savedInstanceState); setContentView(R.layout.godot_app_layout); + handleStartIntent(getIntent(), true); + Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.godot_fragment_container); if (currentFragment instanceof Godot) { Log.v(TAG, "Reusing existing Godot fragment instance."); @@ -109,11 +114,35 @@ public abstract class FullScreenGodotApp extends FragmentActivity implements God @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); + setIntent(intent); + + handleStartIntent(intent, false); + if (godotFragment != null) { godotFragment.onNewIntent(intent); } } + private void handleStartIntent(Intent intent, boolean newLaunch) { + boolean forceQuitRequested = intent.getBooleanExtra(EXTRA_FORCE_QUIT, false); + if (forceQuitRequested) { + Log.d(TAG, "Force quit requested, terminating.."); + ProcessPhoenix.forceQuit(this); + return; + } + + if (!newLaunch) { + boolean newLaunchRequested = intent.getBooleanExtra(EXTRA_NEW_LAUNCH, false); + if (newLaunchRequested) { + Log.d(TAG, "New launch requested, restarting.."); + + Intent restartIntent = new Intent(intent).putExtra(EXTRA_NEW_LAUNCH, false); + ProcessPhoenix.triggerRebirth(this, restartIntent); + return; + } + } + } + @CallSuper @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {