CallActivity.kt
6.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package io.livekit.android.sample
import android.app.Activity
import android.media.projection.MediaProjectionManager
import android.os.Bundle
import android.os.Parcelable
import android.view.WindowManager
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.xwray.groupie.GroupieAdapter
import io.livekit.android.sample.databinding.CallActivityBinding
import io.livekit.android.sample.dialog.showDebugMenuDialog
import io.livekit.android.sample.dialog.showSelectAudioDeviceDialog
import kotlinx.coroutines.flow.collectLatest
import kotlinx.parcelize.Parcelize
class CallActivity : AppCompatActivity() {
val viewModel: CallViewModel by viewModelByFactory {
val args = intent.getParcelableExtra<BundleArgs>(KEY_ARGS)
?: throw NullPointerException("args is null!")
CallViewModel(args.url, args.token, application, args.e2ee, args.e2eeKey)
}
lateinit var binding: CallActivityBinding
private val screenCaptureIntentLauncher =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
val resultCode = result.resultCode
val data = result.data
if (resultCode != Activity.RESULT_OK || data == null) {
return@registerForActivityResult
}
viewModel.startScreenCapture(data)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
binding = CallActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
// Audience row setup
val audienceAdapter = GroupieAdapter()
binding.audienceRow.apply {
layoutManager = LinearLayoutManager(this@CallActivity, LinearLayoutManager.HORIZONTAL, false)
adapter = audienceAdapter
}
lifecycleScope.launchWhenCreated {
viewModel.participants
.collect { participants ->
val items = participants.map { participant -> ParticipantItem(viewModel.room, participant) }
audienceAdapter.update(items)
}
}
// speaker view setup
val speakerAdapter = GroupieAdapter()
binding.speakerView.apply {
layoutManager = LinearLayoutManager(this@CallActivity, LinearLayoutManager.HORIZONTAL, false)
adapter = speakerAdapter
}
lifecycleScope.launchWhenCreated {
viewModel.primarySpeaker.collectLatest { speaker ->
val items = listOfNotNull(speaker)
.map { participant -> ParticipantItem(viewModel.room, participant, speakerView = true) }
speakerAdapter.update(items)
}
}
// Controls setup
viewModel.cameraEnabled.observe(this) { enabled ->
binding.camera.setOnClickListener { viewModel.setCameraEnabled(!enabled) }
binding.camera.setImageResource(
if (enabled) {
R.drawable.outline_videocam_24
} else {
R.drawable.outline_videocam_off_24
}
)
binding.flipCamera.isEnabled = enabled
}
viewModel.micEnabled.observe(this) { enabled ->
binding.mic.setOnClickListener { viewModel.setMicEnabled(!enabled) }
binding.mic.setImageResource(
if (enabled) {
R.drawable.outline_mic_24
} else {
R.drawable.outline_mic_off_24
}
)
}
binding.flipCamera.setOnClickListener { viewModel.flipCamera() }
viewModel.screenshareEnabled.observe(this) { enabled ->
binding.screenShare.setOnClickListener {
if (enabled) {
viewModel.stopScreenCapture()
} else {
requestMediaProjection()
}
}
binding.screenShare.setImageResource(
if (enabled) {
R.drawable.baseline_cast_connected_24
} else {
R.drawable.baseline_cast_24
}
)
}
binding.message.setOnClickListener {
val editText = EditText(this)
AlertDialog.Builder(this)
.setTitle("Send Message")
.setView(editText)
.setPositiveButton("Send") { dialog, _ ->
viewModel.sendData(editText.text?.toString() ?: "")
}
.setNegativeButton("Cancel") { _, _ -> }
.create()
.show()
}
binding.exit.setOnClickListener { finish() }
// Controls row 2
binding.audioSelect.setOnClickListener {
showSelectAudioDeviceDialog(viewModel)
}
lifecycleScope.launchWhenCreated {
viewModel.permissionAllowed.collect { allowed ->
val resource = if (allowed) R.drawable.account_cancel_outline else R.drawable.account_cancel
binding.permissions.setImageResource(resource)
}
}
binding.permissions.setOnClickListener {
viewModel.toggleSubscriptionPermissions()
}
binding.debugMenu.setOnClickListener {
showDebugMenuDialog(viewModel)
}
}
override fun onResume() {
super.onResume()
lifecycleScope.launchWhenResumed {
viewModel.error.collect {
if (it != null) {
Toast.makeText(this@CallActivity, "Error: $it", Toast.LENGTH_LONG).show()
viewModel.dismissError()
}
}
}
lifecycleScope.launchWhenResumed {
viewModel.dataReceived.collect {
Toast.makeText(this@CallActivity, "Data received: $it", Toast.LENGTH_LONG).show()
}
}
}
private fun requestMediaProjection() {
val mediaProjectionManager =
getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
screenCaptureIntentLauncher.launch(mediaProjectionManager.createScreenCaptureIntent())
}
override fun onDestroy() {
binding.audienceRow.adapter = null
binding.speakerView.adapter = null
super.onDestroy()
}
companion object {
const val KEY_ARGS = "args"
}
@Parcelize
data class BundleArgs(val url: String, val token: String, val e2ee: Boolean, val e2eeKey: String) : Parcelable
}