Advertisement
Guest User

MainActivity WebView

a guest
May 3rd, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.49 KB | None | 0 0
  1. import ...
  2.  
  3. class MainActivity : AppCompatActivity() {
  4.     private val URL = "https://mydomain.com/"
  5.     private val DOMAIN = "mydomain.com"
  6.     private var doubleBackToExitPressedOnce = false
  7.  
  8.     @SuppressLint("SetJavaScriptEnabled")
  9.     override fun onCreate(savedInstanceState: Bundle?) {
  10.         super.onCreate(savedInstanceState)
  11.         setContentView(R.layout.activity_main)
  12.         webView.settings.javaScriptEnabled = true
  13.         webView.settings.domStorageEnabled = true
  14.         val SDK_INT = Build.VERSION.SDK_INT
  15.         if (SDK_INT > 16) {
  16.             webView.settings.mediaPlaybackRequiresUserGesture = false
  17.         }
  18.  
  19.         webView.webChromeClient = object : WebChromeClient(){
  20.             // Popup permissions for cam + mic
  21.             override fun onPermissionRequest(request: PermissionRequest) {
  22.                 runOnUiThread {
  23.                     request.grant(request.resources)
  24.                 }
  25.             }
  26.         }
  27.  
  28.         WebView.setWebContentsDebuggingEnabled(true)
  29.         webView.webViewClient = object : WebViewClient() {
  30.             override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
  31.                 super.onPageStarted(view, url, favicon)
  32.                 webView.visibility = View.VISIBLE
  33.                 progressBar.visibility = View.GONE
  34.             }
  35.  
  36.             override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
  37.                 if (AppUtils.checkInternetConnection(this@MainActivity)) {
  38.                     if (url.startsWith("mailto:")) {
  39.                         val mailTo = MailTo.parse(url)
  40.                         val intent = Intent(Intent.ACTION_SEND)
  41.                         intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(mailTo.to))
  42.                         intent.putExtra(Intent.EXTRA_TEXT, mailTo.body)
  43.                         intent.putExtra(Intent.EXTRA_SUBJECT, mailTo.subject)
  44.                         intent.putExtra(Intent.EXTRA_CC, mailTo.cc)
  45.                         intent.type = "message/rfc822"
  46.                         startActivity(intent)
  47.                         view.reload()
  48.                         return true
  49.  
  50.                     } else if (!url.contains(DOMAIN)) {
  51.                         val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
  52.                         startActivity(intent)
  53.                         return true
  54.                     } else {
  55.                         view.loadUrl(url)
  56.                         return true
  57.                     }
  58.                 } else {
  59.                     return false
  60.                 }
  61.             }
  62.         }
  63.  
  64.         if (savedInstanceState == null) {
  65.             webView.loadUrl(URL)
  66.         }
  67.     }
  68.  
  69.     override fun onSaveInstanceState(outState: Bundle) {
  70.         super.onSaveInstanceState(outState)
  71.         webView.saveState(outState)
  72.     }
  73.  
  74.     override fun onRestoreInstanceState(savedInstanceState: Bundle) {
  75.         super.onRestoreInstanceState(savedInstanceState)
  76.         webView.restoreState(savedInstanceState)
  77.     }
  78.  
  79.     override fun onBackPressed() {
  80.         if (webView.canGoBack()) {
  81.             webView.goBack()
  82.         } else {
  83.             if (doubleBackToExitPressedOnce) {
  84.                 super.onBackPressed()
  85.                 return
  86.             }
  87.  
  88.             this.doubleBackToExitPressedOnce = true
  89.             Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show()
  90.             Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000)
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement