Skip to content

Commit 1072c66

Browse files
committed
avoid trying to sync a note to file, if we have no permission for it
1 parent 6a623c7 commit 1072c66

File tree

1 file changed

+17
-3
lines changed
  • app/src/main/kotlin/com/simplemobiletools/notes/pro/activities

1 file changed

+17
-3
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ class MainActivity : SimpleActivity() {
237237
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
238238
super.onActivityResult(requestCode, resultCode, resultData)
239239
if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
240-
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
241-
applicationContext.contentResolver.takePersistableUriPermission(resultData.data!!, takeFlags)
242-
243240
importUri(resultData.data!!)
244241
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
245242
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
@@ -625,9 +622,26 @@ class MainActivity : SimpleActivity() {
625622
val content = inputStream?.bufferedReader().use { it!!.readText() }
626623
val checklistItems = content.parseChecklistItems()
627624

625+
// if we got here by some other app invoking the file open intent, we have no permission for updating the original file itself
626+
// we can do it only after using "Export as file" or "Open file" from our app
627+
val canSyncNoteWithFile = if (hasPermission(PERMISSION_WRITE_STORAGE)) {
628+
true
629+
} else {
630+
try {
631+
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
632+
applicationContext.contentResolver.takePersistableUriPermission(uri, takeFlags)
633+
true
634+
} catch (e: Exception) {
635+
false
636+
}
637+
}
638+
628639
if (checklistItems != null) {
629640
val note = Note(null, noteTitle, content, NoteType.TYPE_CHECKLIST.value)
630641
displayNewNoteDialog(note.value, title = noteTitle, setChecklistAsDefault = true)
642+
} else if (!canSyncNoteWithFile) {
643+
val note = Note(null, noteTitle, content, NoteType.TYPE_TEXT.value)
644+
displayNewNoteDialog(note.value, title = noteTitle, "")
631645
} else {
632646
val items = arrayListOf(
633647
RadioItem(EXPORT_FILE_SYNC, getString(R.string.update_file_at_note)),

0 commit comments

Comments
 (0)