Skip to content

Commit e3caa53

Browse files
authored
Merge pull request #620 from Naveen3Singh/parse_exported_notes
Parse exported JSON notes when opening files
2 parents 6b31bc5 + 61dde7f commit e3caa53

File tree

2 files changed

+81
-42
lines changed

2 files changed

+81
-42
lines changed

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

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import com.simplemobiletools.notes.pro.dialogs.*
4242
import com.simplemobiletools.notes.pro.extensions.*
4343
import com.simplemobiletools.notes.pro.fragments.TextFragment
4444
import com.simplemobiletools.notes.pro.helpers.*
45+
import com.simplemobiletools.notes.pro.helpers.NotesImporter.ImportResult
4546
import com.simplemobiletools.notes.pro.models.Note
4647
import kotlinx.android.synthetic.main.activity_main.*
4748
import kotlinx.android.synthetic.main.item_checklist.*
@@ -290,7 +291,7 @@ class MainActivity : SimpleActivity() {
290291
val outputStream = contentResolver.openOutputStream(resultData.data!!)
291292
exportNotesTo(outputStream)
292293
} else if (requestCode == PICK_IMPORT_NOTES_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
293-
importNotesFrom(resultData.data!!)
294+
tryImportingAsJson(resultData.data!!)
294295
}
295296
}
296297

@@ -729,23 +730,29 @@ class MainActivity : SimpleActivity() {
729730
}
730731

