Skip to content

Commit 4a8d8f3

Browse files
authored
feat(android-sdk): support copy text for WebView (#219)
1 parent de791ff commit 4a8d8f3

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoWebViewAuthActivity.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class LogtoWebViewAuthActivity : AppCompatActivity() {
2626
webView = WebView(this).apply {
2727
settings.javaScriptEnabled = true
2828
settings.cacheMode = WebSettings.LOAD_NO_CACHE
29+
30+
val polyfill = LogtoWebViewPolyfill(hostActivity = this@LogtoWebViewAuthActivity)
31+
addJavascriptInterface(polyfill, LogtoWebViewPolyfill.NAME)
32+
2933
val socialHandler = LogtoWebViewSocialHandler(
3034
webView = this,
3135
hostActivity = this@LogtoWebViewAuthActivity,
@@ -34,9 +38,12 @@ class LogtoWebViewAuthActivity : AppCompatActivity() {
3438
socialHandler,
3539
LogtoWebViewSocialHandler.NAME,
3640
)
41+
42+
val injectScript = polyfill.getInjectScript() + socialHandler.getInjectScript()
43+
3744
webViewClient = LogtoWebViewAuthClient(
3845
hostActivity = this@LogtoWebViewAuthActivity,
39-
injectScript = socialHandler.getInjectSocialScript(),
46+
injectScript = injectScript,
4047
)
4148
}
4249
webView.loadUrl(uri)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.logto.sdk.android.auth.logto
2+
3+
import android.app.Activity
4+
import android.content.ClipData
5+
import android.content.ClipboardManager
6+
import android.content.Context
7+
import android.webkit.JavascriptInterface
8+
9+
class LogtoWebViewPolyfill(private val hostActivity: Activity) {
10+
companion object {
11+
const val NAME = "Polyfill"
12+
}
13+
14+
fun getInjectScript() = """
15+
window.addEventListener("load", () => {
16+
window.navigator.clipboard = {
17+
writeText: (text) => window.$NAME.writeText(text),
18+
};
19+
});
20+
""".trimIndent()
21+
22+
@JavascriptInterface
23+
fun writeText(text: String) {
24+
val clipboard = hostActivity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
25+
clipboard.setPrimaryClip(ClipData.newPlainText(text, text))
26+
}
27+
}

android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoWebViewSocialHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LogtoWebViewSocialHandler(
2323
}
2424

2525
// Note: the `universal` will always be true for it is supported by the Android OS.
26-
fun getInjectSocialScript() = """
26+
fun getInjectScript() = """
2727
window.logtoNativeSdk = {
2828
platform: 'android',
2929
getPostMessage: () => (data) => window.$NAME.postMessage(JSON.stringify(data)),

android-sdk/android/src/test/kotlin/io/logto/sdk/android/auth/logto/LogtoWebViewSocialHandlerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ class LogtoWebViewSocialHandlerTest {
4848
SocialSessionHelper.getSupportedNativeConnectorTargets()
4949
} returns mutableListOf("wechat", "alipay")
5050

51-
val injectSocialScript = logtoWebViewSocialHandler.getInjectSocialScript()
51+
val injectScript = logtoWebViewSocialHandler.getInjectScript()
5252
verify {
5353
SocialSessionHelper.getSupportedNativeConnectorTargets()
5454
}
5555

56-
assertThat(injectSocialScript)
56+
assertThat(injectScript)
5757
.isEqualTo(
5858
"""
5959
window.logtoNativeSdk = {

0 commit comments

Comments
 (0)