-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from portto/feature/support_web_sdk_social_login
Support Web SDK social login in app browser
- Loading branch information
Showing
2 changed files
with
43 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters