comparison 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
comparison
equal deleted inserted replaced
2981:0058ab32e1bd 2982:e6fb2558e29e
38 import android.util.* 38 import android.util.*
39 import android.util.Base64 39 import android.util.Base64
40 import android.view.* 40 import android.view.*
41 import android.view.View.OnTouchListener 41 import android.view.View.OnTouchListener
42 import android.view.inputmethod.EditorInfo 42 import android.view.inputmethod.EditorInfo
43 import android.webkit.JavascriptInterface
43 import android.webkit.WebView 44 import android.webkit.WebView
44 import android.webkit.WebViewClient 45 import android.webkit.WebViewClient
45 import android.widget.* 46 import android.widget.*
46 import android.widget.AdapterView.OnItemClickListener 47 import android.widget.AdapterView.OnItemClickListener
47 import android.widget.SeekBar.OnSeekBarChangeListener 48 import android.widget.SeekBar.OnSeekBarChangeListener
49 import androidx.annotation.RequiresApi
48 import androidx.appcompat.app.AlertDialog 50 import androidx.appcompat.app.AlertDialog
49 import androidx.appcompat.app.AppCompatActivity 51 import androidx.appcompat.app.AppCompatActivity
50 import androidx.appcompat.widget.AppCompatEditText 52 import androidx.appcompat.widget.AppCompatEditText
51 import androidx.collection.SimpleArrayMap 53 import androidx.collection.SimpleArrayMap
52 import androidx.constraintlayout.widget.ConstraintLayout 54 import androidx.constraintlayout.widget.ConstraintLayout
1493 const val SWITCH_PAGE = 15 1495 const val SWITCH_PAGE = 15
1494 const val TREE_EXPAND = 16 1496 const val TREE_EXPAND = 16
1495 const val COLUMN_CLICK = 17 1497 const val COLUMN_CLICK = 17
1496 const val HTML_RESULT = 18 1498 const val HTML_RESULT = 18
1497 const val HTML_CHANGED = 19 1499 const val HTML_CHANGED = 19
1500 const val HTML_MESSAGE = 20
1498 } 1501 }
1499 1502
1500 val DWImageExts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif") 1503 val DWImageExts = arrayOf("", ".png", ".webp", ".jpg", ".jpeg", ".gif")
1501 1504
1502 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() { 1505 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() {
1522 1525
1523 class DWEventViewHolder(var view: View) : RecyclerView.ViewHolder(view) 1526 class DWEventViewHolder(var view: View) : RecyclerView.ViewHolder(view)
1524 } 1527 }
1525 1528
1526 private class DWWebViewClient : WebViewClient() { 1529 private class DWWebViewClient : WebViewClient() {
1530 val HTMLAdds = mutableListOf<String>()
1531
1527 //Implement shouldOverrideUrlLoading// 1532 //Implement shouldOverrideUrlLoading//
1528 @Deprecated("Deprecated in Java") 1533 @Deprecated("Deprecated in Java")
1529 override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { 1534 override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
1530 // We always want to load in our own WebView, 1535 // We always want to load in our own WebView,
1531 // to match the behavior on the other platforms 1536 // to match the behavior on the other platforms
1535 // Handle the DW_HTML_CHANGE_STARTED event 1540 // Handle the DW_HTML_CHANGE_STARTED event
1536 eventHandlerHTMLChanged(view, DWEvent.HTML_CHANGED, url, 1) 1541 eventHandlerHTMLChanged(view, DWEvent.HTML_CHANGED, url, 1)
1537 } 1542 }
1538 1543
1539 override fun onPageFinished(view: WebView, url: String) { 1544 override fun onPageFinished(view: WebView, url: String) {
1545 // Inject the functions on the page on complete
1546 HTMLAdds.forEach { e -> view.loadUrl("javascript:function $e(body) { DWindows.postMessage($e, body) }") }
1547
1540 // Handle the DW_HTML_CHANGE_COMPLETE event 1548 // Handle the DW_HTML_CHANGE_COMPLETE event
1541 eventHandlerHTMLChanged(view, DWEvent.HTML_CHANGED, url, 4) 1549 eventHandlerHTMLChanged(view, DWEvent.HTML_CHANGED, url, 4)
1542 } 1550 }
1543 1551
1544 external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int) 1552 external fun eventHandlerHTMLChanged(obj1: View, message: Int, URI: String, status: Int)
1553 }
1554
1555 class DWWebViewInterface internal constructor(var view: View) {
1556 /* Show a toast from the web page */
1557 @JavascriptInterface
1558 fun postMessage(name: String?, body: String?) {
1559 // Handle the DW_HTML_CHANGE_COMPLETE event
1560 eventHandlerHTMLMessage(view, DWEvent.HTML_CHANGED, name, body)
1561 }
1562
1563 external fun eventHandlerHTMLMessage(obj1: View, message: Int, hmltName: String?, htmlBody: String?)
1545 } 1564 }
1546 1565
1547 class DWPrintDocumentAdapter : PrintDocumentAdapter() 1566 class DWPrintDocumentAdapter : PrintDocumentAdapter()
1548 { 1567 {
1549 var context: Context? = null 1568 var context: Context? = null
4738 html!!.tag = dataArrayMap 4757 html!!.tag = dataArrayMap
4739 html!!.id = cid 4758 html!!.id = cid
4740 // Configure a few settings to make it behave as we expect 4759 // Configure a few settings to make it behave as we expect
4741 html!!.webViewClient = DWWebViewClient() 4760 html!!.webViewClient = DWWebViewClient()
4742 html!!.settings.javaScriptEnabled = true 4761 html!!.settings.javaScriptEnabled = true
4762 html!!.addJavascriptInterface(DWWebViewInterface(html!!), "DWindows");
4743 } 4763 }
4744 return html 4764 return html
4745 } 4765 }
4746 4766
4747 fun htmlLoadURL(html: WebView, url: String) 4767 fun htmlLoadURL(html: WebView, url: String)
4764 waitOnUiThread { 4784 waitOnUiThread {
4765 html.evaluateJavascript(javascript) { value -> 4785 html.evaluateJavascript(javascript) { value ->
4766 // Execute onReceiveValue's code 4786 // Execute onReceiveValue's code
4767 eventHandlerHTMLResult(html, DWEvent.HTML_RESULT, value, data) 4787 eventHandlerHTMLResult(html, DWEvent.HTML_RESULT, value, data)
4768 } 4788 }
4789 }
4790 }
4791
4792 fun htmlJavascriptAdd(html: WebView, name: String)
4793 {
4794 waitOnUiThread {
4795 val client = html.webViewClient as DWWebViewClient
4796 client.HTMLAdds += name
4769 } 4797 }
4770 } 4798 }
4771 4799
4772 fun htmlAction(html: WebView, action: Int) 4800 fun htmlAction(html: WebView, action: Int)
4773 { 4801 {