CallActivity.kt
5.4 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
/*
* Copyright 2025 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.livekit.android.videoencodedecode
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTag
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import io.livekit.android.videoencodedecode.ui.theme.AppTheme
import kotlinx.parcelize.Parcelize
class CallActivity : AppCompatActivity() {
private lateinit var viewModel1: CallViewModel
private lateinit var viewModel2: CallViewModel
@OptIn(ExperimentalMaterialApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModelProvider = ViewModelProvider(this, object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
val args = intent.getParcelableExtra<BundleArgs>(KEY_ARGS)
?: throw NullPointerException("args is null!")
val key = extras[ViewModelProvider.NewInstanceFactory.VIEW_MODEL_KEY]
val token = if (key == VIEWMODEL_KEY1) args.token1 else args.token2
val showVideo = key == VIEWMODEL_KEY1
@Suppress("UNCHECKED_CAST")
return CallViewModel(
args.url,
token,
args.useDefaultVideoEncoderFactory,
args.codecWhiteList,
showVideo,
application
) as T
}
})
viewModel1 = viewModelProvider.get(VIEWMODEL_KEY1, CallViewModel::class.java)
viewModel2 = viewModelProvider.get(VIEWMODEL_KEY2, CallViewModel::class.java)
// Setup compose view.
setContent {
Content(
viewModel1,
viewModel2
)
}
}
@ExperimentalMaterialApi
@Composable
fun Content(viewModel1: CallViewModel, viewModel2: CallViewModel) {
AppTheme(darkTheme = true) {
ConstraintLayout(
modifier = Modifier
.fillMaxSize()
.background(Color.Red)
) {
val (topConnectionItem, bottomConnectionItem) = createRefs()
Surface(
color = Color.Cyan,
modifier = Modifier
.semantics {
testTag = TEST_TAG1
}
.constrainAs(topConnectionItem) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(bottomConnectionItem.top)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
}
) {
ConnectionItem(viewModel = viewModel1)
}
Surface(
color = Color.Magenta,
modifier = Modifier
.semantics {
testTag = TEST_TAG2
}
.constrainAs(bottomConnectionItem) {
top.linkTo(topConnectionItem.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
}
) {
ConnectionItem(viewModel = viewModel2)
}
}
}
}
companion object {
const val KEY_ARGS = "args"
const val VIEWMODEL_KEY1 = "key1"
const val VIEWMODEL_KEY2 = "key2"
const val TEST_TAG1 = "tag1"
const val TEST_TAG2 = "tag2"
}
@Parcelize
data class BundleArgs(
val url: String,
val token1: String,
val token2: String,
val useDefaultVideoEncoderFactory: Boolean = false,
val codecWhiteList: List<String>? = null,
) : Parcelable
}