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

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.isImage
import sushi.hardcore.droidfs.ConstValues.Companion.isText import sushi.hardcore.droidfs.ConstValues.Companion.isText
import sushi.hardcore.droidfs.ConstValues.Companion.isVideo 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.R
import sushi.hardcore.droidfs.adapters.DialogSingleChoiceAdapter import sushi.hardcore.droidfs.adapters.DialogSingleChoiceAdapter
import sushi.hardcore.droidfs.adapters.ExplorerElementAdapter import sushi.hardcore.droidfs.adapters.ExplorerElementAdapter
import sushi.hardcore.droidfs.adapters.OpenAsDialogAdapter 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_operations.OperationFile
import sushi.hardcore.droidfs.file_viewers.AudioPlayer import sushi.hardcore.droidfs.file_viewers.AudioPlayer
import sushi.hardcore.droidfs.file_viewers.ImageViewer import sushi.hardcore.droidfs.file_viewers.ImageViewer
import sushi.hardcore.droidfs.file_viewers.TextEditor import sushi.hardcore.droidfs.file_viewers.TextEditor
import sushi.hardcore.droidfs.file_viewers.VideoPlayer 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.util.PathUtils
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
@ -214,12 +214,15 @@ open class BaseExplorerActivity : BaseActivity() {
} }
protected fun setCurrentPath(path: String) { protected fun setCurrentPath(path: String) {
synchronized(this) {
explorerElements = gocryptfsVolume.listDir(path) explorerElements = gocryptfsVolume.listDir(path)
}
textDirEmpty.visibility = if (explorerElements.size == 0) View.VISIBLE else View.INVISIBLE textDirEmpty.visibility = if (explorerElements.size == 0) View.VISIBLE else View.INVISIBLE
currentDirectoryPath = path currentDirectoryPath = path
currentPathText.text = getString(R.string.location, currentDirectoryPath) currentPathText.text = getString(R.string.location, currentDirectoryPath)
Thread{ Thread{
var totalSize: Long = 0 var totalSize: Long = 0
synchronized(this) {
for (element in explorerElements){ for (element in explorerElements){
if (element.isDirectory){ if (element.isDirectory){
var dirSize: Long = 0 var dirSize: Long = 0
@ -234,15 +237,20 @@ open class BaseExplorerActivity : BaseActivity() {
totalSize += element.size totalSize += element.size
} }
} }
}
runOnUiThread { runOnUiThread {
totalSizeText.text = getString(R.string.total_size, PathUtils.formatSize(totalSize)) totalSizeText.text = getString(R.string.total_size, PathUtils.formatSize(totalSize))
synchronized(this) {
sortExplorerElements() sortExplorerElements()
}
if (path.isNotEmpty()) { //not root if (path.isNotEmpty()) { //not root
synchronized(this) {
explorerElements.add( explorerElements.add(
0, 0,
ExplorerElement("..", (-1).toShort(), -1, -1, currentDirectoryPath) ExplorerElement("..", (-1).toShort(), -1, -1, currentDirectoryPath)
) )
} }
}
explorerAdapter.setExplorerElements(explorerElements) explorerAdapter.setExplorerElements(explorerElements)
} }
}.start() }.start()