Skip to content

Commit e2aba7f

Browse files
committed
Extend persistent preferences for font size and full screen
1 parent 22aec66 commit e2aba7f

1 file changed

Lines changed: 76 additions & 24 deletions

File tree

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,80 @@
11
package com.netormium.client.util;
2+
23
import javax.microedition.rms.RecordStore;
3-
/** Persistent lightweight preferences for the Netormium MIDlet. */
4+
5+
/** Persistent MIDlet preferences. Version 2 extends the original Data Saver record. */
46
public final class BrowserPreferences {
5-
private static final String STORE="NetormiumPrefs";
6-
private static final int VERSION=1;
7-
public boolean loadDataSaver(boolean fallback) {
8-
RecordStore rs=null;
9-
try {
10-
rs=RecordStore.openRecordStore(STORE,false);
11-
if(rs.getNumRecords()<1)return fallback;
12-
byte[] d=rs.getRecord(1);
13-
if(d==null||d.length<2||(d[0]&255)!=VERSION)return fallback;
14-
return d[1]!=0;
15-
} catch(Exception ignored){return fallback;}
16-
finally{try{if(rs!=null)rs.closeRecordStore();}catch(Exception ignored){}}
17-
}
18-
public void saveDataSaver(boolean enabled) {
19-
RecordStore rs=null;
20-
try {
21-
rs=RecordStore.openRecordStore(STORE,true);
22-
byte[] d=new byte[]{(byte)VERSION,(byte)(enabled?1:0)};
23-
if(rs.getNumRecords()<1)rs.addRecord(d,0,d.length);
24-
else rs.setRecord(1,d,0,d.length);
25-
} catch(Exception ignored){}
26-
finally{try{if(rs!=null)rs.closeRecordStore();}catch(Exception ignored){}}
27-
}
7+
private static final String STORE = "NetormiumPrefs";
8+
private static final int VERSION = 2;
9+
private static final int DEFAULT_FONT_SIZE = 1;
10+
11+
private byte[] read() {
12+
RecordStore rs = null;
13+
try {
14+
rs = RecordStore.openRecordStore(STORE, false);
15+
if (rs.getNumRecords() < 1) return null;
16+
return rs.getRecord(1);
17+
} catch (Exception ignored) { return null; }
18+
finally { try { if (rs != null) rs.closeRecordStore(); } catch (Exception ignored) {} }
19+
}
20+
21+
private void write(byte[] data) {
22+
RecordStore rs = null;
23+
try {
24+
rs = RecordStore.openRecordStore(STORE, true);
25+
if (rs.getNumRecords() < 1) rs.addRecord(data, 0, data.length);
26+
else rs.setRecord(1, data, 0, data.length);
27+
} catch (Exception ignored) {}
28+
finally { try { if (rs != null) rs.closeRecordStore(); } catch (Exception ignored) {} }
29+
}
30+
31+
private byte[] normalized() {
32+
byte[] old = read();
33+
byte saver = 1;
34+
byte font = DEFAULT_FONT_SIZE;
35+
byte full = 0;
36+
if (old != null && old.length >= 2) {
37+
saver = old[1];
38+
if (old.length >= 4 && (old[0] & 255) >= 2) {
39+
font = old[2];
40+
full = old[3];
41+
}
42+
}
43+
if (font < 0 || font > 2) font = DEFAULT_FONT_SIZE;
44+
return new byte[] {(byte) VERSION, saver, font, full};
45+
}
46+
47+
public boolean loadDataSaver(boolean fallback) {
48+
byte[] d = read();
49+
if (d == null || d.length < 2) return fallback;
50+
return d[1] != 0;
51+
}
52+
53+
public void saveDataSaver(boolean enabled) {
54+
byte[] d = normalized();
55+
d[1] = (byte) (enabled ? 1 : 0);
56+
write(d);
57+
}
58+
59+
public int loadFontSize() {
60+
byte[] d = normalized();
61+
return d[2];
62+
}
63+
64+
public void saveFontSize(int size) {
65+
if (size < 0 || size > 2) return;
66+
byte[] d = normalized();
67+
d[2] = (byte) size;
68+
write(d);
69+
}
70+
71+
public boolean loadFullScreen() {
72+
return normalized()[3] != 0;
73+
}
74+
75+
public void saveFullScreen(boolean enabled) {
76+
byte[] d = normalized();
77+
d[3] = (byte) (enabled ? 1 : 0);
78+
write(d);
79+
}
2880
}

0 commit comments

Comments
 (0)