@@ -17,6 +17,7 @@ import com.google.gson.Gson
1717import org.json.JSONObject
1818import java.util.*
1919
20+ /* * Bridge between WebView and its client where all the javascript interfaces are designed. */
2021class AndroidWebViewBridge (
2122 private val context : Activity ,
2223 private val webViewClient : ForemWebViewClient
@@ -37,18 +38,35 @@ class AndroidWebViewBridge(
3738 }
3839 }
3940
41+ /* *
42+ * Function which gets called once the webview has fully loaded a valid forem instance which
43+ * contains the details of forem like name, logo, etc.
44+ *
45+ * @param foremMetaDataJsonObject an object which contains meta data of forem.
46+ */
4047 @JavascriptInterface
4148 fun processForemMetaData (foremMetaDataJsonObject : String ) {
4249 var foremMetaDataMap: Map <String , String > = HashMap ()
4350 foremMetaDataMap = Gson ().fromJson(foremMetaDataJsonObject, foremMetaDataMap.javaClass)
4451 webViewClient.foremMetaDataReceived(foremMetaDataMap)
4552 }
4653
54+ /* *
55+ * Function which gets called by website when an error occurs.
56+ *
57+ * @param errorTag tag required for log message.
58+ * @param errorMessage message required for log message.
59+ */
4760 @JavascriptInterface
4861 fun logError (errorTag : String , errorMessage : String ) {
4962 Log .e(errorTag, errorMessage)
5063 }
5164
65+ /* *
66+ * Function which gets called once the user has successfully logged-in.
67+ *
68+ * @param message contains user related meta data like name, id, etc.
69+ */
5270 @JavascriptInterface
5371 fun userLoginMessage (message : String ) {
5472 val userDataObject = JSONObject (message)
@@ -59,17 +77,28 @@ class AndroidWebViewBridge(
5977 }
6078 }
6179
80+ /* *
81+ * Function which gets called once the user has successfully logged-out.
82+ *
83+ * @param message contains user related data.
84+ */
6285 @JavascriptInterface
6386 fun userLogoutMessage (message : String ) {
6487 // User logged-out
6588 webViewClient.userLoggedOut()
6689 }
6790
91+ /* *
92+ * Function which gets triggered when user does any action related to podcast.
93+ *
94+ * @param message contains information related to podcast like its url, play action, pause
95+ * action, title, etc. which can be used to load th podcast in audio service.
96+ */
6897 @JavascriptInterface
6998 fun podcastMessage (message : String ) {
7099 // Reference: https://stackoverflow.com/questions/9446868/access-ui-from-javascript-on-android
71100 context.runOnUiThread {
72- var map: Map <String , String > = java.util. HashMap ()
101+ var map: Map <String , String > = HashMap ()
73102 map = Gson ().fromJson(message, map.javaClass)
74103 when (map[" action" ]) {
75104 " load" -> loadPodcast(map[" url" ])
@@ -115,7 +144,7 @@ class AndroidWebViewBridge(
115144 }
116145 }
117146
118- fun podcastTimeUpdate () {
147+ private fun podcastTimeUpdate () {
119148 audioService?.let {
120149 val time = it.currentTimeInSec() / 1000
121150 val duration = it.durationInSec() / 1000
@@ -133,6 +162,11 @@ class AndroidWebViewBridge(
133162 }
134163 }
135164
165+ /* *
166+ * Function which gets triggered when user first clicks on video in webview player.
167+ *
168+ * @param message contains information like video url and current-time in video.
169+ */
136170 @JavascriptInterface
137171 fun videoMessage (message : String ) {
138172 var map: Map <String , String > = HashMap ()
@@ -157,6 +191,8 @@ class AndroidWebViewBridge(
157191 /* *
158192 * This method is used to open the native share-sheet of Android when simple text is to be
159193 * shared from the web-view.
194+ *
195+ * @param text contains the text which needs to be shared natively.
160196 */
161197 @JavascriptInterface
162198 fun shareText (text : String ) {
@@ -171,23 +207,41 @@ class AndroidWebViewBridge(
171207 context.startActivity(shareIntent)
172208 }
173209
210+ /* *
211+ * This function copies the text natively to clipboard.
212+ *
213+ * @param copyText contains the text which gets saved to clipboard natively.
214+ */
174215 @JavascriptInterface
175216 fun copyToClipboard (copyText : String ) {
176217 val clipboard = context.getSystemService(Context .CLIPBOARD_SERVICE ) as ? ClipboardManager
177218 val clipData = ClipData .newPlainText(" Forem" , copyText)
178219 clipboard?.setPrimaryClip(clipData)
179220 }
180221
222+ /* *
223+ * Shows toast in android app triggered via website.
224+ *
225+ * @param message required to be displayed in Toast.
226+ */
181227 @JavascriptInterface
182228 fun showToast (message : String ) {
183229 Toast .makeText(context, message, Toast .LENGTH_LONG ).show()
184230 }
185231
232+ /* *
233+ * Helps in sending the timer value for the video to the backend.
234+ *
235+ * @param seconds new updated value of current time in video.
236+ */
186237 fun updateTimer (seconds : String ) {
187238 val message = mapOf (" action" to " tick" , " currentTime" to seconds)
188239 webViewClient.sendBridgeMessage(BridgeMessageType .VIDEO , message)
189240 }
190241
242+ /* *
243+ * Helps in sending the information to webview that the video has been paused.
244+ */
191245 fun videoPlayerPaused () {
192246 webViewClient.sendBridgeMessage(BridgeMessageType .VIDEO , mapOf (" action" to " pause" ))
193247 }
0 commit comments