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() {
TasksXmlExporter.exportTasks(this, false, new Runnable() {
TasksXmlExporter.exportTasks(this, TasksXmlExporter.ExportType.EXPORT_TYPE_MANUAL, new Runnable() {
@Override
public void run() {
setResult(RESULT_OK);

@ -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
/**

@ -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) {

@ -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.

@ -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);

@ -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);

Loading…
Cancel
Save