diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7df2aedd..88f60429 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,6 +32,12 @@ + + + + + + 10 * 1000 * 1000) { - toast(R.string.file_too_large) - } else if (mDb.doesTitleExist(it.getFilenameFromPath())) { - toast(R.string.title_taken) - } else { - OpenFileDialog(this, it) { + openFile(it, true, { + OpenFileDialog(this, it.path) { addNewNote(it) } - } + }) } } + private fun openFile(path: String, checkTitle: Boolean, onChecksPassed: (file: File) -> Unit) { + val file = File(path) + if (file.isImageVideoGif()) { + toast(R.string.invalid_file_format) + } else if (file.length() > 10 * 1000 * 1000) { + toast(R.string.file_too_large) + } else if (checkTitle && mDb.doesTitleExist(path.getFilenameFromPath())) { + toast(R.string.title_taken) + } else { + onChecksPassed(file) + } + } + + private fun importFileWithSync(path: String) { + openFile(path, false, { + var title = path.getFilenameFromPath() + if (mDb.doesTitleExist(title)) title += " (file)" + + val note = Note(0, title, "", TYPE_NOTE, path) + addNewNote(note) + }) + } + private fun tryExportAsFile() { if (hasWriteStoragePermission()) { exportAsFile() @@ -365,6 +406,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { openFile() } else if (requestCode == STORAGE_EXPORT_AS_FILE) { exportAsFile() + } else if (requestCode == STORAGE_OPEN_FILE_ACTION) { + importFileWithSync(openFilePath) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/DBHelper.kt index 284b04a5..81270a8d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/DBHelper.kt @@ -138,6 +138,22 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe return note } + fun getNoteId(path: String): Int { + val cols = arrayOf(COL_ID) + val selection = "$COL_PATH = ?" + val selectionArgs = arrayOf(path) + var cursor: Cursor? = null + try { + cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null) + if (cursor?.moveToFirst() == true) { + return cursor.getIntValue(COL_ID) + } + } finally { + cursor?.close() + } + return 0 + } + fun updateNoteValue(note: Note) { val values = ContentValues().apply { put(COL_VALUE, note.value) } updateNote(note.id, values) @@ -158,4 +174,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe val selectionArgs = arrayOf(id.toString()) mDb.update(TABLE_NAME, values, selection, selectionArgs) } + + fun isValidId(id: Int) = id > 0 }