feat: 添加自动填充阻止功能,优化 WebView 表单数据处理
This commit is contained in:
@@ -16,6 +16,7 @@ import android.util.Base64
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.autofill.AutofillManager
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.WebStorage
|
||||
import android.webkit.WebView
|
||||
@@ -59,6 +60,13 @@ class MainActivity : FlutterActivity() {
|
||||
disableAutofillForCurrentWindow()
|
||||
}
|
||||
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
super.onWindowFocusChanged(hasFocus)
|
||||
if (hasFocus) {
|
||||
disableAutofillForCurrentWindow()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
pendingCaptureConfirmationDialog?.dismiss()
|
||||
pendingCaptureConfirmationDialog = null
|
||||
@@ -405,21 +413,18 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
|
||||
private fun installAutofillBlocker() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
return
|
||||
}
|
||||
|
||||
disableAutofillForCurrentWindow()
|
||||
window.decorView.viewTreeObserver.addOnGlobalLayoutListener {
|
||||
disableAutofillForCurrentWindow()
|
||||
}
|
||||
window.decorView.post {
|
||||
disableAutofillForCurrentWindow()
|
||||
}
|
||||
}
|
||||
|
||||
private fun disableAutofillForCurrentWindow() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
return
|
||||
}
|
||||
disableAutofill(window.decorView)
|
||||
cancelAutofillSession()
|
||||
}
|
||||
|
||||
private fun disableAutofill(view: View) {
|
||||
@@ -427,6 +432,10 @@ class MainActivity : FlutterActivity() {
|
||||
view.importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS
|
||||
}
|
||||
|
||||
if (view is WebView) {
|
||||
disableWebViewAutofill(view)
|
||||
}
|
||||
|
||||
if (view is ViewGroup) {
|
||||
for (index in 0 until view.childCount) {
|
||||
disableAutofill(view.getChildAt(index))
|
||||
@@ -434,6 +443,35 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun disableWebViewAutofill(webView: WebView) {
|
||||
webView.settings.setSaveFormData(false)
|
||||
webView.clearFormData()
|
||||
webView.isSaveEnabled = false
|
||||
webView.setSaveFromParentEnabled(false)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
webView.importantForAutofill =
|
||||
View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS
|
||||
}
|
||||
|
||||
webView.post {
|
||||
webView.settings.setSaveFormData(false)
|
||||
webView.clearFormData()
|
||||
webView.isSaveEnabled = false
|
||||
webView.setSaveFromParentEnabled(false)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
webView.importantForAutofill =
|
||||
View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun cancelAutofillSession() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
getSystemService(AutofillManager::class.java)?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private fun normalizeMimeTypes(acceptTypes: List<String>): List<String> {
|
||||
val mimeTypes = linkedSetOf<String>()
|
||||
acceptTypes
|
||||
|
||||
Reference in New Issue
Block a user