Commit ae57d9c8 authored by leichao.gao's avatar leichao.gao

update

parent 1fb04e75
...@@ -6,7 +6,9 @@ import android.animation.ObjectAnimator ...@@ -6,7 +6,9 @@ import android.animation.ObjectAnimator
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.text.Editable
import android.text.TextUtils import android.text.TextUtils
import android.text.TextWatcher
import android.view.View import android.view.View
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
...@@ -143,10 +145,17 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() { ...@@ -143,10 +145,17 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
} }
private fun resetSearchLayout() { private fun resetSearchLayout() {
val screenWidth = Utils.getScreenWidth(MyApplication.context) - MyApplication.context.dpToPx(30f) val screenWidth =
Utils.getScreenWidth(MyApplication.context) - MyApplication.context.dpToPx(30f)
val reverseScaleXAnimator = val reverseScaleXAnimator =
ObjectAnimator.ofFloat(binding.clSearch, "scaleX", screenWidth / binding.clSearch.width.toFloat(), 1f) ObjectAnimator.ofFloat(
val reverseScaleYAnimator = ObjectAnimator.ofFloat(binding.clSearch, "scaleY", 50f / 44f, 1f) binding.clSearch,
"scaleX",
screenWidth / binding.clSearch.width.toFloat(),
1f
)
val reverseScaleYAnimator =
ObjectAnimator.ofFloat(binding.clSearch, "scaleY", 50f / 44f, 1f)
binding.clSearch.pivotX = 0f binding.clSearch.pivotX = 0f
binding.clSearch.pivotY = 0f binding.clSearch.pivotY = 0f
reverseScaleXAnimator.setDuration(500) reverseScaleXAnimator.setDuration(500)
...@@ -191,7 +200,10 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() { ...@@ -191,7 +200,10 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
"WevStoreActivity" -> { "WevStoreActivity" -> {
if (System.currentTimeMillis() - lastShowInter > 40 * 1000 * 1000 || lastShowInter == 0L) { if (System.currentTimeMillis() - lastShowInter > 40 * 1000 * 1000 || lastShowInter == 0L) {
AdmobMaxHelper.admobMaxShowInterstitialAd(this@WebBrowserActivity, false) { AdmobMaxHelper.admobMaxShowInterstitialAd(
this@WebBrowserActivity,
false
) {
if (it) { if (it) {
lastShowInter = System.currentTimeMillis() lastShowInter = System.currentTimeMillis()
} }
...@@ -300,7 +312,12 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() { ...@@ -300,7 +312,12 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
}, },
joinAction = { joinAction = {
if (json == null) { if (json == null) {
currentFragment?.bookmarkBean?.let { SpBeanUtils.addSpBean(BOOKMARK_SP_KEY, it) } currentFragment?.bookmarkBean?.let {
SpBeanUtils.addSpBean(
BOOKMARK_SP_KEY,
it
)
}
val dialog = showAddRootBookmarkDialog { val dialog = showAddRootBookmarkDialog {
BookmarkEditActivity.editBookmark = currentFragment?.bookmarkBean BookmarkEditActivity.editBookmark = currentFragment?.bookmarkBean
startActivity(Intent(this, BookmarkEditActivity::class.java)) startActivity(Intent(this, BookmarkEditActivity::class.java))
...@@ -356,10 +373,41 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() { ...@@ -356,10 +373,41 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
) )
} }
binding.ivPageSearchClose.setOnClickListener { binding.ivPageSearchClose.setOnClickListener {
binding.editPageSearch.setText("")
binding.llPageSearch.visibility = View.GONE binding.llPageSearch.visibility = View.GONE
binding.clBottom.visibility = View.VISIBLE binding.clBottom.visibility = View.VISIBLE
} }
binding.editPageSearch.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
val key = s.toString()
if (currentFragment != null) {
currentFragment?.findAll(key)
}
}
})
binding.ivLeftSearch.setOnClickListener {
if(currentFragment!=null){
currentFragment?.lastSearch()
}
}
binding.ivRightSearch.setOnClickListener {
if(currentFragment!=null){
currentFragment?.nextSearch()
}
}
}
fun setSearchNum(int: Int,max:Int) {
binding.tvPageSearchNumber.text =""+int+"/"+max
} }
override fun onResume() { override fun onResume() {
...@@ -389,7 +437,8 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() { ...@@ -389,7 +437,8 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
} }
private fun getAddedBookmarkJson(url: String): String? { private fun getAddedBookmarkJson(url: String): String? {
return SpBeanUtils.getSpBeanList(BOOKMARK_SP_KEY).find { Gson().fromJson(it, BookmarkBean::class.java).url == url } return SpBeanUtils.getSpBeanList(BOOKMARK_SP_KEY)
.find { Gson().fromJson(it, BookmarkBean::class.java).url == url }
} }
} }
\ No newline at end of file
...@@ -3,13 +3,15 @@ package com.base.browserwhite.ui.activity.webbrowser ...@@ -3,13 +3,15 @@ package com.base.browserwhite.ui.activity.webbrowser
import android.animation.ValueAnimator import android.animation.ValueAnimator
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Dialog import android.app.Dialog
import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.net.http.SslError import android.net.http.SslError
import android.text.Html
import android.util.Log
import android.view.View import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator import android.view.animation.AccelerateDecelerateInterpolator
import android.webkit.CookieManager import android.webkit.CookieManager
import android.webkit.JavascriptInterface
import android.webkit.JsResult import android.webkit.JsResult
import android.webkit.SslErrorHandler import android.webkit.SslErrorHandler
import android.webkit.ValueCallback import android.webkit.ValueCallback
...@@ -20,7 +22,6 @@ import android.webkit.WebSettings ...@@ -20,7 +22,6 @@ import android.webkit.WebSettings
import android.webkit.WebStorage import android.webkit.WebStorage
import android.webkit.WebView import android.webkit.WebView
import android.webkit.WebViewClient import android.webkit.WebViewClient
import android.widget.Toast
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.MyApplication import com.base.browserwhite.MyApplication
...@@ -53,6 +54,7 @@ import okhttp3.OkHttpClient ...@@ -53,6 +54,7 @@ import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import java.io.IOException import java.io.IOException
import java.util.regex.Pattern
import kotlin.random.Random import kotlin.random.Random
...@@ -81,6 +83,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -81,6 +83,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
var downloadDialog: Dialog? = null var downloadDialog: Dialog? = null
override fun setListener() { override fun setListener() {
super.setListener() super.setListener()
searchResult = mutableListOf()
binding.tvReload.setOnClickListener { binding.tvReload.setOnClickListener {
reloadWebView() reloadWebView()
} }
...@@ -121,9 +124,85 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -121,9 +124,85 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
} }
} }
binding.webView.setFindListener { activeMatchOrdinal, numberOfMatches, isDoneCounting ->
if (numberOfMatches > 0) {
if (isDoneCounting) {
binding.webView.loadUrl("javascript:window.android.onSearchResult(document.body.innerText);")
}
} else {
if (activity is WebBrowserActivity) {
(activity as WebBrowserActivity)?.setSearchNum(0, 0)
}
searchResult.clear()
searchPosition = 0
}
}
}
private var keyword = ""
fun findAll(keyword: String) {
this.keyword = keyword
searchResult.clear() // 清空搜索结果
binding.webView.findAllAsync(keyword)
}
private var searchPosition = 0
fun nextSearch() {
if (searchResult.isNotEmpty()) {
searchPosition++
if (searchPosition >= searchResult.size) {
searchPosition = 0
}
scrollToPosition(true)
}
}
private fun scrollToPosition(position: Boolean) {
binding.webView.findNext(position)
searchResult.clear() // 清空搜索结果
} }
fun lastSearch() {
if (searchResult.isNotEmpty()) {
searchPosition--
if (searchPosition < 0) {
searchPosition = searchResult.size - 1
}
scrollToPosition(false)
}
}
private fun highlightSearchResult() {
binding.webView.loadUrl("javascript:document.body.innerHTML = document.body.innerHTML.replace(/${keyword}/g, '<span style=\"background-color: #FFD700;\">$0</span>')")
}
private lateinit var searchResult: MutableList<Int>
@JavascriptInterface
fun onSearchResult(text: String?) {
text?.let {
val filteredText = Html.fromHtml(it).toString()
val pattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(filteredText)
while (matcher.find()) {
val start = matcher.start()
val end = matcher.end()
searchResult.add(start)
}
if (activity is WebBrowserActivity) {
(activity as WebBrowserActivity)?.setSearchNum(
searchPosition + 1,
searchResult.size
)
}
highlightSearchResult()
}
}
fun dropAndBounceButton(button: View) { fun dropAndBounceButton(button: View) {
// 初始位置 // 初始位置
button.isVisible = downloadList.isNotEmpty() button.isVisible = downloadList.isNotEmpty()
...@@ -133,7 +212,6 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -133,7 +212,6 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
// 掉落位置 // 掉落位置
val dropTranslationY = -(MyApplication.context.dpToPx(60f)).toFloat() val dropTranslationY = -(MyApplication.context.dpToPx(60f)).toFloat()
val dropTranslationY1 = -(MyApplication.context.dpToPx(20f)).toFloat() val dropTranslationY1 = -(MyApplication.context.dpToPx(20f)).toFloat()
val dropTranslationY2 = -(MyApplication.context.dpToPx(20f)).toFloat()
// 回弹高度 // 回弹高度
val animator = ValueAnimator.ofFloat( val animator = ValueAnimator.ofFloat(
...@@ -175,7 +253,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -175,7 +253,7 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
val webSettings = binding.webView.settings val webSettings = binding.webView.settings
webSettings.allowFileAccess = true// 设置允许访问文件数据 webSettings.allowFileAccess = true// 设置允许访问文件数据
webSettings.setSupportZoom(true) webSettings.setSupportZoom(false)
webSettings.builtInZoomControls = true webSettings.builtInZoomControls = true
webSettings.cacheMode = WebSettings.LOAD_NO_CACHE webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
webSettings.domStorageEnabled = true webSettings.domStorageEnabled = true
...@@ -205,7 +283,6 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -205,7 +283,6 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
webSettings.setUseWideViewPort(false); webSettings.setUseWideViewPort(false);
webSettings.setLoadsImagesAutomatically(true); // 确保自动加载图片 webSettings.setLoadsImagesAutomatically(true); // 确保自动加载图片
//设置WebChromeClient //设置WebChromeClient
binding.webView.webChromeClient = object : WebChromeClient() { binding.webView.webChromeClient = object : WebChromeClient() {
...@@ -327,12 +404,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() { ...@@ -327,12 +404,9 @@ class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
//设置Cookie //设置Cookie
val instance = CookieManager.getInstance() val instance = CookieManager.getInstance()
instance.setAcceptThirdPartyCookies(binding.webView, true) instance.setAcceptThirdPartyCookies(binding.webView, true)
binding.webView.addJavascriptInterface(this, "android")
}
override fun onAttach(context: Context) {
super.onAttach(context)
} }
private val downloadList = arrayListOf<DownloadBean>() private val downloadList = arrayListOf<DownloadBean>()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment