Index: /trunk/hiptop/pester/net/sabi/pester/Alarm.java
===================================================================
--- /trunk/hiptop/pester/net/sabi/pester/Alarm.java	(revision 275)
+++ /trunk/hiptop/pester/net/sabi/pester/Alarm.java	(revision 276)
@@ -32,8 +32,10 @@
     private int mAbsoluteFireTime; // only valid if periodic
     private danger.app.Alarm mAlarm;
+    private int mUID;
 
     public Alarm() {
 	mState = STATE_INVALID;
 	mAlarm = new danger.app.Alarm(0, Application.getCurrentApp(), this);
+	mUID = 0;
     }
 
@@ -55,4 +57,7 @@
     public RingToneObject getAlert() {
 	return mAlert;
+    }
+    public int getUID() {
+	return mUID;
     }
 
@@ -70,4 +75,7 @@
     public void setAlert(RingToneObject alert) {
 	mAlert = alert.isValid() ? alert : null;
+    }
+    public void setUID(int uid) {
+	mUID = uid;
     }
 
Index: /trunk/hiptop/pester/net/sabi/pester/AlarmListView.java
===================================================================
--- /trunk/hiptop/pester/net/sabi/pester/AlarmListView.java	(revision 275)
+++ /trunk/hiptop/pester/net/sabi/pester/AlarmListView.java	(revision 276)
@@ -22,10 +22,9 @@
     public void onDecoded() {
 	sAlarmListWindow = (AlarmListWindow)getWindow();
+	mToolTipAlarm = new danger.app.Alarm(0, this);
 	setAutoResize(true);
-	setHasBorder(false); // this doesn't work in the rsrc file
 	setList(Alarms.getList());
-	if (alarmsSet() > 0)
-	    setHasBorder(true);
-	mToolTipAlarm = new danger.app.Alarm(0, this);
+	if (alarmsSet() == 0)
+	    setHasBorder(false); // this doesn't work in the rsrc file
 	Application.registerForEvent(this, Event.EVENT_TIME_CHANGED);
 	super.onDecoded();
@@ -65,5 +64,5 @@
 	    Alarms.removeAlarm((Alarm)getFocusedItem());
 	    return true;
-        case Event.EVENT_TIME_CHANGED:
+        case Event.EVENT_TIME_CHANGED: // XXX EVENT_TIME_FORMAT_CHANGED too
 	    if (mToolTipWindow == null || !mToolTipWindow.isVisible())
 		return true;
Index: /trunk/hiptop/pester/net/sabi/pester/Alarms.java
===================================================================
--- /trunk/hiptop/pester/net/sabi/pester/Alarms.java	(revision 275)
+++ /trunk/hiptop/pester/net/sabi/pester/Alarms.java	(revision 276)
@@ -9,4 +9,5 @@
 import danger.internal.Date;
 import danger.util.StdActiveList;
+import danger.util.DEBUG;
 
 public class Alarms extends StdActiveList {
@@ -22,12 +23,21 @@
     private Alarms() {
 	mDataStore = DataStore.createDataStore("alarms", true /* auto sync */);
+	byte[][] alarmsData = mDataStore.getRecords();
+	int i;
+	for (i = 0 ; i < alarmsData.length ; ++i) {
+	    Alarm alarm = new Alarm();
+	    alarm.fromByteArray(alarmsData[i]);
+	    alarm.setUID(mDataStore.getRecordUID(i));
+	    insertItemSorted(alarm, alarm);
+	    alarm.resume();
+	}
     }
 
     public static Alarms getList() {
 	if (sAlarmList == null) {
+	    Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
+	    sSettingsDB = new SettingsDB("settings", true /* auto sync */);
+	    sListener = new Listener();
 	    sAlarmList = new Alarms();
-	    sListener = new Listener();
-	    sSettingsDB = new SettingsDB("settings", true /* auto sync */);
-	    Application.registerForEvent(sListener, Event.EVENT_TIME_CHANGED);
 	}
 	return sAlarmList;
@@ -39,4 +49,39 @@
     public static void removeAlarm(Alarm alarm) {
 	sAlarmList.removeItem(alarm);
+    }
+
+    // XXX handle EVENT_DATASTORE_RESTORED: note that in the event of
+    // a hard reset, items may come back out of order, so we need to
+    // implement an in-place sort, then insert the current alarm list.
+    // Aiee.  Also, what on earth happens when UIDs aren't synchronized?
+    
+    protected void onItemAdded(Object item, int index) {
+	if (sAlarmList == null) // restoring from service
+	    return;
+	Alarm alarm = (Alarm)item;
+	DEBUG.p("adding: " + alarm +
+		" next UID: " + mDataStore.getAutoSyncNextUID());
+	index = mDataStore.addRecord(alarm.toByteArray(), index);
+	alarm.setUID(mDataStore.getRecordUID(index));
+	DEBUG.p("added UID " + alarm.getUID() + " @ " + index);
+    }
+
+    protected void onItemRemoved(Object item, int index) {
+	Alarm alarm = (Alarm)item;
+	// XXX make sure +/- UIDs are OK
+	int uid = alarm.getUID();
+	if (uid == 0)
+	    return;
+	DEBUG.p("removing; " + alarm + " UID " + uid);
+	mDataStore.removeRecordByUID(uid);
+    }
+
+    public void onItemUpdated(Object item, int index) {
+	if (sAlarmList == null) // restoring from service
+	    return;
+	Alarm alarm = (Alarm)item;
+	// XXX check that updating a + UID by a - one is OK
+	mDataStore.setRecordDataByUID(alarm.getUID(), alarm.toByteArray(), true);
+	DEBUG.p("updated: " + alarm + " UID " + alarm.getUID());
     }
 
@@ -67,5 +112,4 @@
 	public boolean receiveEvent(Event e) {
 	    if (e.type == Event.EVENT_TIME_CHANGED) {
-		// XXX do we get this when the time zone changes?
 		Alarms.getList().forEach(this);
 		return true;
@@ -75,3 +119,2 @@
     }
 }
-
