Committed by
GitHub
Add e2ee options to compose example (#263)
正在显示
4 个修改的文件
包含
79 行增加
和
14 行删除
| @@ -44,7 +44,7 @@ class CallViewModel( | @@ -44,7 +44,7 @@ class CallViewModel( | ||
| 44 | 44 | ||
| 45 | private fun getE2EEOptions(): E2EEOptions? { | 45 | private fun getE2EEOptions(): E2EEOptions? { |
| 46 | var e2eeOptions: E2EEOptions? = null | 46 | var e2eeOptions: E2EEOptions? = null |
| 47 | - if(e2ee && e2eeKey != null) { | 47 | + if (e2ee && e2eeKey != null) { |
| 48 | e2eeOptions = E2EEOptions() | 48 | e2eeOptions = E2EEOptions() |
| 49 | } | 49 | } |
| 50 | e2eeOptions?.keyProvider?.setKey(e2eeKey!!, null, 0) | 50 | e2eeOptions?.keyProvider?.setKey(e2eeKey!!, null, 0) |
| @@ -13,7 +13,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | @@ -13,7 +13,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | ||
| 13 | fun getSavedUrl() = preferences.getString(PREFERENCES_KEY_URL, URL) as String | 13 | fun getSavedUrl() = preferences.getString(PREFERENCES_KEY_URL, URL) as String |
| 14 | fun getSavedToken() = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) as String | 14 | fun getSavedToken() = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) as String |
| 15 | fun getE2EEOptionsOn() = preferences.getBoolean(PREFERENCES_KEY_E2EE_ON, false) | 15 | fun getE2EEOptionsOn() = preferences.getBoolean(PREFERENCES_KEY_E2EE_ON, false) |
| 16 | - fun getSavedE2EEKey() = preferences.getString(PREFERENCES_KEY_E2EE_KEY, "12345678") as String | 16 | + fun getSavedE2EEKey() = preferences.getString(PREFERENCES_KEY_E2EE_KEY, E2EE_KEY) as String |
| 17 | 17 | ||
| 18 | fun setSavedUrl(url: String) { | 18 | fun setSavedUrl(url: String) { |
| 19 | preferences.edit { | 19 | preferences.edit { |
| @@ -51,5 +51,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | @@ -51,5 +51,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | ||
| 51 | 51 | ||
| 52 | const val URL = BuildConfig.DEFAULT_URL | 52 | const val URL = BuildConfig.DEFAULT_URL |
| 53 | const val TOKEN = BuildConfig.DEFAULT_TOKEN | 53 | const val TOKEN = BuildConfig.DEFAULT_TOKEN |
| 54 | + const val E2EE_KEY = "12345678" | ||
| 54 | } | 55 | } |
| 55 | } | 56 | } |
| @@ -40,7 +40,13 @@ class CallActivity : AppCompatActivity() { | @@ -40,7 +40,13 @@ class CallActivity : AppCompatActivity() { | ||
| 40 | private val viewModel: CallViewModel by viewModelByFactory { | 40 | private val viewModel: CallViewModel by viewModelByFactory { |
| 41 | val args = intent.getParcelableExtra<BundleArgs>(KEY_ARGS) | 41 | val args = intent.getParcelableExtra<BundleArgs>(KEY_ARGS) |
| 42 | ?: throw NullPointerException("args is null!") | 42 | ?: throw NullPointerException("args is null!") |
| 43 | - CallViewModel(args.url, args.token, application) | 43 | + CallViewModel( |
| 44 | + url = args.url, | ||
| 45 | + token = args.token, | ||
| 46 | + e2ee = args.e2eeOn, | ||
| 47 | + e2eeKey = args.e2eeKey, | ||
| 48 | + application = application | ||
| 49 | + ) | ||
| 44 | } | 50 | } |
| 45 | 51 | ||
| 46 | private val screenCaptureIntentLauncher = | 52 | private val screenCaptureIntentLauncher = |
| @@ -446,5 +452,10 @@ class CallActivity : AppCompatActivity() { | @@ -446,5 +452,10 @@ class CallActivity : AppCompatActivity() { | ||
| 446 | } | 452 | } |
| 447 | 453 | ||
| 448 | @Parcelize | 454 | @Parcelize |
| 449 | - data class BundleArgs(val url: String, val token: String) : Parcelable | 455 | + data class BundleArgs( |
| 456 | + val url: String, | ||
| 457 | + val token: String, | ||
| 458 | + val e2eeKey: String, | ||
| 459 | + val e2eeOn: Boolean | ||
| 460 | + ) : Parcelable | ||
| 450 | } | 461 | } |
| @@ -7,11 +7,29 @@ import androidx.activity.ComponentActivity | @@ -7,11 +7,29 @@ import androidx.activity.ComponentActivity | ||
| 7 | import androidx.activity.compose.setContent | 7 | import androidx.activity.compose.setContent |
| 8 | import androidx.activity.viewModels | 8 | import androidx.activity.viewModels |
| 9 | import androidx.compose.foundation.Image | 9 | import androidx.compose.foundation.Image |
| 10 | -import androidx.compose.foundation.layout.* | 10 | +import androidx.compose.foundation.layout.Arrangement |
| 11 | +import androidx.compose.foundation.layout.Box | ||
| 12 | +import androidx.compose.foundation.layout.Column | ||
| 13 | +import androidx.compose.foundation.layout.Row | ||
| 14 | +import androidx.compose.foundation.layout.Spacer | ||
| 15 | +import androidx.compose.foundation.layout.defaultMinSize | ||
| 16 | +import androidx.compose.foundation.layout.fillMaxSize | ||
| 17 | +import androidx.compose.foundation.layout.fillMaxWidth | ||
| 18 | +import androidx.compose.foundation.layout.height | ||
| 19 | +import androidx.compose.foundation.layout.padding | ||
| 11 | import androidx.compose.foundation.rememberScrollState | 20 | import androidx.compose.foundation.rememberScrollState |
| 12 | import androidx.compose.foundation.verticalScroll | 21 | import androidx.compose.foundation.verticalScroll |
| 13 | -import androidx.compose.material.* | ||
| 14 | -import androidx.compose.runtime.* | 22 | +import androidx.compose.material.Button |
| 23 | +import androidx.compose.material.MaterialTheme | ||
| 24 | +import androidx.compose.material.OutlinedTextField | ||
| 25 | +import androidx.compose.material.Surface | ||
| 26 | +import androidx.compose.material.Switch | ||
| 27 | +import androidx.compose.material.Text | ||
| 28 | +import androidx.compose.runtime.Composable | ||
| 29 | +import androidx.compose.runtime.getValue | ||
| 30 | +import androidx.compose.runtime.mutableStateOf | ||
| 31 | +import androidx.compose.runtime.remember | ||
| 32 | +import androidx.compose.runtime.setValue | ||
| 15 | import androidx.compose.ui.Alignment | 33 | import androidx.compose.ui.Alignment |
| 16 | import androidx.compose.ui.Modifier | 34 | import androidx.compose.ui.Modifier |
| 17 | import androidx.compose.ui.res.painterResource | 35 | import androidx.compose.ui.res.painterResource |
| @@ -34,21 +52,27 @@ class MainActivity : ComponentActivity() { | @@ -34,21 +52,27 @@ class MainActivity : ComponentActivity() { | ||
| 34 | MainContent( | 52 | MainContent( |
| 35 | defaultUrl = viewModel.getSavedUrl(), | 53 | defaultUrl = viewModel.getSavedUrl(), |
| 36 | defaultToken = viewModel.getSavedToken(), | 54 | defaultToken = viewModel.getSavedToken(), |
| 37 | - onConnect = { url, token -> | 55 | + defaultE2eeKey = viewModel.getSavedE2EEKey(), |
| 56 | + defaultE2eeOn = viewModel.getE2EEOptionsOn(), | ||
| 57 | + onConnect = { url, token, e2eeKey, e2eeOn -> | ||
| 38 | val intent = Intent(this@MainActivity, CallActivity::class.java).apply { | 58 | val intent = Intent(this@MainActivity, CallActivity::class.java).apply { |
| 39 | putExtra( | 59 | putExtra( |
| 40 | CallActivity.KEY_ARGS, | 60 | CallActivity.KEY_ARGS, |
| 41 | CallActivity.BundleArgs( | 61 | CallActivity.BundleArgs( |
| 42 | url, | 62 | url, |
| 43 | - token | 63 | + token, |
| 64 | + e2eeKey, | ||
| 65 | + e2eeOn, | ||
| 44 | ) | 66 | ) |
| 45 | ) | 67 | ) |
| 46 | } | 68 | } |
| 47 | startActivity(intent) | 69 | startActivity(intent) |
| 48 | }, | 70 | }, |
| 49 | - onSave = { url, token -> | 71 | + onSave = { url, token, e2eeKey, e2eeOn -> |
| 50 | viewModel.setSavedUrl(url) | 72 | viewModel.setSavedUrl(url) |
| 51 | viewModel.setSavedToken(token) | 73 | viewModel.setSavedToken(token) |
| 74 | + viewModel.setSavedE2EEKey(e2eeKey) | ||
| 75 | + viewModel.setSavedE2EEOn(e2eeOn) | ||
| 52 | 76 | ||
| 53 | Toast.makeText( | 77 | Toast.makeText( |
| 54 | this@MainActivity, | 78 | this@MainActivity, |
| @@ -76,13 +100,17 @@ class MainActivity : ComponentActivity() { | @@ -76,13 +100,17 @@ class MainActivity : ComponentActivity() { | ||
| 76 | fun MainContent( | 100 | fun MainContent( |
| 77 | defaultUrl: String = MainViewModel.URL, | 101 | defaultUrl: String = MainViewModel.URL, |
| 78 | defaultToken: String = MainViewModel.TOKEN, | 102 | defaultToken: String = MainViewModel.TOKEN, |
| 79 | - onConnect: (url: String, token: String) -> Unit = { _, _ -> }, | ||
| 80 | - onSave: (url: String, token: String) -> Unit = { _, _ -> }, | 103 | + defaultE2eeKey: String = MainViewModel.E2EE_KEY, |
| 104 | + defaultE2eeOn: Boolean = false, | ||
| 105 | + onConnect: (url: String, token: String, e2eeKey: String, e2eeOn: Boolean) -> Unit = { _, _, _, _ -> }, | ||
| 106 | + onSave: (url: String, token: String, e2eeKey: String, e2eeOn: Boolean) -> Unit = { _, _, _, _ -> }, | ||
| 81 | onReset: () -> Unit = {}, | 107 | onReset: () -> Unit = {}, |
| 82 | ) { | 108 | ) { |
| 83 | AppTheme { | 109 | AppTheme { |
| 84 | var url by remember { mutableStateOf(defaultUrl) } | 110 | var url by remember { mutableStateOf(defaultUrl) } |
| 85 | var token by remember { mutableStateOf(defaultToken) } | 111 | var token by remember { mutableStateOf(defaultToken) } |
| 112 | + var e2eeKey by remember { mutableStateOf(defaultE2eeKey) } | ||
| 113 | + var e2eeOn by remember { mutableStateOf(defaultE2eeOn) } | ||
| 86 | val scrollState = rememberScrollState() | 114 | val scrollState = rememberScrollState() |
| 87 | // A surface container using the 'background' color from the theme | 115 | // A surface container using the 'background' color from the theme |
| 88 | Surface( | 116 | Surface( |
| @@ -119,13 +147,38 @@ class MainActivity : ComponentActivity() { | @@ -119,13 +147,38 @@ class MainActivity : ComponentActivity() { | ||
| 119 | modifier = Modifier.fillMaxWidth(), | 147 | modifier = Modifier.fillMaxWidth(), |
| 120 | ) | 148 | ) |
| 121 | 149 | ||
| 150 | + if (e2eeOn) { | ||
| 151 | + Spacer(modifier = Modifier.height(20.dp)) | ||
| 152 | + OutlinedTextField( | ||
| 153 | + value = e2eeKey, | ||
| 154 | + onValueChange = { e2eeKey = it }, | ||
| 155 | + label = { Text("E2EE Key") }, | ||
| 156 | + modifier = Modifier.fillMaxWidth(), | ||
| 157 | + enabled = e2eeOn | ||
| 158 | + ) | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + Spacer(modifier = Modifier.height(20.dp)) | ||
| 162 | + Row( | ||
| 163 | + horizontalArrangement = Arrangement.SpaceBetween, | ||
| 164 | + verticalAlignment = Alignment.CenterVertically, | ||
| 165 | + modifier = Modifier.fillMaxWidth() | ||
| 166 | + ) { | ||
| 167 | + Text("Enable E2EE") | ||
| 168 | + Switch( | ||
| 169 | + checked = e2eeOn, | ||
| 170 | + onCheckedChange = { e2eeOn = it }, | ||
| 171 | + modifier = Modifier.defaultMinSize(minHeight = 100.dp) | ||
| 172 | + ) | ||
| 173 | + } | ||
| 174 | + | ||
| 122 | Spacer(modifier = Modifier.height(20.dp)) | 175 | Spacer(modifier = Modifier.height(20.dp)) |
| 123 | - Button(onClick = { onConnect(url, token) }) { | 176 | + Button(onClick = { onConnect(url, token, e2eeKey, e2eeOn) }) { |
| 124 | Text("Connect") | 177 | Text("Connect") |
| 125 | } | 178 | } |
| 126 | 179 | ||
| 127 | Spacer(modifier = Modifier.height(20.dp)) | 180 | Spacer(modifier = Modifier.height(20.dp)) |
| 128 | - Button(onClick = { onSave(url, token) }) { | 181 | + Button(onClick = { onSave(url, token, e2eeKey, e2eeOn) }) { |
| 129 | Text("Save Values") | 182 | Text("Save Values") |
| 130 | } | 183 | } |
| 131 | 184 |
-
请 注册 或 登录 后发表评论