Skip to content

Commit

Permalink
feat(android-sdk): support copy text for WebView (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun authored Oct 18, 2023
1 parent de791ff commit 4a8d8f3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class LogtoWebViewAuthActivity : AppCompatActivity() {
webView = WebView(this).apply {
settings.javaScriptEnabled = true
settings.cacheMode = WebSettings.LOAD_NO_CACHE

val polyfill = LogtoWebViewPolyfill(hostActivity = this@LogtoWebViewAuthActivity)
addJavascriptInterface(polyfill, LogtoWebViewPolyfill.NAME)

val socialHandler = LogtoWebViewSocialHandler(
webView = this,
hostActivity = this@LogtoWebViewAuthActivity,
Expand All @@ -34,9 +38,12 @@ class LogtoWebViewAuthActivity : AppCompatActivity() {
socialHandler,
LogtoWebViewSocialHandler.NAME,
)

val injectScript = polyfill.getInjectScript() + socialHandler.getInjectScript()

webViewClient = LogtoWebViewAuthClient(
hostActivity = this@LogtoWebViewAuthActivity,
injectScript = socialHandler.getInjectSocialScript(),
injectScript = injectScript,
)
}
webView.loadUrl(uri)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.logto.sdk.android.auth.logto

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.webkit.JavascriptInterface

class LogtoWebViewPolyfill(private val hostActivity: Activity) {
companion object {
const val NAME = "Polyfill"
}

fun getInjectScript() = """
window.addEventListener("load", () => {
window.navigator.clipboard = {
writeText: (text) => window.$NAME.writeText(text),
};
});
""".trimIndent()

@JavascriptInterface
fun writeText(text: String) {
val clipboard = hostActivity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newPlainText(text, text))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LogtoWebViewSocialHandler(
}

// Note: the `universal` will always be true for it is supported by the Android OS.
fun getInjectSocialScript() = """
fun getInjectScript() = """
window.logtoNativeSdk = {
platform: 'android',
getPostMessage: () => (data) => window.$NAME.postMessage(JSON.stringify(data)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class LogtoWebViewSocialHandlerTest {
SocialSessionHelper.getSupportedNativeConnectorTargets()
} returns mutableListOf("wechat", "alipay")

val injectSocialScript = logtoWebViewSocialHandler.getInjectSocialScript()
val injectScript = logtoWebViewSocialHandler.getInjectScript()
verify {
SocialSessionHelper.getSupportedNativeConnectorTargets()
}

assertThat(injectSocialScript)
assertThat(injectScript)
.isEqualTo(
"""
window.logtoNativeSdk = {
Expand Down

0 comments on commit 4a8d8f3

Please sign in to comment.