Mercurial > dwindows
diff android/DWindows.kt @ 2982:e6fb2558e29e
Android: Support for dw_html_javascript_add() and DW_SIGNAL_HTML_MESSAGE.
Have to bump Android minimum API version to 26 (Oreo) for this to work.
Not completely functional but I need change locations, it builds.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Mon, 27 Mar 2023 00:40:17 +0000 |
parents | e400d349aca5 |
children | 81611e54ff1d |
line wrap: on
line diff
--- a/android/DWindows.kt Sun Mar 26 22:10:10 2023 +0000 +++ b/android/DWindows.kt Mon Mar 27 00:40:17 2023 +0000 @@ -40,11 +40,13 @@ import android.view.* import android.view.View.OnTouchListener import android.view.inputmethod.EditorInfo +import android.webkit.JavascriptInterface import android.webkit.WebView import android.webkit.WebViewClient import android.widget.* import android.widget.AdapterView.OnItemClickListener import android.widget.SeekBar.OnSeekBarChangeListener +import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.AppCompatEditText @@ -1495,6 +1497,7 @@ const val COLUMN_CLICK = 17 const val HTML_RESULT = 18 const val HTML_CHANGED = 19 + const val HTML_MESSAGE = 20 } val DWImageExts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif") @@ -1524,6 +1527,8 @@ } private class DWWebViewClient : WebViewClient() { + val HTMLAdds = mutableListOf<String>() + //Implement shouldOverrideUrlLoading// @Deprecated("Deprecated in Java") override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { @@ -1537,6 +1542,9 @@ } override fun onPageFinished(view: WebView, url: String) { + // Inject the functions on the page on complete + HTMLAdds.forEach { e -> view.loadUrl("javascript:function $e(body) { DWindows.postMessage($e, body) }") } + // Handle the DW_HTML_CHANGE_COMPLETE event eventHandlerHTMLChanged(view, DWEvent.HTML_CHANGED, url, 4) } @@ -1544,6 +1552,17 @@ external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int) } +class DWWebViewInterface internal constructor(var view: View) { + /* Show a toast from the web page */ + @JavascriptInterface + fun postMessage(name: String?, body: String?) { + // Handle the DW_HTML_CHANGE_COMPLETE event + eventHandlerHTMLMessage(view, DWEvent.HTML_CHANGED, name, body) + } + + external fun eventHandlerHTMLMessage(obj1: View, message: Int, hmltName: String?, htmlBody: String?) +} + class DWPrintDocumentAdapter : PrintDocumentAdapter() { var context: Context? = null @@ -4740,6 +4759,7 @@ // Configure a few settings to make it behave as we expect html!!.webViewClient = DWWebViewClient() html!!.settings.javaScriptEnabled = true + html!!.addJavascriptInterface(DWWebViewInterface(html!!), "DWindows"); } return html } @@ -4769,6 +4789,14 @@ } } + fun htmlJavascriptAdd(html: WebView, name: String) + { + waitOnUiThread { + val client = html.webViewClient as DWWebViewClient + client.HTMLAdds += name + } + } + fun htmlAction(html: WebView, action: Int) { waitOnUiThread {