Skip to content

Commit

Permalink
added exception handling for when browser doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Oct 17, 2023
1 parent 7588700 commit 584e258
Showing 1 changed file with 60 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ internal class EngineWebView @JvmOverloads constructor(
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)
try {
webView = WebView(context)
this.addView(webView)
} catch (e: Exception) {
Log.e(GIST_TAG, "Error while creating EngineWebView: ${e.message}")

Check warning on line 36 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#L32-L36

Added lines #L32 - L36 were not covered by tests
}
}

@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -38,40 +43,60 @@ internal class EngineWebView @JvmOverloads constructor(
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 47 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#L46-L47

Added lines #L46 - L47 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 {
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 57 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#L50-L57

Added lines #L50 - L57 were not covered by tests

it.webViewClient = object : WebViewClient() {

Check warning on line 59 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#L59

Added line #L59 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 62 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#L61-L62

Added lines #L61 - L62 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 75 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#L75

Added line #L75 was not covered by tests

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

Check warning on line 83 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#L83

Added line #L83 was not covered by tests

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

Check warning on line 91 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#L91

Added line #L91 was not covered by tests

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

Check warning on line 99 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#L99

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

0 comments on commit 584e258

Please sign in to comment.