Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ...
- class MainActivity : AppCompatActivity() {
- private val URL = "https://mydomain.com/"
- private val DOMAIN = "mydomain.com"
- private var doubleBackToExitPressedOnce = false
- @SuppressLint("SetJavaScriptEnabled")
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- webView.settings.javaScriptEnabled = true
- webView.settings.domStorageEnabled = true
- val SDK_INT = Build.VERSION.SDK_INT
- if (SDK_INT > 16) {
- webView.settings.mediaPlaybackRequiresUserGesture = false
- }
- webView.webChromeClient = object : WebChromeClient(){
- // Popup permissions for cam + mic
- override fun onPermissionRequest(request: PermissionRequest) {
- runOnUiThread {
- request.grant(request.resources)
- }
- }
- }
- WebView.setWebContentsDebuggingEnabled(true)
- webView.webViewClient = object : WebViewClient() {
- override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
- super.onPageStarted(view, url, favicon)
- webView.visibility = View.VISIBLE
- progressBar.visibility = View.GONE
- }
- override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
- if (AppUtils.checkInternetConnection(this@MainActivity)) {
- if (url.startsWith("mailto:")) {
- val mailTo = MailTo.parse(url)
- val intent = Intent(Intent.ACTION_SEND)
- intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(mailTo.to))
- intent.putExtra(Intent.EXTRA_TEXT, mailTo.body)
- intent.putExtra(Intent.EXTRA_SUBJECT, mailTo.subject)
- intent.putExtra(Intent.EXTRA_CC, mailTo.cc)
- intent.type = "message/rfc822"
- startActivity(intent)
- view.reload()
- return true
- } else if (!url.contains(DOMAIN)) {
- val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
- startActivity(intent)
- return true
- } else {
- view.loadUrl(url)
- return true
- }
- } else {
- return false
- }
- }
- }
- if (savedInstanceState == null) {
- webView.loadUrl(URL)
- }
- }
- override fun onSaveInstanceState(outState: Bundle) {
- super.onSaveInstanceState(outState)
- webView.saveState(outState)
- }
- override fun onRestoreInstanceState(savedInstanceState: Bundle) {
- super.onRestoreInstanceState(savedInstanceState)
- webView.restoreState(savedInstanceState)
- }
- override fun onBackPressed() {
- if (webView.canGoBack()) {
- webView.goBack()
- } else {
- if (doubleBackToExitPressedOnce) {
- super.onBackPressed()
- return
- }
- this.doubleBackToExitPressedOnce = true
- Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show()
- Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement