diff --git a/data/src/main/java/com/github/nullptroma/wallenc/data/vaults/UnlockManager.kt b/data/src/main/java/com/github/nullptroma/wallenc/data/vaults/UnlockManager.kt index 1ffc7dd..3c4e806 100644 --- a/data/src/main/java/com/github/nullptroma/wallenc/data/vaults/UnlockManager.kt +++ b/data/src/main/java/com/github/nullptroma/wallenc/data/vaults/UnlockManager.kt @@ -22,7 +22,13 @@ class UnlockManager(dao: StorageKeyDao, ioDispatcher: CoroutineDispatcher): IUnl TODO("Not yet implemented") } - override fun close(storage: IStorage) { - TODO("Not yet implemented") + override fun close(uuid: UUID) { + val enc = _openedStorages.value[uuid] + if(enc == null) + return + _openedStorages.value = _openedStorages.value.toMutableMap().apply { + remove(uuid) + } + enc.dispose() } } \ No newline at end of file diff --git a/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorage.kt b/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorage.kt index d18c665..bdec978 100644 --- a/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorage.kt +++ b/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorage.kt @@ -4,8 +4,8 @@ import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo import com.github.nullptroma.wallenc.domain.interfaces.ILogger import com.github.nullptroma.wallenc.domain.interfaces.IStorage -import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.DisposableHandle import kotlinx.coroutines.flow.StateFlow import java.util.UUID @@ -14,7 +14,7 @@ class EncryptedStorage( key: EncryptKey, logger: ILogger, ioDispatcher: CoroutineDispatcher, -) : IStorage { +) : IStorage, DisposableHandle { override val size: StateFlow get() = TODO("Not yet implemented") override val numberOfFiles: StateFlow @@ -27,10 +27,14 @@ class EncryptedStorage( get() = TODO("Not yet implemented") override val encInfo: StateFlow get() = TODO("Not yet implemented") - override val accessor: IStorageAccessor = + override val accessor: EncryptedStorageAccessor = EncryptedStorageAccessor(source.accessor, key, logger, ioDispatcher) override suspend fun rename(newName: String) { TODO("Not yet implemented") } + + override fun dispose() { + accessor.dispose() + } } \ No newline at end of file diff --git a/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorageAccessor.kt b/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorageAccessor.kt index d5de5ea..4e816bf 100644 --- a/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorageAccessor.kt +++ b/domain/src/main/java/com/github/nullptroma/wallenc/domain/encrypt/EncryptedStorageAccessor.kt @@ -12,6 +12,8 @@ import com.github.nullptroma.wallenc.domain.interfaces.IMetaInfo import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.DisposableHandle +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow @@ -36,7 +38,10 @@ class EncryptedStorageAccessor( key: EncryptKey, private val logger: ILogger, ioDispatcher: CoroutineDispatcher -) : IStorageAccessor { +) : IStorageAccessor, DisposableHandle { + private val _job = Job() + private val _scope = CoroutineScope(ioDispatcher + _job) + override val size: StateFlow = source.size override val numberOfFiles: StateFlow = source.numberOfFiles override val isAvailable: StateFlow = source.isAvailable @@ -50,11 +55,11 @@ class EncryptedStorageAccessor( private val _secretKey = SecretKeySpec(key.to32Bytes(), "AES") init { - collectSourceState(CoroutineScope(ioDispatcher)) + collectSourceState() } - private fun collectSourceState(coroutineScope: CoroutineScope) { - coroutineScope.launch { + private fun collectSourceState() { + _scope.launch { launch { source.filesUpdates.collect { val files = it.data.map(::decryptEntity) @@ -246,6 +251,11 @@ class EncryptedStorageAccessor( source.moveToTrash(encryptPath(path)) } + override fun dispose() { + _job.cancel() + // TODO сделать удаление ключа, чтобы нельзя было вызвать ни один из методов + } + companion object { private const val IV_LEN = 16 diff --git a/domain/src/main/java/com/github/nullptroma/wallenc/domain/interfaces/IUnlockManager.kt b/domain/src/main/java/com/github/nullptroma/wallenc/domain/interfaces/IUnlockManager.kt index 35cd6ca..c519622 100644 --- a/domain/src/main/java/com/github/nullptroma/wallenc/domain/interfaces/IUnlockManager.kt +++ b/domain/src/main/java/com/github/nullptroma/wallenc/domain/interfaces/IUnlockManager.kt @@ -11,5 +11,5 @@ interface IUnlockManager { val openedStorages: StateFlow> fun open(storage: IStorage, key: EncryptKey) - fun close(storage: IStorage) + fun close(storage: UUID) } \ No newline at end of file