Created upgrade from Alarm in separate table to Alarm in metadata

pull/14/head
Tim Su 16 years ago
parent 3446d6c6db
commit 017dc5c0c3

@ -3,7 +3,7 @@
* All Rights Reserved
* http://www.todoroo.com
*/
package com.todoroo.astrid.alarms;
package com.todoroo.astrid.legacy;
import com.todoroo.andlib.data.AbstractDatabase;
import com.todoroo.andlib.data.GenericDao;
@ -15,7 +15,7 @@ import com.todoroo.andlib.data.Table;
* @author Tim Su <tim@todoroo.com>
*
*/
@SuppressWarnings("nls")
@SuppressWarnings({"nls","deprecation"})
public class AlarmDatabase extends AbstractDatabase {
// --- constants
@ -29,7 +29,7 @@ public class AlarmDatabase extends AbstractDatabase {
/**
* Database name (must be unique)
*/
private static final String NAME = "alarms";
public static final String NAME = "alarms";
/**
* List of table/ If you're adding a new table, add it to this list and

@ -1,7 +1,7 @@
/**
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.alarms;
package com.todoroo.astrid.legacy;
import android.content.ContentValues;

@ -5,8 +5,6 @@ import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
@ -23,29 +21,31 @@ import com.timsu.astrid.utilities.LegacyTasksXmlExporter;
import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.GenericDao;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.ExceptionService;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.activity.TaskListActivity;
import com.todoroo.astrid.alarms.TransitionalAlarm;
import com.todoroo.astrid.alarms.AlarmDatabase;
import com.todoroo.astrid.alarms.Alarm;
import com.todoroo.astrid.backup.TasksXmlImporter;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.legacy.AlarmDatabase;
import com.todoroo.astrid.legacy.LegacyAlertModel;
import com.todoroo.astrid.legacy.LegacyRepeatInfo;
import com.todoroo.astrid.legacy.LegacyTaskModel;
import com.todoroo.astrid.legacy.TransitionalAlarm;
import com.todoroo.astrid.model.Metadata;
import com.todoroo.astrid.model.Task;
import com.todoroo.astrid.rmilk.data.MilkTask;
import com.todoroo.astrid.tags.TagService;
import com.todoroo.astrid.utility.Preferences;
@SuppressWarnings("deprecation")
public class Astrid2To3UpgradeHelper {
@Autowired
@ -75,9 +75,6 @@ public class Astrid2To3UpgradeHelper {
@Autowired
private String syncTable;
@Autowired
private DialogUtilities dialogUtilities;
@Autowired
private ExceptionService exceptionService;
@ -110,11 +107,29 @@ public class Astrid2To3UpgradeHelper {
}
// ---------------------------------------------------------------- 3 => 3.1
/**
* Perform the upgrade from Astrid 3 to 3.1
* @param context
* @param upgradeService
* @param from
*/
public void upgrade3To3_1(final Context context, final int from) {
if(!checkIfDatabaseExists(context, alertsTable))
return;
database.openForWriting();
migrateAlarmsToMetadata();
}
// ----------------------------------------------------------------- 2 => 3
/**
* Perform the upgrade from Astrid 2 to Astrid 3
* @param context2
*/
public void upgrade2To3(final Context context, final UpgradeService upgradeService, final int from) {
public void upgrade2To3(final Context context, final int from) {
// if from < 1 (we don't know what version, and database exists, leave it alone)
if(from < 1 && checkIfDatabaseExists(context, database.getName()))
@ -129,98 +144,72 @@ public class Astrid2To3UpgradeHelper {
context.deleteDatabase(database.getName());
database.openForWriting();
// pop up a progress dialog
final ProgressDialog dialog;
if(context instanceof Activity)
dialog = dialogUtilities.progressDialog(context,
context.getString(R.string.DLG_upgrading));
else
dialog = null;
new Thread(new Runnable() {
@Override
public void run() {
// initiate a backup
String backupFile = legacyBackup();
try {
// --- upgrade tasks table
HashMap<String, Property<?>> propertyMap =
new HashMap<String, Property<?>>();
propertyMap.put("_id", Task.ID); //$NON-NLS-1$
propertyMap.put(LegacyTaskModel.NAME, Task.TITLE);
propertyMap.put(LegacyTaskModel.NOTES, Task.NOTES);
// (don't update progress percentage, we don't use this anymore)
propertyMap.put(LegacyTaskModel.IMPORTANCE, Task.IMPORTANCE);
propertyMap.put(LegacyTaskModel.ESTIMATED_SECONDS, Task.ESTIMATED_SECONDS);
propertyMap.put(LegacyTaskModel.ELAPSED_SECONDS, Task.ELAPSED_SECONDS);
propertyMap.put(LegacyTaskModel.TIMER_START, Task.TIMER_START);
propertyMap.put(LegacyTaskModel.DEFINITE_DUE_DATE, Task.DUE_DATE);
propertyMap.put(LegacyTaskModel.HIDDEN_UNTIL, Task.HIDE_UNTIL);
propertyMap.put(LegacyTaskModel.POSTPONE_COUNT, Task.POSTPONE_COUNT);
propertyMap.put(LegacyTaskModel.NOTIFICATIONS, Task.REMINDER_PERIOD);
propertyMap.put(LegacyTaskModel.NOTIFICATION_FLAGS, Task.REMINDER_FLAGS);
propertyMap.put(LegacyTaskModel.LAST_NOTIFIED, Task.REMINDER_LAST);
propertyMap.put(LegacyTaskModel.REPEAT, Task.RECURRENCE);
propertyMap.put(LegacyTaskModel.CREATION_DATE, Task.CREATION_DATE);
propertyMap.put(LegacyTaskModel.COMPLETION_DATE, Task.COMPLETION_DATE);
propertyMap.put(LegacyTaskModel.CALENDAR_URI, Task.CALENDAR_URI);
propertyMap.put(LegacyTaskModel.FLAGS, Task.FLAGS);
upgradeTable(context, tasksTable,
propertyMap, new Task(), taskDao);
// --- upgrade tags tables
migrateTagsToMetadata();
// --- upgrade alerts
AlarmDatabase alarmsDatabase = new AlarmDatabase();
alarmsDatabase.openForWriting();
propertyMap.clear();
propertyMap.put("_id", TransitionalAlarm.ID); //$NON-NLS-1$
propertyMap.put(LegacyAlertModel.TASK, TransitionalAlarm.TASK);
propertyMap.put(LegacyAlertModel.DATE, TransitionalAlarm.TIME);
upgradeTable(context, alertsTable, propertyMap, new TransitionalAlarm(),
alarmsDatabase.getDao());
alarmsDatabase.close();
// --- upgrade RTM sync mappings
migrateSyncMappingToMetadata();
// --- clean up database
metadataService.cleanup();
// --- upgrade properties
SharedPreferences prefs = Preferences.getPrefs(context);
Editor editor = prefs.edit();
int random = Preferences.getIntegerFromString(R.string.p_rmd_default_random_hours, -1);
if(random != -1) {
// convert days => hours
editor.putString(context.getString(R.string.p_rmd_default_random_hours),
Integer.toString(random * 24));
}
} catch (Exception e) {
exceptionService.reportError("backup-error", e); //$NON-NLS-1$
if(backupFile != null) {
// try to restore the latest XML
TasksXmlImporter.importTasks(context, backupFile, null);
}
} finally {
if(context instanceof Activity) {
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
if(dialog != null)
dialog.dismiss();
upgradeService.showChangeLog(context, from);
if(context instanceof TaskListActivity)
((TaskListActivity)context).loadTaskListContent(true);
}
});
}
}
}
}).start();
// initiate a backup
String backupFile = legacyBackup();
try {
// --- upgrade tasks table
HashMap<String, Property<?>> propertyMap =
new HashMap<String, Property<?>>();
propertyMap.put("_id", Task.ID); //$NON-NLS-1$
propertyMap.put(LegacyTaskModel.NAME, Task.TITLE);
propertyMap.put(LegacyTaskModel.NOTES, Task.NOTES);
// (don't update progress percentage, we don't use this anymore)
propertyMap.put(LegacyTaskModel.IMPORTANCE, Task.IMPORTANCE);
propertyMap.put(LegacyTaskModel.ESTIMATED_SECONDS, Task.ESTIMATED_SECONDS);
propertyMap.put(LegacyTaskModel.ELAPSED_SECONDS, Task.ELAPSED_SECONDS);
propertyMap.put(LegacyTaskModel.TIMER_START, Task.TIMER_START);
propertyMap.put(LegacyTaskModel.DEFINITE_DUE_DATE, Task.DUE_DATE);
propertyMap.put(LegacyTaskModel.HIDDEN_UNTIL, Task.HIDE_UNTIL);
propertyMap.put(LegacyTaskModel.POSTPONE_COUNT, Task.POSTPONE_COUNT);
propertyMap.put(LegacyTaskModel.NOTIFICATIONS, Task.REMINDER_PERIOD);
propertyMap.put(LegacyTaskModel.NOTIFICATION_FLAGS, Task.REMINDER_FLAGS);
propertyMap.put(LegacyTaskModel.LAST_NOTIFIED, Task.REMINDER_LAST);
propertyMap.put(LegacyTaskModel.REPEAT, Task.RECURRENCE);
propertyMap.put(LegacyTaskModel.CREATION_DATE, Task.CREATION_DATE);
propertyMap.put(LegacyTaskModel.COMPLETION_DATE, Task.COMPLETION_DATE);
propertyMap.put(LegacyTaskModel.CALENDAR_URI, Task.CALENDAR_URI);
propertyMap.put(LegacyTaskModel.FLAGS, Task.FLAGS);
upgradeTable(context, tasksTable,
propertyMap, new Task(), taskDao);
// --- upgrade tags tables
migrateTagsToMetadata();
// --- upgrade alerts
AlarmDatabase alarmsDatabase = new AlarmDatabase();
alarmsDatabase.openForWriting();
propertyMap.clear();
propertyMap.put("_id", TransitionalAlarm.ID); //$NON-NLS-1$
propertyMap.put(LegacyAlertModel.TASK, TransitionalAlarm.TASK);
propertyMap.put(LegacyAlertModel.DATE, TransitionalAlarm.TIME);
upgradeTable(context, alertsTable, propertyMap, new TransitionalAlarm(),
alarmsDatabase.getDao());
alarmsDatabase.close();
// --- upgrade RTM sync mappings
migrateSyncMappingToMetadata();
// --- clean up database
metadataService.cleanup();
// --- upgrade properties
SharedPreferences prefs = Preferences.getPrefs(context);
Editor editor = prefs.edit();
int random = Preferences.getIntegerFromString(R.string.p_rmd_default_random_hours, -1);
if(random != -1) {
// convert days => hours
editor.putString(context.getString(R.string.p_rmd_default_random_hours),
Integer.toString(random * 24));
}
} catch (Exception e) {
exceptionService.reportError("backup-error", e); //$NON-NLS-1$
if(backupFile != null) {
// try to restore the latest XML
TasksXmlImporter.importTasks(context, backupFile, null);
}
}
}
// --- database upgrade helpers
@ -484,4 +473,41 @@ public class Astrid2To3UpgradeHelper {
}
}
/**
* Move data from alert table into metadata table.
*/
private void migrateAlarmsToMetadata() {
Context context = ContextManager.getContext();
if(!checkIfDatabaseExists(context, AlarmDatabase.NAME))
return;
AlarmDatabase alarmsDatabase = new AlarmDatabase();
GenericDao<TransitionalAlarm> dao = new GenericDao<TransitionalAlarm>(
TransitionalAlarm.class, alarmsDatabase);
TodorooCursor<TransitionalAlarm> cursor = dao.query(Query.select(TransitionalAlarm.PROPERTIES));
try {
if(cursor.getCount() == 0)
return;
Metadata metadata = new Metadata();
metadata.setValue(Metadata.KEY, Alarm.METADATA_KEY);
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
long task = cursor.get(TransitionalAlarm.TASK);
long time = cursor.get(TransitionalAlarm.TIME);
metadata.setValue(Metadata.TASK, task);
metadata.setValue(Alarm.TIME, time);
metadata.setValue(Alarm.TYPE, Alarm.TYPE_SINGLE);
metadataDao.createNew(metadata);
metadata.clearValue(Metadata.ID);
}
} finally {
cursor.close();
alarmsDatabase.close();
}
}
}

