Modern Apps git

← Modern-Apps.git

library:ui: relaunch after restoring a backup so it sticks (#548)

Commit
1b9b1b538ca91a046ae7e560516cdf7ccadcd320
Author
vayun-mathur <[email protected]>
When
2026-09-07 01:26 UTC (3 months ago)
Parents
342487c

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/BackupButtons.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/BackupButtons.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/BackupButtons.ktindex 7d109d4187..6170dde3f4 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/BackupButtons.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/BackupButtons.kt@@ -21,6 +21,7 @@ import com.vayunmathur.library.util.BackupFormat import com.vayunmathur.library.util.DbBackupCodec import com.vayunmathur.library.util.ZipBackupFormat import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File@@ -136,6 +137,20 @@ fun BackupButtons(format: BackupFormat) {  private enum class PasswordDialogAction { EXPORT, IMPORT } +/** Long enough for the import message to be read before the process goes away. */+private const val RESTART_DELAY_MS = 1500L++private fun relaunch(context: android.content.Context) {+    context.packageManager.getLaunchIntentForPackage(context.packageName)?.let { intent ->+        intent.addFlags(+            android.content.Intent.FLAG_ACTIVITY_NEW_TASK or+                android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK+        )+        context.startActivity(intent)+    }+    Runtime.getRuntime().exit(0)+}+ private suspend fun runExport(     context: android.content.Context,     format: BackupFormat,@@ -170,6 +185,12 @@ private suspend fun runImport(         withContext(Dispatchers.Main) {             AppMessages.show(context.getString(R.string.backup_import_success))         }+        // DataStore and Room assume exclusive ownership of their files and keep the contents+        // cached, so a live process neither picks up the files the import just replaced nor stops+        // writing the pre-restore state back over them - which silently undid the restore before+        // the user got round to restarting (#548). Give the message a moment, then relaunch.+        delay(RESTART_DELAY_MS)+        withContext(Dispatchers.Main) { relaunch(context) }     } catch (e: Exception) {         Log.e("BackupButtons", "Import FAILED", e)         withContext(Dispatchers.Main) {
library/ui/src/main/res/values/strings.xml
diff --git a/library/ui/src/main/res/values/strings.xml b/library/ui/src/main/res/values/strings.xmlindex ad71e13e74..fc9fa33eb7 100644--- a/library/ui/src/main/res/values/strings.xml+++ b/library/ui/src/main/res/values/strings.xml@@ -11,8 +11,8 @@     <string name="backup_export_success">Backup exported successfully</string>     <!-- Shared library: UI – BackupButtons: Toast when export fails. %1$s = raw exception message, do not translate placeholder content -->     <string name="backup_export_failed_format">Backup export failed: <xliff:g id="error" example="permission denied">%1$s</xliff:g></string>-    <!-- Shared library: UI – BackupButtons: Toast when import succeeds, asks user to restart app. Long duration -->-    <string name="backup_import_success">Backup imported successfully. Please restart the app.</string>+    <!-- Shared library: UI – BackupButtons: Toast when import succeeds, shown just before the app relaunches itself so the restored files take effect. Long duration -->+    <string name="backup_import_success">Backup imported successfully. Restarting the app…</string>     <!-- Shared library: UI – BackupButtons: Toast when import fails. %1$s = raw exception message, do not translate placeholder content -->     <string name="backup_import_failed_format">Backup import failed: <xliff:g id="error" example="invalid file">%1$s</xliff:g></string>     <!-- Shared library: UI – TimePickerDialog: Dialog title for time picker, shown in top of picker. Noun phrase, keep short -->