Skip to content

Kotlin WebView

Will Morgan edited this page Sep 24, 2021 · 2 revisions
package com.iproov.webview

import android.Manifest
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.webkit.PermissionRequest
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivityKotlin : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        WebView.setWebContentsDebuggingEnabled(true)

        webView.settings.javaScriptEnabled = true
        webView.settings.domStorageEnabled = true
        webView.settings.mediaPlaybackRequiresUserGesture = false
        webView.webViewClient = WebViewClient()
        webView.webChromeClient = object : WebChromeClient() {
            var customView: View? = null
            var callback: CustomViewCallback? = null
            var originalOrientation: Int = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
            var originalVisibility: Int = View.INVISIBLE
            override fun onPermissionRequest(request: PermissionRequest?) {
                request?.grant(request.resources);
            }

            override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
                if (customView != null) {
                    onHideCustomView()
                    return
                }
                customView = view
                originalVisibility = window.decorView.systemUiVisibility
                originalOrientation = requestedOrientation
                (window.decorView as FrameLayout).addView(this.customView, FrameLayout.LayoutParams(-1, -1))
                window.decorView.systemUiVisibility = 3846 or View.SYSTEM_UI_FLAG_LAYOUT_STABLE

            }

            override fun onHideCustomView() {
                (window.decorView as FrameLayout).removeView(customView)
                customView = null
                window.decorView.systemUiVisibility = originalVisibility
                requestedOrientation = originalOrientation
                callback?.onCustomViewHidden()
                callback = null
            }
        }

        val cameraPermission: Int = ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.CAMERA
        )

        if (cameraPermission == PackageManager.PERMISSION_GRANTED)
            webView.loadUrl("https://demo.iproov.com/")
        else
            requestPermissions()
    }

    override fun onBackPressed() {

        if (webView.canGoBack()) {
            webView.goBack()
        } else {
            super.onBackPressed()
        }
    }

    fun requestPermissions() {

        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.CAMERA),
            12345
        )
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == 12345 && grantResults.size > 0) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                webView.loadUrl("https://demo.iproov.com/")
            }
        }
    }

}
Clone this wiki locally