Async file loading in file viewers

This commit is contained in:
Matéo Duparc 2022-10-04 13:30:51 +02:00
parent cdc269f2f7
commit 34aad2596d
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
5 changed files with 35 additions and 26 deletions

View File

@ -1,10 +1,7 @@
package sushi.hardcore.droidfs
import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
@ -17,8 +14,8 @@ open class BaseActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
themeValue = sharedPrefs.getString(ConstValues.THEME_VALUE_KEY, ConstValues.DEFAULT_THEME_VALUE)!!
if (shouldCheckTheme && applyCustomTheme) {
themeValue = sharedPrefs.getString(ConstValues.THEME_VALUE_KEY, ConstValues.DEFAULT_THEME_VALUE)!!
when (themeValue) {
"black_green" -> setTheme(R.style.BlackGreen)
"dark_red" -> setTheme(R.style.DarkRed)

View File

@ -6,6 +6,11 @@ import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import sushi.hardcore.droidfs.BaseActivity
import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R
@ -81,22 +86,29 @@ abstract class FileViewerActivity: BaseActivity() {
}
}
protected fun loadWholeFile(path: String, fileSize: Long? = null): ByteArray? {
val result = encryptedVolume.loadWholeFile(path, size = fileSize)
if (result.second != 0) {
val dialog = CustomAlertDialogBuilder(this, themeValue)
.setTitle(R.string.error)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
when (result.second) {
1 -> dialog.setMessage(R.string.get_size_failed)
2 -> dialog.setMessage(R.string.outofmemoryerror_msg)
3 -> dialog.setMessage(R.string.read_file_failed)
4 -> dialog.setMessage(R.string.io_error)
protected fun loadWholeFile(path: String, fileSize: Long? = null, callback: (ByteArray) -> Unit) {
lifecycleScope.launch(Dispatchers.IO) {
val result = encryptedVolume.loadWholeFile(path, size = fileSize)
if (isActive) {
withContext(Dispatchers.Main) {
if (result.second == 0) {
callback(result.first!!)
} else {
val dialog = CustomAlertDialogBuilder(this@FileViewerActivity, themeValue)
.setTitle(R.string.error)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
when (result.second) {
1 -> dialog.setMessage(R.string.get_size_failed)
2 -> dialog.setMessage(R.string.outofmemoryerror_msg)
3 -> dialog.setMessage(R.string.read_file_failed)
4 -> dialog.setMessage(R.string.io_error)
}
dialog.show()
}
}
}
dialog.show()
}
return result.first
}
protected fun createPlaylist() {

View File

@ -163,13 +163,13 @@ class ImageViewer: FileViewerActivity() {
}
private fun loadImage(){
fileName = File(filePath).name
binding.textFilename.text = fileName
requestBuilder = null
loadWholeFile(filePath)?.let {
loadWholeFile(filePath) {
originalOrientation = 0f
requestBuilder = Glide.with(this).load(it)
requestBuilder?.into(binding.imageViewer)
fileName = File(filePath).name
binding.textFilename.text = fileName
rotationAngle = originalOrientation
}
}

View File

@ -22,7 +22,7 @@ class PdfViewer: FileViewerActivity() {
val fileName = File(filePath).name
title = fileName
val fileSize = encryptedVolume.getAttr(filePath)?.size
loadWholeFile(filePath, fileSize)?.let {
loadWholeFile(filePath, fileSize) {
pdfViewer.loadPdf(ByteArrayInputStream(it), fileName, fileSize)
}
}

View File

@ -23,8 +23,10 @@ class TextEditor: FileViewerActivity() {
}
override fun viewFile() {
loadWholeFile(filePath)?.let {
fileName = File(filePath).name
fileName = File(filePath).name
title = fileName
supportActionBar?.setDisplayHomeAsUpEnabled(true)
loadWholeFile(filePath) {
try {
loadLayout(String(it))
} catch (e: OutOfMemoryError){
@ -43,8 +45,6 @@ class TextEditor: FileViewerActivity() {
} else {
setContentView(R.layout.activity_text_editor)
}
title = fileName
supportActionBar?.setDisplayHomeAsUpEnabled(true)
editor = findViewById(R.id.text_editor)
editor.setText(fileContent)
editor.addTextChangedListener(object: TextWatcher {