From 1072c661d7f2cedf88fcd54213838245a2b03dbf Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 20 Apr 2021 23:35:48 +0200 Subject: [PATCH] avoid trying to sync a note to file, if we have no permission for it --- .../notes/pro/activities/MainActivity.kt | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 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 90af3efa..3ce1a5bc 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 @@ -237,9 +237,6 @@ class MainActivity : SimpleActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { super.onActivityResult(requestCode, resultCode, resultData) if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) { - val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - applicationContext.contentResolver.takePersistableUriPermission(resultData.data!!, takeFlags) - importUri(resultData.data!!) } else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) { val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION @@ -625,9 +622,26 @@ class MainActivity : SimpleActivity() { val content = inputStream?.bufferedReader().use { it!!.readText() } val checklistItems = content.parseChecklistItems() + // if we got here by some other app invoking the file open intent, we have no permission for updating the original file itself + // we can do it only after using "Export as file" or "Open file" from our app + val canSyncNoteWithFile = if (hasPermission(PERMISSION_WRITE_STORAGE)) { + true + } else { + try { + val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + applicationContext.contentResolver.takePersistableUriPermission(uri, takeFlags) + true + } catch (e: Exception) { + false + } + } + if (checklistItems != null) { val note = Note(null, noteTitle, content, NoteType.TYPE_CHECKLIST.value) displayNewNoteDialog(note.value, title = noteTitle, setChecklistAsDefault = true) + } else if (!canSyncNoteWithFile) { + val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value) + displayNewNoteDialog(note.value, title = noteTitle, "") } else { val items = arrayListOf( RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),