WebViewChat.kt 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package io.r_a_d.radio2.ui.chat
  2. import android.annotation.SuppressLint
  3. import android.content.Intent
  4. import android.net.Uri
  5. import android.webkit.WebChromeClient
  6. import android.webkit.WebView
  7. class WebViewChat(private val webView: WebView) {
  8. @SuppressLint("SetJavaScriptEnabled")
  9. fun start() {
  10. val webSetting = this.webView.settings
  11. webSetting.javaScriptEnabled = true
  12. webSetting.setSupportZoom(false)
  13. /* TODO: in the future, it could be nice to have a parameters screen where you can:
  14. - Set the text zoom
  15. - Set your username (to not type it every time, would it be possible?)
  16. - Hide the chat?
  17. - do more? */
  18. webSetting.textZoom = 90
  19. webSetting.setSupportMultipleWindows(true)
  20. // needs to open target="_blank" links as KiwiIRC links have this attribute.
  21. // shamelessly ripped off https://stackoverflow.com/questions/18187714/android-open-target-blank-links-in-webview-with-external-browser
  22. this.webView.webChromeClient = object : WebChromeClient() {
  23. override fun onCreateWindow(
  24. view: WebView,
  25. dialog: Boolean,
  26. userGesture: Boolean,
  27. resultMsg: android.os.Message
  28. ): Boolean {
  29. val result = view.hitTestResult
  30. val data = result.extra
  31. val context = view.context
  32. val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(data))
  33. context.startActivity(browserIntent)
  34. return false
  35. }
  36. }
  37. webView.loadUrl("file:///android_asset/chat.html")
  38. }
  39. }