diff android/DWindows.kt @ 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 f54051c3f2a5
children 211044d98e86
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 {