Skip to content

Commit

Permalink
Implement webview error handling for android(inactive)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rokt33r committed Jul 21, 2021
1 parent c475c65 commit 7aba2a3
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions android/app/src/main/java/com/boostio/boostnote/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@

package com.boostio.boostnote2021

import android.content.ComponentName
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.webkit.*
import android.webkit.WebView
import androidx.annotation.RequiresApi
//import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity

Expand All @@ -31,8 +32,9 @@ class MainActivity : AppCompatActivity() {
val ctx: AppCompatActivity = this

val authStatePreferencesKey = "com.boostio.boostnote.authState"
// val mobileBaseUrl = "http://localhost:3005"
val mobileBaseUrl = "https://m.boostnote.io"
// val mobileBaseUrl = "http://localhost:3005"
val mobileBaseUrl = "https://m.boostnote.io"
private var webviewReloadAlertDialog: AlertDialog? = null

class WebAppInterface(private val mContext: MainActivity) {

Expand All @@ -54,6 +56,26 @@ class MainActivity : AppCompatActivity() {
}
}

fun showReloadDialog(description: String?) {
if (this.webviewReloadAlertDialog != null) {
return
}
val builder = AlertDialog.Builder(this)

builder.setMessage("Choose OK to reload the app")
.setTitle("""Error $description""")
.setPositiveButton("OK", DialogInterface.OnClickListener { _, _ ->
this.webviewReloadAlertDialog = null
val view = findViewById<WebView>(R.id.webview);
view?.reload()
})
.setCancelable(false)

this.webviewReloadAlertDialog = builder.create()
this.webviewReloadAlertDialog!!.show()
}


override fun onCreate(savedInstanceState: Bundle?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
Expand All @@ -67,7 +89,31 @@ class MainActivity : AppCompatActivity() {
settings.allowContentAccess = true;
settings.domStorageEnabled = true;
settings.userAgentString = settings.userAgentString + " BoostNote-Mobile-Android"
view.webViewClient = WebViewClient()

view.webViewClient = object: WebViewClient() {
// override fun onReceivedError(
// view: WebView?,
// errorCode: Int,
// description: String?,
// failingUrl: String?
// ) {
// super.onReceivedError(view, errorCode, description, failingUrl)
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ return }
//
// this@MainActivity.showReloadDialog(description)
// }
//
// @RequiresApi(Build.VERSION_CODES.M)
// override fun onReceivedError(
// view: WebView?,
// request: WebResourceRequest?,
// error: WebResourceError?
// ) {
// super.onReceivedError(view, request, error)
//
// this@MainActivity.showReloadDialog(error?.description as String)
// }
}
view.webChromeClient = object : WebChromeClient() {
override fun onConsoleMessage(message: String, lineNumber: Int, sourceID: String) {
Log.d("WebView", "$message -- From line $lineNumber of $sourceID")
Expand All @@ -86,7 +132,6 @@ class MainActivity : AppCompatActivity() {
}

fun handleIntent(intent: Intent) {

if (
!this.resolveAuthIntent(intent)) {

Expand Down

0 comments on commit 7aba2a3

Please sign in to comment.