Allow exporting locked notes

This commit is contained in:
Naveen
2023-03-15 02:09:28 +05:30
parent f1bbad2c9c
commit 0bc818245c
10 changed files with 199 additions and 58 deletions

View File

@ -904,17 +904,24 @@ class MainActivity : SimpleActivity() {
}
}
private fun exportNotesTo(outputStream: OutputStream?) {
toast(R.string.exporting)
ensureBackgroundThread {
val notesExporter = NotesExporter(this)
notesExporter.exportNotes(outputStream) {
val toastId = when (it) {
NotesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
else -> R.string.exporting_failed
}
private fun requestUnlockNotes(callback: (List<Long>) -> Unit) {
val lockedNotes = mNotes.filter { it.isLocked() }
UnlockNotesDialog(this, lockedNotes, callback)
}
toast(toastId)
private fun exportNotesTo(outputStream: OutputStream?) {
requestUnlockNotes { unlockedIds ->
toast(R.string.exporting)
ensureBackgroundThread {
val notesExporter = NotesExporter(this)
notesExporter.exportNotes(outputStream, unlockedIds) {
val toastId = when (it) {
NotesExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
else -> R.string.exporting_failed
}
toast(toastId)
}
}
}
}
@ -1011,49 +1018,51 @@ class MainActivity : SimpleActivity() {
}
private fun exportAllNotesBelowQ() {
ExportFilesDialog(this, mNotes) { parent, extension ->
val items = arrayListOf(
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),
RadioItem(EXPORT_FILE_NO_SYNC, getString(R.string.only_export_file_content))
)
requestUnlockNotes { unlockedIds ->
ExportFilesDialog(this, mNotes) { parent, extension ->
val items = arrayListOf(
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),
RadioItem(EXPORT_FILE_NO_SYNC, getString(R.string.only_export_file_content))
)
RadioGroupDialog(this, items) {
val syncFile = it as Int == EXPORT_FILE_SYNC
var failCount = 0
NotesHelper(this).getNotes {
mNotes = it
mNotes.filter { !it.isLocked() }.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
val noteStoredValue = note.getNoteStoredValue(this) ?: ""
tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully ->
if (exportedSuccessfully) {
if (syncFile) {
note.path = file.absolutePath
note.value = ""
} else {
note.path = ""
note.value = noteStoredValue
RadioGroupDialog(this, items) { any ->
val syncFile = any as Int == EXPORT_FILE_SYNC
var failCount = 0
NotesHelper(this).getNotes { notes ->
mNotes = notes
mNotes.filter { !it.isLocked() || it.id in unlockedIds }.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
val noteStoredValue = note.getNoteStoredValue(this) ?: ""
tryExportNoteValueToFile(file.absolutePath, mCurrentNote.title, note.value, false) { exportedSuccessfully ->
if (exportedSuccessfully) {
if (syncFile) {
note.path = file.absolutePath
note.value = ""
} else {
note.path = ""
note.value = noteStoredValue
}
NotesHelper(this).insertOrUpdateNote(note)
}
NotesHelper(this).insertOrUpdateNote(note)
}
if (mCurrentNote.id == note.id) {
mCurrentNote.value = note.value
mCurrentNote.path = note.path
getPagerAdapter().updateCurrentNoteData(view_pager.currentItem, mCurrentNote.path, mCurrentNote.value)
}
if (mCurrentNote.id == note.id) {
mCurrentNote.value = note.value
mCurrentNote.path = note.path
getPagerAdapter().updateCurrentNoteData(view_pager.currentItem, mCurrentNote.path, mCurrentNote.value)
}
if (!exportedSuccessfully) {
failCount++
}
if (!exportedSuccessfully) {
failCount++
}
if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
}
}
}
}

View File

@ -24,16 +24,16 @@ class NewNoteDialog(val activity: Activity, title: String? = null, val setCheckl
new_note_type.check(defaultType)
}
view.note_title.setText(title)
view.locked_note_title.setText(title)
activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.apply {
activity.setupDialogStuff(view, this, R.string.new_note) { alertDialog ->
alertDialog.showKeyboard(view.note_title)
alertDialog.showKeyboard(view.locked_note_title)
alertDialog.getButton(BUTTON_POSITIVE).setOnClickListener {
val newTitle = view.note_title.value
val newTitle = view.locked_note_title.value
ensureBackgroundThread {
when {
newTitle.isEmpty() -> activity.toast(R.string.no_title)

View File

@ -18,16 +18,16 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, val current
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_rename_note, null)
view.note_title.setText(note.title)
view.locked_note_title.setText(note.title)
activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.apply {
activity.setupDialogStuff(view, this, R.string.rename_note) { alertDialog ->
alertDialog.showKeyboard(view.note_title)
alertDialog.showKeyboard(view.locked_note_title)
alertDialog.getButton(BUTTON_POSITIVE).setOnClickListener {
val title = view.note_title.value
val title = view.locked_note_title.value
ensureBackgroundThread {
newTitleConfirmed(title, alertDialog)
}

View File

@ -0,0 +1,64 @@
package com.simplemobiletools.notes.pro.dialogs
import android.content.DialogInterface
import android.view.ViewGroup
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.models.Note
import kotlinx.android.synthetic.main.dialog_unlock_notes.view.*
import kotlinx.android.synthetic.main.item_locked_note.view.*
class UnlockNotesDialog(val activity: BaseSimpleActivity, notes: List<Note>, callback: (unlockedNoteIds: List<Long>) -> Unit) {
private val view = activity.layoutInflater.inflate(R.layout.dialog_unlock_notes, null) as ViewGroup
private val redColor = activity.getColor(R.color.md_red)
private val greenColor = activity.getColor(R.color.md_green)
private val unlockedNoteIds = mutableListOf<Long>()
init {
for (note in notes) {
addLockedNoteView(note)
}
activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.apply {
activity.setupDialogStuff(view, this, R.string.unlock_notes, cancelOnTouchOutside = false) { alertDialog ->
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
callback(unlockedNoteIds)
alertDialog.dismiss()
}
}
}
}
private fun addLockedNoteView(note: Note) {
activity.layoutInflater.inflate(R.layout.item_locked_note, null).apply {
view.notes_holder.addView(this)
activity.updateTextColors(view.notes_holder)
locked_note_title.text = note.title
locked_unlocked_image.applyColorFilter(redColor)
locked_note_holder.setOnClickListener {
if (note.id !in unlockedNoteIds) {
activity.performSecurityCheck(
protectionType = note.protectionType,
requiredHash = note.protectionHash,
successCallback = { _, _ ->
unlockedNoteIds.add(note.id!!)
locked_unlocked_image.apply {
setImageResource(R.drawable.ic_lock_open_vector)
applyColorFilter(greenColor)
}
}
)
} else {
unlockedNoteIds.remove(note.id)
locked_unlocked_image.apply {
setImageResource(R.drawable.ic_lock_vector)
applyColorFilter(redColor)
}
}
}
}
}
}

View File

@ -16,7 +16,7 @@ class NotesExporter(private val context: Context) {
private val gson = Gson()
fun exportNotes(outputStream: OutputStream?, callback: (result: ExportResult) -> Unit) {
fun exportNotes(outputStream: OutputStream?, unlockedNoteIds: List<Long>, callback: (result: ExportResult) -> Unit) {
ensureBackgroundThread {
if (outputStream == null) {
callback.invoke(ExportResult.EXPORT_FAIL)
@ -29,7 +29,7 @@ class NotesExporter(private val context: Context) {
writer.beginArray()
val notes = context.notesDB.getNotes() as ArrayList<Note>
for (note in notes) {
if (note.protectionType == PROTECTION_NONE) {
if (!note.isLocked() || note.id in unlockedNoteIds) {
val noteToSave = getNoteToExport(note)
writer.jsonValue(gson.toJson(noteToSave))
written++