From f58517e9043d845cd89862cf8cc8916c40470f29 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sat, 26 Jun 2021 12:09:29 +0200 Subject: [PATCH] Thread-safe directory listing --- .../droidfs/explorers/BaseExplorerActivity.kt | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index c374ee2..672d488 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -26,19 +26,19 @@ import sushi.hardcore.droidfs.ConstValues.Companion.isAudio import sushi.hardcore.droidfs.ConstValues.Companion.isImage import sushi.hardcore.droidfs.ConstValues.Companion.isText import sushi.hardcore.droidfs.ConstValues.Companion.isVideo -import sushi.hardcore.droidfs.file_operations.FileOperationService +import sushi.hardcore.droidfs.GocryptfsVolume import sushi.hardcore.droidfs.R import sushi.hardcore.droidfs.adapters.DialogSingleChoiceAdapter import sushi.hardcore.droidfs.adapters.ExplorerElementAdapter import sushi.hardcore.droidfs.adapters.OpenAsDialogAdapter +import sushi.hardcore.droidfs.content_providers.ExternalProvider +import sushi.hardcore.droidfs.content_providers.RestrictedFileProvider +import sushi.hardcore.droidfs.file_operations.FileOperationService import sushi.hardcore.droidfs.file_operations.OperationFile import sushi.hardcore.droidfs.file_viewers.AudioPlayer import sushi.hardcore.droidfs.file_viewers.ImageViewer import sushi.hardcore.droidfs.file_viewers.TextEditor import sushi.hardcore.droidfs.file_viewers.VideoPlayer -import sushi.hardcore.droidfs.content_providers.RestrictedFileProvider -import sushi.hardcore.droidfs.content_providers.ExternalProvider -import sushi.hardcore.droidfs.GocryptfsVolume import sushi.hardcore.droidfs.util.PathUtils import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder @@ -214,34 +214,42 @@ open class BaseExplorerActivity : BaseActivity() { } protected fun setCurrentPath(path: String) { - explorerElements = gocryptfsVolume.listDir(path) + synchronized(this) { + explorerElements = gocryptfsVolume.listDir(path) + } textDirEmpty.visibility = if (explorerElements.size == 0) View.VISIBLE else View.INVISIBLE currentDirectoryPath = path currentPathText.text = getString(R.string.location, currentDirectoryPath) Thread{ var totalSize: Long = 0 - for (element in explorerElements){ - if (element.isDirectory){ - var dirSize: Long = 0 - for (subFile in gocryptfsVolume.recursiveMapFiles(element.fullPath)){ - if (subFile.isRegularFile){ - dirSize += subFile.size + synchronized(this) { + for (element in explorerElements){ + if (element.isDirectory){ + var dirSize: Long = 0 + for (subFile in gocryptfsVolume.recursiveMapFiles(element.fullPath)){ + if (subFile.isRegularFile){ + dirSize += subFile.size + } } + element.size = dirSize + totalSize += dirSize + } else if (element.isRegularFile) { + totalSize += element.size } - element.size = dirSize - totalSize += dirSize - } else if (element.isRegularFile) { - totalSize += element.size } } runOnUiThread { totalSizeText.text = getString(R.string.total_size, PathUtils.formatSize(totalSize)) - sortExplorerElements() + synchronized(this) { + sortExplorerElements() + } if (path.isNotEmpty()) { //not root - explorerElements.add( - 0, - ExplorerElement("..", (-1).toShort(), -1, -1, currentDirectoryPath) - ) + synchronized(this) { + explorerElements.add( + 0, + ExplorerElement("..", (-1).toShort(), -1, -1, currentDirectoryPath) + ) + } } explorerAdapter.setExplorerElements(explorerElements) }