Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package com.danser.js_test
  2.  
  3. import android.annotation.SuppressLint
  4. import android.app.Activity
  5. import android.os.Bundle
  6. import android.webkit.JavascriptInterface
  7. import android.webkit.WebView
  8. import kotlinx.android.synthetic.main.activity_main.*
  9. import kotlin.random.Random
  10.  
  11. class MainActivity : Activity() {
  12.  
  13. lateinit var jsExecutor: JSExecutor
  14.  
  15. public override fun onCreate(savedInstanceState: Bundle?) {
  16. super.onCreate(savedInstanceState)
  17. setContentView(R.layout.activity_main)
  18.  
  19.  
  20. WebView.setWebContentsDebuggingEnabled(true)
  21.  
  22. jsExecutor = JSExecutor(this)
  23.  
  24. bExecute.setOnClickListener {
  25. callJavaScriptFunctionAndGetResultBack(333, Random.nextInt(100, 200))
  26. }
  27. }
  28.  
  29. private fun callJavaScriptFunctionAndGetResultBack(val1: Int, val2: Int) {
  30. jsExecutor.execute(
  31. jsCode = JS_CODE,
  32. methodUrl = "addSomething(%s, %s)",
  33. params = *arrayOf(val1, val2)
  34. ) { result ->
  35. runOnUiThread { tvResult.text = "Callback got val: $result" }
  36. }
  37. }
  38. }
  39.  
  40. private val JS_CODE =
  41. """
  42. function addSomething(a, b) {
  43. console.log('log from javascript');
  44. return a + b;
  45. }
  46. """
  47.  
  48.  
  49. class JSExecutor(activity: Activity) {
  50.  
  51. private val webView = WebView(activity)
  52. private val jsInterface: JavaScriptInterface = JavaScriptInterface(activity, webView)
  53.  
  54. init {
  55. initWebView()
  56. }
  57.  
  58. @SuppressLint("SetJavaScriptEnabled")
  59. private fun initWebView() {
  60. webView.settings.javaScriptEnabled = true
  61. webView.addJavascriptInterface(jsInterface, HANDLER_NAME)
  62. }
  63.  
  64. fun execute(jsCode: String, methodUrl: String, vararg params: Any, onResult: (Int) -> Unit) {
  65. jsInterface.onResult = onResult
  66. webView.loadUrl(getHtml(jsCode))
  67. webView.loadUrl(getMethodUrl(methodUrl, *params))
  68. }
  69.  
  70. private fun getMethodUrl(method: String, vararg params: Any): String {
  71. val methodCall = String.format(method, *params)
  72. return "javascript:window.$HANDLER_NAME.setResult( $methodCall )"
  73. }
  74.  
  75. private fun getHtml(jsText: String): String =
  76. """<html><head><script type="text/javascript">function addSomething(a, b) {
  77. console.log('log from javascript');
  78. return a + b;
  79. }</script></head></html>"""
  80.  
  81. class JavaScriptInterface(
  82. private val activity: Activity,
  83. private val webView: WebView
  84. ) {
  85. var onResult: (Int) -> Unit = {}
  86.  
  87. @JavascriptInterface
  88. fun loadURL(url: String) {
  89. activity.runOnUiThread { webView.loadUrl(url) }
  90. }
  91.  
  92. @JavascriptInterface
  93. fun setResult(result: Int) {
  94. onResult(result)
  95. }
  96. }
  97.  
  98. companion object {
  99. private const val HANDLER_NAME = "interface handler"
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement