Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit c28e212

Browse files
committed
Code cleanup
1 parent 43345d5 commit c28e212

10 files changed

Lines changed: 204 additions & 89 deletions

File tree

app/src/main/java/io/qtalk/qgamejsinterfacetester/MainActivity.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ class MainActivity : AppCompatActivity() {
4040
.title(text = "Select User")
4141
.show {
4242
listItems(items = QTalkTestUsers.values().map { it.displayName }) { _, index, _ ->
43-
PreferenceManager.writeString(this@MainActivity, PreferenceManager.KEY_SELECTED_USER, QTalkTestUsers.values()[index].userName)
43+
PreferenceManager.writeString(
44+
this@MainActivity,
45+
PreferenceManager.KEY_SELECTED_USER,
46+
QTalkTestUsers.values()[index].userName
47+
)
4448
refreshSelectedUser()
4549
dismiss()
4650
}
@@ -60,28 +64,31 @@ class MainActivity : AppCompatActivity() {
6064
}
6165
}
6266

63-
private fun openUrlSelectionDialog(){
67+
private fun openUrlSelectionDialog() {
6468
MaterialDialog(this)
6569
.title(text = "Enter URL")
6670
.cancelOnTouchOutside(true)
67-
.negativeButton(text = "Test URL"){
71+
.negativeButton(text = "Test URL") {
6872
openTestUrl()
6973
}
7074
.checkBoxPrompt(text = "Make a preview call", onToggle = null)
7175
.input(prefill = getUrlFromPrefOrIntent()) { materialDialog, charSequence ->
7276
val urlToOpen = charSequence.toString()
73-
if (urlToOpen.isNotEmpty() && Patterns.WEB_URL.matcher(urlToOpen).matches()){
77+
if (urlToOpen.isNotEmpty() && Patterns.WEB_URL.matcher(urlToOpen).matches()) {
7478
materialDialog.dismiss()
75-
WebViewActivity.startActivity(this, urlToOpen, if (materialDialog.isCheckPromptChecked()) InteractionType.WEB_SHARING else InteractionType.IN_CALL)
76-
}else if (urlToOpen == "test-url"){
79+
WebViewActivity.startActivity(
80+
this,
81+
urlToOpen,
82+
if (materialDialog.isCheckPromptChecked()) InteractionType.WEB_SHARING else InteractionType.IN_CALL
83+
)
84+
} else if (urlToOpen == "test-url") {
7785
materialDialog.dismiss()
7886
openTestUrl()
79-
}
80-
else{
87+
} else {
8188
Toast.makeText(this, "Invalid Url!", Toast.LENGTH_SHORT).show()
8289
}
8390
}
84-
.positiveButton { }
91+
.positiveButton { }
8592
.show()
8693
}
8794

@@ -92,13 +99,13 @@ class MainActivity : AppCompatActivity() {
9299
consumeIntent()
93100
}
94101

95-
private fun consumeIntent(){
102+
private fun consumeIntent() {
96103
if (intent.action == Intent.ACTION_VIEW) {
97104
openUrlSelectionDialog()
98105
}
99106
}
100107

101-
private fun openTestUrl(){
108+
private fun openTestUrl() {
102109
val testUrl = "file:///android_asset/test.html"
103110
WebViewActivity.startActivity(this, testUrl)
104111
}

app/src/main/java/io/qtalk/qgamejsinterfacetester/WebViewActivity.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package io.qtalk.qgamejsinterfacetester
32

43
import android.content.Context
@@ -13,13 +12,18 @@ class WebViewActivity : AppCompatActivity() {
1312
companion object {
1413
private const val EXTRA_WEBSITE_URL = "extra-website-url"
1514
private const val EXTRA_INTERACTION_TYPE = "extra-interaction-type"
16-
fun startActivity(context: Context, url: String, interactionType: InteractionType = InteractionType.IN_CALL){
17-
context.startActivity(Intent(
18-
context,
19-
WebViewActivity::class.java
20-
)
21-
.putExtra(EXTRA_WEBSITE_URL, url)
22-
.putExtra(EXTRA_INTERACTION_TYPE, interactionType.name)
15+
fun startActivity(
16+
context: Context,
17+
url: String,
18+
interactionType: InteractionType = InteractionType.IN_CALL
19+
) {
20+
context.startActivity(
21+
Intent(
22+
context,
23+
WebViewActivity::class.java
24+
)
25+
.putExtra(EXTRA_WEBSITE_URL, url)
26+
.putExtra(EXTRA_INTERACTION_TYPE, interactionType.name)
2327
)
2428
}
2529
}
@@ -32,15 +36,17 @@ class WebViewActivity : AppCompatActivity() {
3236

3337
supportFragmentManager
3438
.beginTransaction()
35-
.replace(R.id.container, WebViewFragment.init(
36-
intent.getStringExtra(EXTRA_WEBSITE_URL),
37-
InteractionType.valueOf(intent.getStringExtra(EXTRA_INTERACTION_TYPE))
38-
))
39+
.replace(
40+
R.id.container, WebViewFragment.init(
41+
intent.getStringExtra(EXTRA_WEBSITE_URL),
42+
InteractionType.valueOf(intent.getStringExtra(EXTRA_INTERACTION_TYPE))
43+
)
44+
)
3945
.commit()
4046
}
4147

4248
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
43-
if (item?.itemId == android.R.id.home){
49+
if (item?.itemId == android.R.id.home) {
4450
onBackPressed()
4551
}
4652
return super.onOptionsItemSelected(item)

app/src/main/java/io/qtalk/qgamejsinterfacetester/WebViewFragment.kt

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import io.qtalk.qgamejsinterfacetester.helpers.generateSHA1
2020
import io.qtalk.qgamejsinterfacetester.views.PermissionAwareWebViewFragment
2121
import kotlinx.android.synthetic.main.webview_fragment.*
2222

23-
class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterfaceBridge {
23+
class WebViewFragment : PermissionAwareWebViewFragment(), JSInterface.JSInterfaceBridge {
2424

2525
companion object {
2626
private const val JS_INTERFACE_OBJECT_NAME = "QTalkApp"
2727

28-
private const val TEST_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
28+
private const val TEST_TOKEN =
29+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
2930

3031
private const val ARG_URL_STRING = "arg-url"
3132
private const val ARG_INTERACTION_TYPE = "arg-interaction-type"
@@ -34,7 +35,10 @@ class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterface
3435
private const val RTDB_CHILD_PARTICIPANTS = "prtcpnts"
3536
private const val RTDB_VALUE_CALL_ENDED_AT = "clEdAt"
3637

37-
fun init(url: String, interactionType: InteractionType = InteractionType.IN_CALL): WebViewFragment {
38+
fun init(
39+
url: String,
40+
interactionType: InteractionType = InteractionType.IN_CALL
41+
): WebViewFragment {
3842
return WebViewFragment().apply {
3943
arguments = Bundle(1).also {
4044
it.putString(ARG_URL_STRING, url)
@@ -68,44 +72,60 @@ class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterface
6872
var dialedAt: Long
6973
)
7074

71-
private fun writeParticipantInfoAndCallDetailsToRTDB(){
75+
private fun writeParticipantInfoAndCallDetailsToRTDB() {
7276

7377
// while writing the information to RTDB, we create the key as the user id
7478
val selectedUser = QTalkTestUsers
7579
.values()
76-
.firstOrNull { it.userName == PreferenceManager.getString(activity!!, PreferenceManager.KEY_SELECTED_USER) }?.userIdRemote
80+
.firstOrNull {
81+
it.userName == PreferenceManager.getString(
82+
activity!!,
83+
PreferenceManager.KEY_SELECTED_USER
84+
)
85+
}?.userIdRemote
7786
?: run {
7887
Toast.makeText(activity!!, "No user selected!", Toast.LENGTH_SHORT).show()
7988
return
8089
}
8190

8291
val loadedUrl = getLoadedUrl()!!
8392

84-
val callId = Uri.parse(loadedUrl).getQueryParameter("id")?: run {
85-
Toast.makeText(activity!!, "No Call ID provided, check entered URL!", Toast.LENGTH_LONG).show()
93+
val callId = Uri.parse(loadedUrl).getQueryParameter("id") ?: run {
94+
Toast.makeText(activity!!, "No Call ID provided, check entered URL!", Toast.LENGTH_LONG)
95+
.show()
8696
return
8797
}
8898

89-
Log.d("WebViewFragment", "writeParticipantInfoAndCallDetailsToRTDB: ${callId}, $selectedUser")
99+
Log.d(
100+
"WebViewFragment",
101+
"writeParticipantInfoAndCallDetailsToRTDB: ${callId}, $selectedUser"
102+
)
90103

91104
val database = FirebaseDatabase.getInstance()
92105

93106
// get reference to clDts/prtcpnts which is where the participants are store in by the main QTalk app.
94107
val ref = database.getReference("qtalkDebugAndStaging/calls").child(callId)
95108

96109
// store the participant info with the key as the user id
97-
ref.child(RTDB_CHILD_PARTICIPANTS).child(selectedUser).setValue(TestUserObject(selectedUser))
110+
ref.child(RTDB_CHILD_PARTICIPANTS).child(selectedUser)
111+
.setValue(TestUserObject(selectedUser))
98112

99113
// store call details
100-
ref.child(RTDB_CHILD_CALL_DETAILS).setValue(RTDBPreviewCallDetails(
101-
callId,
102-
System.currentTimeMillis()
103-
))
114+
ref.child(RTDB_CHILD_CALL_DETAILS).setValue(
115+
RTDBPreviewCallDetails(
116+
callId,
117+
System.currentTimeMillis()
118+
)
119+
)
104120

105121
rtdbReference = ref
106122
}
107123

108-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
124+
override fun onCreateView(
125+
inflater: LayoutInflater,
126+
container: ViewGroup?,
127+
savedInstanceState: Bundle?
128+
): View? {
109129
return inflater.inflate(R.layout.webview_fragment, container, false)
110130
}
111131

@@ -117,10 +137,19 @@ class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterface
117137
isTestUrl = loadedUrl.startsWith("file:///")
118138

119139
if (!isTestUrl) {
120-
PreferenceManager.writeString(activity!!, PreferenceManager.KEY_LAST_ENTERED_URL, loadedUrl)
140+
PreferenceManager.writeString(
141+
activity!!,
142+
PreferenceManager.KEY_LAST_ENTERED_URL,
143+
loadedUrl
144+
)
121145
}
122146

123-
interactionType = InteractionType.valueOf((arguments?: return).getString(ARG_INTERACTION_TYPE, InteractionType.IN_CALL.name))
147+
interactionType = InteractionType.valueOf(
148+
(arguments ?: return).getString(
149+
ARG_INTERACTION_TYPE,
150+
InteractionType.IN_CALL.name
151+
)
152+
)
124153

125154
Log.d("WebViewFragment", "onViewCreated: interaction type: $interactionType")
126155

@@ -130,8 +159,9 @@ class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterface
130159

131160
endCallButton.visibility = View.VISIBLE
132161

133-
endCallButton.setOnClickListener{
134-
rtdbReference.child(RTDB_CHILD_CALL_DETAILS).child(RTDB_VALUE_CALL_ENDED_AT).setValue(System.currentTimeMillis())
162+
endCallButton.setOnClickListener {
163+
rtdbReference.child(RTDB_CHILD_CALL_DETAILS).child(RTDB_VALUE_CALL_ENDED_AT)
164+
.setValue(System.currentTimeMillis())
135165
activity?.finish()
136166
}
137167
}
@@ -169,32 +199,43 @@ class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterface
169199
return getSelectedUserFromPref()?.generateSHA1() ?: TEST_TOKEN
170200
}
171201

172-
private fun getSelectedUserFromPref() = PreferenceManager.getString(activity!!, PreferenceManager.KEY_SELECTED_USER)
202+
private fun getSelectedUserFromPref() =
203+
PreferenceManager.getString(activity!!, PreferenceManager.KEY_SELECTED_USER)
173204

174-
override fun notifyGameRoundStarted(){
205+
override fun notifyGameRoundStarted() {
175206
Toast.makeText(context, "Game round started notified!", Toast.LENGTH_SHORT).show()
176207
}
177208

178-
override fun notifyGameRoundEnded(){
209+
override fun notifyGameRoundEnded() {
179210
Toast.makeText(context, "Game round ended notified!", Toast.LENGTH_SHORT).show()
180211
}
181212

182-
override fun updateGamePrompts(prompts: String){
213+
override fun updateGamePrompts(prompts: String) {
183214
if (isTestUrl) {
184215
webView.handler.post {
185-
webView.evaluateJavascript("setPromptToText($prompts)") { Log.d("JSInterface", "onReceiveValue") }
216+
webView.evaluateJavascript("setPromptToText($prompts)") {
217+
Log.d(
218+
"JSInterface",
219+
"onReceiveValue"
220+
)
221+
}
186222
}
187-
}else {
223+
} else {
188224
Toast.makeText(context, "Prompts recieved: $prompts", Toast.LENGTH_LONG).show()
189225
}
190226
}
191227

192228
override fun clearGamePrompts() {
193229
if (isTestUrl) {
194230
webView.handler.post {
195-
webView.evaluateJavascript("setPromptToText(\"\")") { Log.d("JSInterface", "onReceiveValue") }
231+
webView.evaluateJavascript("setPromptToText(\"\")") {
232+
Log.d(
233+
"JSInterface",
234+
"onReceiveValue"
235+
)
236+
}
196237
}
197-
}else {
238+
} else {
198239
Toast.makeText(context, "Prompts cleared", Toast.LENGTH_LONG).show()
199240
}
200241
}
@@ -204,7 +245,7 @@ class WebViewFragment: PermissionAwareWebViewFragment(), JSInterface.JSInterface
204245
}
205246

206247
// test only
207-
override fun clearWebViewCache(){
248+
override fun clearWebViewCache() {
208249
Log.d("JSInterfaceBridge", "clearWebViewCache: ")
209250
webView.handler.post {
210251
Toast.makeText(context, "Cache cleared!", Toast.LENGTH_LONG).show()

app/src/main/java/io/qtalk/qgamejsinterfacetester/core/JSInterface.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.webkit.JavascriptInterface
55
@Suppress("unused")
66
class JSInterface(private var jsInterfaceBridge: JSInterfaceBridge? = null) {
77

8-
fun setJSInterfaceBridge(jsInterfaceBridge: JSInterfaceBridge?){
8+
fun setJSInterfaceBridge(jsInterfaceBridge: JSInterfaceBridge?) {
99
this.jsInterfaceBridge = jsInterfaceBridge
1010
}
1111

@@ -15,17 +15,17 @@ class JSInterface(private var jsInterfaceBridge: JSInterfaceBridge? = null) {
1515
}
1616

1717
@JavascriptInterface
18-
fun notifyGameRoundStarted(){
18+
fun notifyGameRoundStarted() {
1919
jsInterfaceBridge?.notifyGameRoundStarted()
2020
}
2121

2222
@JavascriptInterface
23-
fun notifyGameRoundEnded(){
23+
fun notifyGameRoundEnded() {
2424
jsInterfaceBridge?.notifyGameRoundEnded()
2525
}
2626

2727
@JavascriptInterface
28-
fun updateGamePrompts(prompts: String){
28+
fun updateGamePrompts(prompts: String) {
2929
jsInterfaceBridge?.updateGamePrompts(prompts)
3030
}
3131

@@ -34,14 +34,17 @@ class JSInterface(private var jsInterfaceBridge: JSInterfaceBridge? = null) {
3434
jsInterfaceBridge?.clearGamePrompts()
3535
}
3636

37-
@Deprecated("Not supported any more", replaceWith = ReplaceWith("saveBase64Image(base64EncodedImageString)"))
37+
@Deprecated(
38+
"Not supported any more",
39+
replaceWith = ReplaceWith("saveBase64Image(base64EncodedImageString)")
40+
)
3841
@JavascriptInterface
39-
fun onBase64ImageSaved(base64EncodedImageString: String){
42+
fun onBase64ImageSaved(base64EncodedImageString: String) {
4043
saveBase64Image(base64EncodedImageString)
4144
}
4245

4346
@JavascriptInterface
44-
fun saveBase64Image(base64EncodedImageString: String){
47+
fun saveBase64Image(base64EncodedImageString: String) {
4548
jsInterfaceBridge?.saveBase64Image(base64EncodedImageString)
4649
}
4750

@@ -52,11 +55,11 @@ class JSInterface(private var jsInterfaceBridge: JSInterfaceBridge? = null) {
5255

5356
// test only
5457
@JavascriptInterface
55-
fun clearWebViewCache(){
58+
fun clearWebViewCache() {
5659
jsInterfaceBridge?.clearWebViewCache()
5760
}
5861

59-
interface JSInterfaceBridge{
62+
interface JSInterfaceBridge {
6063

6164
fun getUserAuthToken(): String
6265

app/src/main/java/io/qtalk/qgamejsinterfacetester/helpers/Extensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ fun ByteArray.toHexString(): String {
2323
return hexString.toString()
2424
}
2525

26-
fun String.generateSHA1(): String{
26+
fun String.generateSHA1(): String {
2727
return MessageDigest.getInstance("SHA-1")
2828
.digest(this.toByteArray(Charset.forName("UTF-8")))
2929
.toHexString()
3030
}
3131

32-
fun String.generateMD5(): String{
32+
fun String.generateMD5(): String {
3333
return MessageDigest.getInstance("MD5")
3434
.digest(this.toByteArray(Charset.forName("UTF-8")))
3535
.toHexString()

0 commit comments

Comments
 (0)