Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android-sdk): support copy text for WebView #219

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

Check warning on line 25 in android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoWebViewPolyfill.kt

View check run for this annotation

Codecov / codecov/patch

android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoWebViewPolyfill.kt#L25

Added line #L25 was not covered by tests
}
}
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
Loading