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

View File

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

View File

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

View File

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

View File

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