| |
"Building End-to-End Palm Applications"
Vol. 7, Issue 3, p. 106
Listing 1
public MemoList (SecurePad parent) {
super("SecurePad", List.IMPLICIT);
m_parent = parent;
// The command listener handles all input from the form
setCommandListener(this) ;
// Commands are represented as both buttons and menu items.
addCommand(new Command("New", Command.OK, 1));
addCommand(new Command("Exit", Command.EXIT, 2));
addCommand(new Command("Help", Command.HELP, HELP_PAGE));
addCommand(new Command("Erase", Command.SCREEN, DELETE_RECORD));
try {
// The following code opens a RecordStore and iterates over the records twice
// One flaw in the design is that nextRecordId() increments the record pointer
// forcing us to iterate once for record IDs and once for the actual data
secureRMS = RecordStore.openRecordStore("SecurePad", true);
if (secureRMS.getNumRecords() > 0) {
RecordEnumeration re = secureRMS.enumerateRecords(null, null, true);
for (int i=0; i < secureRMS.getNumRecords(); i++) {
int recID = re.nextRecordId();
recordIDs.addElement(new Integer(recID));
if (recID > highRecId)
highRecId = recID;
}
re.reset();
for (int i=0; i < secureRMS.getNumRecords(); i++) {
String title = new String (re.nextRecord());
append(Utils.makeTitle(title), null);
}
re.destroy();
}
} catch(Exception e) {
Alert a = new Alert("SecurePad", e.toString(), null, AlertType.INFO);
a.setTimeout(3000);
Display.getDisplay(m_parent).setCurrent(a);
}
}
Listing 2
// The following routine writes data from the desktop to the handheld
public void writeToHH() {
DataInputStream din;
SecurePadRecord rec;
DbList list[];
int db, count, i;
try {
din = new DataInputStream(new FileInputStream(props.localName));
db = SyncManager.openDB("SecurePad-spad", (short)0,
(byte)(SyncManager.OPEN_READ | SyncManager.OPEN_WRITE
| SyncManager.OPEN_EXCLUSIVE));
rec = new SecurePadRecord();
SyncManager.purgeAllRecs(db);
count = din.readInt();
Log.out("Count: "+count);
byte [] record = new byte[1024];
for (i=0; i<count; i++) {
int recLength = din.readInt();
try {
Log.out("Record Len:"+recLength);
for (int j=0; j<recLength; j++)
din.read(record, j, 1);
} catch (EOFException e) {
e.printStackTrace();
}
rec.setId(i);
rec.setIndex(i+1);
rec.setMemo(new String(record, 0, recLength));
SyncManager.writeRec(db, rec);
}
din.close();
SyncManager.closeDB(db);
} catch (Exception e) {
e.printStackTrace();
}
}
// The following routine writes data from the Handheld to the desktop
public void readFromHH() {
DataOutputStream out;
SecurePadRecord rec;
DbList list[];
int db, count, i;
try {
// Create our output file
out = new DataOutputStream(new FileOutputStream(props.localName));
// Open our handheld DB
Log.out(props.toString());
db = SyncManager.openDB("SecurePad-spad", (short)0,
(byte)(SyncManager.OPEN_READ |SyncManager.OPEN_WRITE|
SyncManager.OPEN_EXCLUSIVE));
// Find out how many memo records there are in the MemoDB
count = SyncManager.getDBRecordCount(db);
// Since we are just printing, we can re-use a single MemoRecord
rec = new SecurePadRecord();
// Loop over all records
out.writeInt(count);
for (i = 0; i < count; i++) {
// Recond a record
rec.setIndex(i);
SyncManager.readRecordByIndex(db, rec);
// Print it out to file
String recData = rec.getMemo();
out.writeInt(recData.length());
out.write(recData.getBytes());
}
// Close DB
SyncManager.closeDB(db);
// Close file
out.flush();
out.close();
} catch (Throwable t) {
// If there was an error, dump the stack trace
// and tell the log we failed
t.printStackTrace();
Log.abortSync();
}
}
|
|