Ported from godot: Fix issue causing the last edited project to open while switching to another one.

Fixes #76562
-m4gr3d
ee07f60b07
This commit is contained in:
Relintai 2023-06-28 13:16:58 +02:00
parent e79ea9f3a8
commit 556ffc09f7
3 changed files with 68 additions and 19 deletions

View File

@ -34,7 +34,7 @@
<activity <activity
android:name=".PandemoniumProjectManager" android:name=".PandemoniumProjectManager"
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode" android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode"
android:launchMode="singleTask" android:launchMode="singleInstance"
android:screenOrientation="userLandscape" android:screenOrientation="userLandscape"
android:exported="true" android:exported="true"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
@ -53,7 +53,7 @@
android:name=".PandemoniumEditor" android:name=".PandemoniumEditor"
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode" android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode"
android:process=":PandemoniumEditor" android:process=":PandemoniumEditor"
android:launchMode="singleTask" android:launchMode="singleInstance"
android:screenOrientation="userLandscape" android:screenOrientation="userLandscape"
android:exported="false" android:exported="false"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
@ -66,7 +66,7 @@
android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode" android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode"
android:label="@string/pandemonium_project_name_string" android:label="@string/pandemonium_project_name_string"
android:process=":PandemoniumGame" android:process=":PandemoniumGame"
android:launchMode="singleTask" android:launchMode="singleInstance"
android:exported="false" android:exported="false"
android:screenOrientation="userLandscape" android:screenOrientation="userLandscape"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

View File

@ -63,7 +63,8 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() {
private val TAG = PandemoniumEditor::class.java.simpleName private val TAG = PandemoniumEditor::class.java.simpleName
private const val WAIT_FOR_DEBUGGER = false 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_ID = 777
private const val EDITOR_ARG = "--editor" private const val EDITOR_ARG = "--editor"
@ -96,9 +97,7 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() {
// requested on demand based on use-cases. // requested on demand based on use-cases.
PermissionsUtil.requestManifestPermissions(this, setOf(Manifest.permission.RECORD_AUDIO)) PermissionsUtil.requestManifestPermissions(this, setOf(Manifest.permission.RECORD_AUDIO))
val params : Array<String>? = getIntent().getStringArrayExtra(COMMAND_LINE_PARAMS); handleIntentParams(intent)
updateCommandLineParams(params);
if (BuildConfig.BUILD_TYPE == "dev" && WAIT_FOR_DEBUGGER) { if (BuildConfig.BUILD_TYPE == "dev" && WAIT_FOR_DEBUGGER) {
Debug.waitForDebugger(); Debug.waitForDebugger();
@ -107,6 +106,25 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() {
super.onCreate(savedInstanceState); 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 onPandemoniumSetupCompleted() { override fun onPandemoniumSetupCompleted() {
super.onPandemoniumSetupCompleted() super.onPandemoniumSetupCompleted()
val longPressEnabled = enableLongPressGestures() val longPressEnabled = enableLongPressGestures()
@ -173,17 +191,17 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() {
} }
// Launch a new activity // Launch a new activity
val newInstance = Intent(this, targetClass).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(COMMAND_LINE_PARAMS, args) val newInstance = Intent(this, targetClass).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(EXTRA_COMMAND_LINE_PARAMS, args)
if (launchAdjacent) { if (launchAdjacent) {
newInstance.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT) newInstance.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)
} }
if (targetClass == javaClass) { if (targetClass == javaClass) {
Log.d(TAG, "Restarting $targetClass") Log.d(TAG, "Restarting $targetClass with parameters ${args.contentToString()}")
ProcessPhoenix.triggerRebirth(this, newInstance) ProcessPhoenix.triggerRebirth(this, newInstance)
} else { } else {
Log.d(TAG, "Starting $targetClass") Log.d(TAG, "Starting $targetClass with parameters ${args.contentToString()}")
startActivity(newInstance) startActivity(newInstance)
} }
@ -232,17 +250,32 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() {
} }
override fun onPandemoniumForceQuit(pandemoniumInstanceId: Int): Boolean { override fun onPandemoniumForceQuit(pandemoniumInstanceId: Int): Boolean {
val processNameSuffix = when (pandemoniumInstanceId) { val targetClass: Class<*>?
val processNameSuffix: String
when (pandemoniumInstanceId) {
GAME_ID -> { GAME_ID -> {
GAME_PROCESS_NAME_SUFFIX processNameSuffix = GAME_PROCESS_NAME_SUFFIX
targetClass = PandemoniumGame::class.java
} }
EDITOR_ID -> { EDITOR_ID -> {
EDITOR_PROCESS_NAME_SUFFIX processNameSuffix = EDITOR_PROCESS_NAME_SUFFIX
targetClass = PandemoniumEditor::class.java
} }
PROJECT_MANAGER_ID -> { PROJECT_MANAGER_ID -> {
PROJECT_MANAGER_PROCESS_NAME_SUFFIX processNameSuffix = PROJECT_MANAGER_PROCESS_NAME_SUFFIX
targetClass = PandemoniumProjectManager::class.java
} }
else -> "" else -> {
processNameSuffix = ""
targetClass = null
}
}
if (targetClass == javaClass) {
Log.d(TAG, "Force quitting $targetClass")
ProcessPhoenix.forceQuit(this)
return true
} }
if (processNameSuffix.isBlank()) { if (processNameSuffix.isBlank()) {
@ -253,8 +286,17 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() {
val runningProcesses = activityManager.runningAppProcesses val runningProcesses = activityManager.runningAppProcesses
for (runningProcess in runningProcesses) { for (runningProcess in runningProcesses) {
if (runningProcess.processName.endsWith(processNameSuffix)) { if (runningProcess.processName.endsWith(processNameSuffix)) {
Log.v(TAG, "Killing Pandemonium process ${runningProcess.processName}") if (targetClass == null) {
Process.killProcess(runningProcess.pid) // Killing process directly
Log.v(TAG, "Killing Pandemonium 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 return true
} }
} }

View File

@ -26,6 +26,7 @@ import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process; import android.os.Process;
import java.util.ArrayList; import java.util.ArrayList;
@ -90,8 +91,14 @@ public final class ProcessPhoenix extends Activity {
*/ */
public static void forceQuit(Activity activity, int pid) { public static void forceQuit(Activity activity, int pid) {
Process.killProcess(pid); // Kill original main process Process.killProcess(pid); // Kill original main process
activity.finish();
Runtime.getRuntime().exit(0); // Kill kill kill! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.finishAndRemoveTask();
} else {
activity.finish();
}
Runtime.getRuntime().exit(0); // Kill kill kill!
} }
// -- GODOT end -- // -- GODOT end --