WebViewActivity.kt 2.1 KB
Newer Older
1
package chat.rocket.android.webview.ui
2 3 4 5 6

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
7
import androidx.appcompat.app.AppCompatActivity
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
8
import android.webkit.WebView
9 10 11 12 13 14 15 16 17 18 19 20 21
import android.webkit.WebViewClient
import chat.rocket.android.R
import kotlinx.android.synthetic.main.activity_web_view.*
import kotlinx.android.synthetic.main.app_bar.*

fun Context.webViewIntent(webPageUrl: String): Intent {
    return Intent(this, WebViewActivity::class.java).apply {
        putExtra(INTENT_WEB_PAGE_URL, webPageUrl)
    }
}

private const val INTENT_WEB_PAGE_URL = "web_page_url"

22
// Simple WebView to load URL.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
class WebViewActivity : AppCompatActivity() {
    private lateinit var webPageUrl: String

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

        webPageUrl = intent.getStringExtra(INTENT_WEB_PAGE_URL)
        requireNotNull(webPageUrl) { "no web_page_url provided in Intent extras" }

        setupToolbar()
    }

    override fun onResume() {
        super.onResume()
        setupWebView()
    }

    override fun onBackPressed() {
        if (web_view.canGoBack()) {
            web_view.goBack()
        } else {
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
45
            finishActivity()
46 47 48 49 50 51 52
        }
    }

    private fun setupToolbar() {
        toolbar.title = getString(R.string.title_legal_terms)
        toolbar.setNavigationIcon(R.drawable.ic_close_white_24dp)
        toolbar.setNavigationOnClickListener {
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
53
            finishActivity()
54 55 56 57 58 59
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun setupWebView() {
        web_view.settings.javaScriptEnabled = true
60 61 62 63 64 65
        web_view.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                view_loading.hide()
            }
        }
66 67
        web_view.loadUrl(webPageUrl)
    }
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
68 69 70 71 72

    private fun finishActivity() {
        super.onBackPressed()
        overridePendingTransition(R.anim.hold, R.anim.slide_down)
    }
73
}