diff --git a/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumEditor.kt b/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumEditor.kt index 2f360475a..57cd39407 100644 --- a/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumEditor.kt +++ b/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumEditor.kt @@ -190,6 +190,47 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() { return instanceId } + override fun enableForStealingFocus(processId: Int) { + if (shouldGameLaunchAdjacent()) { + return; + } + + var reorder_intent : Intent? = null; + + when (processId) { + GAME_ID -> { + reorder_intent = Intent(this, PandemoniumGame::class.java); + } + EDITOR_ID -> { + reorder_intent = Intent(this, PandemoniumEditor::class.java); + } + PROJECT_MANAGER_ID -> { + reorder_intent = Intent(this, PandemoniumProjectManager::class.java); + } + else -> { + // An unknown PID means the original editor instance + reorder_intent = Intent(this, PandemoniumEditor::class.java); + } + } + + if (reorder_intent != null) { + reorder_intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + reorder_intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivity(reorder_intent); + } + } + + override fun moveWindowToForeground() { + if (shouldGameLaunchAdjacent()) { + return; + } + + var reorder_intent : Intent = Intent(this, javaClass); + reorder_intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + reorder_intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivity(reorder_intent); + } + override fun onPandemoniumForceQuit(pandemoniumInstanceId: Int): Boolean { val processNameSuffix = when (pandemoniumInstanceId) { GAME_ID -> { @@ -261,7 +302,7 @@ open class PandemoniumEditor : FullScreenPandemoniumApp() { - private fun shouldGameLaunchAdjacent(): Boolean { + protected open fun shouldGameLaunchAdjacent(): Boolean { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { try { when (Integer.parseInt(PandemoniumLib.getEditorSetting("run/window_placement/android_window"))) { diff --git a/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumGame.kt b/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumGame.kt index 35b1baee1..8bdef1c86 100644 --- a/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumGame.kt +++ b/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumGame.kt @@ -30,10 +30,15 @@ package net.relintai.pandemonium.editor +import android.content.Intent +import android.os.* + /** * Drives the 'run project' window of the Pandemonium Editor. */ class PandemoniumGame : PandemoniumEditor() { + private var isAdjacentFlagSet = false + override fun overrideOrientationRequest() = false override fun enableLongPressGestures() = false @@ -44,4 +49,14 @@ class PandemoniumGame : PandemoniumEditor() { // Nothing to do.. by the time we get here, the project permissions will have already // been requested by the Editor window. } + + override fun onCreate(savedInstanceState : Bundle?) { + isAdjacentFlagSet = (getIntent().getFlags() and Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT) != 0; + + super.onCreate(savedInstanceState); + } + + override fun shouldGameLaunchAdjacent(): Boolean { + return isAdjacentFlagSet + } } diff --git a/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumProjectManager.kt b/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumProjectManager.kt index 85baf22d8..1f66caed5 100644 --- a/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumProjectManager.kt +++ b/platform/android/java/editor/src/main/java/net/relintai/pandemonium/editor/PandemoniumProjectManager.kt @@ -42,5 +42,9 @@ class PandemoniumProjectManager : PandemoniumEditor() { override fun checkForProjectPermissionsToEnable() { // Nothing to do here.. we have yet to select a project to load. } + + override fun shouldGameLaunchAdjacent(): Boolean { + return false + } } diff --git a/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/Pandemonium.java b/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/Pandemonium.java index ee85230d6..632a5c2ef 100644 --- a/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/Pandemonium.java +++ b/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/Pandemonium.java @@ -558,6 +558,26 @@ public class Pandemonium extends Fragment implements SensorEventListener, IDownl }); } + /** + * Used by the native code (java_pandemonium_wrapper.h). + */ + @Keep + public void enableForStealingFocus(int processId) { + if (pandemoniumHost != null) { + pandemoniumHost.enableForStealingFocus(processId); + } + } + + /** + * Used by the native code (java_pandemonium_wrapper.h). + */ + @Keep + public void moveWindowToForeground() { + if (pandemoniumHost != null) { + pandemoniumHost.moveWindowToForeground(); + } + } + public int getGLESVersionCode() { ActivityManager am = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE); ConfigurationInfo deviceInfo = am.getDeviceConfigurationInfo(); @@ -1264,7 +1284,7 @@ public class Pandemonium extends Fragment implements SensorEventListener, IDownl @Keep private int createNewPandemoniumInstance(String[] args) { if (pandemoniumHost != null) { - pandemoniumHost.onNewPandemoniumInstanceRequested(args); + return pandemoniumHost.onNewPandemoniumInstanceRequested(args); } return 0; diff --git a/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/PandemoniumHost.java b/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/PandemoniumHost.java index 4d108729e..c571512e5 100644 --- a/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/PandemoniumHost.java +++ b/platform/android/java/lib/src/net/relintai/pandemonium/pandemonium/PandemoniumHost.java @@ -86,4 +86,10 @@ public interface PandemoniumHost { default int onNewPandemoniumInstanceRequested(String[] args) { return 0; } + + default public void enableForStealingFocus(int processId) { + } + + default public void moveWindowToForeground() { + } } diff --git a/platform/android/java_pandemonium_wrapper.cpp b/platform/android/java_pandemonium_wrapper.cpp index 9206f68bb..ce3ef672a 100644 --- a/platform/android/java_pandemonium_wrapper.cpp +++ b/platform/android/java_pandemonium_wrapper.cpp @@ -66,6 +66,8 @@ PandemoniumJavaWrapper::PandemoniumJavaWrapper(JNIEnv *p_env, jobject p_activity _finish = p_env->GetMethodID(pandemonium_class, "forceQuit", "(I)Z"); _set_keep_screen_on = p_env->GetMethodID(pandemonium_class, "setKeepScreenOn", "(Z)V"); _alert = p_env->GetMethodID(pandemonium_class, "alert", "(Ljava/lang/String;Ljava/lang/String;)V"); + _enable_for_stealing_focus = p_env->GetMethodID(pandemonium_class, "enableForStealingFocus", "(I)V"); + _move_window_to_foreground = p_env->GetMethodID(pandemonium_class, "moveWindowToForeground", "()V"); _get_GLES_version_code = p_env->GetMethodID(pandemonium_class, "getGLESVersionCode", "()I"); _get_clipboard = p_env->GetMethodID(pandemonium_class, "getClipboard", "()Ljava/lang/String;"); _set_clipboard = p_env->GetMethodID(pandemonium_class, "setClipboard", "(Ljava/lang/String;)V"); @@ -252,6 +254,23 @@ void PandemoniumJavaWrapper::alert(const String &p_message, const String &p_titl } } +void PandemoniumJavaWrapper::enable_for_stealing_focus(int pid) { + JNIEnv *env = get_jni_env(); + ERR_FAIL_COND(env == nullptr); + + if (_enable_for_stealing_focus) { + env->CallVoidMethod(pandemonium_instance, _enable_for_stealing_focus, pid); + } +} +void PandemoniumJavaWrapper::move_window_to_foreground() { + JNIEnv *env = get_jni_env(); + ERR_FAIL_COND(env == nullptr); + + if (_move_window_to_foreground) { + env->CallVoidMethod(pandemonium_instance, _move_window_to_foreground); + } +} + int PandemoniumJavaWrapper::get_gles_version_code() { JNIEnv *env = get_jni_env(); ERR_FAIL_COND_V(env == nullptr, 0); diff --git a/platform/android/java_pandemonium_wrapper.h b/platform/android/java_pandemonium_wrapper.h index 36369525d..d1c8c3643 100644 --- a/platform/android/java_pandemonium_wrapper.h +++ b/platform/android/java_pandemonium_wrapper.h @@ -59,6 +59,8 @@ private: jmethodID _finish = 0; jmethodID _set_keep_screen_on = 0; jmethodID _alert = 0; + jmethodID _enable_for_stealing_focus = 0; + jmethodID _move_window_to_foreground = 0; jmethodID _get_GLES_version_code = 0; jmethodID _get_clipboard = 0; jmethodID _set_clipboard = 0; @@ -102,6 +104,8 @@ public: bool force_quit(JNIEnv *p_env = NULL, int p_instance_id = 0); void set_keep_screen_on(bool p_enabled); void alert(const String &p_message, const String &p_title); + void enable_for_stealing_focus(int pid); + void move_window_to_foreground(); int get_gles_version_code(); bool has_get_clipboard(); String get_clipboard(); diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 9edf6ec97..74e22bb0e 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -580,6 +580,13 @@ OS::ScreenOrientation OS_Android::get_screen_orientation() const { return OS::ScreenOrientation(orientation); } +void OS_Android::enable_for_stealing_focus(ProcessID pid) { + pandemonium_java->enable_for_stealing_focus(pid); +} +void OS_Android::move_window_to_foreground() { + pandemonium_java->move_window_to_foreground(); +} + String OS_Android::get_unique_id() const { String unique_id = pandemonium_io_java->get_unique_id(); if (unique_id != "") diff --git a/platform/android/os_android.h b/platform/android/os_android.h index c095ddb2b..4d3296c99 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -176,6 +176,9 @@ public: virtual void set_screen_orientation(ScreenOrientation p_orientation); virtual ScreenOrientation get_screen_orientation() const; + virtual void enable_for_stealing_focus(ProcessID pid); + virtual void move_window_to_foreground(); + virtual Error shell_open(String p_uri); virtual String get_executable_path() const; virtual String get_user_data_dir() const;