changeset 2514:5f711e86a211

Android: Implement dw_main_sleep() and dw_main_iteration(). Using the sort of hacky exception handler method for now.... since Google doesn't seem to want us to do this in a clean way.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 07 May 2021 19:54:05 +0000
parents 0fa54c340902
children 211044d98e86
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Fri May 07 11:11:10 2021 +0000
+++ b/android/DWindows.kt	Fri May 07 19:54:05 2021 +0000
@@ -12,6 +12,7 @@
 import android.os.Bundle
 import android.os.Handler
 import android.os.Looper
+import android.os.MessageQueue
 import android.text.InputFilter
 import android.text.InputFilter.LengthFilter
 import android.text.InputType
@@ -1063,6 +1064,43 @@
         return retval
     }
 
+    fun mainSleep(milliseconds: Int)
+    {
+        // If we are on the main UI thread... add an idle handler
+        // Then loop until we throw an exception when the time expires
+        // in the idle handler, if we are already thrown... remove the handler
+        if(Looper.getMainLooper() == Looper.myLooper()) {
+            val starttime = System.currentTimeMillis()
+
+            // Waiting for Idle to make sure Toast gets rendered.
+            Looper.myQueue().addIdleHandler(object : MessageQueue.IdleHandler {
+                var thrown: Boolean = false
+
+                override fun queueIdle(): Boolean {
+                    if(System.currentTimeMillis() - starttime >= milliseconds) {
+                        if (thrown == false) {
+                            thrown = true
+                            throw java.lang.RuntimeException()
+                        }
+                        return false
+                    }
+                    return true
+                }
+            })
+
+            // loop till a runtime exception is triggered.
+            try {
+                Looper.loop()
+            } catch (e2: RuntimeException) {
+            }
+        }
+        else
+        {
+            // If we are in a different thread just sleep
+            Thread.sleep(milliseconds.toLong())
+        }
+    }
+
     fun dwindowsExit(exitcode: Int)
     {
         waitOnUiThread {
--- a/android/dw.cpp	Fri May 07 11:11:10 2021 +0000
+++ b/android/dw.cpp	Fri May 07 19:54:05 2021 +0000
@@ -512,6 +512,18 @@
  */
 void API dw_main_sleep(int milliseconds)
 {
+    JNIEnv *env;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID mainSleep = env->GetMethodID(clazz, "mainSleep",
+                                                  "(I)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, mainSleep, milliseconds);
+    }
 }
 
 /*
@@ -519,6 +531,10 @@
  */
 void API dw_main_iteration(void)
 {
+    /* If we sleep for 0 milliseconds... we will drop out
+     * of the loop at the first idle moment
+     */
+    dw_main_sleep(0);
 }
 
 /*