Skip to content

Commit

Permalink
lab: use tencent x5 web engine as webview
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Oct 13, 2024
1 parent 10906f1 commit 3382197
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 95 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,7 @@ dependencies {
implementation(libs.okhttp)
implementation(libs.okhttp.sse)
implementation(libs.kotlinx.serialization.json)

api("com.tencent.tbs:tbssdk:44286")
}

14 changes: 13 additions & 1 deletion app/proguard-rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@
public static int i(...);
}

-keepnames class <1>$$serializer {static <1>$$serializer INSTANCE;}
-keepnames class <1>$$serializer {static <1>$$serializer INSTANCE;}


-dontwarn dalvik.**
-dontwarn com.tencent.smtt.**

-keep class com.tencent.smtt.** {
*;
}

-keep class com.tencent.tbs.** {
*;
}
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- https://developer.android.com/guide/topics/manifest/uses-feature-element#permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-feature
android:name="android.hardware.location"
android:required="false" />
Expand Down Expand Up @@ -229,7 +231,7 @@
</provider>

<meta-data
android:name="android.webkit.WebView.MetricsOptOut"
android:name="com.tencent.smtt.sdk.WebView.MetricsOptOut"
android:value="true" />
<meta-data
android:name="android.allow_multiple_resumed_activities"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/info/plateaukao/einkbro/EinkBroApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.app.Application
import android.content.SharedPreferences
import android.preference.PreferenceManager
import androidx.appcompat.app.AppCompatDelegate
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.QbSdk.PreInitCallback
import info.plateaukao.einkbro.browser.AdBlock
import info.plateaukao.einkbro.browser.AdBlockV2
import info.plateaukao.einkbro.browser.Cookie
Expand Down Expand Up @@ -53,6 +55,8 @@ class EinkBroApplication : Application() {
modules(myModule)
}

initX5()

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

