Skip to content

Commit ed12a62

Browse files
authored
Add native bridge functionality to handle "copy to clipboard" (#70)
1 parent d5ff696 commit ed12a62

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

app/src/main/java/to/dev/dev_android/util/AndroidWebViewBridge.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package to.dev.dev_android.util
22

3+
import android.content.ClipData
4+
import android.content.ClipboardManager
5+
import android.content.Context
36
import android.util.Log
47
import android.webkit.JavascriptInterface
58

69
/**
710
* This class currently is empty because more methods would be added to it
811
* when new bridge functionalities are added.
912
*/
10-
class AndroidWebViewBridge {
13+
class AndroidWebViewBridge(private val mContext: Context) {
14+
1115
/**
1216
* Every method that has to be accessed from web-view needs to be marked with
1317
* `@JavascriptInterface`.
@@ -17,4 +21,11 @@ class AndroidWebViewBridge {
1721
fun logError(errorTag: String, errorMessage: String) {
1822
Log.e(errorTag, errorMessage)
1923
}
24+
25+
@JavascriptInterface
26+
fun copyToClipboard(text: String) {
27+
val clipboard = mContext.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
28+
val clip = ClipData.newPlainText("clipboard", text)
29+
clipboard?.primaryClip = clip
30+
}
2031
}

app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ import android.annotation.SuppressLint
44
import android.app.Activity
55
import android.content.Intent
66
import android.net.Uri
7+
import android.os.Build
78
import android.os.Bundle
89
import android.view.View
910
import android.webkit.ValueCallback
11+
import android.webkit.WebView
1012
import to.dev.dev_android.R
1113
import to.dev.dev_android.base.BuildConfig
1214
import to.dev.dev_android.base.activity.BaseActivity
1315
import to.dev.dev_android.databinding.ActivityMainBinding
1416
import to.dev.dev_android.util.AndroidWebViewBridge
1517

1618
class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.CustomListener {
17-
private val webViewBridge: AndroidWebViewBridge = AndroidWebViewBridge()
19+
private val webViewBridge: AndroidWebViewBridge = AndroidWebViewBridge(this)
1820

1921
private var filePathCallback: ValueCallback<Array<Uri>>? = null
2022

@@ -48,9 +50,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
4850

4951
@SuppressLint("SetJavaScriptEnabled")
5052
private fun setWebViewSettings() {
53+
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
54+
WebView.setWebContentsDebuggingEnabled(true)
55+
}
56+
5157
binding.webView.settings.javaScriptEnabled = true
5258
binding.webView.settings.domStorageEnabled = true
53-
binding.webView.addJavascriptInterface(webViewBridge, "androidWebViewBridge")
59+
binding.webView.settings.userAgentString = BuildConfig.userAgent
60+
61+
binding.webView.addJavascriptInterface(webViewBridge, "AndroidBridge")
5462
binding.webView.webViewClient = CustomWebViewClient(this@MainActivity) {
5563
binding.splash.visibility = View.GONE
5664
}

buildSrc/src/main/kotlin/Android.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ fun Project.configureBuildConfig(android: BaseExtension) {
4040
android.buildTypes.all {
4141
resValue("string", "baseUrl", extra.string("chrome.baseUrl"))
4242
buildConfigString("baseUrl", extra.string("chrome.baseUrl"))
43+
resValue("string", "userAgent", extra.string("chrome.userAgent"))
44+
buildConfigString("userAgent", extra.string("chrome.userAgent"))
4345
}
4446
}
4547

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## dev.to configurations
22
chrome.baseUrl=https://dev.to/
3+
chrome.userAgent=DEV-Native-android
4+
5+
# Android release configurations
36
android.targetSdkVersion=28
47
android.compileSdkVersion=28
58
android.minSdkVersion=18

0 commit comments

Comments
 (0)