@ -2,14 +2,26 @@ package com.todoroo.astrid.service;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.webkit.WebView;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.activity.TaskListActivity;
public final class UpgradeService {
@Autowired
private DialogUtilities dialogUtilities;
public UpgradeService() {
DependencyInjectionService.getInstance().inject(this);
}
/**
* Perform upgrade from one version to the next. Needs to be called
* on the UI thread so it can display a progress bar and then
@ -18,16 +30,45 @@ public final class UpgradeService {
* @param from
* @param to
*/
public void performUpgrade(Context context, int from) {
public void performUpgrade(final Context context, final int from) {
if(from == 135)
AddOnService.recordOem();
if(from < 136) {
new Astrid2To3UpgradeHelper().upgrade2To3(context, this, from);
} else {
// display changelog
showChangeLog(context, from);
}
// pop up a progress dialog
final ProgressDialog dialog;
if(context instanceof Activity)
dialog = dialogUtilities.progressDialog(context,
context.getString(R.string.DLG_upgrading));
else
dialog = null;
new Thread(new Runnable() {
@Override
public void run() {
try {
if(from < 136)
new Astrid2To3UpgradeHelper().upgrade2To3(context, from);
if(from < 146)
new Astrid2To3UpgradeHelper().upgrade3To3_1(context, from);
} finally {
if(context instanceof Activity) {
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
if(dialog != null)
dialog.dismiss();
// display changelog
showChangeLog(context, from);
if(context instanceof TaskListActivity)
((TaskListActivity)context).loadTaskListContent(true);
}
});
}
}
}
}).start();
}
/**
@ -46,15 +87,25 @@ public final class UpgradeService {
StringBuilder changeLog = new StringBuilder();
if(from <= 135)
newVersionString(changeLog, "3.0.6 (8/4/10)", new String[] {
newVersionString(changeLog, "3.1.0 (8/9/10)", new String[] {
"Astrid is brand new inside and out! In addition to a new " +
"look and feel, a new add-on system allows Astrid to become " +
"more powerful, while other improvements have made it faster " +
"and easier to use. Hope you like it!",
"look and feel, a new add-on system allows Astrid to become " +
"more powerful, while other improvements have made it faster " +
"and easier to use. Hope you like it!",
"This update contains for free all of the " +
"powerpack's features for evaluation purposes",
"powerpack's features for evaluation purposes",
"If you liked the old version, you can also go back by " +
"<a href='http://bit.ly/oldastrid'>clicking here</a>",
"<a href='http://bit.ly/oldastrid'>clicking here</a>",
});
if(from > 135 && from <= 145)
newVersionString(changeLog, "3.1.0 (8/9/10)", new String[] {
"Linkify phone numbers, e-mails, and web pages",
"Added day-of-week to all repeating intervals",
"Moved task priorities to left side",
"Added ability to create fixed alerts for a task",
"Restored tag hiding when tag begins with underscore ('_')",
"FROYO: disabled moving app to SD card, it would break alarms and widget",
"Also gone: a couple force closes, bugs with repeating tasks",
});
if(from > 135 && from <= 144)
newVersionString(changeLog, "3.0.6 (8/4/10)", new String[] {
@ -63,11 +114,6 @@ public final class UpgradeService {
"Fixed widget not updating when tasks are edited",
"Added a setting for displaying task notes in the list",
});
if(from > 135 && from <= 145)
newVersionString(changeLog, "3.0.7 (8/5/10)", new String[] {
"Linkify phone numbers, e-mails, and web pages",
"Made the 'Astrid Notifications' setting in the menu work",
});
if(changeLog.length() == 0)
return;

@ -4,8 +4,8 @@ import java.io.File;
import com.todoroo.andlib.service.TestDependencyInjector;
import com.todoroo.andlib.test.TodorooTestCase;
import com.todoroo.astrid.alarms.AlarmDatabase;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.legacy.AlarmDatabase;
import com.todoroo.astrid.service.AstridDependencyInjector;
/**

@ -10,11 +10,12 @@ import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.TestDependencyInjector;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.alarms.TransitionalAlarm;
import com.todoroo.astrid.alarms.AlarmDatabase;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.legacy.AlarmDatabase;
import com.todoroo.astrid.legacy.TransitionalAlarm;
import com.todoroo.astrid.legacy.data.alerts.AlertController;
import com.todoroo.astrid.legacy.data.enums.Importance;
import com.todoroo.astrid.legacy.data.enums.RepeatInterval;
@ -267,7 +268,8 @@ public class Astrid2To3UpgradeTests extends DatabaseTestCase {
// verify that data exists in our new table
database.openForReading();
TagService tagService = TagService.getInstance();
Tag[] tags = tagService.getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA);
Tag[] tags = tagService.getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA,
TaskCriteria.isActive());
assertEquals(2, tags.length);
assertEquals("salty", tags[0].tag);
assertEquals("tasty", tags[1].tag);
@ -321,7 +323,8 @@ public class Astrid2To3UpgradeTests extends DatabaseTestCase {
// verify that data exists in our new table
database.openForReading();
TagService tagService = TagService.getInstance();
Tag[] tags = tagService.getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA);
Tag[] tags = tagService.getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA,
TaskCriteria.isActive());
assertEquals(1, tags.length);
assertEquals("attached", tags[0].tag);

Loading…
Cancel
Save