if (config.uiLocaleLanguage.isNotEmpty()) {
Expand All @@ -71,6 +75,16 @@ class EinkBroApplication : Application() {
ttsManager.release()
}

private fun initX5() {
QbSdk.initX5Environment(this, object : PreInitCallback {
override fun onCoreInitFinished() {
}

override fun onViewInitFinished(p0: Boolean) {
}
})
}

companion object {
lateinit var instance: EinkBroApplication
private set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.WindowInsets
import android.view.WindowManager
import android.webkit.CookieManager
import android.webkit.ValueCallback
import android.webkit.WebChromeClient.CustomViewCallback
import android.webkit.WebView
import android.webkit.WebView.HitTestResult
import com.tencent.smtt.sdk.CookieManager
import com.tencent.smtt.sdk.ValueCallback
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebView.HitTestResult
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ProgressBar
Expand Down Expand Up @@ -1550,7 +1550,7 @@ open class BrowserActivity : FragmentActivity(), BrowserController {

ConfigManager.K_TOOLBAR_ICONS_FOR_LARGE,
ConfigManager.K_TOOLBAR_ICONS,
-> {
-> {
composeToolbarViewController.updateIcons()
}

Expand Down Expand Up @@ -1648,7 +1648,7 @@ open class BrowserActivity : FragmentActivity(), BrowserController {

ConfigManager.K_CUSTOM_USER_AGENT,
ConfigManager.K_ENABLE_CUSTOM_USER_AGENT,
-> {
-> {
ninjaWebView.updateUserAgentString()
ninjaWebView.reload()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.WindowManager
import android.webkit.WebView
import com.tencent.smtt.sdk.WebView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import info.plateaukao.einkbro.R
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.os.Message
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.webkit.ValueCallback
import android.webkit.WebChromeClient.CustomViewCallback
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback
import com.tencent.smtt.sdk.ValueCallback
import info.plateaukao.einkbro.preference.TranslationMode
import info.plateaukao.einkbro.view.NinjaWebView
import info.plateaukao.einkbro.viewmodel.TRANSLATE_API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package info.plateaukao.einkbro.browser

import android.app.Activity
import android.content.Context
import android.webkit.DownloadListener
import com.tencent.smtt.sdk.DownloadListener
import info.plateaukao.einkbro.unit.BrowserUnit.download

class NinjaDownloadListener(private val context: Context) : DownloadListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import android.os.Message
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.webkit.ConsoleMessage
import android.webkit.CookieManager
import android.webkit.GeolocationPermissions
import android.webkit.PermissionRequest
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebView.WebViewTransport
import android.webkit.WebViewClient
import com.tencent.smtt.export.external.interfaces.ConsoleMessage
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback
import com.tencent.smtt.export.external.interfaces.PermissionRequest
import com.tencent.smtt.export.external.interfaces.WebResourceRequest
import com.tencent.smtt.sdk.CookieManager
import com.tencent.smtt.sdk.ValueCallback
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebView.WebViewTransport
import com.tencent.smtt.sdk.WebViewClient
import info.plateaukao.einkbro.unit.HelperUnit
import info.plateaukao.einkbro.view.NinjaWebView

Expand Down Expand Up @@ -90,13 +91,13 @@ class NinjaWebChromeClient(
.replace("wv", "")
.replace(Regex("Version/\\d+\\.\\d+\\s"), "")
webSettings.cacheMode = WebSettings.LOAD_DEFAULT
webSettings.allowFileAccessFromFileURLs = true
webSettings.allowUniversalAccessFromFileURLs = true
// webSettings.allowFileAccessFromFileURLs = true
// webSettings.allowUniversalAccessFromFileURLs = true
webSettings.domStorageEnabled = true
webSettings.databaseEnabled = true
webSettings.javaScriptEnabled = true
webSettings.javaScriptCanOpenWindowsAutomatically = true
webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
// webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
val manager = CookieManager.getInstance()
manager.setAcceptThirdPartyCookies(webView, true)

Expand Down Expand Up @@ -142,7 +143,7 @@ class NinjaWebChromeClient(
}

override fun onPermissionRequest(request: PermissionRequest?) {
if (request?.resources?.contains("android.webkit.resource.AUDIO_CAPTURE") == true) {
if (request?.resources?.contains("com.tencent.smtt.sdk.resource.AUDIO_CAPTURE") == true) {
HelperUnit.grantPermissionsMicrophone(ninjaWebView.context as Activity)
request.grant(request.resources)
} else {
Expand All @@ -152,7 +153,7 @@ class NinjaWebChromeClient(

override fun onGeolocationPermissionsShowPrompt(
origin: String,
callback: GeolocationPermissions.Callback,
callback: GeolocationPermissionsCallback,
) {
val activity = ninjaWebView.context as Activity
HelperUnit.grantPermissionsLoc(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.net.http.SslError
import android.os.Message
import android.security.KeyChain
import android.util.Log
import android.view.View
import android.webkit.ClientCertRequest
import android.webkit.CookieManager
import android.webkit.HttpAuthHandler
import android.webkit.SslErrorHandler
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.TextView
import androidx.core.net.toUri
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.tencent.smtt.export.external.interfaces.ClientCertRequest
import com.tencent.smtt.export.external.interfaces.HttpAuthHandler
import com.tencent.smtt.export.external.interfaces.SslError
import com.tencent.smtt.export.external.interfaces.SslErrorHandler
import com.tencent.smtt.export.external.interfaces.WebResourceError
import com.tencent.smtt.export.external.interfaces.WebResourceRequest
import com.tencent.smtt.export.external.interfaces.WebResourceResponse
import com.tencent.smtt.sdk.CookieManager
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.caption.DualCaptionProcessor
import info.plateaukao.einkbro.preference.ConfigManager
Expand Down Expand Up @@ -370,14 +370,14 @@ class NinjaWebViewClient(
val title = "An Error Occurred!!!"
var message =
"The page you are trying to view cannot be shown because the connection isn't private or the authenticity of the received data could not be verified. \n\nIf you want to take the risk and continue viewing the page, please press OK.\n\n\nReason: "
when (error.primaryError) {
SslError.SSL_UNTRUSTED -> message += """"Certificate authority is not trusted.""""
SslError.SSL_EXPIRED -> message += """"Certificate has expired.""""
SslError.SSL_IDMISMATCH -> message += """"Certificate Hostname mismatch.""""
SslError.SSL_NOTYETVALID -> message += """"Certificate is not yet valid.""""
SslError.SSL_DATE_INVALID -> message += """"Certificate date is invalid.""""
SslError.SSL_INVALID -> message += """"Certificate is invalid.""""
}
// when (error.primaryError) {
// SslError.SSL_UNTRUSTED -> message += """"Certificate authority is not trusted.""""
// SslError.SSL_EXPIRED -> message += """"Certificate has expired.""""
// SslError.SSL_IDMISMATCH -> message += """"Certificate Hostname mismatch.""""
// SslError.SSL_NOTYETVALID -> message += """"Certificate is not yet valid.""""
// SslError.SSL_DATE_INVALID -> message += """"Certificate date is invalid.""""
// SslError.SSL_INVALID -> message += """"Certificate is invalid.""""
// }

Log.e(TAG, "onReceivedSslError: $message")
if (config.enableCertificateErrorDialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.webkit.WebView
import com.tencent.smtt.sdk.WebView
import android.widget.ProgressBar
import androidx.core.content.FileProvider.getUriForFile
import info.plateaukao.einkbro.unit.HelperUnit.fileName
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/info/plateaukao/einkbro/unit/BrowserUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.webkit.CookieManager
import android.webkit.URLUtil
import android.webkit.WebView
import android.webkit.WebView.HitTestResult.ANCHOR_TYPE
import android.webkit.WebView.HitTestResult.IMAGE_ANCHOR_TYPE
import android.webkit.WebView.HitTestResult.IMAGE_TYPE
import android.webkit.WebView.HitTestResult.SRC_ANCHOR_TYPE
import android.webkit.WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.RequiresApi
import androidx.appcompat.view.ContextThemeWrapper
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.tencent.smtt.sdk.CookieManager
import com.tencent.smtt.sdk.URLUtil
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebView.HitTestResult.ANCHOR_TYPE
import com.tencent.smtt.sdk.WebView.HitTestResult.IMAGE_ANCHOR_TYPE
import com.tencent.smtt.sdk.WebView.HitTestResult.IMAGE_TYPE
import com.tencent.smtt.sdk.WebView.HitTestResult.SRC_ANCHOR_TYPE
import com.tencent.smtt.sdk.WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE
import com.tencent.smtt.sdk.WebViewClient
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.browser.Cookie
import info.plateaukao.einkbro.database.RecordDb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import android.net.Uri
import android.os.Build
import android.provider.OpenableColumns
import android.view.View
import android.webkit.MimeTypeMap
import com.tencent.smtt.sdk.MimeTypeMap
import androidx.activity.result.ActivityResultLauncher
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/info/plateaukao/einkbro/unit/ViewUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.webkit.WebView.LAYER_TYPE_HARDWARE
import com.tencent.smtt.sdk.WebView.LAYER_TYPE_HARDWARE
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintSet
Expand Down
Loading

0 comments on commit 3382197

Please sign in to comment.