731732
private fun importUri(uri: Uri) {
732-
when (uri.scheme) {
733-
"file" -> openPath(uri.path!!)
734-
"content" -> {
735-
val realPath = getRealPathFromURI(uri)
736-
if (hasPermission(PERMISSION_READ_STORAGE)) {
737-
if (realPath != null) {
738-
openPath(realPath)
733+
tryImportingAsJson(uri, force = true, showToasts = false) { success ->
734+
if (success) {
735+
return@tryImportingAsJson
736+
}
737+
738+
when (uri.scheme) {
739+
"file" -> openPath(uri.path!!)
740+
"content" -> {
741+
val realPath = getRealPathFromURI(uri)
742+
if (hasPermission(PERMISSION_READ_STORAGE)) {
743+
if (realPath != null) {
744+
openPath(realPath)
745+
} else {
746+
R.string.unknown_error_occurred
747+
}
748+
} else if (realPath != null && realPath != "") {
749+
checkFile(realPath, false) {
750+
addNoteFromUri(uri, realPath.getFilenameFromPath())
751+
}
739752
} else {
740-
R.string.unknown_error_occurred
741-
}
742-
} else if (realPath != null && realPath != "") {
743-
checkFile(realPath, false) {
744-
addNoteFromUri(uri, realPath.getFilenameFromPath())
745-
}
746-
} else {
747-
checkUri(uri) {
748-
addNoteFromUri(uri)
753+
checkUri(uri) {
754+
addNoteFromUri(uri)
755+
}
749756
}
750757
}
751758
}
@@ -954,43 +961,66 @@ class MainActivity : SimpleActivity() {
954961
}
955962
}
956963

957-
private fun importNotes(path: String, filename: String) {
958-
toast(R.string.importing)
959-
ensureBackgroundThread {
960-
NotesImporter(this).importNotes(path, filename) {
961-
toast(
962-
when (it) {
963-
NotesImporter.ImportResult.IMPORT_OK -> R.string.importing_successful
964-
NotesImporter.ImportResult.IMPORT_PARTIAL -> R.string.importing_some_entries_failed
965-
else -> R.string.no_new_items
966-
}
967-
)
968-
initViewPager()
969-
}
970-
}
971-
}
972-
973-
private fun importNotesFrom(uri: Uri) {
964+
private fun tryImportingAsJson(uri: Uri, force: Boolean = false, showToasts: Boolean = true, callback: ((success: Boolean) -> Unit)? = null) {
965+
val path: String
966+
val filename: String
974967
when (uri.scheme) {
975-
"file" -> importNotes(uri.path!!, uri.path!!.getFilenameFromPath())
968+
"file" -> {
969+
path = uri.path!!
970+
filename = path.getFilenameFromPath()
971+
}
976972
"content" -> {
977973
val tempFile = getTempFile("messages", "backup.txt")
978974
if (tempFile == null) {
979-
toast(R.string.unknown_error_occurred)
975+
maybeToast(R.string.unknown_error_occurred, showToasts)
976+
callback?.invoke(false)
980977
return
981978
}
982979

983980
try {
984-
val filename = getFilenameFromUri(uri)
981+
filename = getFilenameFromUri(uri)
985982
val inputStream = contentResolver.openInputStream(uri)
986983
val out = FileOutputStream(tempFile)
987984
inputStream!!.copyTo(out)
988-
importNotes(tempFile.absolutePath, filename)
985+
path = tempFile.absolutePath
989986
} catch (e: Exception) {
990987
showErrorToast(e)
988+
callback?.invoke(false)
989+
return
991990
}
992991
}
993-
else -> toast(R.string.invalid_file_format)
992+
else -> {
993+
maybeToast(R.string.invalid_file_format, showToasts)
994+
callback?.invoke(false)
995+
return
996+
}
997+
}
998+
999+
maybeToast(R.string.importing, showToasts)
1000+
ensureBackgroundThread {
1001+
NotesImporter(this).importNotes(path, filename, force) { importResult ->
1002+
if (importResult == ImportResult.IMPORT_FAIL) {
1003+
maybeToast(R.string.no_new_items, showToasts)
1004+
runOnUiThread { callback?.invoke(false) }
1005+
return@importNotes
1006+
}
1007+
1008+
toast(
1009+
when (importResult) {
1010+
ImportResult.IMPORT_OK -> R.string.importing_successful
1011+
ImportResult.IMPORT_PARTIAL -> R.string.importing_some_entries_failed
1012+
else -> R.string.no_new_items
1013+
}
1014+
)
1015+
initViewPager()
1016+
runOnUiThread { callback?.invoke(true) }
1017+
}
1018+
}
1019+
}
1020+
1021+
private fun maybeToast(id: Int, show: Boolean) {
1022+
if (show) {
1023+
toast(id)
9941024
}
9951025
}
9961026

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class NotesImporter(private val context: Context) {
1919
private val gson = Gson()
2020
private var notesImported = 0
2121
private var notesFailed = 0
22+
private var notesSkipped = 0
2223

23-
fun importNotes(path: String, filename: String, callback: (result: ImportResult) -> Unit) {
24+
fun importNotes(path: String, filename: String, force: Boolean = false, callback: (result: ImportResult) -> Unit) {
2425
ensureBackgroundThread {
2526
try {
2627
val inputStream = if (path.contains("/")) {
@@ -33,9 +34,9 @@ class NotesImporter(private val context: Context) {
3334
val json = reader.readText()
3435
val type = object : TypeToken<List<Note>>() {}.type
3536
val notes = gson.fromJson<List<Note>>(json, type)
36-
val totalNotes = notes.size
37+
val totalNotes = notes?.size ?: 0
3738
if (totalNotes <= 0) {
38-
callback.invoke(ImportResult.IMPORT_NOTHING_NEW)
39+
callback.invoke(ImportResult.IMPORT_FAIL)
3940
return@ensureBackgroundThread
4041
}
4142

@@ -44,10 +45,17 @@ class NotesImporter(private val context: Context) {
4445
if (!exists) {
4546
context.notesDB.insertOrUpdate(note)
4647
notesImported++
48+
} else {
49+
notesSkipped++
4750
}
4851
}
4952
}
5053
} catch (e: JsonSyntaxException) {
54+
if (force) {
55+
callback(ImportResult.IMPORT_FAIL)
56+
return@ensureBackgroundThread
57+
}
58+
5159
// 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
5260
val inputStream = if (path.contains("/")) {
5361
File(path).inputStream()
@@ -82,6 +90,7 @@ class NotesImporter(private val context: Context) {
8290

8391
callback.invoke(
8492
when {
93+
notesSkipped > 0 && notesImported == 0 -> ImportResult.IMPORT_NOTHING_NEW
8594
notesImported == 0 -> ImportResult.IMPORT_FAIL
8695
notesFailed > 0 -> ImportResult.IMPORT_PARTIAL
8796
else -> ImportResult.IMPORT_OK

0 commit comments

Comments
 (0)