Thread-safe directory listing

This commit is contained in:
Matéo Duparc 2021-06-26 12:09:29 +02:00
parent e5ed825557
commit f58517e904
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
1 changed files with 28 additions and 20 deletions

View File

@ -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)
}