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

fix: added exception handling for when browser doesn't exist #271

Merged
merged 2 commits into from
Oct 17, 2023
Merged
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 @@ -25,11 +25,17 @@
var listener: EngineWebViewListener? = null
private var timer: Timer? = null
private var timerTask: TimerTask? = null
private var webView: WebView = WebView(context)
private var webView: WebView? = null
private var elapsedTimer: ElapsedTimer = ElapsedTimer()

init {
this.addView(webView)
// exception handling is required for webview in-case webview is not supported in the device
try {
webView = WebView(context)
this.addView(webView)
} catch (e: Exception) {
Log.e(GIST_TAG, "Error while creating EngineWebView: ${e.message}")

Check warning on line 37 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L33-L37

Added lines #L33 - L37 were not covered by tests
mrehan27 marked this conversation as resolved.
Show resolved Hide resolved
}
}

@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -38,40 +44,60 @@
val jsonString = Gson().toJson(configuration)
encodeToBase64(jsonString)?.let { options ->
elapsedTimer.start("Engine render for message: ${configuration.messageId}")
val messageUrl = "${GistSdk.gistEnvironment.getGistRendererUrl()}/index.html?options=$options"
val messageUrl =
"${GistSdk.gistEnvironment.getGistRendererUrl()}/index.html?options=$options"

Check warning on line 48 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L47-L48

Added lines #L47 - L48 were not covered by tests
Log.i(GIST_TAG, "Rendering message with URL: $messageUrl")
webView.loadUrl(messageUrl)
webView.settings.javaScriptEnabled = true
webView.settings.allowFileAccess = true
webView.settings.allowContentAccess = true
webView.settings.domStorageEnabled = true
webView.settings.textZoom = 100
webView.setBackgroundColor(Color.TRANSPARENT)
webView.addJavascriptInterface(EngineWebViewInterface(this), "appInterface")

webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String?) {
view.loadUrl("javascript:window.parent.postMessage = function(message) {window.appInterface.postMessage(JSON.stringify(message))}")
}

override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return !url.startsWith("https://code.gist.build")
}

override fun onReceivedError(view: WebView?, errorCod: Int, description: String, failingUrl: String?) {
listener?.error()
}

override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) {
listener?.error()
}

override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
listener?.error()
}

override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
listener?.error()
webView?.let {
mrehan27 marked this conversation as resolved.
Show resolved Hide resolved
it.loadUrl(messageUrl)
it.settings.javaScriptEnabled = true
it.settings.allowFileAccess = true
it.settings.allowContentAccess = true
it.settings.domStorageEnabled = true
it.settings.textZoom = 100
it.setBackgroundColor(Color.TRANSPARENT)
it.addJavascriptInterface(EngineWebViewInterface(this), "appInterface")

Check warning on line 58 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L51-L58

Added lines #L51 - L58 were not covered by tests

it.webViewClient = object : WebViewClient() {

Check warning on line 60 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L60

Added line #L60 was not covered by tests
override fun onPageFinished(view: WebView, url: String?) {
view.loadUrl("javascript:window.parent.postMessage = function(message) {window.appInterface.postMessage(JSON.stringify(message))}")
}

Check warning on line 63 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L62-L63

Added lines #L62 - L63 were not covered by tests

override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return !url.startsWith("https://code.gist.build")
}

override fun onReceivedError(
view: WebView?,
errorCod: Int,
description: String,
failingUrl: String?
) {
listener?.error()
}

Check warning on line 76 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L76

Added line #L76 was not covered by tests

override fun onReceivedHttpError(
view: WebView?,
request: WebResourceRequest?,
errorResponse: WebResourceResponse?
) {
listener?.error()
}

Check warning on line 84 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L84

Added line #L84 was not covered by tests

override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
listener?.error()
}

Check warning on line 92 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L92

Added line #L92 was not covered by tests

override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
listener?.error()
}

Check warning on line 100 in messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt

View check run for this annotation

Codecov / codecov/patch

messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/engine/EngineWebView.kt#L100

Added line #L100 was not covered by tests
}
}
} ?: run {
Expand Down
Loading