Skip to content

Commit

Permalink
Merge pull request #78306 from m4gr3d/cleanup_launch_flow_main
Browse files Browse the repository at this point in the history
Add parameters for the Godot Activity starting intent to allow restarting or force-quitting the engine
  • Loading branch information
akien-mga committed Jun 16, 2023
2 parents 7dddd8a + 0664f5e commit 6d1ca52
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion platform/android/java/app/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -154,7 +136,7 @@ open class GodotEditor : FullScreenGodotApp() {
private fun updateCommandLineParams(args: Array<String>?) {
// 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") {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
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;

Expand All @@ -59,6 +62,8 @@ public void onCreate(Bundle savedInstanceState) {
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.");
Expand Down Expand Up @@ -109,11 +114,35 @@ public final void onGodotRestartRequested(Godot instance) {
@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) {
Expand Down

0 comments on commit 6d1ca52

Please sign in to comment.