Make a special backup before upgrades

pull/14/head
Sam Bosley 13 years ago
parent ac40dc9d4f
commit cc3a70962b

@ -56,7 +56,7 @@ public class BackupActivity extends Activity {
} }
private void exportTasks() { private void exportTasks() {
TasksXmlExporter.exportTasks(this, false, new Runnable() { TasksXmlExporter.exportTasks(this, TasksXmlExporter.ExportType.EXPORT_TYPE_MANUAL, new Runnable() {
@Override @Override
public void run() { public void run() {
setResult(RESULT_OK); setResult(RESULT_OK);

@ -56,6 +56,8 @@ public class BackupConstants {
public static final String BACKUP_FILE_NAME = "auto.%s.xml"; public static final String BACKUP_FILE_NAME = "auto.%s.xml";
public static final String UPGRADE_FILE_NAME = "upgradefrom.%s.xml";
// --- methods // --- methods
/** /**

@ -77,7 +77,7 @@ public class BackupService extends Service {
Log.e("error-deleting", "Error deleting old backups", e); //$NON-NLS-1$ //$NON-NLS-2$ 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()); backupDirectorySetting.getBackupDirectory());
} catch (Exception e) { } catch (Exception e) {

@ -41,16 +41,22 @@ public class TasksXmlExporter {
* Import tasks from the given file * Import tasks from the given file
* *
* @param context context * @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 runAfterExport runnable to run after exporting
* @param backupDirectoryOverride new backupdirectory, or null to use default * @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) { Runnable runAfterExport, File backupDirectoryOverride) {
new TasksXmlExporter(context, isService, runAfterExport, new TasksXmlExporter(context, exportType, runAfterExport,
backupDirectoryOverride); backupDirectoryOverride);
} }
public static enum ExportType {
EXPORT_TYPE_SERVICE,
EXPORT_TYPE_MANUAL,
EXPORT_TYPE_ON_UPGRADE
}
// --- implementation // --- implementation
private static final int FORMAT = 2; private static final int FORMAT = 2;
@ -65,6 +71,7 @@ public class TasksXmlExporter {
private final ProgressDialog progressDialog; private final ProgressDialog progressDialog;
private final Handler handler; private final Handler handler;
private final File backupDirectory; private final File backupDirectory;
private final String latestSetVersionName;
private void setProgress(final int taskNumber, final int total) { private void setProgress(final int taskNumber, final int total) {
handler.post(new Runnable() { 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) { final Runnable runAfterExport, File backupDirectoryOverride) {
this.context = context; this.context = context;
this.exportCount = 0; this.exportCount = 0;
this.backupDirectory = backupDirectoryOverride == null ? this.backupDirectory = backupDirectoryOverride == null ?
BackupConstants.defaultExportDirectory() : backupDirectoryOverride; BackupConstants.defaultExportDirectory() : backupDirectoryOverride;
this.latestSetVersionName = AstridPreferences.getCurrentVersionName();
handler = new Handler(); handler = new Handler();
progressDialog = new ProgressDialog(context); progressDialog = new ProgressDialog(context);
if(!isService) { if(exportType == ExportType.EXPORT_TYPE_MANUAL) {
progressDialog.setIcon(android.R.drawable.ic_dialog_info); progressDialog.setIcon(android.R.drawable.ic_dialog_info);
progressDialog.setTitle(R.string.export_progress_title); progressDialog.setTitle(R.string.export_progress_title);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
@ -101,7 +109,7 @@ public class TasksXmlExporter {
public void run() { public void run() {
try { try {
String output = setupFile(backupDirectory, String output = setupFile(backupDirectory,
isService); exportType);
int tasks = taskService.countTasks(); int tasks = taskService.countTasks();
if(tasks > 0) if(tasks > 0)
@ -111,15 +119,22 @@ public class TasksXmlExporter {
DateUtilities.now()); DateUtilities.now());
Preferences.setString(BackupPreferences.PREF_BACKUP_LAST_ERROR, null); Preferences.setString(BackupPreferences.PREF_BACKUP_LAST_ERROR, null);
if (!isService) if (exportType == ExportType.EXPORT_TYPE_MANUAL)
onFinishExport(output); onFinishExport(output);
} catch (IOException e) { } catch (IOException e) {
if(!isService) switch(exportType) {
case EXPORT_TYPE_MANUAL:
exceptionService.displayAndReportError(context, exceptionService.displayAndReportError(context,
context.getString(R.string.backup_TXI_error), e); context.getString(R.string.backup_TXI_error), e);
else { break;
case EXPORT_TYPE_SERVICE:
exceptionService.reportError("background-backup", e); //$NON-NLS-1$ exceptionService.reportError("background-backup", e); //$NON-NLS-1$
Preferences.setString(BackupPreferences.PREF_BACKUP_LAST_ERROR, e.toString()); 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 { } finally {
if(runAfterExport != null) if(runAfterExport != null)
@ -320,18 +335,25 @@ public class TasksXmlExporter {
* @return output file name * @return output file name
* @throws IOException * @throws IOException
*/ */
private String setupFile(File directory, boolean isService) throws IOException { private String setupFile(File directory, ExportType exportType) throws IOException {
File astridDir = directory; File astridDir = directory;
if (astridDir != null) { if (astridDir != null) {
// Check for /sdcard/astrid directory. If it doesn't exist, make it. // Check for /sdcard/astrid directory. If it doesn't exist, make it.
if (astridDir.exists() || astridDir.mkdir()) { if (astridDir.exists() || astridDir.mkdir()) {
String fileName; String fileName = ""; //$NON-NLS-1$
if (isService) { switch(exportType) {
fileName = BackupConstants.BACKUP_FILE_NAME; case EXPORT_TYPE_SERVICE:
} else { fileName = String.format(BackupConstants.BACKUP_FILE_NAME, BackupDateUtilities.getDateForExport());
fileName = BackupConstants.EXPORT_FILE_NAME; 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; return astridDir.getAbsolutePath() + File.separator + fileName;
} else { } else {
// Unable to make the /sdcard/astrid directory. // Unable to make the /sdcard/astrid directory.

@ -34,6 +34,7 @@ import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.actfm.sync.ActFmSyncService; import com.todoroo.astrid.actfm.sync.ActFmSyncService;
import com.todoroo.astrid.backup.BackupConstants; import com.todoroo.astrid.backup.BackupConstants;
import com.todoroo.astrid.backup.BackupService; import com.todoroo.astrid.backup.BackupService;
import com.todoroo.astrid.backup.TasksXmlExporter;
import com.todoroo.astrid.backup.TasksXmlImporter; import com.todoroo.astrid.backup.TasksXmlImporter;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
@ -148,10 +149,12 @@ public class StartupService {
} }
int version = 0; int version = 0;
String versionName = "0"; //$NON-NLS-1$
try { try {
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(Constants.PACKAGE, PackageManager.GET_META_DATA); PackageInfo pi = pm.getPackageInfo(Constants.PACKAGE, PackageManager.GET_META_DATA);
version = pi.versionCode; version = pi.versionCode;
versionName = pi.versionName;
} catch (Exception e) { } catch (Exception e) {
exceptionService.reportError("astrid-startup-package-read", e); //$NON-NLS-1$ exceptionService.reportError("astrid-startup-package-read", e); //$NON-NLS-1$
} }
@ -165,9 +168,12 @@ public class StartupService {
boolean justUpgraded = latestSetVersion != version; boolean justUpgraded = latestSetVersion != version;
if(justUpgraded && version > 0) { if(justUpgraded && version > 0) {
if(latestSetVersion > 0) { if(latestSetVersion > 0) {
// Perform backup before upgrade
TasksXmlExporter.exportTasks(context, TasksXmlExporter.ExportType.EXPORT_TYPE_ON_UPGRADE, null, null);
upgradeService.performUpgrade(context, latestSetVersion); upgradeService.performUpgrade(context, latestSetVersion);
} }
AstridPreferences.setCurrentVersion(version); AstridPreferences.setCurrentVersion(version);
AstridPreferences.setCurrentVersionName(versionName);
} }
upgradeService.performSecondaryUpgrade(context); upgradeService.performSecondaryUpgrade(context);

@ -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 = "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_TASK = "ft"; //$NON-NLS-1$
public static final String P_FIRST_LIST = "fl"; //$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); 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 */ /** If true, can show a help popover. If false, another one was recently shown */
public static boolean canShowPopover() { public static boolean canShowPopover() {
long last = Preferences.getLong(P_LAST_POPOVER, 0); long last = Preferences.getLong(P_LAST_POPOVER, 0);

Loading…
Cancel
Save