diff --git a/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java b/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java index f6d45e464..1ec407f45 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupActivity.java @@ -56,13 +56,13 @@ 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); finish(); } - }, null); + }, null, null); } } 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..c47119522 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/BackupService.java @@ -77,8 +77,8 @@ 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, - backupDirectorySetting.getBackupDirectory()); + TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_SERVICE, null, + 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 08c6afefe..90e308860 100644 --- a/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java +++ b/astrid/plugin-src/com/todoroo/astrid/backup/TasksXmlExporter.java @@ -41,14 +41,20 @@ 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, - Runnable runAfterExport, File backupDirectoryOverride) { - new TasksXmlExporter(context, isService, runAfterExport, - backupDirectoryOverride); + public static void exportTasks(Context context, ExportType exportType, + Runnable runAfterExport, File backupDirectoryOverride, String versionName) { + new TasksXmlExporter(context, exportType, runAfterExport, + backupDirectoryOverride, versionName); + } + + public static enum ExportType { + EXPORT_TYPE_SERVICE, + EXPORT_TYPE_MANUAL, + EXPORT_TYPE_ON_UPGRADE } // --- implementation @@ -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, - final Runnable runAfterExport, File backupDirectoryOverride) { + private TasksXmlExporter(final Context context, final ExportType exportType, + final Runnable runAfterExport, File backupDirectoryOverride, String versionName) { this.context = context; this.exportCount = 0; this.backupDirectory = backupDirectoryOverride == null ? BackupConstants.defaultExportDirectory() : backupDirectoryOverride; + this.latestSetVersionName = versionName; 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..af1df49c8 100644 --- a/astrid/src/com/todoroo/astrid/service/StartupService.java +++ b/astrid/src/com/todoroo/astrid/service/StartupService.java @@ -148,10 +148,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$ } @@ -168,6 +170,7 @@ public class StartupService { upgradeService.performUpgrade(context, latestSetVersion); } AstridPreferences.setCurrentVersion(version); + AstridPreferences.setCurrentVersionName(versionName); } upgradeService.performSecondaryUpgrade(context); 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); diff --git a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java index 2bd06f982..ff3315f9e 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,18 @@ 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() { + String versionName = Preferences.getStringValue(P_CURRENT_VERSION_NAME); + if (versionName == null) + versionName = "0"; //$NON-NLS-1$ + return versionName; + } + + 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);