diff android/DWindows.kt @ 2501:41984ffb5ca2

Android: Initial implement of HTML control with WebView. Getting an error about view calls not on the UI thread, even though the call stack is on the main thread... confusing.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 06 May 2021 00:05:32 +0000
parents ac0b7e579229
children b6319aed3298
line wrap: on
line diff
--- a/android/DWindows.kt	Wed May 05 21:59:23 2021 +0000
+++ b/android/DWindows.kt	Thu May 06 00:05:32 2021 +0000
@@ -14,17 +14,18 @@
 import android.text.InputFilter
 import android.text.InputFilter.LengthFilter
 import android.text.method.PasswordTransformationMethod
+import android.util.Base64
 import android.util.Log
 import android.view.Gravity
 import android.view.View
 import android.view.ViewGroup
+import android.webkit.WebView
 import android.widget.*
 import android.widget.SeekBar.OnSeekBarChangeListener
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.collection.SimpleArrayMap
 import androidx.recyclerview.widget.RecyclerView
-import androidx.viewpager.widget.ViewPager
 import androidx.viewpager2.widget.ViewPager2
 import com.google.android.material.tabs.TabLayout
 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
@@ -678,6 +679,53 @@
         percent.max = range
     }
 
+    fun htmlNew(cid: Int): WebView?
+    {
+        val looper = Looper.myLooper()
+
+        // WebView requires an active Looper
+        if(looper == null) {
+            Looper.prepare()
+        }
+
+        var dataArrayMap = SimpleArrayMap<String, Long>()
+        val html = WebView(this)
+
+        html.tag = dataArrayMap
+        html.id = cid
+        return html
+    }
+
+    fun htmlLoadURL(html: WebView, url: String)
+    {
+        html.loadUrl(url)
+    }
+
+    fun htmlRaw(html: WebView, data: String)
+    {
+        val encodedHtml: String = Base64.encodeToString(data.toByteArray(), Base64.NO_PADDING)
+        html.loadData(encodedHtml, "text/html", "base64")
+    }
+
+    fun htmlJavascriptRun(html: WebView, javascript: String, data: Long)
+    {
+        html.evaluateJavascript(javascript) { value ->
+            // Execute onReceiveValue's code
+            eventHandlerHTMLResult(html, 18, value, data)
+        }
+    }
+
+    fun htmlAction(html: WebView, action: Int)
+    {
+        when (action) {
+            0 -> html.goBack()
+            1 -> html.goForward()
+            2 -> html.loadUrl("http://dwindows.netlabs.org")
+            4 -> html.reload()
+            5 -> html.stopLoading()
+        }
+    }
+
     fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
     {
         // creating timer task, timer
@@ -801,6 +849,7 @@
     external fun eventHandlerSimple(obj1: View, message: Int)
     external fun eventHandlerNotebook(obj1: View, message: Int, pageID: Long)
     external fun eventHandlerTimer(sigfunc: Long, data: Long): Int
+    external fun eventHandlerHTMLResult(obj1: View, message: Int, result: String, data: Long)
 
     companion object
     {