show file name at ReadTextActivity if available

This commit is contained in:
tibbi 2022-05-15 20:19:24 +02:00
parent 376b521170
commit e0802b72fe
1 changed files with 49 additions and 44 deletions

View File

@ -54,13 +54,32 @@ class ReadTextActivity : SimpleActivity() {
searchPrevBtn = findViewById(R.id.search_previous) searchPrevBtn = findViewById(R.id.search_previous)
searchNextBtn = findViewById(R.id.search_next) searchNextBtn = findViewById(R.id.search_next)
searchClearBtn = findViewById(R.id.search_clear) searchClearBtn = findViewById(R.id.search_clear)
read_text_view.setTextColor(config.textColor)
if (checkAppSideloading()) { if (checkAppSideloading()) {
return return
} }
val uri = if (intent.extras?.containsKey(REAL_FILE_PATH) == true) {
Uri.fromFile(File(intent.extras?.get(REAL_FILE_PATH).toString()))
} else {
intent.data
}
if (uri == null) {
finish()
return
}
val filename = getFilenameFromUri(uri)
if (filename.isNotEmpty()) {
title = filename
}
read_text_view.onGlobalLayout { read_text_view.onGlobalLayout {
checkIntent() ensureBackgroundThread {
checkIntent(uri)
}
} }
setupSearchButtons() setupSearchButtons()
@ -213,54 +232,40 @@ class ReadTextActivity : SimpleActivity() {
} }
} }
private fun checkIntent() { private fun checkIntent(uri: Uri) {
read_text_view.setTextColor(config.textColor) originalText = if (uri.scheme == "file") {
val uri = if (intent.extras?.containsKey(REAL_FILE_PATH) == true) { filePath = uri.path!!
Uri.fromFile(File(intent.extras?.get(REAL_FILE_PATH).toString())) val file = File(filePath)
} else { if (file.exists()) {
intent.data try {
} file.readText()
} catch (e: Exception) {
if (uri == null) { showErrorToast(e)
finish()
return
}
ensureBackgroundThread {
originalText = if (uri.scheme == "file") {
filePath = uri.path!!
val file = File(filePath)
if (file.exists()) {
try {
file.readText()
} catch (e: Exception) {
showErrorToast(e)
""
}
} else {
toast(R.string.unknown_error_occurred)
"" ""
} }
} else { } else {
try { toast(R.string.unknown_error_occurred)
contentResolver.openInputStream(uri)!!.bufferedReader().use { it.readText() } ""
} catch (e: OutOfMemoryError) {
showErrorToast(e.toString())
return@ensureBackgroundThread
} catch (e: Exception) {
showErrorToast(e)
finish()
return@ensureBackgroundThread
}
} }
} else {
try {
contentResolver.openInputStream(uri)!!.bufferedReader().use { it.readText() }
} catch (e: OutOfMemoryError) {
showErrorToast(e.toString())
return
} catch (e: Exception) {
showErrorToast(e)
finish()
return
}
}
runOnUiThread { runOnUiThread {
read_text_view.setText(originalText) read_text_view.setText(originalText)
if (originalText.isNotEmpty()) { if (originalText.isNotEmpty()) {
hideKeyboard() hideKeyboard()
} else { } else {
showKeyboard(read_text_view) showKeyboard(read_text_view)
}
} }
} }
} }