fix #539, make Import notes more flexible, handle nonjsons too

This commit is contained in:
tibbi 2022-06-26 20:53:07 +02:00
parent 38da059411
commit 670d646cab
2 changed files with 37 additions and 6 deletions

View File

@ -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)
}

View File

@ -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++