Clear SDK cache properly (#4396)

* Use close() instead of destroy, because close() is synchronized.

* Use new method to clear the SDK cache.

* Format file.

* Remove the legacy way to clear the SDK cache.

* Remove unused import

* revert name change
This commit is contained in:
Benoit Marty 2025-03-20 14:33:59 +01:00 committed by GitHub
parent f85abe27fc
commit 93d27735e4
6 changed files with 41 additions and 33 deletions

View file

@ -8,7 +8,6 @@
package io.element.android.libraries.matrix.impl
import io.element.android.libraries.androidutils.file.getSizeOfFiles
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.bool.orFalse
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.coroutine.childScope
@ -488,11 +487,11 @@ class RustMatrixClient(
verificationService.destroy()
sessionDelegate.clearCurrentClient()
innerRoomListService.destroy()
notificationService.destroy()
innerRoomListService.close()
notificationService.close()
notificationProcessSetup.destroy()
encryptionService.destroy()
innerClient.destroy()
encryptionService.close()
innerClient.close()
}
override suspend fun getCacheSize(): Long {
@ -500,8 +499,8 @@ class RustMatrixClient(
}
override suspend fun clearCache() {
innerClient.clearCaches()
close()
deleteSessionDirectory(deleteCryptoDb = false)
}
override suspend fun logout(userInitiated: Boolean, ignoreSdkError: Boolean) {
@ -526,7 +525,7 @@ class RustMatrixClient(
}
close()
deleteSessionDirectory(deleteCryptoDb = true)
deleteSessionDirectory()
if (userInitiated) {
sessionStore.removeSession(sessionId.value)
}
@ -575,7 +574,7 @@ class RustMatrixClient(
}
}
close()
deleteSessionDirectory(deleteCryptoDb = true)
deleteSessionDirectory()
sessionStore.removeSession(sessionId.value)
}.onFailure {
Timber.e(it, "Failed to deactivate account")
@ -663,25 +662,9 @@ class RustMatrixClient(
}
}
private suspend fun deleteSessionDirectory(
deleteCryptoDb: Boolean = false,
): Boolean = withContext(sessionDispatcher) {
val sessionPaths = sessionPathsProvider.provides(sessionId) ?: return@withContext false
// Always delete the cache directory
sessionPaths.cacheDirectory.deleteRecursively()
if (deleteCryptoDb) {
// Delete the folder and all its content
sessionPaths.fileDirectory.deleteRecursively()
} else {
// Do not delete the crypto database files.
sessionPaths.fileDirectory.listFiles().orEmpty()
.filterNot { it.name.contains("matrix-sdk-crypto") }
.forEach { file ->
Timber.w("Deleting file ${file.name}...")
file.safeDelete()
}
true
}
private suspend fun deleteSessionDirectory() = withContext(sessionDispatcher) {
// Delete all the files for this session
sessionPathsProvider.provides(sessionId)?.deleteRecursively()
}
}

View file

@ -232,7 +232,7 @@ internal class RustEncryptionService(
) ?: error("User identity not found")
}
fun destroy() {
service.destroy()
fun close() {
service.close()
}
}

View file

@ -36,7 +36,7 @@ class RustNotificationService(
}
}
fun destroy() {
notificationClient.destroy()
fun close() {
notificationClient.close()
}
}