From 53182f050783065dbfb274cd35be7872058f1ecf Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 20 May 2021 20:43:38 +0200 Subject: [PATCH] adding some protection related fields to the Note model --- .../notes/pro/activities/MainActivity.kt | 12 +++---- .../notes/pro/databases/NotesDatabase.kt | 31 +++++++++++++------ .../notes/pro/dialogs/ImportFolderDialog.kt | 3 +- .../notes/pro/dialogs/NewNoteDialog.kt | 3 +- .../notes/pro/dialogs/OpenFileDialog.kt | 3 +- .../notes/pro/helpers/NotesHelper.kt | 3 +- .../notes/pro/models/Note.kt | 4 ++- 7 files changed, 38 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index b0ca6621..44e84c52 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -538,7 +538,7 @@ class MainActivity : SimpleActivity() { val checklistItems = fileText.parseChecklistItems() if (checklistItems != null) { val title = it.absolutePath.getFilenameFromPath().substringBeforeLast('.') - val note = Note(null, title, fileText, NoteType.TYPE_CHECKLIST.value) + val note = Note(null, title, fileText, NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "") displayNewNoteDialog(note.value, title = title, setChecklistAsDefault = true) } else { runOnUiThread { @@ -637,10 +637,10 @@ class MainActivity : SimpleActivity() { } if (checklistItems != null) { - val note = Note(null, noteTitle, content, NoteType.TYPE_CHECKLIST.value) + val note = Note(null, noteTitle, content, NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "") displayNewNoteDialog(note.value, title = noteTitle, setChecklistAsDefault = true) } else if (!canSyncNoteWithFile) { - val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value) + val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") displayNewNoteDialog(note.value, title = noteTitle, "") } else { val items = arrayListOf( @@ -650,7 +650,7 @@ class MainActivity : SimpleActivity() { RadioGroupDialog(this, items) { val syncFile = it as Int == EXPORT_FILE_SYNC val path = if (syncFile) uri.toString() else "" - val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value) + val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") displayNewNoteDialog(note.value, title = noteTitle, path) } } @@ -663,9 +663,9 @@ class MainActivity : SimpleActivity() { val fileText = it.readText().trim() val checklistItems = fileText.parseChecklistItems() val note = if (checklistItems != null) { - Note(null, title.substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST.value) + Note(null, title.substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST.value, "", PROTECTION_NONE, "") } else { - Note(null, title, "", NoteType.TYPE_TEXT.value, path) + Note(null, title, "", NoteType.TYPE_TEXT.value, path, PROTECTION_NONE, "") } if (mNotes.any { it.title.equals(note.title, true) }) { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt index e685c66b..dcab5fb0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt @@ -7,6 +7,7 @@ import androidx.room.RoomDatabase import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import com.simplemobiletools.commons.helpers.DEFAULT_WIDGET_BG_COLOR +import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.helpers.DEFAULT_WIDGET_TEXT_COLOR import com.simplemobiletools.notes.pro.helpers.NoteType @@ -16,7 +17,7 @@ import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Widget import java.util.concurrent.Executors -@Database(entities = [Note::class, Widget::class], version = 2) +@Database(entities = [Note::class, Widget::class], version = 3) abstract class NotesDatabase : RoomDatabase() { abstract fun NotesDao(): NotesDao @@ -31,14 +32,15 @@ abstract class NotesDatabase : RoomDatabase() { synchronized(NotesDatabase::class) { if (db == null) { db = Room.databaseBuilder(context.applicationContext, NotesDatabase::class.java, "notes.db") - .addCallback(object : Callback() { - override fun onCreate(db: SupportSQLiteDatabase) { - super.onCreate(db) - insertFirstNote(context) - } - }) - .addMigrations(MIGRATION_1_2) - .build() + .addCallback(object : Callback() { + override fun onCreate(db: SupportSQLiteDatabase) { + super.onCreate(db) + insertFirstNote(context) + } + }) + .addMigrations(MIGRATION_1_2) + .addMigrations(MIGRATION_2_3) + .build() db!!.openHelper.setWriteAheadLoggingEnabled(true) } } @@ -53,7 +55,7 @@ abstract class NotesDatabase : RoomDatabase() { private fun insertFirstNote(context: Context) { Executors.newSingleThreadScheduledExecutor().execute { val generalNote = context.resources.getString(R.string.general_note) - val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value) + val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") db!!.NotesDao().insertOrUpdate(note) } } @@ -66,5 +68,14 @@ abstract class NotesDatabase : RoomDatabase() { } } } + + private val MIGRATION_2_3 = object : Migration(2, 3) { + override fun migrate(database: SupportSQLiteDatabase) { + database.apply { + execSQL("ALTER TABLE notes ADD COLUMN protection_type INTEGER DEFAULT $PROTECTION_NONE NOT NULL") + execSQL("ALTER TABLE notes ADD COLUMN protection_hash TEXT DEFAULT '' NOT NULL") + } + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt index 35a66a83..a0a518ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt @@ -6,6 +6,7 @@ import com.simplemobiletools.commons.extensions.getFilenameFromPath import com.simplemobiletools.commons.extensions.humanizePath import com.simplemobiletools.commons.extensions.isMediaFile import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.activities.SimpleActivity @@ -77,7 +78,7 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal } private fun saveNote(title: String, value: String, type: Int, path: String) { - val note = Note(null, title, value, type, path) + val note = Note(null, title, value, type, path, PROTECTION_NONE, "") NotesHelper(activity).insertOrUpdateNote(note) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt index e7645eac..5a2499f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt @@ -7,6 +7,7 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.showKeyboard import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.value +import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.extensions.config @@ -44,7 +45,7 @@ class NewNoteDialog(val activity: Activity, title: String? = null, val setCheckl else -> { val type = if (view.new_note_type.checkedRadioButtonId == view.type_checklist.id) NoteType.TYPE_CHECKLIST.value else NoteType.TYPE_TEXT.value activity.config.lastCreatedNoteType = type - val newNote = Note(null, title, "", type) + val newNote = Note(null, title, "", type, "", PROTECTION_NONE, "") callback(newNote) dismiss() } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenFileDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenFileDialog.kt index d4b420cc..fbea6566 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenFileDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenFileDialog.kt @@ -5,6 +5,7 @@ import androidx.appcompat.app.AlertDialog import com.simplemobiletools.commons.extensions.getFilenameFromPath import com.simplemobiletools.commons.extensions.humanizePath import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.activities.SimpleActivity import com.simplemobiletools.notes.pro.helpers.NoteType @@ -45,7 +46,7 @@ class OpenFileDialog(val activity: SimpleActivity, val path: String, val callbac private fun saveNote(storeContent: String, storePath: String) { val filename = path.getFilenameFromPath() - val note = Note(null, filename, storeContent, NoteType.TYPE_TEXT.value, storePath) + val note = Note(null, filename, storeContent, NoteType.TYPE_TEXT.value, storePath, PROTECTION_NONE, "") callback(note) dialog.dismiss() } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt index bd200bbe..a757eb89 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt @@ -4,6 +4,7 @@ import android.content.Context import android.net.Uri import android.os.Handler import android.os.Looper +import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.extensions.config @@ -35,7 +36,7 @@ class NotesHelper(val context: Context) { if (notes.isEmpty()) { val generalNote = context.resources.getString(R.string.general_note) - val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value) + val note = Note(null, generalNote, "", NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") context.notesDB.insertOrUpdate(note) notes.add(note) } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt index 70c55a80..26a262ca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt @@ -14,7 +14,9 @@ data class Note( @ColumnInfo(name = "title") var title: String, @ColumnInfo(name = "value") var value: String, @ColumnInfo(name = "type") var type: Int, - @ColumnInfo(name = "path") var path: String = "") { + @ColumnInfo(name = "path") var path: String, + @ColumnInfo(name = "protection_type") var protectionType: Int, + @ColumnInfo(name = "protection_hash") var protectionHash: String) { fun getNoteStoredValue(context: Context): String? { return if (path.isNotEmpty()) {