From cc3a70962b76784f7c89d64aa87695a778575604 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 5 Apr 2012 12:52:00 -0700 Subject: [PATCH 1/3] Make a special backup before upgrades --- .../todoroo/astrid/backup/BackupActivity.java | 2 +- .../astrid/backup/BackupConstants.java | 2 + .../todoroo/astrid/backup/BackupService.java | 2 +- .../astrid/backup/TasksXmlExporter.java | 54 +++++++++++++------ .../astrid/service/StartupService.java | 6 +++ .../astrid/utility/AstridPreferences.java | 11 ++++ 6 files changed, 59 insertions(+), 18 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java b/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java index f6d45e464..9d170b30b 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java @@ -56,7 +56,7 @@ public class BackupActivity extends Activity { } private void exportTasks() { - TasksXmlExporter.exportTasks(this, false, new Runnable() { + TasksXmlExporter.exportTasks(this, TasksXmlExporter.ExportType.EXPORT_TYPE_MANUAL, new Runnable() { @Override public void run() { setResult(RESULT_OK); diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/BackupConstants.java b/astrid/plugin-src/com/todoroo/astrid/backup/BackupConstants.java index 9cc96591b..927fb455d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupConstants.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupConstants.java @@ -56,6 +56,8 @@ public class BackupConstants { public static final String BACKUP_FILE_NAME = "auto.%s.xml"; + public static final String UPGRADE_FILE_NAME = "upgradefrom.%s.xml"; + // --- methods /** diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java b/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java index 4e4c2f297..91218bde8 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java @@ -77,7 +77,7 @@ public class BackupService extends Service { Log.e("error-deleting", "Error deleting old backups", e); //$NON-NLS-1$ //$NON-NLS-2$ } - TasksXmlExporter.exportTasks(context, true, null, + TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_SERVICE, null, backupDirectorySetting.getBackupDirectory()); } catch (Exception e) { diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java b/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java index 08c6afefe..2f4ad81cd 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java @@ -41,16 +41,22 @@ public class TasksXmlExporter { * Import tasks from the given file * * @param context context - * @param isService if false, displays ui dialogs + * @param exportType from service, manual, or on upgrade * @param runAfterExport runnable to run after exporting * @param backupDirectoryOverride new backupdirectory, or null to use default */ - public static void exportTasks(Context context, boolean isService, + public static void exportTasks(Context context, ExportType exportType, Runnable runAfterExport, File backupDirectoryOverride) { - new TasksXmlExporter(context, isService, runAfterExport, + new TasksXmlExporter(context, exportType, runAfterExport, backupDirectoryOverride); } + public static enum ExportType { + EXPORT_TYPE_SERVICE, + EXPORT_TYPE_MANUAL, + EXPORT_TYPE_ON_UPGRADE + } + // --- implementation private static final int FORMAT = 2; @@ -65,6 +71,7 @@ public class TasksXmlExporter { private final ProgressDialog progressDialog; private final Handler handler; private final File backupDirectory; + private final String latestSetVersionName; private void setProgress(final int taskNumber, final int total) { handler.post(new Runnable() { @@ -75,16 +82,17 @@ public class TasksXmlExporter { }); } - private TasksXmlExporter(final Context context, final boolean isService, + private TasksXmlExporter(final Context context, final ExportType exportType, final Runnable runAfterExport, File backupDirectoryOverride) { this.context = context; this.exportCount = 0; this.backupDirectory = backupDirectoryOverride == null ? BackupConstants.defaultExportDirectory() : backupDirectoryOverride; + this.latestSetVersionName = AstridPreferences.getCurrentVersionName(); handler = new Handler(); progressDialog = new ProgressDialog(context); - if(!isService) { + if(exportType == ExportType.EXPORT_TYPE_MANUAL) { progressDialog.setIcon(android.R.drawable.ic_dialog_info); progressDialog.setTitle(R.string.export_progress_title); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); @@ -101,7 +109,7 @@ public class TasksXmlExporter { public void run() { try { String output = setupFile(backupDirectory, - isService); + exportType); int tasks = taskService.countTasks(); if(tasks > 0) @@ -111,15 +119,22 @@ public class TasksXmlExporter { DateUtilities.now()); Preferences.setString(BackupPreferences.PREF_BACKUP_LAST_ERROR, null); - if (!isService) + if (exportType == ExportType.EXPORT_TYPE_MANUAL) onFinishExport(output); } catch (IOException e) { - if(!isService) + switch(exportType) { + case EXPORT_TYPE_MANUAL: exceptionService.displayAndReportError(context, context.getString(R.string.backup_TXI_error), e); - else { + break; + case EXPORT_TYPE_SERVICE: + exceptionService.reportError("background-backup", e); //$NON-NLS-1$ + Preferences.setString(BackupPreferences.PREF_BACKUP_LAST_ERROR, e.toString()); + break; + case EXPORT_TYPE_ON_UPGRADE: exceptionService.reportError("background-backup", e); //$NON-NLS-1$ Preferences.setString(BackupPreferences.PREF_BACKUP_LAST_ERROR, e.toString()); + break; } } finally { if(runAfterExport != null) @@ -320,18 +335,25 @@ public class TasksXmlExporter { * @return output file name * @throws IOException */ - private String setupFile(File directory, boolean isService) throws IOException { + private String setupFile(File directory, ExportType exportType) throws IOException { File astridDir = directory; if (astridDir != null) { // Check for /sdcard/astrid directory. If it doesn't exist, make it. if (astridDir.exists() || astridDir.mkdir()) { - String fileName; - if (isService) { - fileName = BackupConstants.BACKUP_FILE_NAME; - } else { - fileName = BackupConstants.EXPORT_FILE_NAME; + String fileName = ""; //$NON-NLS-1$ + switch(exportType) { + case EXPORT_TYPE_SERVICE: + fileName = String.format(BackupConstants.BACKUP_FILE_NAME, BackupDateUtilities.getDateForExport()); + break; + case EXPORT_TYPE_MANUAL: + fileName = String.format(BackupConstants.EXPORT_FILE_NAME, BackupDateUtilities.getDateForExport()); + break; + case EXPORT_TYPE_ON_UPGRADE: + fileName = String.format(BackupConstants.UPGRADE_FILE_NAME, latestSetVersionName); + break; + default: + throw new IllegalArgumentException("Invalid export type"); //$NON-NLS-1$ } - fileName = String.format(fileName, BackupDateUtilities.getDateForExport()); return astridDir.getAbsolutePath() + File.separator + fileName; } else { // Unable to make the /sdcard/astrid directory. diff --git a/astrid/src/com/todoroo/astrid/service/StartupService.java b/astrid/src/com/todoroo/astrid/service/StartupService.java index bc28aef17..1e9b3f7c0 100644 --- a/astrid/src/com/todoroo/astrid/service/StartupService.java +++ b/astrid/src/com/todoroo/astrid/service/StartupService.java @@ -34,6 +34,7 @@ import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.actfm.sync.ActFmSyncService; import com.todoroo.astrid.backup.BackupConstants; import com.todoroo.astrid.backup.BackupService; +import com.todoroo.astrid.backup.TasksXmlExporter; import com.todoroo.astrid.backup.TasksXmlImporter; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.gtasks.GtasksPreferenceService; @@ -148,10 +149,12 @@ public class StartupService { } int version = 0; + String versionName = "0"; //$NON-NLS-1$ try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(Constants.PACKAGE, PackageManager.GET_META_DATA); version = pi.versionCode; + versionName = pi.versionName; } catch (Exception e) { exceptionService.reportError("astrid-startup-package-read", e); //$NON-NLS-1$ } @@ -165,9 +168,12 @@ public class StartupService { boolean justUpgraded = latestSetVersion != version; if(justUpgraded && version > 0) { if(latestSetVersion > 0) { + // Perform backup before upgrade + TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_ON_UPGRADE, null, null); upgradeService.performUpgrade(context, latestSetVersion); } AstridPreferences.setCurrentVersion(version); + AstridPreferences.setCurrentVersionName(versionName); } upgradeService.performSecondaryUpgrade(context); diff --git a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java index 2bd06f982..c873192e1 100644 --- a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java +++ b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java @@ -16,6 +16,8 @@ public class AstridPreferences { private static final String P_CURRENT_VERSION = "cv"; //$NON-NLS-1$ + private static final String P_CURRENT_VERSION_NAME = "cvname"; //$NON-NLS-1$ + public static final String P_FIRST_TASK = "ft"; //$NON-NLS-1$ public static final String P_FIRST_LIST = "fl"; //$NON-NLS-1$ @@ -77,6 +79,15 @@ public class AstridPreferences { Preferences.setInt(P_CURRENT_VERSION, version); } + /** The name (e.g. 4.0.1) of the currently installed version of astrid*/ + public static String getCurrentVersionName() { + return Preferences.getStringValue(P_CURRENT_VERSION_NAME); + } + + public static void setCurrentVersionName(String versionName) { + Preferences.setString(P_CURRENT_VERSION_NAME, versionName); + } + /** If true, can show a help popover. If false, another one was recently shown */ public static boolean canShowPopover() { long last = Preferences.getLong(P_LAST_POPOVER, 0); From 09f4d16aa1356b52e81047bb5177acef1f2d1be6 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 5 Apr 2012 13:59:30 -0700 Subject: [PATCH 2/3] Don't let last version name be null --- astrid/src/com/todoroo/astrid/utility/AstridPreferences.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java index c873192e1..ff3315f9e 100644 --- a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java +++ b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java @@ -81,7 +81,10 @@ public class AstridPreferences { /** The name (e.g. 4.0.1) of the currently installed version of astrid*/ public static String getCurrentVersionName() { - return Preferences.getStringValue(P_CURRENT_VERSION_NAME); + String versionName = Preferences.getStringValue(P_CURRENT_VERSION_NAME); + if (versionName == null) + versionName = "0"; //$NON-NLS-1$ + return versionName; } public static void setCurrentVersionName(String versionName) { From 75b51c883913571db9e9916ecce7b6c745421c7d Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 5 Apr 2012 16:26:51 -0700 Subject: [PATCH 3/3] Only do backups before upgrade when necessary. Added a reminder comment in upgrade service to remind developers to do this --- .../com/todoroo/astrid/backup/BackupActivity.java | 2 +- .../com/todoroo/astrid/backup/BackupService.java | 2 +- .../com/todoroo/astrid/backup/TasksXmlExporter.java | 8 ++++---- astrid/src/com/todoroo/astrid/service/StartupService.java | 3 --- astrid/src/com/todoroo/astrid/service/UpgradeService.java | 5 +++++ 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java b/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java index 9d170b30b..1ec407f45 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java @@ -62,7 +62,7 @@ public class BackupActivity extends Activity { setResult(RESULT_OK); finish(); } - }, null); + }, null, null); } } diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java b/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java index 91218bde8..c47119522 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java @@ -78,7 +78,7 @@ public class BackupService extends Service { } TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_SERVICE, null, - backupDirectorySetting.getBackupDirectory()); + backupDirectorySetting.getBackupDirectory(), null); } catch (Exception e) { Log.e("error-backup", "Error starting backups", e); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java b/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java index 2f4ad81cd..90e308860 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java @@ -46,9 +46,9 @@ public class TasksXmlExporter { * @param backupDirectoryOverride new backupdirectory, or null to use default */ public static void exportTasks(Context context, ExportType exportType, - Runnable runAfterExport, File backupDirectoryOverride) { + Runnable runAfterExport, File backupDirectoryOverride, String versionName) { new TasksXmlExporter(context, exportType, runAfterExport, - backupDirectoryOverride); + backupDirectoryOverride, versionName); } public static enum ExportType { @@ -83,12 +83,12 @@ public class TasksXmlExporter { } private TasksXmlExporter(final Context context, final ExportType exportType, - final Runnable runAfterExport, File backupDirectoryOverride) { + final Runnable runAfterExport, File backupDirectoryOverride, String versionName) { this.context = context; this.exportCount = 0; this.backupDirectory = backupDirectoryOverride == null ? BackupConstants.defaultExportDirectory() : backupDirectoryOverride; - this.latestSetVersionName = AstridPreferences.getCurrentVersionName(); + this.latestSetVersionName = versionName; handler = new Handler(); progressDialog = new ProgressDialog(context); diff --git a/astrid/src/com/todoroo/astrid/service/StartupService.java b/astrid/src/com/todoroo/astrid/service/StartupService.java index 1e9b3f7c0..af1df49c8 100644 --- a/astrid/src/com/todoroo/astrid/service/StartupService.java +++ b/astrid/src/com/todoroo/astrid/service/StartupService.java @@ -34,7 +34,6 @@ import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.actfm.sync.ActFmSyncService; import com.todoroo.astrid.backup.BackupConstants; import com.todoroo.astrid.backup.BackupService; -import com.todoroo.astrid.backup.TasksXmlExporter; import com.todoroo.astrid.backup.TasksXmlImporter; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.gtasks.GtasksPreferenceService; @@ -168,8 +167,6 @@ public class StartupService { boolean justUpgraded = latestSetVersion != version; if(justUpgraded && version > 0) { if(latestSetVersion > 0) { - // Perform backup before upgrade - TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_ON_UPGRADE, null, null); upgradeService.performUpgrade(context, latestSetVersion); } AstridPreferences.setCurrentVersion(version); diff --git a/astrid/src/com/todoroo/astrid/service/UpgradeService.java b/astrid/src/com/todoroo/astrid/service/UpgradeService.java index 5007e0560..e841439fd 100644 --- a/astrid/src/com/todoroo/astrid/service/UpgradeService.java +++ b/astrid/src/com/todoroo/astrid/service/UpgradeService.java @@ -145,12 +145,17 @@ public final class UpgradeService { else dialog = null; + final String lastSetVersionName = AstridPreferences.getCurrentVersionName(); + Preferences.setInt(AstridPreferences.P_UPGRADE_FROM, from); new Thread(new Runnable() { @Override public void run() { try { + // NOTE: This line should be uncommented whenever any new version requires a data migration + // TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_ON_UPGRADE, null, null, lastSetVersionName); + if(from < V3_0_0) new Astrid2To3UpgradeHelper().upgrade2To3(context, from);