From 53c5f54677c2daf1a22e2cb3ff940d9e676e3fd6 Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 16:25:39 +1000 Subject: [PATCH 01/11] Added settings menu item --- .../notes/pro/activities/SettingsActivity.kt | 1 + app/src/main/res/layout/activity_settings.xml | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt index d2c73540..b6ea6db3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt @@ -71,6 +71,7 @@ class SettingsActivity : SimpleActivity() { settings_startup_label, settings_saving_label, settings_migrating_label, + settings_backups_label, ).forEach { it.setTextColor(getProperPrimaryColor()) } diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 93fc0d18..857ee1ec 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -386,6 +386,47 @@ android:text="@string/import_notes" /> + + + + + + + + + + + + + + + + From ada0702929616d3f10f98f86928853108251d985 Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 16:33:53 +1000 Subject: [PATCH 02/11] Added backup receivers --- app/src/main/AndroidManifest.xml | 16 ++++++++++++++++ .../pro/receivers/AutomaticBackupReceiver.kt | 16 ++++++++++++++++ .../pro/receivers/BootCompletedReceiver.kt | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/AutomaticBackupReceiver.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/BootCompletedReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6fbba142..55edd70f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -107,6 +107,22 @@ android:resource="@xml/widget_info" /> + + + + + + + + + + + + Date: Fri, 28 Jul 2023 17:28:48 +1000 Subject: [PATCH 03/11] Permissions added for AlarmManager --- app/src/main/AndroidManifest.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 55edd70f..29ccdc42 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,10 @@ + + + + Date: Fri, 28 Jul 2023 17:29:09 +1000 Subject: [PATCH 04/11] Commons updated --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index c2f55d37..e21d2d02 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,7 +66,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:f737f6c38b' + implementation 'com.github.SimpleMobileTools:Simple-Commons:91763fe00f' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.documentfile:documentfile:1.0.1' From c93e7fe063faf7c8a14c26c4e5946f444dda8289 Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 17:30:50 +1000 Subject: [PATCH 05/11] ManageAutoBackupsDialog and DateTimePatternInfoDialog added --- .../pro/dialogs/DateTimePatternInfoDialog.kt | 18 ++++ .../pro/dialogs/ManageAutoBackupsDialog.kt | 99 +++++++++++++++++++ .../layout/datetime_pattern_info_layout.xml | 9 ++ .../dialog_manage_automatic_backups.xml | 54 ++++++++++ 4 files changed, 180 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/DateTimePatternInfoDialog.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ManageAutoBackupsDialog.kt create mode 100644 app/src/main/res/layout/datetime_pattern_info_layout.xml create mode 100644 app/src/main/res/layout/dialog_manage_automatic_backups.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/DateTimePatternInfoDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/DateTimePatternInfoDialog.kt new file mode 100644 index 00000000..b9ffa41a --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/DateTimePatternInfoDialog.kt @@ -0,0 +1,18 @@ +package com.simplemobiletools.notes.pro.dialogs + +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.getAlertDialogBuilder +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.notes.pro.R + +class DateTimePatternInfoDialog(activity: BaseSimpleActivity) { + + init { + val view = activity.layoutInflater.inflate(R.layout.datetime_pattern_info_layout, null) + activity.getAlertDialogBuilder() + .setPositiveButton(R.string.ok) { _, _ -> { } } + .apply { + activity.setupDialogStuff(view, this) + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ManageAutoBackupsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ManageAutoBackupsDialog.kt new file mode 100644 index 00000000..b03cc3e2 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ManageAutoBackupsDialog.kt @@ -0,0 +1,99 @@ +package com.simplemobiletools.notes.pro.dialogs + +import android.view.ViewGroup +import androidx.appcompat.app.AlertDialog +import com.simplemobiletools.commons.dialogs.FilePickerDialog +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.notes.pro.R +import com.simplemobiletools.notes.pro.activities.SimpleActivity +import com.simplemobiletools.notes.pro.extensions.config +import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_notes_filename +import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_notes_filename_hint +import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_notes_folder +import java.io.File + +class ManageAutoBackupsDialog(private val activity: SimpleActivity, onSuccess: () -> Unit) { + private val view = (activity.layoutInflater.inflate(R.layout.dialog_manage_automatic_backups, null) as ViewGroup) + private val config = activity.config + private var backupFolder = config.autoBackupFolder + + init { + view.apply { + backup_notes_folder.setText(activity.humanizePath(backupFolder)) + val filename = config.autoBackupFilename.ifEmpty { + "${activity.getString(R.string.notes)}_%Y%M%D_%h%m%s" + } + + backup_notes_filename.setText(filename) + backup_notes_filename_hint.setEndIconOnClickListener { + DateTimePatternInfoDialog(activity) + } + + backup_notes_filename_hint.setEndIconOnLongClickListener { + DateTimePatternInfoDialog(activity) + true + } + + backup_notes_folder.setOnClickListener { + selectBackupFolder() + } + } + + activity.getAlertDialogBuilder() + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, null) + .apply { + activity.setupDialogStuff(view, this, R.string.manage_automatic_backups) { dialog -> + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + val filename = view.backup_notes_filename.value + when { + filename.isEmpty() -> activity.toast(R.string.empty_name) + filename.isAValidFilename() -> { + val file = File(backupFolder, "$filename.json") + if (file.exists() && !file.canWrite()) { + activity.toast(R.string.name_taken) + return@setOnClickListener + } + + ensureBackgroundThread { + config.apply { + autoBackupFolder = backupFolder + autoBackupFilename = filename + } + + activity.runOnUiThread { + onSuccess() + } + + dialog.dismiss() + } + } + + else -> activity.toast(R.string.invalid_name) + } + } + } + } + } + + private fun selectBackupFolder() { + activity.hideKeyboard(view.backup_notes_filename) + FilePickerDialog(activity, backupFolder, false, showFAB = true) { path -> + activity.handleSAFDialog(path) { grantedSAF -> + if (!grantedSAF) { + return@handleSAFDialog + } + + activity.handleSAFDialogSdk30(path) { grantedSAF30 -> + if (!grantedSAF30) { + return@handleSAFDialogSdk30 + } + + backupFolder = path + view.backup_notes_folder.setText(activity.humanizePath(path)) + } + } + } + } +} diff --git a/app/src/main/res/layout/datetime_pattern_info_layout.xml b/app/src/main/res/layout/datetime_pattern_info_layout.xml new file mode 100644 index 00000000..36ee34d1 --- /dev/null +++ b/app/src/main/res/layout/datetime_pattern_info_layout.xml @@ -0,0 +1,9 @@ + + diff --git a/app/src/main/res/layout/dialog_manage_automatic_backups.xml b/app/src/main/res/layout/dialog_manage_automatic_backups.xml new file mode 100644 index 00000000..9aef64b4 --- /dev/null +++ b/app/src/main/res/layout/dialog_manage_automatic_backups.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + From 938c050e1d7e21bbf65d8f59f29ea3708998eeaf Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 17:33:13 +1000 Subject: [PATCH 06/11] Implemented backupNotes and other functions in Context.kt --- .../notes/pro/extensions/Context.kt | 123 +++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt index 554728ef..43410e47 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt @@ -1,18 +1,28 @@ package com.simplemobiletools.notes.pro.extensions +import android.app.AlarmManager +import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.Context import android.content.Intent +import androidx.core.app.AlarmManagerCompat import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ExportResult +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.databases.NotesDatabase import com.simplemobiletools.notes.pro.dialogs.UnlockNotesDialog -import com.simplemobiletools.notes.pro.helpers.Config -import com.simplemobiletools.notes.pro.helpers.MyWidgetProvider +import com.simplemobiletools.notes.pro.helpers.* import com.simplemobiletools.notes.pro.interfaces.NotesDao import com.simplemobiletools.notes.pro.interfaces.WidgetsDao import com.simplemobiletools.notes.pro.models.Note +import com.simplemobiletools.notes.pro.receivers.AutomaticBackupReceiver +import org.joda.time.DateTime +import java.io.File +import java.io.FileOutputStream val Context.config: Config get() = Config.newInstance(applicationContext) @@ -43,3 +53,112 @@ fun BaseSimpleActivity.requestUnlockNotes(notes: List, callback: (unlocked callback(emptyList()) } } + +fun Context.getAutomaticBackupIntent(): PendingIntent { + val intent = Intent(this, AutomaticBackupReceiver::class.java) + return PendingIntent.getBroadcast(this, AUTOMATIC_BACKUP_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) +} + +fun Context.scheduleNextAutomaticBackup() { + if (config.autoBackup) { + val backupAtMillis = getNextAutoBackupTime().millis + val pendingIntent = getAutomaticBackupIntent() + val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager + try { + AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, backupAtMillis, pendingIntent) + } catch (e: Exception) { + showErrorToast(e) + } + } +} + +fun Context.cancelScheduledAutomaticBackup() { + val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager + alarmManager.cancel(getAutomaticBackupIntent()) +} + +fun Context.checkAndBackupNotesOnBoot() { + if (config.autoBackup) { + val previousRealBackupTime = config.lastAutoBackupTime + val previousScheduledBackupTime = getPreviousAutoBackupTime().millis + val missedPreviousBackup = previousRealBackupTime < previousScheduledBackupTime + if (missedPreviousBackup) { + // device was probably off at the scheduled time so backup now + backupNotes() + } + } +} + +fun Context.backupNotes() { + require(isRPlus()) + ensureBackgroundThread { + val config = config + NotesHelper(this).getNotes { notesToBackup -> + if (notesToBackup.isEmpty()) { + toast(R.string.no_entries_for_exporting) + config.lastAutoBackupTime = DateTime.now().millis + scheduleNextAutomaticBackup() + return@getNotes + } + + + val now = DateTime.now() + val year = now.year.toString() + val month = now.monthOfYear.ensureTwoDigits() + val day = now.dayOfMonth.ensureTwoDigits() + val hours = now.hourOfDay.ensureTwoDigits() + val minutes = now.minuteOfHour.ensureTwoDigits() + val seconds = now.secondOfMinute.ensureTwoDigits() + + val filename = config.autoBackupFilename + .replace("%Y", year, false) + .replace("%M", month, false) + .replace("%D", day, false) + .replace("%h", hours, false) + .replace("%m", minutes, false) + .replace("%s", seconds, false) + + val outputFolder = File(config.autoBackupFolder).apply { + mkdirs() + } + + var exportFile = File(outputFolder, "$filename.json") + var exportFilePath = exportFile.absolutePath + val outputStream = try { + if (hasProperStoredFirstParentUri(exportFilePath)) { + val exportFileUri = createDocumentUriUsingFirstParentTreeUri(exportFilePath) + if (!getDoesFilePathExist(exportFilePath)) { + createSAFFileSdk30(exportFilePath) + } + applicationContext.contentResolver.openOutputStream(exportFileUri, "wt") ?: FileOutputStream(exportFile) + } else { + var num = 0 + while (getDoesFilePathExist(exportFilePath) && !exportFile.canWrite()) { + num++ + exportFile = File(outputFolder, "${filename}_${num}.json") + exportFilePath = exportFile.absolutePath + } + FileOutputStream(exportFile) + } + } catch (e: Exception) { + showErrorToast(e) + scheduleNextAutomaticBackup() + return@getNotes + } + + val exportResult = try { + NotesHelper(this).exportNotes(notesToBackup, outputStream) + } catch (e: Exception) { + showErrorToast(e) + } + + when (exportResult) { + ExportResult.EXPORT_OK -> toast(R.string.exporting_successful) + else -> toast(R.string.exporting_failed) + } + + config.lastAutoBackupTime = DateTime.now().millis + scheduleNextAutomaticBackup() + } + } +} From ecac42eb9412ce69a1fe47562be37c417b5c01f5 Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 17:33:53 +1000 Subject: [PATCH 07/11] Added autoBackup constants --- .../notes/pro/helpers/Constants.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt index 5d7e2b19..a61b28d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/Constants.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.notes.pro.helpers import android.graphics.Color +import org.joda.time.DateTime const val NOTE_ID = "note_id" const val OPEN_NOTE_ID = "open_note_id" @@ -39,6 +40,26 @@ const val FONT_SIZE_PERCENTAGE = "font_size_percentage" const val EXPORT_MIME_TYPE = "text/plain" const val ADD_NEW_CHECKLIST_ITEMS_TOP = "add_new_checklist_items_top" +// auto backups +const val AUTOMATIC_BACKUP_REQUEST_CODE = 10001 +const val AUTO_BACKUP_INTERVAL_IN_DAYS = 1 + +// 6 am is the hardcoded automatic backup time, intervals shorter than 1 day are not yet supported. +fun getNextAutoBackupTime(): DateTime { + val now = DateTime.now() + val sixHour = now.withHourOfDay(6) + return if (now.millis < sixHour.millis) { + sixHour + } else { + sixHour.plusDays(AUTO_BACKUP_INTERVAL_IN_DAYS) + } +} + +fun getPreviousAutoBackupTime(): DateTime { + val nextBackupTime = getNextAutoBackupTime() + return nextBackupTime.minusDays(AUTO_BACKUP_INTERVAL_IN_DAYS) +} + // gravity const val GRAVITY_START = 0 const val GRAVITY_CENTER = 1 From 571ecfdd758c61786848d968941df6effadefa99 Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 17:34:28 +1000 Subject: [PATCH 08/11] Receivers refactoring --- .../notes/pro/receivers/AutomaticBackupReceiver.kt | 1 + .../notes/pro/receivers/BootCompletedReceiver.kt | 1 + 2 files changed, 2 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/AutomaticBackupReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/AutomaticBackupReceiver.kt index 2aa60afc..7d4949be 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/AutomaticBackupReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/AutomaticBackupReceiver.kt @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.PowerManager +import com.simplemobiletools.notes.pro.extensions.backupNotes class AutomaticBackupReceiver : BroadcastReceiver() { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/BootCompletedReceiver.kt index d208b877..8cc03250 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/BootCompletedReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/receivers/BootCompletedReceiver.kt @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.notes.pro.extensions.checkAndBackupNotesOnBoot class BootCompletedReceiver : BroadcastReceiver() { From 2fd8dcf4b130f6eeaebc7e4229ce5398cbf319de Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 17:35:26 +1000 Subject: [PATCH 09/11] Implemented autoBackup setup functions in SettingsActivity --- .../notes/pro/activities/SettingsActivity.kt | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt index b6ea6db3..cac69065 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt @@ -13,10 +13,8 @@ import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.dialogs.ExportNotesDialog -import com.simplemobiletools.notes.pro.extensions.config -import com.simplemobiletools.notes.pro.extensions.requestUnlockNotes -import com.simplemobiletools.notes.pro.extensions.updateWidgets -import com.simplemobiletools.notes.pro.extensions.widgetsDB +import com.simplemobiletools.notes.pro.dialogs.ManageAutoBackupsDialog +import com.simplemobiletools.notes.pro.extensions.* import com.simplemobiletools.notes.pro.helpers.* import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Widget @@ -62,6 +60,8 @@ class SettingsActivity : SimpleActivity() { setupCustomizeWidgetColors() setupNotesExport() setupNotesImport() + setupEnableAutomaticBackups() + setupManageAutomaticBackups() updateTextColors(settings_nested_scrollview) arrayOf( @@ -356,4 +356,43 @@ class SettingsActivity : SimpleActivity() { showErrorToast(e) } } + + private fun setupEnableAutomaticBackups() { + settings_backups_label.beVisibleIf(isRPlus()) + settings_enable_automatic_backups_holder.beVisibleIf(isRPlus()) + settings_enable_automatic_backups.isChecked = config.autoBackup + settings_enable_automatic_backups_holder.setOnClickListener { + val wasBackupDisabled = !config.autoBackup + if (wasBackupDisabled) { + ManageAutoBackupsDialog( + activity = this, + onSuccess = { + enableOrDisableAutomaticBackups(true) + scheduleNextAutomaticBackup() + } + ) + } else { + cancelScheduledAutomaticBackup() + enableOrDisableAutomaticBackups(false) + } + } + } + + private fun setupManageAutomaticBackups() { + settings_manage_automatic_backups_holder.beVisibleIf(isRPlus() && config.autoBackup) + settings_manage_automatic_backups_holder.setOnClickListener { + ManageAutoBackupsDialog( + activity = this, + onSuccess = { + scheduleNextAutomaticBackup() + } + ) + } + } + + private fun enableOrDisableAutomaticBackups(enable: Boolean) { + config.autoBackup = enable + settings_enable_automatic_backups.isChecked = enable + settings_manage_automatic_backups_holder.beVisibleIf(enable) + } } From 838f50efb2dbb1370e63c49434bd5c70a9e08d60 Mon Sep 17 00:00:00 2001 From: merkost Date: Fri, 28 Jul 2023 17:36:13 +1000 Subject: [PATCH 10/11] Created exportNotes function in NotesHelper --- .../notes/pro/helpers/NotesHelper.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 bf25adb5..53fb7227 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.os.Handler import android.os.Looper import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.helpers.ExportResult import com.simplemobiletools.commons.helpers.PROTECTION_NONE import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.notes.pro.R @@ -11,7 +12,10 @@ import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.notesDB import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.NoteType +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import java.io.File +import java.io.OutputStream class NotesHelper(val context: Context) { fun getNotes(callback: (notes: List) -> Unit) { @@ -124,6 +128,18 @@ class NotesHelper(val context: Context) { } } + fun exportNotes(notesToBackup: List, outputStream: OutputStream): ExportResult { + return try { + val jsonString = Json.encodeToString(notesToBackup) + outputStream.use { + it.write(jsonString.toByteArray()) + } + ExportResult.EXPORT_OK + } catch (_: Error) { + ExportResult.EXPORT_FAIL + } + } + enum class ImportResult { IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW } From 63669734b2fcedabaea9a0485c92d2d02abc256a Mon Sep 17 00:00:00 2001 From: merkost Date: Sat, 29 Jul 2023 10:31:41 +1000 Subject: [PATCH 11/11] Removed Exporting successful toast on auto backup --- .../com/simplemobiletools/notes/pro/extensions/Context.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt index 43410e47..38792a57 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt @@ -152,9 +152,8 @@ fun Context.backupNotes() { showErrorToast(e) } - when (exportResult) { - ExportResult.EXPORT_OK -> toast(R.string.exporting_successful) - else -> toast(R.string.exporting_failed) + if (exportResult == ExportResult.EXPORT_FAIL) { + toast(R.string.exporting_failed) } config.lastAutoBackupTime = DateTime.now().millis