Skip to content

Commit

Permalink
Remove PresentationSession from Holder App (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitrejcevski authored Oct 10, 2023
1 parent 0b9b31e commit 4592ed6
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.android.identity.wallet.composables

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.android.identity.securearea.SecureArea
import com.android.identity.wallet.R

@Composable
fun curveLabelFor(
@SecureArea.EcCurve curveOption: Int
): String {
return when (curveOption) {
SecureArea.EC_CURVE_P256 -> stringResource(id = R.string.curve_p_256)
SecureArea.EC_CURVE_P384 -> stringResource(id = R.string.curve_p_384)
SecureArea.EC_CURVE_P521 -> stringResource(id = R.string.curve_p_521)
SecureArea.EC_CURVE_BRAINPOOLP256R1 -> stringResource(id = R.string.curve_brain_pool_p_256R1)
SecureArea.EC_CURVE_BRAINPOOLP320R1 -> stringResource(id = R.string.curve_brain_pool_p_320R1)
SecureArea.EC_CURVE_BRAINPOOLP384R1 -> stringResource(id = R.string.curve_brain_pool_p_384R1)
SecureArea.EC_CURVE_BRAINPOOLP512R1 -> stringResource(id = R.string.curve_brain_pool_p_512R1)
SecureArea.EC_CURVE_ED25519 -> stringResource(id = R.string.curve_ed25519)
SecureArea.EC_CURVE_X25519 -> stringResource(id = R.string.curve_x25519)
SecureArea.EC_CURVE_ED448 -> stringResource(id = R.string.curve_ed448)
SecureArea.EC_CURVE_X448 -> stringResource(id = R.string.curve_X448)
else -> ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.android.identity.securearea.SecureArea
import com.android.identity.wallet.R
import com.android.identity.wallet.composables.PreviewLightDark
import com.android.identity.wallet.composables.curveLabelFor
import com.android.identity.wallet.composables.gradientFor
import com.android.identity.wallet.composables.keystoreNameFor
import com.android.identity.wallet.document.DocumentColor
Expand Down Expand Up @@ -991,26 +991,6 @@ private fun mdocAuthOptionLabelFor(
}
}

@Composable
private fun curveLabelFor(
@SecureArea.EcCurve curveOption: Int
): String {
return when (curveOption) {
SecureArea.EC_CURVE_P256 -> stringResource(id = R.string.curve_p_256)
SecureArea.EC_CURVE_P384 -> stringResource(id = R.string.curve_p_384)
SecureArea.EC_CURVE_P521 -> stringResource(id = R.string.curve_p_521)
SecureArea.EC_CURVE_BRAINPOOLP256R1 -> stringResource(id = R.string.curve_brain_pool_p_256R1)
SecureArea.EC_CURVE_BRAINPOOLP320R1 -> stringResource(id = R.string.curve_brain_pool_p_320R1)
SecureArea.EC_CURVE_BRAINPOOLP384R1 -> stringResource(id = R.string.curve_brain_pool_p_384R1)
SecureArea.EC_CURVE_BRAINPOOLP512R1 -> stringResource(id = R.string.curve_brain_pool_p_512R1)
SecureArea.EC_CURVE_ED25519 -> stringResource(id = R.string.curve_ed25519)
SecureArea.EC_CURVE_X25519 -> stringResource(id = R.string.curve_x25519)
SecureArea.EC_CURVE_ED448 -> stringResource(id = R.string.curve_ed448)
SecureArea.EC_CURVE_X448 -> stringResource(id = R.string.curve_X448)
else -> ""
}
}

@StringRes
private fun documentNameFor(documentType: DocumentType): Int {
return when (documentType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SettingsFragment : Fragment() {
modifier = Modifier.fillMaxSize(),
screenState = state,
onAutoCloseChanged = settingsViewModel::onConnectionAutoCloseChanged,
onEphemeralKeyCurveChanged = settingsViewModel::onEphemeralKeyCurveChanged,
onUseStaticHandoverChanged = settingsViewModel::onUseStaticHandoverChanged,
onUseL2CAPChanged = settingsViewModel::onL2CAPChanged,
onBLEDataRetrievalModeChanged = settingsViewModel::onBleDataRetrievalChanged,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.android.identity.wallet.settings

import android.content.res.Configuration
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -9,21 +10,33 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.android.identity.wallet.composables.curveLabelFor
import com.android.identity.wallet.theme.HolderAppTheme

@Composable
fun SettingsScreen(
modifier: Modifier = Modifier,
screenState: SettingsScreenState,
onAutoCloseChanged: (Boolean) -> Unit,
onEphemeralKeyCurveChanged: (newValue: SettingsScreenState.EphemeralKeyCurveOption) -> Unit,
onUseStaticHandoverChanged: (Boolean) -> Unit,
onUseL2CAPChanged: (Boolean) -> Unit,
onBLEServiceCacheChanged: (Boolean) -> Unit,
Expand All @@ -49,6 +62,11 @@ fun SettingsScreen(
isChecked = screenState.autoCloseEnabled,
onCheckedChange = onAutoCloseChanged
)
SettingsDropDown(
title = "Ephemeral Key Curve",
description = curveLabelFor(screenState.ephemeralKeyCurveOption.toEcCurve()),
onCurveChanged = onEphemeralKeyCurveChanged
)
SettingSectionTitle(title = "NFC Engagement")
SettingToggle(
title = "Use static handover",
Expand Down Expand Up @@ -162,6 +180,63 @@ private fun SettingToggle(
}
}

@Composable
private fun SettingsDropDown(
modifier: Modifier = Modifier,
title: String,
description: String,
onCurveChanged: (selection: SettingsScreenState.EphemeralKeyCurveOption) -> Unit
) {
var dropDownExpanded by remember { mutableStateOf(false) }
val expandDropDown = { dropDownExpanded = true }
Row(
modifier = modifier.clickable { expandDropDown() },
verticalAlignment = Alignment.CenterVertically
) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = title,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurface
)
Text(
text = description,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface
)
}
IconButton(onClick = expandDropDown) {
Icon(
imageVector = Icons.Default.ArrowDropDown,
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurface
)
}
val entries = SettingsScreenState.EphemeralKeyCurveOption.values().toList()
DropdownMenu(
expanded = dropDownExpanded,
onDismissRequest = { dropDownExpanded = false }
) {
for (entry in entries) {
DropdownMenuItem(
modifier = modifier,
text = {
Text(
modifier = Modifier.fillMaxWidth(),
text = curveLabelFor(curveOption = entry.toEcCurve())
)
},
onClick = {
onCurveChanged(entry)
dropDownExpanded = false
}
)
}
}
}
}


@Preview(showBackground = true)
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
Expand All @@ -171,6 +246,7 @@ private fun SettingsScreenPreview() {
modifier = Modifier.fillMaxSize(),
screenState = SettingsScreenState(),
onAutoCloseChanged = {},
onEphemeralKeyCurveChanged = {},
onUseStaticHandoverChanged = {},
onUseL2CAPChanged = {},
onBLEServiceCacheChanged = {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.android.identity.wallet.settings

import android.os.Parcelable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import com.android.identity.securearea.SecureArea
import com.android.identity.securearea.SecureArea.EcCurve
import kotlinx.parcelize.Parcelize

@Stable
@Immutable
data class SettingsScreenState(
val autoCloseEnabled: Boolean = true,
val ephemeralKeyCurveOption: EphemeralKeyCurveOption = EphemeralKeyCurveOption.P256,
val useStaticHandover: Boolean = true,
val isL2CAPEnabled: Boolean = false,
val isBleClearCacheEnabled: Boolean = false,
Expand Down Expand Up @@ -48,4 +53,43 @@ data class SettingsScreenState(
|| wifiAwareEnabled
|| nfcEnabled
}

@Parcelize
enum class EphemeralKeyCurveOption : Parcelable {
P256,
P384,
P521,
BrainPoolP256R1,
BrainPoolP320R1,
BrainPoolP384R1,
BrainPoolP512R1;

fun toEcCurve(): Int {

return when (this) {
P256 -> SecureArea.EC_CURVE_P256
P384 -> SecureArea.EC_CURVE_P384
P521 -> SecureArea.EC_CURVE_P521
BrainPoolP256R1 -> SecureArea.EC_CURVE_BRAINPOOLP256R1
BrainPoolP320R1 -> SecureArea.EC_CURVE_BRAINPOOLP320R1
BrainPoolP384R1 -> SecureArea.EC_CURVE_BRAINPOOLP384R1
BrainPoolP512R1 -> SecureArea.EC_CURVE_BRAINPOOLP512R1
}
}

companion object {
fun fromEcCurve(@EcCurve curve: Int): EphemeralKeyCurveOption {
return when (curve) {
SecureArea.EC_CURVE_P256 -> P256
SecureArea.EC_CURVE_P384 -> P384
SecureArea.EC_CURVE_P521 -> P521
SecureArea.EC_CURVE_BRAINPOOLP256R1 -> BrainPoolP256R1
SecureArea.EC_CURVE_BRAINPOOLP320R1 -> BrainPoolP320R1
SecureArea.EC_CURVE_BRAINPOOLP384R1 -> BrainPoolP384R1
SecureArea.EC_CURVE_BRAINPOOLP512R1 -> BrainPoolP512R1
else -> throw IllegalStateException("Unknown EcCurve")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class SettingsViewModel : ViewModel() {
fun loadSettings() {
val settingsState = SettingsScreenState(
autoCloseEnabled = PreferencesHelper.isConnectionAutoCloseEnabled(),
ephemeralKeyCurveOption = SettingsScreenState.EphemeralKeyCurveOption.fromEcCurve(
PreferencesHelper.getEphemeralKeyCurveOption()
),
useStaticHandover = PreferencesHelper.shouldUseStaticHandover(),
isL2CAPEnabled = PreferencesHelper.isBleL2capEnabled(),
isBleClearCacheEnabled = PreferencesHelper.isBleClearCacheEnabled(),
Expand All @@ -31,6 +34,13 @@ class SettingsViewModel : ViewModel() {
mutableSettingsState.update { it.copy(autoCloseEnabled = newValue) }
}

fun onEphemeralKeyCurveChanged(
ephemeralKeyCurveOption: SettingsScreenState.EphemeralKeyCurveOption
) {
PreferencesHelper.setEphemeralKeyCurveOption(ephemeralKeyCurveOption.toEcCurve())
mutableSettingsState.update { it.copy(ephemeralKeyCurveOption = ephemeralKeyCurveOption) }
}

fun onUseStaticHandoverChanged(newValue: Boolean) {
PreferencesHelper.setUseStaticHandover(newValue)
mutableSettingsState.update { it.copy(useStaticHandover = newValue) }
Expand Down

This file was deleted.

Loading

0 comments on commit 4592ed6

Please sign in to comment.