Skip to content
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
@@ -1,13 +1,17 @@
package to.dev.dev_android.util

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.util.Log
import android.webkit.JavascriptInterface

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

/**
* Every method that has to be accessed from web-view needs to be marked with
* `@JavascriptInterface`.
Expand All @@ -17,4 +21,11 @@ class AndroidWebViewBridge {
fun logError(errorTag: String, errorMessage: String) {
Log.e(errorTag, errorMessage)
}

@JavascriptInterface
fun copyToClipboard(text: String) {
val clipboard = mContext.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
val clip = ClipData.newPlainText("clipboard", text)
clipboard?.primaryClip = clip
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.View
import android.webkit.ValueCallback
import android.webkit.WebView
import to.dev.dev_android.R
import to.dev.dev_android.base.BuildConfig
import to.dev.dev_android.base.activity.BaseActivity
import to.dev.dev_android.databinding.ActivityMainBinding
import to.dev.dev_android.util.AndroidWebViewBridge

class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.CustomListener {
private val webViewBridge: AndroidWebViewBridge = AndroidWebViewBridge()
private val webViewBridge: AndroidWebViewBridge = AndroidWebViewBridge(this)

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

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

@SuppressLint("SetJavaScriptEnabled")
private fun setWebViewSettings() {
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true)
}

binding.webView.settings.javaScriptEnabled = true
binding.webView.settings.domStorageEnabled = true
binding.webView.addJavascriptInterface(webViewBridge, "androidWebViewBridge")
binding.webView.settings.userAgentString = BuildConfig.userAgent

binding.webView.addJavascriptInterface(webViewBridge, "AndroidBridge")
binding.webView.webViewClient = CustomWebViewClient(this@MainActivity) {
binding.splash.visibility = View.GONE
}
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ fun Project.configureBuildConfig(android: BaseExtension) {
android.buildTypes.all {
resValue("string", "baseUrl", extra.string("chrome.baseUrl"))
buildConfigString("baseUrl", extra.string("chrome.baseUrl"))
resValue("string", "userAgent", extra.string("chrome.userAgent"))
buildConfigString("userAgent", extra.string("chrome.userAgent"))
}
}

Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## dev.to configurations
chrome.baseUrl=https://dev.to/
chrome.userAgent=DEV-Native-android

# Android release configurations
android.targetSdkVersion=28
android.compileSdkVersion=28
android.minSdkVersion=18
Expand Down