Skip to content

Commit 670d646

Browse files
committed
fix #539, make Import notes more flexible, handle nonjsons too
1 parent 38da059 commit 670d646

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,10 @@ class MainActivity : SimpleActivity() {
926926
}
927927
}
928928

929-
private fun importNotes(path: String) {
929+
private fun importNotes(path: String, filename: String) {
930930
toast(R.string.importing)
931931
ensureBackgroundThread {
932-
NotesImporter(this).importNotes(path) {
932+
NotesImporter(this).importNotes(path, filename) {
933933
toast(
934934
when (it) {
935935
NotesImporter.ImportResult.IMPORT_OK -> R.string.importing_successful
@@ -944,19 +944,20 @@ class MainActivity : SimpleActivity() {
944944

945945
private fun importNotesFrom(uri: Uri) {
946946
when (uri.scheme) {
947-
"file" -> importNotes(uri.path!!)
947+
"file" -> importNotes(uri.path!!, uri.path!!.getFilenameFromPath())
948948
"content" -> {
949-
val tempFile = getTempFile("messages", "backup.json")
949+
val tempFile = getTempFile("messages", "backup.txt")
950950
if (tempFile == null) {
951951
toast(R.string.unknown_error_occurred)
952952
return
953953
}
954954

955955
try {
956+
val filename = getFilenameFromUri(uri)
956957
val inputStream = contentResolver.openInputStream(uri)
957958
val out = FileOutputStream(tempFile)
958959
inputStream!!.copyTo(out)
959-
importNotes(tempFile.absolutePath)
960+
importNotes(tempFile.absolutePath, filename)
960961
} catch (e: Exception) {
961962
showErrorToast(e)
962963
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.simplemobiletools.notes.pro.helpers
22

33
import android.content.Context
44
import com.google.gson.Gson
5+
import com.google.gson.JsonSyntaxException
56
import com.google.gson.reflect.TypeToken
67
import com.simplemobiletools.commons.extensions.showErrorToast
8+
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
79
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
810
import com.simplemobiletools.notes.pro.extensions.notesDB
911
import com.simplemobiletools.notes.pro.models.Note
@@ -18,7 +20,7 @@ class NotesImporter(private val context: Context) {
1820
private var notesImported = 0
1921
private var notesFailed = 0
2022

21-
fun importNotes(path: String, callback: (result: ImportResult) -> Unit) {
23+
fun importNotes(path: String, filename: String, callback: (result: ImportResult) -> Unit) {
2224
ensureBackgroundThread {
2325
try {
2426
val inputStream = if (path.contains("/")) {
@@ -45,6 +47,34 @@ class NotesImporter(private val context: Context) {
4547
}
4648
}
4749
}
50+
} catch (e: JsonSyntaxException) {
51+
// 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
52+
val inputStream = if (path.contains("/")) {
53+
File(path).inputStream()
54+
} else {
55+
context.assets.open(path)
56+
}
57+
58+
inputStream.bufferedReader().use { reader ->
59+
val text = reader.readText()
60+
val note = Note(null, filename, text, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "")
61+
var i = 1
62+
if (context.notesDB.getNoteIdWithTitle(note.title) != null) {
63+
while (true) {
64+
val tryTitle = "$filename ($i)"
65+
if (context.notesDB.getNoteIdWithTitle(tryTitle) == null) {
66+
break
67+
}
68+
i++
69+
}
70+
71+
note.title = "$filename ($i)"
72+
}
73+
74+
context.notesDB.insertOrUpdate(note)
75+
notesImported++
76+
}
77+
4878
} catch (e: Exception) {
4979
context.showErrorToast(e)
5080
notesFailed++

0 commit comments

Comments
 (0)