Skip to content

Commit

Permalink
Merge pull request #43 from portto/feature/support_web_sdk_social_login
Browse files Browse the repository at this point in the history
Support Web SDK social login in app browser
  • Loading branch information
imjacklai authored Jun 5, 2023
2 parents 5c94377 + b46e3a3 commit ac104bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
76 changes: 42 additions & 34 deletions fcl/src/main/java/com/portto/fcl/webview/FclWebView.kt
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
package com.portto.fcl.webview

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.os.Message
import android.util.AttributeSet
import android.webkit.WebSettings
import android.webkit.WebView
import com.portto.fcl.BuildConfig

@SuppressLint("SetJavaScriptEnabled")
internal class FclWebView : WebView {
private var callback: WebViewCallback? = null

constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
super(context, attrs, defStyleAttr)

init {
with(settings) {
loadsImagesAutomatically = true
javaScriptEnabled = true
webViewClient = WebViewClient()
webChromeClient = WebChromeClient()
domStorageEnabled = true
}
settings.setup()
webChromeClient = WebChromeClient()
setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
}

fun setWebViewCallback(callback: WebViewCallback?) {
this.callback = callback
}

private inner class WebChromeClient : android.webkit.WebChromeClient() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (view.progress == newProgress) {
callback?.onProgressChange(view.progress / 100f)
}
}

override fun onReceivedTitle(view: WebView?, title: String?) {
super.onReceivedTitle(view, title)
callback?.onTitleChange(title.orEmpty())
}
override fun onCreateWindow(
view: WebView?,
isDialog: Boolean,
isUserGesture: Boolean,
resultMsg: Message?
): Boolean = onCreateWebWindow(resultMsg)
}

private inner class WebViewClient : android.webkit.WebViewClient() {
private fun onCreateWebWindow(resultMsg: Message?): Boolean {
val webView = WebView(context)
webView.settings.setup()

override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
webView.webViewClient = object : android.webkit.WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
// Make WebView height match parent
val displayRectangle = Rect()
(context as? Activity)?.window?.decorView?.getWindowVisibleDisplayFrame(displayRectangle)
view?.layoutParams = view?.layoutParams?.apply {
height = displayRectangle.height()
}
}
}

override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
super.doUpdateVisitedHistory(view, url, isReload)
callback?.onPageUrlChange(url.orEmpty(), isReload)
webView.webChromeClient = object : android.webkit.WebChromeClient() {
override fun onCloseWindow(window: WebView?) {
super.onCloseWindow(window)
this@FclWebView.removeView(window)
}
}

this@FclWebView.addView(webView)
val transport = resultMsg?.obj as WebView.WebViewTransport
transport.webView = webView
resultMsg.sendToTarget()
return true
}

interface WebViewCallback {
fun onScrollChange(scrollY: Int, offset: Int)
fun onProgressChange(progress: Float)
fun onTitleChange(title: String)
fun onPageUrlChange(url: String, isReload: Boolean)
private fun WebSettings.setup() {
javaScriptEnabled = true
domStorageEnabled = true
setSupportMultipleWindows(true)
// Remove "wv" from user agent to make Google login work
userAgentString = userAgentString.replace(oldValue = "; wv", newValue = "")
}
}
6 changes: 1 addition & 5 deletions fcl/src/main/java/com/portto/fcl/webview/WebViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebSettings
import androidx.appcompat.app.AppCompatActivity

internal class WebViewActivity : AppCompatActivity() {
Expand All @@ -17,12 +16,9 @@ internal class WebViewActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
instance = this
setContentView(webView)
supportActionBar?.hide()
webView.loadUrl(url)
FclWebViewLifecycle.onWebViewOpen(url)
webView.settings.userAgentString = WebSettings.getDefaultUserAgent(this).replace("; wv", "")

actionBar?.hide()
supportActionBar?.hide()
}

override fun onDestroy() {
Expand Down

0 comments on commit ac104bb

Please sign in to comment.