Cleared MainActivity

This commit is contained in:
merkost 2023-07-07 23:16:58 +10:00
parent 323ca2c56e
commit 9503b1e5da
2 changed files with 16 additions and 242 deletions

View File

@ -49,7 +49,6 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_checklist.*
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.nio.charset.Charset
import java.util.*
@ -63,9 +62,6 @@ class MainActivity : SimpleActivity() {
private val PICK_OPEN_FILE_INTENT = 1
private val PICK_EXPORT_FILE_INTENT = 2
private val PICK_IMPORT_NOTES_INTENT = 3
private val PICK_EXPORT_NOTES_INTENT = 4
private lateinit var mCurrentNote: Note
private var mNotes = listOf<Note>()
private var mAdapter: NotesPagerAdapter? = null
@ -183,12 +179,10 @@ class MainActivity : SimpleActivity() {
findItem(R.id.rename_note).isVisible = multipleNotesExist
findItem(R.id.open_note).isVisible = multipleNotesExist
findItem(R.id.delete_note).isVisible = multipleNotesExist
findItem(R.id.export_all_notes).isVisible = multipleNotesExist
findItem(R.id.open_search).isVisible = !isCurrentItemChecklist
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist
findItem(R.id.sort_checklist).isVisible = isCurrentItemChecklist
findItem(R.id.import_folder).isVisible = !isQPlus()
findItem(R.id.import_notes).isVisible = isQPlus()
findItem(R.id.lock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && !mCurrentNote.isLocked())
findItem(R.id.unlock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(R.bool.hide_google_relations)
@ -223,8 +217,6 @@ class MainActivity : SimpleActivity() {
R.id.open_file -> tryOpenFile()
R.id.import_folder -> openFolder()
R.id.export_as_file -> fragment?.handleUnlocking { tryExportAsFile() }
R.id.export_all_notes -> tryExportNotes()
R.id.import_notes -> tryImportNotes()
R.id.print -> fragment?.handleUnlocking { printText() }
R.id.delete_note -> fragment?.handleUnlocking { displayDeleteNotePrompt() }
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
@ -288,11 +280,6 @@ class MainActivity : SimpleActivity() {
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
applicationContext.contentResolver.takePersistableUriPermission(resultData.data!!, takeFlags)
showExportFilePickUpdateDialog(resultData.dataString!!, getCurrentNoteValue())
} else if (requestCode == PICK_EXPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
val outputStream = contentResolver.openOutputStream(resultData.data!!)
exportNotesTo(outputStream)
} else if (requestCode == PICK_IMPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
tryImportingAsJson(resultData.data!!)
}
}
@ -732,11 +719,6 @@ class MainActivity : SimpleActivity() {
}
private fun importUri(uri: Uri) {
tryImportingAsJson(uri, force = true, showToasts = false) { success ->
if (success) {
return@tryImportingAsJson
}
when (uri.scheme) {
"file" -> openPath(uri.path!!)
"content" -> {
@ -759,7 +741,6 @@ class MainActivity : SimpleActivity() {
}
}
}
}
private fun addNoteFromUri(uri: Uri, filename: String? = null) {
val noteTitle = when {
@ -896,138 +877,6 @@ class MainActivity : SimpleActivity() {
}
}
private fun tryExportNotes() {
if (isQPlus()) {
hideKeyboard()
val fileName = "${getString(R.string.notes)}_${getCurrentFormattedDateTime()}"
Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
type = EXPORT_MIME_TYPE
putExtra(Intent.EXTRA_TITLE, fileName)
addCategory(Intent.CATEGORY_OPENABLE)
try {
startActivityForResult(this, PICK_EXPORT_NOTES_INTENT)
} catch (e: ActivityNotFoundException) {
toast(R.string.system_service_disabled, Toast.LENGTH_LONG)
} catch (e: Exception) {
showErrorToast(e)
}
}
} else {
tryExportAllNotesBelowQ()
}
}
private fun requestUnlockNotes(callback: (unlockedNoteIds: List<Long>) -> Unit) {
val lockedNotes = mNotes.filter { it.isLocked() }
if (lockedNotes.isNotEmpty()) {
runOnUiThread {
UnlockNotesDialog(this, lockedNotes, callback)
}
} else {
callback(emptyList())
}
}
private fun exportNotesTo(outputStream: OutputStream?) {
ensureBackgroundThread {
NotesHelper(this).getNotes {
mNotes = it
requestUnlockNotes { unlockedNoteIds ->
toast(R.string.exporting)
val notesExporter = NotesExporter(this)
notesExporter.exportNotes(mNotes, unlockedNoteIds, outputStream) { result ->
val toastId = when (result) {
NotesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
else -> R.string.exporting_failed
}
toast(toastId)
}
}
}
}
}
private fun tryImportNotes() {
hideKeyboard()
Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = EXPORT_MIME_TYPE
try {
startActivityForResult(this, PICK_IMPORT_NOTES_INTENT)
} catch (e: ActivityNotFoundException) {
toast(R.string.system_service_disabled, Toast.LENGTH_LONG)
} catch (e: Exception) {
showErrorToast(e)
}
}
}
private fun tryImportingAsJson(uri: Uri, force: Boolean = false, showToasts: Boolean = true, callback: ((success: Boolean) -> Unit)? = null) {
val path: String
val filename: String
when (uri.scheme) {
"file" -> {
path = uri.path!!
filename = path.getFilenameFromPath()
}
"content" -> {
val tempFile = getTempFile("messages", "backup.txt")
if (tempFile == null) {
maybeToast(R.string.unknown_error_occurred, showToasts)
callback?.invoke(false)
return
}
try {
filename = getFilenameFromUri(uri)
val inputStream = contentResolver.openInputStream(uri)
val out = FileOutputStream(tempFile)
inputStream!!.copyTo(out)
path = tempFile.absolutePath
} catch (e: Exception) {
showErrorToast(e)
callback?.invoke(false)
return
}
}
else -> {
maybeToast(R.string.invalid_file_format, showToasts)
callback?.invoke(false)
return
}
}
maybeToast(R.string.importing, showToasts)
ensureBackgroundThread {
NotesImporter(this).importNotes(path, filename, force) { importResult ->
if (importResult == ImportResult.IMPORT_FAIL) {
maybeToast(R.string.no_new_items, showToasts)
runOnUiThread { callback?.invoke(false) }
return@importNotes
}
toast(
when (importResult) {
ImportResult.IMPORT_OK -> R.string.importing_successful
ImportResult.IMPORT_PARTIAL -> R.string.importing_some_entries_failed
else -> R.string.no_new_items
}
)
initViewPager()
runOnUiThread { callback?.invoke(true) }
}
}
}
private fun maybeToast(id: Int, show: Boolean) {
if (show) {
toast(id)
}
}
private fun showExportFilePickUpdateDialog(exportPath: String, textToExport: String) {
val items = arrayListOf(
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),
@ -1053,73 +902,6 @@ class MainActivity : SimpleActivity() {
}
}
private fun tryExportAllNotesBelowQ() {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
exportAllNotesBelowQ()
} else {
toast(R.string.no_storage_permissions)
}
}
}
private fun exportAllNotesBelowQ() {
ensureBackgroundThread {
NotesHelper(this).getNotes { notes ->
mNotes = notes
requestUnlockNotes { unlockedNoteIds ->
ExportFilesDialog(this, mNotes) { parent, extension ->
val items = arrayListOf(
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),
RadioItem(EXPORT_FILE_NO_SYNC, getString(R.string.only_export_file_content))
)
RadioGroupDialog(this, items) { any ->
val syncFile = any as Int == EXPORT_FILE_SYNC
var failCount = 0
mNotes.filter { !it.isLocked() || it.id in unlockedNoteIds }.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
val noteStoredValue = note.getNoteStoredValue(this) ?: ""
tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully ->
if (exportedSuccessfully) {
if (syncFile) {
note.path = file.absolutePath
note.value = ""
} else {
note.path = ""
note.value = noteStoredValue
}
NotesHelper(this).insertOrUpdateNote(note)
}
if (mCurrentNote.id == note.id) {
mCurrentNote.value = note.value
mCurrentNote.path = note.path
getPagerAdapter().updateCurrentNoteData(view_pager.currentItem, mCurrentNote.path, mCurrentNote.value)
}
if (!exportedSuccessfully) {
failCount++
}
if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
}
}
}
}
}
}
}
}
}
}
fun tryExportNoteValueToFile(path: String, title: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
if (path.startsWith("content://")) {
exportNoteValueToUri(Uri.parse(path), title, content, showSuccessToasts, callback)

View File

@ -70,18 +70,10 @@
android:id="@+id/import_folder"
android:title="@string/import_folder"
app:showAsAction="never" />
<item
android:id="@+id/import_notes"
android:title="@string/import_notes"
app:showAsAction="never" />
<item
android:id="@+id/export_as_file"
android:title="@string/export_as_file"
app:showAsAction="never" />
<item
android:id="@+id/export_all_notes"
android:title="@string/export_notes"
app:showAsAction="never" />
<item
android:id="@+id/print"
android:title="@string/print"