From 670d646cab1b4d5c9b0a0191efe03e71d033bbc8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 26 Jun 2022 20:53:07 +0200 Subject: [PATCH] fix #539, make Import notes more flexible, handle nonjsons too --- .../notes/pro/activities/MainActivity.kt | 11 ++++--- .../notes/pro/helpers/NotesImporter.kt | 32 ++++++++++++++++++- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index 18ce6020..b9c2ad0f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -926,10 +926,10 @@ class MainActivity : SimpleActivity() { } } - private fun importNotes(path: String) { + private fun importNotes(path: String, filename: String) { toast(R.string.importing) ensureBackgroundThread { - NotesImporter(this).importNotes(path) { + NotesImporter(this).importNotes(path, filename) { toast( when (it) { NotesImporter.ImportResult.IMPORT_OK -> R.string.importing_successful @@ -944,19 +944,20 @@ class MainActivity : SimpleActivity() { private fun importNotesFrom(uri: Uri) { when (uri.scheme) { - "file" -> importNotes(uri.path!!) + "file" -> importNotes(uri.path!!, uri.path!!.getFilenameFromPath()) "content" -> { - val tempFile = getTempFile("messages", "backup.json") + val tempFile = getTempFile("messages", "backup.txt") if (tempFile == null) { toast(R.string.unknown_error_occurred) return } try { + val filename = getFilenameFromUri(uri) val inputStream = contentResolver.openInputStream(uri) val out = FileOutputStream(tempFile) inputStream!!.copyTo(out) - importNotes(tempFile.absolutePath) + importNotes(tempFile.absolutePath, filename) } catch (e: Exception) { showErrorToast(e) } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt index 86555d41..3a3e03e0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt @@ -2,8 +2,10 @@ package com.simplemobiletools.notes.pro.helpers import android.content.Context import com.google.gson.Gson +import com.google.gson.JsonSyntaxException import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.showErrorToast +import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.notes.pro.extensions.notesDB import com.simplemobiletools.notes.pro.models.Note @@ -18,7 +20,7 @@ class NotesImporter(private val context: Context) { private var notesImported = 0 private var notesFailed = 0 - fun importNotes(path: String, callback: (result: ImportResult) -> Unit) { + fun importNotes(path: String, filename: String, callback: (result: ImportResult) -> Unit) { ensureBackgroundThread { try { val inputStream = if (path.contains("/")) { @@ -45,6 +47,34 @@ class NotesImporter(private val context: Context) { } } } + } catch (e: JsonSyntaxException) { + // Import notes expects a json with note name, content etc, but lets be more flexible and accept the basic files with note content only too + val inputStream = if (path.contains("/")) { + File(path).inputStream() + } else { + context.assets.open(path) + } + + inputStream.bufferedReader().use { reader -> + val text = reader.readText() + val note = Note(null, filename, text, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") + var i = 1 + if (context.notesDB.getNoteIdWithTitle(note.title) != null) { + while (true) { + val tryTitle = "$filename ($i)" + if (context.notesDB.getNoteIdWithTitle(tryTitle) == null) { + break + } + i++ + } + + note.title = "$filename ($i)" + } + + context.notesDB.insertOrUpdate(note) + notesImported++ + } + } catch (e: Exception) { context.showErrorToast(e) notesFailed++