mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-03-23 12:00:15 +01:00
fix #539, make Import notes more flexible, handle nonjsons too
This commit is contained in:
parent
38da059411
commit
670d646cab
@ -926,10 +926,10 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun importNotes(path: String) {
|
private fun importNotes(path: String, filename: String) {
|
||||||
toast(R.string.importing)
|
toast(R.string.importing)
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
NotesImporter(this).importNotes(path) {
|
NotesImporter(this).importNotes(path, filename) {
|
||||||
toast(
|
toast(
|
||||||
when (it) {
|
when (it) {
|
||||||
NotesImporter.ImportResult.IMPORT_OK -> R.string.importing_successful
|
NotesImporter.ImportResult.IMPORT_OK -> R.string.importing_successful
|
||||||
@ -944,19 +944,20 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private fun importNotesFrom(uri: Uri) {
|
private fun importNotesFrom(uri: Uri) {
|
||||||
when (uri.scheme) {
|
when (uri.scheme) {
|
||||||
"file" -> importNotes(uri.path!!)
|
"file" -> importNotes(uri.path!!, uri.path!!.getFilenameFromPath())
|
||||||
"content" -> {
|
"content" -> {
|
||||||
val tempFile = getTempFile("messages", "backup.json")
|
val tempFile = getTempFile("messages", "backup.txt")
|
||||||
if (tempFile == null) {
|
if (tempFile == null) {
|
||||||
toast(R.string.unknown_error_occurred)
|
toast(R.string.unknown_error_occurred)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
val filename = getFilenameFromUri(uri)
|
||||||
val inputStream = contentResolver.openInputStream(uri)
|
val inputStream = contentResolver.openInputStream(uri)
|
||||||
val out = FileOutputStream(tempFile)
|
val out = FileOutputStream(tempFile)
|
||||||
inputStream!!.copyTo(out)
|
inputStream!!.copyTo(out)
|
||||||
importNotes(tempFile.absolutePath)
|
importNotes(tempFile.absolutePath, filename)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
showErrorToast(e)
|
showErrorToast(e)
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@ package com.simplemobiletools.notes.pro.helpers
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.JsonSyntaxException
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.simplemobiletools.commons.extensions.showErrorToast
|
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||||
|
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
|
||||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.notes.pro.extensions.notesDB
|
import com.simplemobiletools.notes.pro.extensions.notesDB
|
||||||
import com.simplemobiletools.notes.pro.models.Note
|
import com.simplemobiletools.notes.pro.models.Note
|
||||||
@ -18,7 +20,7 @@ class NotesImporter(private val context: Context) {
|
|||||||
private var notesImported = 0
|
private var notesImported = 0
|
||||||
private var notesFailed = 0
|
private var notesFailed = 0
|
||||||
|
|
||||||
fun importNotes(path: String, callback: (result: ImportResult) -> Unit) {
|
fun importNotes(path: String, filename: String, callback: (result: ImportResult) -> Unit) {
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
try {
|
try {
|
||||||
val inputStream = if (path.contains("/")) {
|
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) {
|
} catch (e: Exception) {
|
||||||
context.showErrorToast(e)
|
context.showErrorToast(e)
|
||||||
notesFailed++
|
notesFailed++
|
||||||
|
Loading…
x
Reference in New Issue
Block a user