Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Skip to content
- MoreToggle navigation
- M
- Mappan
- Project overview
- Repository
- Files
- Commits
- Branches
- Tags
- Contributors
- Graph
- Compare
- Issues
- 0
- Merge Requests
- 0
- CI / CD
- Operations
- Packages
- Analytics
- Wiki
- Snippets
- Settings
- Close sidebar
- Open sidebar
- Anggit Prayogo
- Mappan
- Repository
- master
- mappan
- ..
- mappan
- MainActivity.kt
- MainActivity.kt 4.46 KB
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- package id.go.pertanian.horti.mappan
- import android.annotation.SuppressLint
- import android.content.Context
- import android.content.Intent
- import android.net.Uri
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.util.Log
- import android.view.KeyEvent
- import android.view.View
- import android.view.Window
- import android.webkit.WebChromeClient
- import android.webkit.WebView
- import android.webkit.WebViewClient
- import kotlinx.android.synthetic.main.activity_main.*
- import android.net.ConnectivityManager
- import kotlinx.android.synthetic.main.layout_internet_problem.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- showWebView()
- onClickListener()
- }
- private fun showWebView() {
- checkConnectionInternet()
- bindWebView()
- }
- private fun onClickListener() {
- btn_reload.setOnClickListener {
- showWebView()
- }
- }
- private fun checkConnectionInternet() {
- if (isNetworkConnected()){
- layout_internet_loss.setGone()
- web_view.setVisible()
- }else{
- layout_internet_loss.setVisible()
- web_view.setGone()
- }
- }
- @SuppressLint("SetJavaScriptEnabled")
- private fun bindWebView() {
- // Makes Progress bar Visible
- window.setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON)
- web_view.webChromeClient = object : WebChromeClient() {
- override fun onProgressChanged(view: WebView, progress: Int) {
- //Make the bar disappear after URL is loaded, and changes string to Loading...
- title = "Loading..."
- setProgress(progress * 100) //Make the bar disappear after URL is loaded
- progress_bar.progress = progress
- Log.e("Progress : ", " Now $progress")
- // Return the app name after finish loading
- if (progress >= 100) {
- progress_bar.visibility = View.GONE
- setTitle(R.string.app_name)
- }else{
- progress_bar.visibility = View.VISIBLE
- }
- }
- }
- this.web_view.webViewClient = object : WebViewClient() {
- override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
- if (url.startsWith("http://") || url.startsWith("https://")){
- view.loadUrl(url)
- return true
- }
- return try {
- val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
- view.context.startActivity(intent)
- true
- } catch (e: Exception) {
- true
- }
- }
- }
- web_view.settings.builtInZoomControls = true
- web_view.settings.javaScriptEnabled = true
- web_view.settings.domStorageEnabled = true
- web_view.loadUrl(BuildConfig.BASE_URL)
- }
- @Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS")
- override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
- if (event?.action === KeyEvent.ACTION_DOWN) {
- when (keyCode) {
- KeyEvent.KEYCODE_BACK -> {
- if (web_view.canGoBack()) {
- web_view.goBack()
- } else {
- finish()
- }
- return true
- }
- }
- }
- return super.onKeyDown(keyCode, event)
- }
- private fun isNetworkConnected(): Boolean {
- val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
- val activeNetwork = cm.activeNetworkInfo
- if (activeNetwork != null) {
- // connected to the internet
- return when (activeNetwork.type) {
- ConnectivityManager.TYPE_WIFI -> {
- true
- }
- ConnectivityManager.TYPE_MOBILE -> {
- true
- }
- else -> {
- true
- }
- }// connected to wifi
- // connected to mobile data
- } else {
- // not connected to the internet
- return false
- }
- }
- fun View.setVisible(){
- visibility = View.VISIBLE
- }
- fun View.setGone(){
- visibility = View.GONE
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement