WebViewActivity.kt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package eu.kanade.tachiyomi.ui.webview
  2. import android.annotation.SuppressLint
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.graphics.Bitmap
  6. import android.graphics.Color
  7. import android.os.Bundle
  8. import android.view.Menu
  9. import android.view.MenuItem
  10. import android.webkit.WebChromeClient
  11. import android.webkit.WebView
  12. import androidx.core.graphics.ColorUtils
  13. import eu.kanade.tachiyomi.R
  14. import eu.kanade.tachiyomi.source.SourceManager
  15. import eu.kanade.tachiyomi.source.online.HttpSource
  16. import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
  17. import eu.kanade.tachiyomi.util.system.WebViewClientCompat
  18. import eu.kanade.tachiyomi.util.system.getResourceColor
  19. import eu.kanade.tachiyomi.util.system.openInBrowser
  20. import eu.kanade.tachiyomi.util.system.toast
  21. import eu.kanade.tachiyomi.util.view.invisible
  22. import eu.kanade.tachiyomi.util.view.visible
  23. import kotlinx.android.synthetic.main.webview_activity.*
  24. import uy.kohesive.injekt.injectLazy
  25. class WebViewActivity : BaseActivity() {
  26. private val sourceManager by injectLazy<SourceManager>()
  27. private var bundle: Bundle? = null
  28. @SuppressLint("SetJavaScriptEnabled")
  29. override fun onCreate(savedInstanceState: Bundle?) {
  30. super.onCreate(savedInstanceState)
  31. setContentView(R.layout.webview_activity)
  32. // Manually override status bar color since it's normally transparent with the app themes
  33. // This is needed to hide the app bar when it scrolls up
  34. window.statusBarColor = getResourceColor(R.attr.colorPrimaryDark)
  35. title = intent.extras?.getString(TITLE_KEY)
  36. setSupportActionBar(toolbar)
  37. supportActionBar?.setDisplayHomeAsUpEnabled(true)
  38. toolbar.setNavigationOnClickListener {
  39. super.onBackPressed()
  40. }
  41. swipe_refresh.isEnabled = false
  42. swipe_refresh.setOnRefreshListener {
  43. refreshPage()
  44. }
  45. if (bundle == null) {
  46. val source = sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource ?: return
  47. val url = intent.extras!!.getString(URL_KEY) ?: return
  48. val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }
  49. webview.settings.javaScriptEnabled = true
  50. webview.settings.userAgentString = source.headers["User-Agent"]
  51. webview.webChromeClient = object : WebChromeClient() {
  52. override fun onProgressChanged(view: WebView?, newProgress: Int) {
  53. progress_bar.visible()
  54. progress_bar.progress = newProgress
  55. if (newProgress == 100) {
  56. progress_bar.invisible()
  57. }
  58. super.onProgressChanged(view, newProgress)
  59. }
  60. }
  61. webview.webViewClient = object : WebViewClientCompat() {
  62. override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean {
  63. view.loadUrl(url)
  64. return true
  65. }
  66. override fun onPageFinished(view: WebView?, url: String?) {
  67. super.onPageFinished(view, url)
  68. invalidateOptionsMenu()
  69. title = view?.title
  70. swipe_refresh.isEnabled = true
  71. swipe_refresh?.isRefreshing = false
  72. }
  73. override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
  74. super.onPageStarted(view, url, favicon)
  75. invalidateOptionsMenu()
  76. }
  77. override fun onPageCommitVisible(view: WebView?, url: String?) {
  78. super.onPageCommitVisible(view, url)
  79. // Reset to top when page refreshes
  80. nested_view.scrollTo(0, 0)
  81. }
  82. }
  83. webview.loadUrl(url, headers)
  84. } else {
  85. webview.restoreState(bundle)
  86. }
  87. }
  88. override fun onCreateOptionsMenu(menu: Menu): Boolean {
  89. menuInflater.inflate(R.menu.webview, menu)
  90. return true
  91. }
  92. override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
  93. val backItem = toolbar.menu.findItem(R.id.action_web_back)
  94. val forwardItem = toolbar.menu.findItem(R.id.action_web_forward)
  95. backItem?.isEnabled = webview.canGoBack()
  96. forwardItem?.isEnabled = webview.canGoForward()
  97. val translucentWhite = ColorUtils.setAlphaComponent(Color.WHITE, 127)
  98. backItem.icon?.setTint(if (webview.canGoBack()) Color.WHITE else translucentWhite)
  99. forwardItem?.icon?.setTint(if (webview.canGoForward()) Color.WHITE else translucentWhite)
  100. return super.onPrepareOptionsMenu(menu)
  101. }
  102. override fun onBackPressed() {
  103. if (webview.canGoBack()) webview.goBack()
  104. else super.onBackPressed()
  105. }
  106. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  107. when (item.itemId) {
  108. R.id.action_web_back -> webview.goBack()
  109. R.id.action_web_forward -> webview.goForward()
  110. R.id.action_web_refresh -> refreshPage()
  111. R.id.action_web_share -> shareWebpage()
  112. R.id.action_web_browser -> openInBrowser()
  113. }
  114. return super.onOptionsItemSelected(item)
  115. }
  116. private fun refreshPage() {
  117. swipe_refresh.isRefreshing = true
  118. webview.reload()
  119. }
  120. private fun shareWebpage() {
  121. try {
  122. val intent = Intent(Intent.ACTION_SEND).apply {
  123. type = "text/plain"
  124. putExtra(Intent.EXTRA_TEXT, webview.url)
  125. }
  126. startActivity(Intent.createChooser(intent, getString(R.string.action_share)))
  127. } catch (e: Exception) {
  128. toast(e.message)
  129. }
  130. }
  131. private fun openInBrowser() {
  132. openInBrowser(webview.url)
  133. }
  134. companion object {
  135. private const val SOURCE_KEY = "source_key"
  136. private const val URL_KEY = "url_key"
  137. private const val TITLE_KEY = "title_key"
  138. fun newIntent(context: Context, sourceId: Long, url: String, title: String?): Intent {
  139. val intent = Intent(context, WebViewActivity::class.java)
  140. intent.putExtra(SOURCE_KEY, sourceId)
  141. intent.putExtra(URL_KEY, url)
  142. intent.putExtra(TITLE_KEY, title)
  143. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
  144. return intent
  145. }
  146. }
  147. }