Skip to content

Add methods to implement native copy-to-clipboard functionality. #60

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

Merged
merged 5 commits into from
Apr 22, 2020
18 changes: 12 additions & 6 deletions app/src/main/java/to/dev/dev_android/util/AndroidWebViewBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import android.content.ClipboardManager
import android.content.Context
import android.util.Log
import android.webkit.JavascriptInterface
import android.widget.Toast
import to.dev.dev_android.BuildConfig

/**
* This class currently is empty because more methods would be added to it
* when new bridge functionalities are added.
*/
class AndroidWebViewBridge(private val mContext: Context) {

class AndroidWebViewBridge(private val context: Context) {
/**
* Every method that has to be accessed from web-view needs to be marked with
* `@JavascriptInterface`.
Expand All @@ -23,9 +24,14 @@ class AndroidWebViewBridge(private val mContext: Context) {
}

@JavascriptInterface
fun copyToClipboard(text: String) {
val clipboard = mContext.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
val clip = ClipData.newPlainText("clipboard", text)
clipboard?.primaryClip = clip
fun copyToClipboard(copyText: String) {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
val clipData = ClipData.newPlainText("DEV Community", copyText)
clipboard?.primaryClip = clipData
}

@JavascriptInterface
fun showToast(message: String) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
}