From 34aad2596d8b306389fc0b8e50e90f2cb5100a47 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 4 Oct 2022 13:30:51 +0200 Subject: [PATCH] Async file loading in file viewers --- .../sushi/hardcore/droidfs/BaseActivity.kt | 5 +-- .../file_viewers/FileViewerActivity.kt | 40 ++++++++++++------- .../droidfs/file_viewers/ImageViewer.kt | 6 +-- .../droidfs/file_viewers/PdfViewer.kt | 2 +- .../droidfs/file_viewers/TextEditor.kt | 8 ++-- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt index a968680..aee1583 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt @@ -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) diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt index d1b0012..610386c 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt @@ -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() { diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt index d97558f..b6ed103 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt @@ -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 } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/PdfViewer.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/PdfViewer.kt index 5844757..8d4c803 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/PdfViewer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/PdfViewer.kt @@ -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) } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt index 563fe65..fce60ba 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt @@ -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 {