@@ -42,6 +42,7 @@ import com.simplemobiletools.notes.pro.dialogs.*
4242import com.simplemobiletools.notes.pro.extensions.*
4343import com.simplemobiletools.notes.pro.fragments.TextFragment
4444import com.simplemobiletools.notes.pro.helpers.*
45+ import com.simplemobiletools.notes.pro.helpers.NotesImporter.ImportResult
4546import com.simplemobiletools.notes.pro.models.Note
4647import kotlinx.android.synthetic.main.activity_main.*
4748import 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
0 commit comments