Advertisement
Guest User

Test

a guest
Apr 8th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.52 KB | None | 0 0
  1. Skip to content
  2. MoreToggle navigation
  3. M
  4. Mappan
  5. Project overview
  6. Repository
  7. Files
  8. Commits
  9. Branches
  10. Tags
  11. Contributors
  12. Graph
  13. Compare
  14. Issues
  15. 0
  16. Merge Requests
  17. 0
  18. CI / CD
  19. Operations
  20. Packages
  21. Analytics
  22. Wiki
  23. Snippets
  24. Settings
  25.  
  26. Close sidebar
  27. Open sidebar
  28. Anggit Prayogo
  29. Mappan
  30. Repository
  31. master
  32. mappan
  33.  ..
  34.  mappan
  35.  MainActivity.kt
  36.  MainActivity.kt 4.46 KB
  37. 1
  38. 2
  39. 3
  40. 4
  41. 5
  42. 6
  43. 7
  44. 8
  45. 9
  46. 10
  47. 11
  48. 12
  49. 13
  50. 14
  51. 15
  52. 16
  53. 17
  54. 18
  55. 19
  56. 20
  57. 21
  58. 22
  59. 23
  60. 24
  61. 25
  62. 26
  63. 27
  64. 28
  65. 29
  66. 30
  67. 31
  68. 32
  69. 33
  70. 34
  71. 35
  72. 36
  73. 37
  74. 38
  75. 39
  76. 40
  77. 41
  78. 42
  79. 43
  80. 44
  81. 45
  82. 46
  83. 47
  84. 48
  85. 49
  86. 50
  87. 51
  88. 52
  89. 53
  90. 54
  91. 55
  92. 56
  93. 57
  94. 58
  95. 59
  96. 60
  97. 61
  98. 62
  99. 63
  100. 64
  101. 65
  102. 66
  103. 67
  104. 68
  105. 69
  106. 70
  107. 71
  108. 72
  109. 73
  110. 74
  111. 75
  112. 76
  113. 77
  114. 78
  115. 79
  116. 80
  117. 81
  118. 82
  119. 83
  120. 84
  121. 85
  122. 86
  123. 87
  124. 88
  125. 89
  126. 90
  127. 91
  128. 92
  129. 93
  130. 94
  131. 95
  132. 96
  133. 97
  134. 98
  135. 99
  136. 100
  137. 101
  138. 102
  139. 103
  140. 104
  141. 105
  142. 106
  143. 107
  144. 108
  145. 109
  146. 110
  147. 111
  148. 112
  149. 113
  150. 114
  151. 115
  152. 116
  153. 117
  154. 118
  155. 119
  156. 120
  157. 121
  158. 122
  159. 123
  160. 124
  161. 125
  162. 126
  163. 127
  164. 128
  165. 129
  166. 130
  167. 131
  168. 132
  169. 133
  170. 134
  171. 135
  172. 136
  173. 137
  174. 138
  175. 139
  176. 140
  177. 141
  178. package id.go.pertanian.horti.mappan
  179. import android.annotation.SuppressLint
  180. import android.content.Context
  181. import android.content.Intent
  182. import android.net.Uri
  183. import androidx.appcompat.app.AppCompatActivity
  184. import android.os.Bundle
  185. import android.util.Log
  186. import android.view.KeyEvent
  187. import android.view.View
  188. import android.view.Window
  189. import android.webkit.WebChromeClient
  190. import android.webkit.WebView
  191. import android.webkit.WebViewClient
  192. import kotlinx.android.synthetic.main.activity_main.*
  193. import android.net.ConnectivityManager
  194. import kotlinx.android.synthetic.main.layout_internet_problem.*
  195. class MainActivity : AppCompatActivity() {
  196.     override fun onCreate(savedInstanceState: Bundle?) {
  197.         super.onCreate(savedInstanceState)
  198.         setContentView(R.layout.activity_main)
  199.         showWebView()
  200.         onClickListener()
  201.     }
  202.     private fun showWebView() {
  203.         checkConnectionInternet()
  204.         bindWebView()
  205.     }
  206.     private fun onClickListener() {
  207.         btn_reload.setOnClickListener {
  208.             showWebView()
  209.         }
  210.     }
  211.     private fun checkConnectionInternet() {
  212.         if (isNetworkConnected()){
  213.             layout_internet_loss.setGone()
  214.             web_view.setVisible()
  215.         }else{
  216.             layout_internet_loss.setVisible()
  217.             web_view.setGone()
  218.         }
  219.     }
  220.     @SuppressLint("SetJavaScriptEnabled")
  221.     private fun bindWebView() {
  222.         // Makes Progress bar Visible
  223.         window.setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON)
  224.         web_view.webChromeClient = object : WebChromeClient() {
  225.             override fun onProgressChanged(view: WebView, progress: Int) {
  226.                 //Make the bar disappear after URL is loaded, and changes string to Loading...
  227.                 title = "Loading..."
  228.                 setProgress(progress * 100) //Make the bar disappear after URL is loaded
  229.                 progress_bar.progress = progress
  230.                 Log.e("Progress : ", " Now $progress")
  231.                 // Return the app name after finish loading
  232.                 if (progress >= 100) {
  233.                     progress_bar.visibility = View.GONE
  234.                     setTitle(R.string.app_name)
  235.                 }else{
  236.                     progress_bar.visibility = View.VISIBLE
  237.                 }
  238.             }
  239.         }
  240.         this.web_view.webViewClient = object : WebViewClient() {
  241.             override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
  242.                 if (url.startsWith("http://") || url.startsWith("https://")){
  243.                     view.loadUrl(url)
  244.                     return true
  245.                 }
  246.                 return try {
  247.                     val intent =  Intent(Intent.ACTION_VIEW, Uri.parse(url))
  248.                     view.context.startActivity(intent)
  249.                     true
  250.                 } catch (e: Exception) {
  251.                     true
  252.                 }
  253.             }
  254.         }
  255.         web_view.settings.builtInZoomControls = true
  256.         web_view.settings.javaScriptEnabled = true
  257.         web_view.settings.domStorageEnabled = true
  258.         web_view.loadUrl(BuildConfig.BASE_URL)
  259.     }
  260.     @Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS")
  261.     override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
  262.         if (event?.action === KeyEvent.ACTION_DOWN) {
  263.             when (keyCode) {
  264.                 KeyEvent.KEYCODE_BACK -> {
  265.                     if (web_view.canGoBack()) {
  266.                         web_view.goBack()
  267.                     } else {
  268.                         finish()
  269.                     }
  270.                     return true
  271.                 }
  272.             }
  273.         }
  274.         return super.onKeyDown(keyCode, event)
  275.     }
  276.     private fun isNetworkConnected(): Boolean {
  277.         val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
  278.         val activeNetwork = cm.activeNetworkInfo
  279.         if (activeNetwork != null) {
  280.             // connected to the internet
  281.             return when (activeNetwork.type) {
  282.                 ConnectivityManager.TYPE_WIFI -> {
  283.                     true
  284.                 }
  285.                 ConnectivityManager.TYPE_MOBILE -> {
  286.                     true
  287.                 }
  288.                 else -> {
  289.                     true
  290.                 }
  291.             }// connected to wifi
  292.             // connected to mobile data
  293.         } else {
  294.             // not connected to the internet
  295.             return false
  296.         }
  297.     }
  298.     fun View.setVisible(){
  299.         visibility = View.VISIBLE
  300.     }
  301.     fun View.setGone(){
  302.         visibility = View.GONE
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement