mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
Added exporting and importing all notes for Android 10+
This commit is contained in:
@ -36,6 +36,7 @@ const val USE_INCOGNITO_MODE = "use_incognito_mode"
|
||||
const val LAST_CREATED_NOTE_TYPE = "last_created_note_type"
|
||||
const val MOVE_DONE_CHECKLIST_ITEMS = "move_undone_checklist_items" // it has been replaced from moving undone items at the top to moving done to bottom
|
||||
const val FONT_SIZE_PERCENTAGE = "font_size_percentage"
|
||||
const val EXPORT_MIME_TYPE = "application/json"
|
||||
|
||||
// gravity
|
||||
const val GRAVITY_LEFT = 0
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.simplemobiletools.notes.pro.helpers
|
||||
|
||||
import android.content.Context
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.stream.JsonWriter
|
||||
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
|
||||
import java.io.OutputStream
|
||||
|
||||
class NotesExporter(private val context: Context) {
|
||||
enum class ExportResult {
|
||||
EXPORT_FAIL, EXPORT_OK
|
||||
}
|
||||
|
||||
private val gson = Gson()
|
||||
|
||||
fun exportNotes(outputStream: OutputStream?, onProgress: (total: Int, current: Int) -> Unit = { _, _ -> }, callback: (result: ExportResult) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
if (outputStream == null) {
|
||||
callback.invoke(ExportResult.EXPORT_FAIL)
|
||||
return@ensureBackgroundThread
|
||||
}
|
||||
val writer = JsonWriter(outputStream.bufferedWriter())
|
||||
writer.use {
|
||||
try {
|
||||
var written = 0
|
||||
writer.beginArray()
|
||||
val notes = context.notesDB.getNotes() as ArrayList<Note>
|
||||
val totalNotes = notes.size
|
||||
for (note in notes) {
|
||||
if (note.protectionType === PROTECTION_NONE) {
|
||||
val noteToSave = getNoteToExport(note)
|
||||
writer.jsonValue(gson.toJson(noteToSave))
|
||||
written++
|
||||
onProgress.invoke(totalNotes, written)
|
||||
}
|
||||
}
|
||||
writer.endArray()
|
||||
callback.invoke(ExportResult.EXPORT_OK)
|
||||
} catch (e: Exception) {
|
||||
callback.invoke(ExportResult.EXPORT_FAIL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNoteToExport(note: Note): Note {
|
||||
return Note(null, note.title, note.getNoteStoredValue(context) ?: "", note.type, "", PROTECTION_NONE, "")
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.simplemobiletools.notes.pro.helpers
|
||||
|
||||
import android.content.Context
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.notes.pro.extensions.notesDB
|
||||
import com.simplemobiletools.notes.pro.models.Note
|
||||
import java.io.File
|
||||
|
||||
class NotesImporter(private val context: Context) {
|
||||
enum class ImportResult {
|
||||
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW
|
||||
}
|
||||
|
||||
private val gson = Gson()
|
||||
private var notesImported = 0
|
||||
private var notesFailed = 0
|
||||
|
||||
fun importNotes(path: String, onProgress: (total: Int, current: Int) -> Unit = { _, _ -> }, callback: (result: ImportResult) -> Unit) {
|
||||
ensureBackgroundThread {
|
||||
try {
|
||||
val inputStream = if (path.contains("/")) {
|
||||
File(path).inputStream()
|
||||
} else {
|
||||
context.assets.open(path)
|
||||
}
|
||||
|
||||
inputStream.bufferedReader().use { reader ->
|
||||
val json = reader.readText()
|
||||
val type = object : TypeToken<List<Note>>() {}.type
|
||||
val notes = gson.fromJson<List<Note>>(json, type)
|
||||
val totalNotes = notes.size
|
||||
if (totalNotes <= 0) {
|
||||
callback.invoke(ImportResult.IMPORT_NOTHING_NEW)
|
||||
return@ensureBackgroundThread
|
||||
}
|
||||
|
||||
onProgress.invoke(totalNotes, notesImported)
|
||||
for (note in notes) {
|
||||
val exists = context.notesDB.getNoteIdWithTitle(note.title) != null
|
||||
if (!exists) {
|
||||
context.notesDB.insertOrUpdate(note)
|
||||
notesImported++
|
||||
onProgress.invoke(totalNotes, notesImported)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
context.showErrorToast(e)
|
||||
notesFailed++
|
||||
}
|
||||
|
||||
callback.invoke(
|
||||
when {
|
||||
notesImported == 0 -> ImportResult.IMPORT_FAIL
|
||||
notesFailed > 0 -> ImportResult.IMPORT_PARTIAL
|
||||
else -> ImportResult.IMPORT_OK
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user