Skip to content

Commit

Permalink
Fix issue causing the last edited project to open while switching to …
Browse files Browse the repository at this point in the history
…another one.

Fixes #76562
  • Loading branch information
m4gr3d committed Jun 12, 2023
1 parent dcd31a2 commit a951a14
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
6 changes: 3 additions & 3 deletions platform/android/java/editor/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<activity
android:name=".GodotProjectManager"
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode"
android:launchMode="singleTask"
android:launchMode="singleInstance"
android:screenOrientation="userLandscape"
android:exported="true"
android:process=":GodotProjectManager">
Expand All @@ -53,7 +53,7 @@
android:name=".GodotEditor"
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode"
android:process=":GodotEditor"
android:launchMode="singleTask"
android:launchMode="singleInstance"
android:screenOrientation="userLandscape"
android:exported="false">
<layout android:defaultHeight="@dimen/editor_default_window_height"
Expand All @@ -65,7 +65,7 @@
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode"
android:label="@string/godot_project_name_string"
android:process=":GodotGame"
android:launchMode="singleTask"
android:launchMode="singleInstance"
android:exported="false"
android:screenOrientation="userLandscape">
<layout android:defaultHeight="@dimen/editor_default_window_height"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ open class GodotEditor : FullScreenGodotApp() {

private const val WAIT_FOR_DEBUGGER = false

private const val COMMAND_LINE_PARAMS = "command_line_params"
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
private const val EDITOR_ARG = "--editor"
Expand Down Expand Up @@ -95,8 +96,7 @@ open class GodotEditor : FullScreenGodotApp() {
// requested on demand based on use-cases.
PermissionsUtil.requestManifestPermissions(this, setOf(Manifest.permission.RECORD_AUDIO))

val params = intent.getStringArrayExtra(COMMAND_LINE_PARAMS)
updateCommandLineParams(params)
handleIntentParams(intent)

if (BuildConfig.BUILD_TYPE == "dev" && WAIT_FOR_DEBUGGER) {
Debug.waitForDebugger()
Expand All @@ -105,6 +105,25 @@ 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 @@ -173,33 +192,48 @@ open class GodotEditor : FullScreenGodotApp() {
// Launch a new activity
val newInstance = Intent(this, targetClass)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(COMMAND_LINE_PARAMS, args)
.putExtra(EXTRA_COMMAND_LINE_PARAMS, args)
if (launchAdjacent) {
newInstance.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)
}
if (targetClass == javaClass) {
Log.d(TAG, "Restarting $targetClass")
Log.d(TAG, "Restarting $targetClass with parameters ${args.contentToString()}")
ProcessPhoenix.triggerRebirth(this, newInstance)
} else {
Log.d(TAG, "Starting $targetClass")
Log.d(TAG, "Starting $targetClass with parameters ${args.contentToString()}")
startActivity(newInstance)
}
return instanceId
}

override fun onGodotForceQuit(godotInstanceId: Int): Boolean {
val processNameSuffix = when (godotInstanceId) {
val targetClass: Class<*>?
val processNameSuffix: String
when (godotInstanceId) {
GAME_ID -> {
GAME_PROCESS_NAME_SUFFIX
processNameSuffix = GAME_PROCESS_NAME_SUFFIX
targetClass = GodotGame::class.java
}
EDITOR_ID -> {
EDITOR_PROCESS_NAME_SUFFIX
processNameSuffix = EDITOR_PROCESS_NAME_SUFFIX
targetClass = GodotEditor::class.java
}
PROJECT_MANAGER_ID -> {
PROJECT_MANAGER_PROCESS_NAME_SUFFIX
processNameSuffix = PROJECT_MANAGER_PROCESS_NAME_SUFFIX
targetClass = GodotProjectManager::class.java
}
else -> ""
else -> {
processNameSuffix = ""
targetClass = null
}
}

if (targetClass == javaClass) {
Log.d(TAG, "Force quitting $targetClass")
ProcessPhoenix.forceQuit(this)
return true
}

if (processNameSuffix.isBlank()) {
return false
}
Expand All @@ -208,8 +242,16 @@ open class GodotEditor : FullScreenGodotApp() {
val runningProcesses = activityManager.runningAppProcesses
for (runningProcess in runningProcesses) {
if (runningProcess.processName.endsWith(processNameSuffix)) {
Log.v(TAG, "Killing Godot process ${runningProcess.processName}")
Process.killProcess(runningProcess.pid)
if (targetClass == null) {
// Killing process directly
Log.v(TAG, "Killing Godot process ${runningProcess.processName}")
Process.killProcess(runningProcess.pid)
} else {
// Activity is running; sending a request for self termination.
Log.v(TAG, "Sending force quit request to $targetClass running on process ${runningProcess.processName}")
val forceQuitIntent = Intent(this, targetClass).putExtra(EXTRA_FORCE_QUIT, true)
startActivity(forceQuitIntent)
}
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void forceQuit(Activity activity) {
*/
public static void forceQuit(Activity activity, int pid) {
Process.killProcess(pid); // Kill original main process
activity.finish();
activity.finishAndRemoveTask();
Runtime.getRuntime().exit(0); // Kill kill kill!
}

Expand Down

0 comments on commit a951a14

Please sign in to comment.