Skip to content

Commit

Permalink
Bugfix time configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Clausen committed Sep 17, 2022
1 parent 15445bd commit bb07176
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
34 changes: 10 additions & 24 deletions src/hotpants/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.ByteBuffer;
import javax.microedition.rms.*;
import java.util.Hashtable;
import util.IntToBytes;

public class Configuration {
private RecordStore recordStore;
Expand All @@ -15,7 +14,7 @@ public class Configuration {
public static final byte TOTP = 1;
public static final byte HOTP = 2;
public static final byte TimeConfig = 3;
private int timeOffset = 0;
private byte timeOffset = 0;
private int offsetRecordId = -1;

public Configuration() {
Expand All @@ -25,9 +24,7 @@ public Configuration() {
recordStore.setMode(RecordStore.AUTHMODE_PRIVATE, false);
RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
int recId = re.nextRecordId();
byte[] record = recordStore.getRecord(recId);
record[record.length-1] = (byte)recId;
byte[] record = re.nextRecord();
int type = getEntryTypeFromRecordBytes(record);
if (Configuration.TOTP == type) {
TotpEntry e = TotpEntry.fromBytes(record);
Expand All @@ -46,23 +43,13 @@ public Configuration() {
}

private void setTimeOffset(byte[] record) {
ByteArrayOutputStream b_id_baos = new ByteArrayOutputStream();
DataOutputStream b_id = new DataOutputStream(b_id_baos);
ByteArrayOutputStream b_sec_baos = new ByteArrayOutputStream();
DataOutputStream b_sec = new DataOutputStream(b_sec_baos);
int section = 0;
for (int i = 0; i < record.length; i++) {
if (Configuration.DELIM == record[i]) {
section++;
}
switch (section) {
case 3:
timeOffset = record[i] - 120;
break;
case 4:
offsetRecordId = record[i];
}
if (record.length != 9) {
System.err.println("Corrupt time offset configuration entry.");
return;
}
timeOffset = record[6];
offsetRecordId = record[8];
System.out.println("Time offset from config: " + timeOffset);
}

private int getEntryTypeFromRecordBytes(byte[] record) {
Expand Down Expand Up @@ -114,7 +101,6 @@ public void updateOffsetSeconds(byte seconds) {
try {
timeOffset = seconds;
byte[] record = new byte[9];

recordStore = RecordStore.openRecordStore("Hotpants",true);
recordStore.setMode(RecordStore.AUTHMODE_PRIVATE, true);
boolean isNew = offsetRecordId == -1;
Expand All @@ -127,7 +113,7 @@ public void updateOffsetSeconds(byte seconds) {
record[3] = DELIM;
record[4] = TimeConfig;
record[5] = DELIM;
record[6] = (byte)(seconds + 120);
record[6] = seconds;
record[7] = DELIM;
record[8] = (byte)offsetRecordId;

Expand All @@ -141,7 +127,7 @@ public void updateOffsetSeconds(byte seconds) {
}
}

public int getOffsetSeconds() {
public byte getOffsetSeconds() {
return timeOffset;
}

Expand Down
6 changes: 3 additions & 3 deletions src/hotpants/TimeConfigForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TimeConfigForm extends Form implements CommandListener, ItemStateL
private final Midlet midlet;
private final Command cancel, save;
private final Gauge secondsInput;
private int offset;
private byte offset;
private final Calendar displayedTime;
private final StringItem helpText;

Expand Down Expand Up @@ -64,14 +64,14 @@ public void updateTimeLabel() {

public void commandAction(Command c, Displayable d) {
if (c == save) {
midlet.getConfiguration().updateOffsetSeconds((byte)offset);
midlet.getConfiguration().updateOffsetSeconds(offset);
}
midlet.showMainForm();
}

public void itemStateChanged(Item item) {
if (item == secondsInput) {
offset = secondsInput.getValue() - 120;
offset = (byte)(secondsInput.getValue() - 120);
updateTimeLabel();
}
}
Expand Down

0 comments on commit bb07176

Please sign in to comment.