From 7ec164ae42277bf01a52a93654f7cb84b9664b07 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 23 Apr 2023 12:54:37 +0530 Subject: [PATCH 1/3] Fix permission issue with export files --- .../calendar/pro/extensions/Context.kt | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index de9e5ad25..85d524e9e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -50,6 +50,7 @@ import org.joda.time.DateTime import org.joda.time.DateTimeZone import org.joda.time.LocalDate import java.io.File +import java.io.FileOutputStream import java.util.* val Context.config: Config get() = Config.newInstance(applicationContext) @@ -222,6 +223,7 @@ fun Context.checkAndBackupEventsOnBoot() { } fun Context.backupEventsAndTasks() { + require(isRPlus()) ensureBackgroundThread { val config = config val events = eventsHelper.getEventsToExport( @@ -258,8 +260,17 @@ fun Context.backupEventsAndTasks() { } val exportFile = File(outputFolder, "$filename.ics") + val exportFilePath = exportFile.absolutePath val outputStream = try { - exportFile.outputStream() + if (hasProperStoredFirstParentUri(exportFilePath)) { + val exportFileUri = createDocumentUriUsingFirstParentTreeUri(exportFilePath) + if (!getDoesFilePathExist(exportFilePath)) { + createSAFFileSdk30(exportFilePath) + } + applicationContext.contentResolver.openOutputStream(exportFileUri, "wt") ?: FileOutputStream(exportFile) + } else { + FileOutputStream(exportFile) + } } catch (e: Exception) { showErrorToast(e) null @@ -273,8 +284,8 @@ fun Context.backupEventsAndTasks() { } MediaScannerConnection.scanFile( this, - arrayOf(exportFile.absolutePath), - arrayOf(exportFile.getMimeType()) + arrayOf(exportFilePath), + arrayOf(exportFilePath.getMimeType()) ) { _, _ -> } config.lastAutoBackupTime = getNowSeconds() @@ -522,6 +533,7 @@ fun Context.getNewEventTimestampFromCode(dayCode: String, allowChangingDay: Bool val currMinutes = calendar.get(Calendar.MINUTE) dateTime.withMinuteOfHour(currMinutes).seconds() } + DEFAULT_START_TIME_NEXT_FULL_HOUR -> newDateTime.seconds() else -> { val hours = defaultStartTime / 60 @@ -682,11 +694,13 @@ fun Context.handleEventDeleting(eventIds: List, timestamps: List, ac eventsHelper.addEventRepetitionException(value, timestamps[index], true) } } + DELETE_FUTURE_OCCURRENCES -> { eventIds.forEachIndexed { index, value -> eventsHelper.addEventRepeatLimit(value, timestamps[index]) } } + DELETE_ALL_OCCURRENCES -> { eventsHelper.deleteEvents(eventIds.toMutableList(), true) } From 710a87f2269a44167827e5b4ef267bab43f2bec9 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 23 Apr 2023 15:04:29 +0530 Subject: [PATCH 2/3] Use a numbered file when destination already exists Only applicable in cases when we don't have access to the parent folder (e.g. Downloads) --- .../calendar/pro/extensions/Context.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 85d524e9e..56a52fed9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -259,8 +259,8 @@ fun Context.backupEventsAndTasks() { mkdirs() } - val exportFile = File(outputFolder, "$filename.ics") - val exportFilePath = exportFile.absolutePath + var exportFile = File(outputFolder, "$filename.ics") + var exportFilePath = exportFile.absolutePath val outputStream = try { if (hasProperStoredFirstParentUri(exportFilePath)) { val exportFileUri = createDocumentUriUsingFirstParentTreeUri(exportFilePath) @@ -269,6 +269,12 @@ fun Context.backupEventsAndTasks() { } applicationContext.contentResolver.openOutputStream(exportFileUri, "wt") ?: FileOutputStream(exportFile) } else { + var num = 0 + while (getDoesFilePathExist(exportFilePath) && !exportFile.canWrite()) { + num++ + exportFile = File(outputFolder, "${filename}_${num}.ics") + exportFilePath = exportFile.absolutePath + } FileOutputStream(exportFile) } } catch (e: Exception) { From 38c414e98cccf2a947378ff37e62677c9ac27b55 Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Mon, 24 Apr 2023 14:47:39 +0200 Subject: [PATCH 3/3] removing some empty lines at switch/case --- .../com/simplemobiletools/calendar/pro/extensions/Context.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 56a52fed9..ad7a1f3e8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -539,7 +539,6 @@ fun Context.getNewEventTimestampFromCode(dayCode: String, allowChangingDay: Bool val currMinutes = calendar.get(Calendar.MINUTE) dateTime.withMinuteOfHour(currMinutes).seconds() } - DEFAULT_START_TIME_NEXT_FULL_HOUR -> newDateTime.seconds() else -> { val hours = defaultStartTime / 60 @@ -700,13 +699,11 @@ fun Context.handleEventDeleting(eventIds: List, timestamps: List, ac eventsHelper.addEventRepetitionException(value, timestamps[index], true) } } - DELETE_FUTURE_OCCURRENCES -> { eventIds.forEachIndexed { index, value -> eventsHelper.addEventRepeatLimit(value, timestamps[index]) } } - DELETE_ALL_OCCURRENCES -> { eventsHelper.deleteEvents(eventIds.toMutableList(), true) }