Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpeziStorage - All changes #125

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0fb7343
Add SpeziStorage
pauljohanneskraft Sep 21, 2024
f1a6105
Merge branch 'main' into feature/spezi-storage
pauljohanneskraft Sep 24, 2024
0ed5bea
detekt
pauljohanneskraft Oct 2, 2024
a7dc44d
Finish up SpeziStorage
pauljohanneskraft Oct 3, 2024
86da8af
detekt
pauljohanneskraft Oct 3, 2024
953073c
Remove unused code
pauljohanneskraft Oct 3, 2024
dbfe0a8
Merge branch 'main' into feature/spezi-storage
pauljohanneskraft Oct 3, 2024
01125f7
detekt
pauljohanneskraft Oct 3, 2024
3e35f37
Use OAEPPadding
pauljohanneskraft Oct 3, 2024
072b5d2
Use other padding
pauljohanneskraft Oct 3, 2024
cc1ee5e
Update
pauljohanneskraft Oct 3, 2024
86e4129
Remove unnecessary char
pauljohanneskraft Oct 3, 2024
0d0ff55
Review adaptions
pauljohanneskraft Oct 3, 2024
acdd6d6
Merge branch 'main' into feature/spezi-storage
Basler182 Oct 6, 2024
f5bcd32
merge main
eldcn Oct 6, 2024
cb74f4d
storage review proposal
eldcn Oct 13, 2024
8456392
implement localstorage, securestorage, key value storage and add tests
eldcn Oct 13, 2024
5e873bb
method sorting
eldcn Oct 13, 2024
231e416
use OAEP padding for rsa encryption
eldcn Oct 13, 2024
02d82a1
introduce clear on android key store and use enum for SecureStorageIt…
eldcn Oct 13, 2024
24e406f
fix detekt
eldcn Oct 13, 2024
89000f8
Apply suggestion
pauljohanneskraft Oct 14, 2024
d71db1f
add review comments
eldcn Oct 16, 2024
39b2901
Merge branch 'main' into storage/review
Basler182 Oct 17, 2024
6bd38de
Merge branch 'main' into feature/spezi-storage
pauljohanneskraft Oct 21, 2024
1cc85b4
Update implementation to include KeyStorage and CredentialStorage sep…
pauljohanneskraft Oct 21, 2024
c20a642
Update SpeziStorage
pauljohanneskraft Oct 21, 2024
7fd0406
detekt
pauljohanneskraft Oct 21, 2024
9e4af65
Fix binding
pauljohanneskraft Oct 21, 2024
9248983
fix tests
pauljohanneskraft Oct 21, 2024
e90f910
Merge branch 'feature/spezi-storage' into storage/review
pauljohanneskraft Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import edu.stanford.spezi.modules.storage.di.Storage
import edu.stanford.spezi.modules.storage.key.KeyValueStorage
import edu.stanford.spezi.modules.storage.key.getSerializableList
import edu.stanford.spezi.modules.storage.key.putSerializable
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -23,14 +22,11 @@ internal class BLEPairedDevicesStorage @Inject constructor(
private val bluetoothAdapter: BluetoothAdapter,
private val bleDevicePairingNotifier: BLEDevicePairingNotifier,
@Storage.Encrypted
private val encryptedKeyValueStorage: KeyValueStorage,
private val storage: KeyValueStorage,
private val timeProvider: TimeProvider,
@Dispatching.IO private val ioScope: CoroutineScope,
) {
private val logger by speziLogger()
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, error ->
logger.e(error) { "Error executing paired devices storage operations" }
}

private val _pairedDevices = MutableStateFlow(emptyList<BLEDevice>())
val pairedDevices = _pairedDevices.asStateFlow()
Expand All @@ -40,8 +36,8 @@ internal class BLEPairedDevicesStorage @Inject constructor(
observeUnpairingEvents()
}

fun updateDeviceConnection(device: BluetoothDevice, connected: Boolean) = execute {
if (isPaired(device).not()) return@execute
fun updateDeviceConnection(device: BluetoothDevice, connected: Boolean) {
if (isPaired(device).not()) return
val currentDevices = getCurrentStoredDevices()
currentDevices.removeAll { it.address == device.address }
val newDevice = BLEDevice(
Expand All @@ -54,24 +50,24 @@ internal class BLEPairedDevicesStorage @Inject constructor(
update(devices = currentDevices + newDevice)
}

private fun refreshState() = execute {
val systemBoundDevices = bluetoothAdapter.bondedDevices ?: return@execute
private fun refreshState() {
val systemBoundDevices = bluetoothAdapter.bondedDevices ?: return
val newDevices = getCurrentStoredDevices().filter { storedDevice ->
systemBoundDevices.any { it.address == storedDevice.address }
}
logger.i { "refreshing with $newDevices" }
update(devices = newDevices)
}

fun onStopped() = execute {
fun onStopped() {
val devices = getCurrentStoredDevices().map {
it.copy(connected = false, lastSeenTimeStamp = timeProvider.currentTimeMillis())
}
update(devices = devices)
}

private fun update(devices: List<BLEDevice>) = execute {
encryptedKeyValueStorage.putSerializable(key = KEY, devices)
private fun update(devices: List<BLEDevice>) {
storage.putSerializable(key = KEY, devices)
_pairedDevices.update { devices }
logger.i { "Updating local storage with $devices" }
}
Expand Down Expand Up @@ -112,12 +108,8 @@ internal class BLEPairedDevicesStorage @Inject constructor(
}
}

private suspend fun getCurrentStoredDevices() =
encryptedKeyValueStorage.getSerializableList<BLEDevice>(key = KEY).toMutableList()

private fun execute(block: suspend () -> Unit) {
ioScope.launch(coroutineExceptionHandler) { block() }
}
private fun getCurrentStoredDevices() =
storage.getSerializableList<BLEDevice>(key = KEY).toMutableList()

private companion object {
const val KEY = "paired_ble_devices"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BLEPairedDevicesStorageTest {
private val pairedDevicesStorage by lazy {
BLEPairedDevicesStorage(
bluetoothAdapter = adapter,
encryptedKeyValueStorage = storage,
storage = storage,
ioScope = SpeziTestScope(),
bleDevicePairingNotifier = bleDevicePairingNotifier,
timeProvider = timeProvider,
Expand Down

This file was deleted.

Loading
Loading