MainActivity.kt
4.0 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
package io.livekit.android.sample
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import io.livekit.android.sample.databinding.MainActivityBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = MainActivityBinding.inflate(layoutInflater)
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val urlString = preferences.getString(PREFERENCES_KEY_URL, URL)
val tokenString = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN)
binding.run {
url.editText?.text = SpannableStringBuilder(urlString)
token.editText?.text = SpannableStringBuilder(tokenString)
connectButton.setOnClickListener {
val intent = Intent(this@MainActivity, CallActivity::class.java).apply {
putExtra(
CallActivity.KEY_ARGS,
CallActivity.BundleArgs(
url.editText?.text.toString(),
token.editText?.text.toString()
)
)
}
startActivity(intent)
}
saveButton.setOnClickListener {
preferences.edit {
putString(PREFERENCES_KEY_URL, url.editText?.text.toString())
putString(PREFERENCES_KEY_TOKEN, token.editText?.text.toString())
}
Toast.makeText(
this@MainActivity,
"Values saved.",
Toast.LENGTH_SHORT
).show()
}
resetButton.setOnClickListener {
preferences.edit {
clear()
}
url.editText?.text = SpannableStringBuilder(URL)
token.editText?.text = SpannableStringBuilder(TOKEN)
Toast.makeText(
this@MainActivity,
"Values reset.",
Toast.LENGTH_SHORT
).show()
}
}
setContentView(binding.root)
requestPermissions()
}
private fun requestPermissions() {
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { grants ->
for (grant in grants.entries) {
if (!grant.value) {
Toast.makeText(
this,
"Missing permission: ${grant.key}",
Toast.LENGTH_SHORT
)
.show()
}
}
}
val neededPermissions = listOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
.filter {
ContextCompat.checkSelfPermission(
this,
it
) == PackageManager.PERMISSION_DENIED
}
.toTypedArray()
if (neededPermissions.isNotEmpty()) {
requestPermissionLauncher.launch(neededPermissions)
}
}
companion object {
const val PREFERENCES_KEY_URL = "url"
const val PREFERENCES_KEY_TOKEN = "token"
const val URL = "ws://192.168.11.5:7880"
const val TOKEN =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTk0NDkwNTMsImlzcyI6IkFQSXdMZWFoN2c0ZnVMWURZQUplYUtzU0UiLCJqdGkiOiJwaG9uZTIiLCJuYmYiOjE2MTY4NTcwNTMsInZpZGVvIjp7InJvb20iOiJteXJvb20iLCJyb29tSm9pbiI6dHJ1ZX19.OiemzgYXe5269q_VDXb912LG3QoqoKy-52-xEWcTr3A"
}
}