This commit is contained in:
lunaticbum 2024-11-12 17:13:05 +09:00
parent 4b51b9807a
commit 0a2cd56c93
3 changed files with 304 additions and 339 deletions

View File

@ -59,7 +59,6 @@ import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kr.lunaticbum.awesomewebview.databinding.AwesomeWebViewBinding import kr.lunaticbum.awesomewebview.databinding.AwesomeWebViewBinding
import kr.lunaticbum.awesomewebview.enums.Position import kr.lunaticbum.awesomewebview.enums.Position
@ -74,7 +73,6 @@ import kr.lunaticbum.awesomewebview.helpers.UrlParser
import kr.lunaticbum.awesomewebview.jsInterface.CommonJsHelper import kr.lunaticbum.awesomewebview.jsInterface.CommonJsHelper
import kr.lunaticbum.awesomewebview.listeners.BroadCastManager import kr.lunaticbum.awesomewebview.listeners.BroadCastManager
import kr.lunaticbum.awesomewebview.objects.CustomMenu import kr.lunaticbum.awesomewebview.objects.CustomMenu
import kr.lunaticbum.awesomewebview.views.ShadowLayout
import kr.lunaticbum.awesomewebview.views.VideoEnabledWebChromeClient import kr.lunaticbum.awesomewebview.views.VideoEnabledWebChromeClient
import kr.lunaticbum.awesomewebview.views.VideoEnabledWebView import kr.lunaticbum.awesomewebview.views.VideoEnabledWebView
import kr.lunaticbum.converters.UnitConverter import kr.lunaticbum.converters.UnitConverter
@ -239,35 +237,35 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
protected var url: String? = null protected var url: String? = null
protected var extraHeaders: Map<String, String>? = null protected var extraHeaders: Map<String, String>? = null
protected var extraHeadersMainPage: Boolean? = null protected var extraHeadersMainPage: Boolean? = null
protected var appBar: AppBarLayout? = null // protected var appBar: AppBarLayout? = null
protected var toolbar: Toolbar? = null // protected var toolbar: Toolbar? = null
protected var toolbarLayout: RelativeLayout? = null // protected var toolbarLayout: RelativeLayout? = null
protected var title: TextView? = null // protected var title: TextView? = null
protected var urlTv: TextView? = null // protected var urlTv: TextView? = null
protected var close: AppCompatImageButton? = null // protected var close: AppCompatImageButton? = null
protected var back: AppCompatImageButton? = null // protected var back: AppCompatImageButton? = null
protected var forward: AppCompatImageButton? = null // protected var forward: AppCompatImageButton? = null
protected var more: AppCompatImageButton? = null // protected var more: AppCompatImageButton? = null
protected var webView: WebView? = null protected var webView: WebView? = null
protected var webChromeClient: WebChromeClient? = null protected var webChromeClient: WebChromeClient? = null
protected var webViewClient: WebViewClient? = null protected var webViewClient: WebViewClient? = null
protected var gradient: View? = null // protected var gradient: View? = null
protected var divider: View? = null // protected var divider: View? = null
protected var progressBar: ProgressBar? = null // protected var progressBar: ProgressBar? = null
protected var menuLayout: RelativeLayout? = null // protected var menuLayout: RelativeLayout? = null
protected var shadowLayout: ShadowLayout? = null // protected var shadowLayout: ShadowLayout? = null
protected var menuBackground: LinearLayout? = null // protected var menuBackground: LinearLayout? = null
protected var menuRefresh: LinearLayout? = null // protected var menuRefresh: LinearLayout? = null
protected var menuRefreshTv: TextView? = null // protected var menuRefreshTv: TextView? = null
protected var menuFind: LinearLayout? = null // protected var menuFind: LinearLayout? = null
protected var menuFindTv: TextView? = null // protected var menuFindTv: TextView? = null
protected var menuShareVia: LinearLayout? = null // protected var menuShareVia: LinearLayout? = null
protected var menuShareViaTv: TextView? = null //// protected var menuShareViaTv: TextView? = null
protected var menuCopyLink: LinearLayout? = null // protected var menuCopyLink: LinearLayout? = null
protected var menuCopyLinkTv: TextView? = null // protected var menuCopyLinkTv: TextView? = null
protected var menuOpenWith: LinearLayout? = null // protected var menuOpenWith: LinearLayout? = null
protected var menuOpenWithTv: TextView? = null // protected var menuOpenWithTv: TextView? = null
protected var webLayout: FrameLayout? = null // protected var webLayout: FrameLayout? = null
var downloadListener: DownloadListener = var downloadListener: DownloadListener =
DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
BroadCastManager.onDownloadStart( BroadCastManager.onDownloadStart(
@ -529,51 +527,18 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
} }
protected fun bindViews() { protected fun bindViews() {
appBar = findViewById(R.id.appBar) binding.toolbarContent.close.setOnClickListener(this)
toolbar = findViewById(R.id.toolbar) binding.toolbarContent.back.setOnClickListener(this)
toolbarLayout = findViewById(R.id.toolbarLayout) binding.toolbarContent.forward.setOnClickListener(this)
binding.toolbarContent.more.setOnClickListener(this)
title = findViewById(R.id.title)
urlTv = findViewById(R.id.url)
close = findViewById(R.id.close)
back = findViewById(R.id.back)
forward = findViewById(R.id.forward)
more = findViewById(R.id.more)
close?.setOnClickListener(this)
back?.setOnClickListener(this)
forward?.setOnClickListener(this)
more?.setOnClickListener(this)
gradient = findViewById(R.id.gradient)
divider = findViewById(R.id.divider)
progressBar = findViewById(R.id.progressBar)
menuLayout = findViewById(R.id.menuLayout)
shadowLayout = findViewById(R.id.shadowLayout)
menuBackground = findViewById(R.id.menuBackground)
menuRefresh = findViewById(R.id.menuRefresh)
menuRefreshTv = findViewById(R.id.menuRefreshTv)
menuFind = findViewById(R.id.menuFind)
menuFindTv = findViewById(R.id.menuFindTv)
menuShareVia = findViewById(R.id.menuShareVia)
menuShareViaTv = findViewById(R.id.menuShareViaTv)
menuCopyLink = findViewById(R.id.menuCopyLink)
menuCopyLinkTv = findViewById(R.id.menuCopyLinkTv)
menuOpenWith = findViewById(R.id.menuOpenWith)
menuOpenWithTv = findViewById(R.id.menuOpenWithTv)
webLayout = findViewById(R.id.webLayout)
webView = buildWebView() webView = buildWebView()
webLayout?.addView(webView) binding.webLayout.addView(webView)
} }
protected fun layoutViews() { protected fun layoutViews() {
if (!toolbarVisible) { if (!toolbarVisible) {
setSupportActionBar(toolbar) setSupportActionBar(binding.toolbar)
toolbar!!.visibility = View.GONE binding.toolbar.visibility = View.GONE
} }
run { run {
@ -586,8 +551,8 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
toolbarHeight.toInt() toolbarHeight.toInt()
) )
appBar!!.layoutParams = params binding.appBar.layoutParams = params
awesomeWebViewBinding.coordinatorLayout.requestLayout() binding.coordinatorLayout.requestLayout()
} }
run { run {
@ -599,25 +564,26 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
toolbarHeight.toInt() toolbarHeight.toInt()
) )
toolbarLayout!!.minimumHeight = toolbarHeight.toInt() binding.toolbarContent.toolbarLayout.minimumHeight = toolbarHeight.toInt()
toolbarLayout!!.layoutParams = params binding.toolbarContent.toolbarLayout.layoutParams = params
awesomeWebViewBinding.coordinatorLayout.requestLayout() binding.coordinatorLayout.requestLayout()
} }
run { run {
// TextViews // TextViews
val maxWidth = this.maxWidth val maxWidth = this.maxWidth
title!!.maxWidth = maxWidth
urlTv!!.maxWidth = maxWidth binding.toolbarContent.title.maxWidth = maxWidth
binding.toolbarContent.url.maxWidth = maxWidth
requestCenterLayout() requestCenterLayout()
} }
run { run {
// Icons // Icons
updateIcon(close, if (rtl) R.drawable.more else R.drawable.close) updateIcon(binding.toolbarContent.close, if (rtl) R.drawable.more else R.drawable.close)
updateIcon(back, R.drawable.back) updateIcon(binding.toolbarContent.back, R.drawable.back)
updateIcon(forward, R.drawable.forward) updateIcon(binding.toolbarContent.forward, R.drawable.forward)
updateIcon(more, if (rtl) R.drawable.close else R.drawable.more) updateIcon(binding.toolbarContent.more, if (rtl) R.drawable.close else R.drawable.more)
} }
run { // Divider run { // Divider
@ -625,15 +591,15 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
val toolbarHeight = val toolbarHeight =
if (toolbarVisible) resources.getDimension(R.dimen.toolbarHeight) else 0f if (toolbarVisible) resources.getDimension(R.dimen.toolbarHeight) else 0f
val params = val params =
gradient!!.layoutParams as CoordinatorLayout.LayoutParams binding.gradient.layoutParams as CoordinatorLayout.LayoutParams
params.setMargins(0, toolbarHeight.toInt(), 0, 0) params.setMargins(0, toolbarHeight.toInt(), 0, 0)
gradient!!.layoutParams = params binding.gradient.layoutParams = params
} }
} }
run { run {
// ProgressBar // ProgressBar
progressBar!!.minimumHeight = progressBarHeight.toInt() binding.progressBar.minimumHeight = progressBarHeight.toInt()
val params = val params =
CoordinatorLayout.LayoutParams( CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
@ -670,7 +636,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
0 0
) )
} }
progressBar!!.layoutParams = params binding.progressBar.layoutParams = params
} }
run { run {
@ -683,7 +649,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
var webLayoutMinimumHeight = var webLayoutMinimumHeight =
screenHeight - toolbarHeight - statusBarHeight screenHeight - toolbarHeight - statusBarHeight
if (showDivider && !gradientDivider) webLayoutMinimumHeight -= dividerHeight if (showDivider && !gradientDivider) webLayoutMinimumHeight -= dividerHeight
webLayout!!.minimumHeight = webLayoutMinimumHeight.toInt() binding.webLayout.minimumHeight = webLayoutMinimumHeight.toInt()
val params = val params =
CoordinatorLayout.LayoutParams( CoordinatorLayout.LayoutParams(
@ -691,15 +657,15 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
ViewGroup.LayoutParams.MATCH_PARENT ViewGroup.LayoutParams.MATCH_PARENT
) )
params.setMargins(0, toolbarHeight.toInt(), 0, 0) params.setMargins(0, toolbarHeight.toInt(), 0, 0)
webLayout!!.layoutParams = params binding.webLayout.layoutParams = params
} }
} }
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
protected fun initializeViews() { protected fun initializeViews() {
if (!toolbarVisible) { if (!toolbarVisible) {
setSupportActionBar(toolbar) setSupportActionBar(binding.toolbar)
toolbar!!.visibility = View.GONE binding.toolbar.visibility = View.GONE
} }
run { run {
@ -721,33 +687,33 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
} }
run { // Toolbar run { // Toolbar
toolbar!!.setBackgroundColor(toolbarColor) binding.toolbar.setBackgroundColor(toolbarColor)
} }
run { run {
// TextViews // TextViews
title!!.text = titleDefault binding.toolbarContent.title.text = titleDefault
title!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize) binding.toolbarContent.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize)
title!!.setTypeface(TypefaceHelper.get(this, titleFont)) binding.toolbarContent.title.setTypeface(TypefaceHelper.get(this, titleFont))
title!!.setTextColor(mTitleColor) binding.toolbarContent.title.setTextColor(mTitleColor)
urlTv!!.visibility = if (showUrl) View.VISIBLE else View.GONE binding.toolbarContent.url.visibility = if (showUrl) View.VISIBLE else View.GONE
urlTv!!.text = UrlParser.getHost(url) binding.toolbarContent.url.text = UrlParser.getHost(url)
urlTv!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, urlSize) binding.toolbarContent.url.setTextSize(TypedValue.COMPLEX_UNIT_PX, urlSize)
urlTv!!.setTypeface(TypefaceHelper.get(this, urlFont)) binding.toolbarContent.url.setTypeface(TypefaceHelper.get(this, urlFont))
urlTv!!.setTextColor(urlColor) binding.toolbarContent.url.setTextColor(urlColor)
requestCenterLayout() requestCenterLayout()
} }
run { run {
// Icons // Icons
close!!.setBackgroundResource(iconSelector) binding.toolbarContent.close.setBackgroundResource(iconSelector)
back!!.setBackgroundResource(iconSelector) binding.toolbarContent.back.setBackgroundResource(iconSelector)
forward!!.setBackgroundResource(iconSelector) binding.toolbarContent.forward.setBackgroundResource(iconSelector)
more!!.setBackgroundResource(iconSelector) binding.toolbarContent.more.setBackgroundResource(iconSelector)
close!!.visibility = if (showIconClose) View.VISIBLE else View.GONE binding.toolbarContent.close.visibility = if (showIconClose) View.VISIBLE else View.GONE
close!!.isEnabled = !disableIconClose binding.toolbarContent.close.isEnabled = !disableIconClose
if (((showMenuRefresh if (((showMenuRefresh
|| showMenuFind || showMenuFind
@ -755,11 +721,11 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
|| showMenuCopyLink || showMenuCopyLink
|| showMenuOpenWith) || customMenus!!.size > 0) && showIconMenu || showMenuOpenWith) || customMenus!!.size > 0) && showIconMenu
) { ) {
more!!.visibility = View.VISIBLE binding.toolbarContent.more.visibility = View.VISIBLE
} else { } else {
more!!.visibility = View.GONE binding.toolbarContent.more.visibility = View.GONE
} }
more!!.isEnabled = !disableIconMenu binding.toolbarContent.more.isEnabled = !disableIconMenu
} }
run { // Cookie run { // Cookie
@ -1012,8 +978,8 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
run { run {
// Divider // Divider
gradient!!.visibility = if (showDivider && gradientDivider) View.VISIBLE else View.GONE binding.gradient.visibility = if (showDivider && gradientDivider) View.VISIBLE else View.GONE
divider!!.visibility = if (showDivider && !gradientDivider) View.VISIBLE else View.GONE binding.toolbarContent.divider.visibility = if (showDivider && !gradientDivider) View.VISIBLE else View.GONE
if (gradientDivider) { if (gradientDivider) {
val dividerWidth = DisplayUtil.width val dividerWidth = DisplayUtil.width
val bitmap = val bitmap =
@ -1023,28 +989,28 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
dividerColor dividerColor
) )
val drawable = BitmapDrawable(resources, bitmap) val drawable = BitmapDrawable(resources, bitmap)
ViewUtil.setBackground(gradient, drawable) ViewUtil.setBackground(binding.gradient, drawable)
val params = val params =
gradient!!.layoutParams as CoordinatorLayout.LayoutParams binding.gradient.layoutParams as CoordinatorLayout.LayoutParams
params.height = dividerHeight.toInt() params.height = dividerHeight.toInt()
gradient!!.layoutParams = params binding.gradient.layoutParams = params
} else { } else {
divider!!.setBackgroundColor(dividerColor) binding.toolbarContent.divider.setBackgroundColor(dividerColor)
val params = val params =
divider!!.layoutParams as LinearLayout.LayoutParams binding.toolbarContent.divider.layoutParams as LinearLayout.LayoutParams
params.height = dividerHeight.toInt() params.height = dividerHeight.toInt()
divider!!.layoutParams = params binding.toolbarContent.divider.layoutParams = params
} }
} }
run { run {
// ProgressBar // ProgressBar
progressBar!!.visibility = if (showProgressBar) View.VISIBLE else View.GONE binding.progressBar.visibility = if (showProgressBar) View.VISIBLE else View.GONE
progressBar!!.progressDrawable binding.progressBar.progressDrawable
.setColorFilter(progressBarColor, PorterDuff.Mode.SRC_IN) .setColorFilter(progressBarColor, PorterDuff.Mode.SRC_IN)
progressBar!!.minimumHeight = progressBarHeight.toInt() binding.progressBar.minimumHeight = progressBarHeight.toInt()
val params = val params =
CoordinatorLayout.LayoutParams( CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
@ -1081,7 +1047,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
0 0
) )
} }
progressBar!!.layoutParams = params binding.progressBar.layoutParams = params
} }
run { run {
@ -1090,13 +1056,13 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
drawable.cornerRadius = resources.getDimension(R.dimen.defaultMenuCornerRadius) drawable.cornerRadius = resources.getDimension(R.dimen.defaultMenuCornerRadius)
drawable.setColor(menuColor) drawable.setColor(menuColor)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
menuBackground!!.background = drawable binding.menus.menuBackground.background = drawable
} else { } else {
menuBackground!!.setBackgroundDrawable(drawable) binding.menus.menuBackground.setBackgroundDrawable(drawable)
} }
shadowLayout!!.setShadowColor(menuDropShadowColor) binding.menus.shadowLayout.setShadowColor(menuDropShadowColor)
shadowLayout!!.setShadowSize(menuDropShadowSize) binding.menus.shadowLayout.setShadowSize(menuDropShadowSize)
val params = val params =
RelativeLayout.LayoutParams( RelativeLayout.LayoutParams(
@ -1108,67 +1074,62 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
params.setMargins(0, margin, margin, 0) params.setMargins(0, margin, margin, 0)
params.addRule(RelativeLayout.ALIGN_PARENT_TOP) params.addRule(RelativeLayout.ALIGN_PARENT_TOP)
params.addRule(if (rtl) RelativeLayout.ALIGN_PARENT_LEFT else RelativeLayout.ALIGN_PARENT_RIGHT) params.addRule(if (rtl) RelativeLayout.ALIGN_PARENT_LEFT else RelativeLayout.ALIGN_PARENT_RIGHT)
shadowLayout!!.layoutParams = params binding.menus.shadowLayout.layoutParams = params
menuRefresh!!.visibility = if (showMenuRefresh) View.VISIBLE else View.GONE binding.menus.menuRefresh.visibility = if (showMenuRefresh) View.VISIBLE else View.GONE
menuRefresh!!.setBackgroundResource(menuSelector) binding.menus.menuRefresh.setBackgroundResource(menuSelector)
menuRefresh!!.gravity = menuTextGravity binding.menus.menuRefresh.gravity = menuTextGravity
menuRefreshTv!!.setText(stringResRefresh) binding.menus.menuRefreshTv.setText(stringResRefresh)
menuRefreshTv!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize) binding.menus.menuRefreshTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize)
menuRefreshTv!!.setTypeface(TypefaceHelper.get(this, menuTextFont)) binding.menus.menuRefreshTv.setTypeface(TypefaceHelper.get(this, menuTextFont))
menuRefreshTv!!.setTextColor(menuTextColor) binding.menus.menuRefreshTv.setTextColor(menuTextColor)
menuRefreshTv!!.setPadding( binding.menus.menuRefreshTv.setPadding(
menuTextPaddingLeft.toInt(), menuTextPaddingLeft.toInt(),
0, 0,
menuTextPaddingRight.toInt(), menuTextPaddingRight.toInt(),
0 0
) )
menuFind!!.visibility = if (showMenuFind) View.VISIBLE else View.GONE binding.menus.menuFind.visibility = if (showMenuFind) View.VISIBLE else View.GONE
menuFind!!.setBackgroundResource(menuSelector) binding.menus.menuFind.setBackgroundResource(menuSelector)
menuFind!!.gravity = menuTextGravity binding.menus.menuFind.gravity = menuTextGravity
menuFindTv!!.setText(stringResFind) binding.menus.menuFindTv.setText(stringResFind)
menuFindTv!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize) binding.menus.menuFindTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize)
menuFindTv!!.setTypeface(TypefaceHelper.get(this, menuTextFont)) binding.menus.menuFindTv.setTypeface(TypefaceHelper.get(this, menuTextFont))
menuFindTv!!.setTextColor(menuTextColor) binding.menus.menuFindTv.setTextColor(menuTextColor)
menuFindTv!!.setPadding(menuTextPaddingLeft.toInt(), 0, menuTextPaddingRight.toInt(), 0) binding.menus.menuFindTv.setPadding(menuTextPaddingLeft.toInt(), 0, menuTextPaddingRight.toInt(), 0)
menuShareVia!!.visibility = if (showMenuShareVia) View.VISIBLE else View.GONE binding.menus.menuShareVia.visibility = if (showMenuShareVia) View.VISIBLE else View.GONE
menuShareVia!!.setBackgroundResource(menuSelector) binding.menus.menuShareVia.setBackgroundResource(menuSelector)
menuShareVia!!.gravity = menuTextGravity binding.menus.menuShareVia.gravity = menuTextGravity
menuShareViaTv!!.setText(stringResShareVia) binding.menus.menuShareViaTv.setText(stringResShareVia)
menuShareViaTv!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize) binding.menus.menuShareViaTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize)
menuShareViaTv!!.setTypeface(TypefaceHelper.get(this, menuTextFont)) binding.menus.menuShareViaTv.setTypeface(TypefaceHelper.get(this, menuTextFont))
menuShareViaTv!!.setTextColor(menuTextColor) binding.menus.menuShareViaTv.setTextColor(menuTextColor)
menuShareViaTv!!.setPadding( binding.menus.menuShareViaTv.setPadding(menuTextPaddingLeft.toInt(), 0, menuTextPaddingRight.toInt(), 0)
binding.menus.menuCopyLink.visibility = if (showMenuCopyLink) View.VISIBLE else View.GONE
binding.menus.menuCopyLink.setBackgroundResource(menuSelector)
binding.menus.menuCopyLink.gravity = menuTextGravity
binding.menus.menuCopyLinkTv.setText(stringResCopyLink)
binding.menus.menuCopyLinkTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize)
binding.menus.menuCopyLinkTv.setTypeface(TypefaceHelper.get(this, menuTextFont))
binding.menus.menuCopyLinkTv.setTextColor(menuTextColor)
binding.menus.menuCopyLinkTv.setPadding(
menuTextPaddingLeft.toInt(), menuTextPaddingLeft.toInt(),
0, 0,
menuTextPaddingRight.toInt(), menuTextPaddingRight.toInt(),
0 0
) )
menuCopyLink!!.visibility = if (showMenuCopyLink) View.VISIBLE else View.GONE binding.menus.menuOpenWith.visibility = if (showMenuOpenWith) View.VISIBLE else View.GONE
menuCopyLink!!.setBackgroundResource(menuSelector) binding.menus.menuOpenWith.setBackgroundResource(menuSelector)
menuCopyLink!!.gravity = menuTextGravity binding.menus.menuOpenWith.gravity = menuTextGravity
menuCopyLinkTv!!.setText(stringResCopyLink) binding.menus.menuOpenWithTv.setText(stringResOpenWith)
menuCopyLinkTv!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize) binding.menus.menuOpenWithTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize)
menuCopyLinkTv!!.setTypeface(TypefaceHelper.get(this, menuTextFont)) binding.menus.menuOpenWithTv.setTypeface(TypefaceHelper.get(this, menuTextFont))
menuCopyLinkTv!!.setTextColor(menuTextColor) binding.menus.menuOpenWithTv.setTextColor(menuTextColor)
menuCopyLinkTv!!.setPadding( binding.menus.menuOpenWithTv.setPadding(
menuTextPaddingLeft.toInt(),
0,
menuTextPaddingRight.toInt(),
0
)
menuOpenWith!!.visibility = if (showMenuOpenWith) View.VISIBLE else View.GONE
menuOpenWith!!.setBackgroundResource(menuSelector)
menuOpenWith!!.gravity = menuTextGravity
menuOpenWithTv!!.setText(stringResOpenWith)
menuOpenWithTv!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, menuTextSize)
menuOpenWithTv!!.setTypeface(TypefaceHelper.get(this, menuTextFont))
menuOpenWithTv!!.setTextColor(menuTextColor)
menuOpenWithTv!!.setPadding(
menuTextPaddingLeft.toInt(), menuTextPaddingLeft.toInt(),
0, 0,
menuTextPaddingRight.toInt(), menuTextPaddingRight.toInt(),
@ -1204,7 +1165,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
hideMenu() hideMenu()
} }
menuBackground!!.addView( binding.menus.menuBackground.addView(
customMenuRoot, customMenuRoot,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT ViewGroup.LayoutParams.WRAP_CONTENT
@ -1219,13 +1180,9 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
protected fun buildWebChromeClient(): WebChromeClient { protected fun buildWebChromeClient(): WebChromeClient {
// Initialize the VideoEnabledWebChromeClient and set event handlers // Initialize the VideoEnabledWebChromeClient and set event handlers
val nonVideoLayout: View? = webLayout // Your own view, read class comments val nonVideoLayout: View? = binding.webLayout // Your own view, read class comments
val videoLayout = val videoLayout = binding.videoLayout
findViewById<View>(R.id.videoLayout) as ViewGroup // Your own view, read class comments val loadingView = layoutInflater.inflate(R.layout.view_loading_video, null) // Your own view, read class comments
val loadingView = layoutInflater.inflate(
R.layout.view_loading_video,
null
) // Your own view, read class comments
val webChromeClient = MyWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) val webChromeClient = MyWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView)
webChromeClient.setOnToggledFullscreen { fullscreen -> // Your code to handle the full-screen change, for example showing and hiding the title bar. Example: webChromeClient.setOnToggledFullscreen { fullscreen -> // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
if (fullscreen) { if (fullscreen) {
@ -1294,7 +1251,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
} }
protected val maxWidth: Int protected val maxWidth: Int
get() = if (forward!!.visibility == View.VISIBLE) { get() = if (binding.toolbarContent.forward.visibility == View.VISIBLE) {
DisplayUtil.width - UnitConverter.dpToPx(100) DisplayUtil.width - UnitConverter.dpToPx(100)
} else { } else {
DisplayUtil.width - UnitConverter.dpToPx(52) DisplayUtil.width - UnitConverter.dpToPx(52)
@ -1348,13 +1305,13 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
// } // }
} }
lateinit var awesomeWebViewBinding: AwesomeWebViewBinding lateinit var binding: AwesomeWebViewBinding
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
initializeOptions() initializeOptions()
awesomeWebViewBinding = AwesomeWebViewBinding.inflate(layoutInflater) binding = AwesomeWebViewBinding.inflate(layoutInflater)
setContentView(awesomeWebViewBinding.root) setContentView(binding.root)
bindViews() bindViews()
layoutViews() layoutViews()
initializeViews() initializeViews()
@ -1369,7 +1326,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
if (webChromeClient is MyWebChromeClient && (webChromeClient as MyWebChromeClient).onBackPressed()) { if (webChromeClient is MyWebChromeClient && (webChromeClient as MyWebChromeClient).onBackPressed()) {
return return
} }
if (menuLayout!!.visibility == View.VISIBLE) { if (binding.menus.menuLayout.visibility == View.VISIBLE) {
hideMenu() hideMenu()
} else if (backPressToClose || !webView!!.canGoBack()) { } else if (backPressToClose || !webView!!.canGoBack()) {
exitActivity() exitActivity()
@ -1427,7 +1384,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
ClipboardManagerUtil.text = webView!!.url ClipboardManagerUtil.text = webView!!.url
val snackbar = Snackbar.make( val snackbar = Snackbar.make(
coordinatorLayout!!, getString(stringResCopiedToClipboard), binding.coordinatorLayout, getString(stringResCopiedToClipboard),
Snackbar.LENGTH_LONG Snackbar.LENGTH_LONG
) )
val snackbarView = snackbar.view val snackbarView = snackbar.view
@ -1466,20 +1423,20 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
} }
protected fun showMenu() { protected fun showMenu() {
menuLayout!!.visibility = View.VISIBLE binding.menus.menuLayout.visibility = View.VISIBLE
val popupAnim = AnimationUtils.loadAnimation(this, R.anim.popup_flyout_show) val popupAnim = AnimationUtils.loadAnimation(this, R.anim.popup_flyout_show)
shadowLayout!!.startAnimation(popupAnim) binding.menus.shadowLayout.startAnimation(popupAnim)
} }
protected fun hideMenu() { protected fun hideMenu() {
val popupAnim = AnimationUtils.loadAnimation(this, R.anim.popup_flyout_hide) val popupAnim = AnimationUtils.loadAnimation(this, R.anim.popup_flyout_hide)
shadowLayout!!.startAnimation(popupAnim) binding.menus.shadowLayout.startAnimation(popupAnim)
popupAnim.setAnimationListener(object : Animation.AnimationListener { popupAnim.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) { override fun onAnimationStart(animation: Animation) {
} }
override fun onAnimationEnd(animation: Animation) { override fun onAnimationEnd(animation: Animation) {
menuLayout!!.visibility = View.GONE binding.menus.menuLayout.visibility = View.GONE
} }
override fun onAnimationRepeat(animation: Animation) { override fun onAnimationRepeat(animation: Animation) {
@ -1499,10 +1456,10 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
DisplayUtil.width - UnitConverter.dpToPx(48) * 2 DisplayUtil.width - UnitConverter.dpToPx(48) * 2
} }
title!!.maxWidth = maxWidth binding.toolbarContent.title.maxWidth = maxWidth
urlTv!!.maxWidth = maxWidth binding.toolbarContent.url.maxWidth = maxWidth
title!!.requestLayout() binding.toolbarContent.title.requestLayout()
urlTv!!.requestLayout() binding.toolbarContent.url.requestLayout()
} }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
@ -1540,7 +1497,6 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
} }
if (msg.what == MSG_CLICK_ON_WEBVIEW) { if (msg.what == MSG_CLICK_ON_WEBVIEW) {
val hitTestResult = webView!!.hitTestResult val hitTestResult = webView!!.hitTestResult
// 如果是图片类型或者是带有图片链接的类型
if (hitTestResult.type == HitTestResult.IMAGE_TYPE || if (hitTestResult.type == HitTestResult.IMAGE_TYPE ||
hitTestResult.type == HitTestResult.SRC_IMAGE_ANCHOR_TYPE hitTestResult.type == HitTestResult.SRC_IMAGE_ANCHOR_TYPE
) { ) {
@ -1563,7 +1519,7 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
BroadCastManager.onProgressChanged(this@AwesomeWebViewActivity, key, progress) BroadCastManager.onProgressChanged(this@AwesomeWebViewActivity, key, progress)
if (progress == 100) progress = 0 if (progress == 100) progress = 0
progressBar!!.progress = progress binding.progressBar.progress = progress
} }
override fun onReceivedTitle(view: WebView, title: String) { override fun onReceivedTitle(view: WebView, title: String) {
@ -1832,20 +1788,20 @@ class AwesomeWebViewActivity : AppCompatActivity(), View.OnClickListener,
override fun onPageFinished(view: WebView, url: String) { override fun onPageFinished(view: WebView, url: String) {
BroadCastManager.onPageFinished(this@AwesomeWebViewActivity, key, url) BroadCastManager.onPageFinished(this@AwesomeWebViewActivity, key, url)
if (updateTitleFromHtml) title!!.text = view.title if (updateTitleFromHtml) binding.toolbarContent.title.text = view.title
urlTv!!.text = UrlParser.getHost(url) binding.toolbarContent.url.text = UrlParser.getHost(url)
requestCenterLayout() requestCenterLayout()
if (view.canGoBack() || view.canGoForward()) { if (view.canGoBack() || view.canGoForward()) {
back!!.visibility = if (showIconBack) View.VISIBLE else View.GONE binding.toolbarContent.back.visibility = if (showIconBack) View.VISIBLE else View.GONE
forward!!.visibility = if (showIconForward) View.VISIBLE else View.GONE binding.toolbarContent.forward.visibility = if (showIconForward) View.VISIBLE else View.GONE
back!!.isEnabled = binding.toolbarContent.back.isEnabled =
!disableIconBack && (if (rtl) view.canGoForward() else view.canGoBack()) !disableIconBack && (if (rtl) view.canGoForward() else view.canGoBack())
forward!!.isEnabled = binding.toolbarContent.forward.isEnabled =
!disableIconForward && (if (rtl) view.canGoBack() else view.canGoForward()) !disableIconForward && (if (rtl) view.canGoBack() else view.canGoForward())
} else { } else {
back!!.visibility = View.GONE binding.toolbarContent.back.visibility = View.GONE
forward!!.visibility = View.GONE binding.toolbarContent.forward.visibility = View.GONE
} }
if (injectJavaScript != null) { if (injectJavaScript != null) {

View File

@ -34,7 +34,8 @@
app:contentInsetRight="0dp" app:contentInsetRight="0dp"
app:contentInsetStart="0dp"> app:contentInsetStart="0dp">
<include layout="@layout/toolbar_content" /> <include
android:id="@+id/toolbar_content" layout="@layout/toolbar_content" />
</androidx.appcompat.widget.Toolbar> </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
@ -54,7 +55,9 @@
android:indeterminate="false" android:indeterminate="false"
android:progressDrawable="@drawable/progress_drawable" /> android:progressDrawable="@drawable/progress_drawable" />
<include layout="@layout/menus" /> <include
android:id="@+id/menus"
layout="@layout/menus" />
<!-- View where the video will be shown when video goes fullscreen --> <!-- View where the video will be shown when video goes fullscreen -->
<RelativeLayout <RelativeLayout

View File

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout>
<data>
</data>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menuLayout" android:id="@+id/menuLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -143,3 +148,4 @@
</LinearLayout> </LinearLayout>
</kr.lunaticbum.awesomewebview.views.ShadowLayout> </kr.lunaticbum.awesomewebview.views.ShadowLayout>
</RelativeLayout> </RelativeLayout>
</layout>