Added lazy initialization of DownloadFile status for better SAF performance

Minor fixes
This commit is contained in:
Nite 2021-12-15 17:44:19 +01:00
parent 66e7732ec2
commit d51544f927
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
6 changed files with 70 additions and 38 deletions

View File

@ -153,13 +153,27 @@ public class StreamProxy implements Runnable
} }
Timber.i("Processing request for file %s", localPath); Timber.i("Processing request for file %s", localPath);
if (!Storage.INSTANCE.isPathExists(localPath)) { if (Storage.INSTANCE.isPathExists(localPath)) return true;
Timber.e("File %s does not exist", localPath);
return false;
}
return true; // Usually the .partial file will be requested here, but sometimes it has already
} // been renamed, so check if it is completed since
String saveFileName = FileUtil.INSTANCE.getSaveFile(localPath);
String completeFileName = FileUtil.INSTANCE.getCompleteFile(saveFileName);
if (Storage.INSTANCE.isPathExists(saveFileName)) {
localPath = saveFileName;
return true;
}
if (Storage.INSTANCE.isPathExists(completeFileName)) {
localPath = completeFileName;
return true;
}
Timber.e("File %s does not exist", localPath);
return false;
}
@Override @Override
public void run() public void run()

View File

@ -42,8 +42,8 @@ class DownloadFile(
save: Boolean save: Boolean
) : KoinComponent, Identifiable { ) : KoinComponent, Identifiable {
val partialFile: String val partialFile: String
val completeFile: String lateinit var completeFile: String
private val saveFile: String = FileUtil.getSongFile(song) val saveFile: String = FileUtil.getSongFile(song)
var shouldSave = save var shouldSave = save
private var downloadTask: CancellableTask? = null private var downloadTask: CancellableTask? = null
var isFailed = false var isFailed = false
@ -68,29 +68,30 @@ class DownloadFile(
private val activeServerProvider: ActiveServerProvider by inject() private val activeServerProvider: ActiveServerProvider by inject()
val progress: MutableLiveData<Int> = MutableLiveData(0) val progress: MutableLiveData<Int> = MutableLiveData(0)
val status: MutableLiveData<DownloadStatus>
val lazyStatus: Lazy<DownloadStatus> = lazy {
when {
Storage.isPathExists(saveFile) -> {
DownloadStatus.PINNED
}
Storage.isPathExists(completeFile) -> {
DownloadStatus.DONE
}
else -> {
DownloadStatus.IDLE
}
}
}
val status: MutableLiveData<DownloadStatus> by lazy {
MutableLiveData(lazyStatus.value)
}
init { init {
val state: DownloadStatus
partialFile = FileUtil.getParentPath(saveFile) + "/" + partialFile = FileUtil.getParentPath(saveFile) + "/" +
FileUtil.getPartialFile(FileUtil.getNameFromPath(saveFile)) FileUtil.getPartialFile(FileUtil.getNameFromPath(saveFile))
completeFile = FileUtil.getParentPath(saveFile) + "/" + completeFile = FileUtil.getParentPath(saveFile) + "/" +
FileUtil.getCompleteFile(FileUtil.getNameFromPath(saveFile)) FileUtil.getCompleteFile(FileUtil.getNameFromPath(saveFile))
when {
Storage.isPathExists(saveFile) -> {
state = DownloadStatus.PINNED
}
Storage.isPathExists(completeFile) -> {
state = DownloadStatus.DONE
}
else -> {
state = DownloadStatus.IDLE
}
}
status = MutableLiveData(state)
} }
/** /**

View File

@ -267,7 +267,8 @@ class Downloader(
temp.addAll(downloadQueue) temp.addAll(downloadQueue)
temp.addAll( temp.addAll(
playlist.filter { playlist.filter {
when (it.status.value) { if (!it.lazyStatus.isInitialized()) false
else when (it.status.value) {
DownloadStatus.DOWNLOADING -> true DownloadStatus.DOWNLOADING -> true
else -> false else -> false
} }

View File

@ -259,7 +259,8 @@ class CacheCleaner : CoroutineScope by CoroutineScope(Dispatchers.IO) {
for (downloadFile in downloader.value.all) { for (downloadFile in downloader.value.all) {
filesToNotDelete.add(downloadFile.partialFile) filesToNotDelete.add(downloadFile.partialFile)
filesToNotDelete.add(downloadFile.completeOrSaveFile) filesToNotDelete.add(downloadFile.completeFile)
filesToNotDelete.add(downloadFile.saveFile)
} }
filesToNotDelete.add(musicDirectory.path) filesToNotDelete.add(musicDirectory.path)

View File

@ -401,6 +401,14 @@ object FileUtil {
return path.substringBeforeLast('/') return path.substringBeforeLast('/')
} }
fun getSaveFile(name: String): String {
val baseName = getBaseName(name)
if (baseName.endsWith(".partial") || baseName.endsWith(".complete")) {
return "${getBaseName(baseName)}.${getExtension(name)}"
}
return name
}
/** /**
* Returns the file name of a .complete file of the given file. * Returns the file name of a .complete file of the given file.
* *

View File

@ -28,24 +28,32 @@ class StorageFile(
override val length: Long override val length: Long
get() { get() {
val resolver = UApp.applicationContext().contentResolver try {
val column = arrayOf(DocumentsContract.Document.COLUMN_SIZE) val resolver = UApp.applicationContext().contentResolver
resolver.query(uri, column, null, null, null)?.use { cursor -> val column = arrayOf(DocumentsContract.Document.COLUMN_SIZE)
if (cursor.moveToFirst()) { resolver.query(uri, column, null, null, null)?.use { cursor ->
return cursor.getLong(0) if (cursor.moveToFirst()) {
return cursor.getLong(0)
}
} }
} catch (_: IllegalArgumentException) {
Timber.d("Tried to get length of $uri but it probably doesn't exists")
} }
return 0 return 0
} }
override val lastModified: Long override val lastModified: Long
get() { get() {
val resolver = UApp.applicationContext().contentResolver try {
val column = arrayOf(DocumentsContract.Document.COLUMN_LAST_MODIFIED) val resolver = UApp.applicationContext().contentResolver
resolver.query(uri, column, null, null, null)?.use { cursor -> val column = arrayOf(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
if (cursor.moveToFirst()) { resolver.query(uri, column, null, null, null)?.use { cursor ->
return cursor.getLong(0) if (cursor.moveToFirst()) {
return cursor.getLong(0)
}
} }
} catch (_: IllegalArgumentException) {
Timber.d("Tried to get length of $uri but it probably doesn't exists")
} }
return 0 return 0
} }
@ -129,7 +137,6 @@ class StorageFile(
val fileName = FileUtil.getNameFromPath(path) val fileName = FileUtil.getNameFromPath(path)
var file: StorageFile? = null var file: StorageFile? = null
Timber.v("StorageFile getFromPath listing files on path: $path")
parent.listFiles().forEach { parent.listFiles().forEach {
if (it.name == fileName) file = it as StorageFile if (it.name == fileName) file = it as StorageFile
storageFilePathDictionary[it.path] = it as StorageFile storageFilePathDictionary[it.path] = it as StorageFile