From f0cb003f203125c0703c5453e004367251e1bfdc Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 30 Sep 2021 21:17:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96webViewActivity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/web/WebViewActivity.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt b/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt index 3b8495774..190c91aae 100644 --- a/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt +++ b/app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt @@ -4,11 +4,13 @@ import android.annotation.SuppressLint import android.content.pm.ActivityInfo import android.net.Uri import android.os.Bundle +import android.view.KeyEvent import android.view.Menu import android.view.MenuItem import android.view.View import android.webkit.* import androidx.activity.viewModels +import androidx.core.view.size import androidx.webkit.WebSettingsCompat import androidx.webkit.WebViewFeature import io.legado.app.R @@ -131,6 +133,38 @@ class WebViewActivity : VMBaseActivity() { } } + override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean { + when (keyCode) { + KeyEvent.KEYCODE_BACK -> { + finish() + return true + } + } + return super.onKeyLongPress(keyCode, event) + } + + override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { + event?.let { + when (keyCode) { + KeyEvent.KEYCODE_BACK -> if (event.isTracking && !event.isCanceled && binding.webView.canGoBack()) { + if (binding.customWebView.size > 0) { + customWebViewCallback?.onCustomViewHidden() + return true + } else if (binding.webView.copyBackForwardList().size > 1) { + binding.webView.goBack() + return true + } + } + } + } + return super.onKeyUp(keyCode, event) + } + + override fun onDestroy() { + super.onDestroy() + binding.webView.destroy() + } + inner class CustomWebChromeClient : WebChromeClient() { override fun onReceivedTitle(view: WebView?, title: String?) {