1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
package juloo.keyboard2;
import android.content.res.XmlResourceParser;
import java.util.ArrayList;
import java.util.List;
class KeyboardData
{
public final List<Row> rows;
/* Total width of the keyboard. Unit is abstract. */
public final float keysWidth;
/* Total height of the keyboard. Unit is abstract. */
public final float keysHeight;
public KeyboardData(List<Row> rows_)
{
float kw = 0.f;
float kh = 0.f;
for (Row r : rows_)
{
kw = Math.max(kw, r.keysWidth);
kh += r.height + r.shift;
}
rows = rows_;
keysWidth = kw;
keysHeight = kh;
}
public static KeyboardData parse(XmlResourceParser parser)
{
ArrayList<Row> rows = new ArrayList<Row>();
try
{
int status;
while (parser.next() != XmlResourceParser.START_TAG)
continue ;
if (!parser.getName().equals("keyboard"))
throw new Exception("Unknow tag: " + parser.getName());
while ((status = parser.next()) != XmlResourceParser.END_DOCUMENT)
{
if (status == XmlResourceParser.START_TAG)
{
String tag = parser.getName();
if (tag.equals("row"))
rows.add(Row.parse(parser));
else
throw new Exception("Unknow keyboard tag: " + tag);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return new KeyboardData(rows);
}
public KeyboardData removeKeys(MapKeys f)
{
ArrayList<Row> rows_ = new ArrayList<Row>();
for (Row r : rows)
rows_.add(r.removeKeys(f));
return new KeyboardData(rows_);
}
public static class Row
{
public final List<Key> keys;
/* Height of the row. Unit is abstract. */
public final float height;
/* Extra empty space on the top. */
public final float shift;
/* Total width of very keys. Unit is abstract. */
private final float keysWidth;
public Row(List<Key> keys_, float h, float s)
{
float kw = 0.f;
for (Key k : keys_) kw += k.width + k.shift;
keys = keys_;
height = h;
shift = s;
keysWidth = kw;
}
public static Row parse(XmlResourceParser parser) throws Exception
{
ArrayList<Key> keys = new ArrayList<Key>();
int status;
float h = parser.getAttributeFloatValue(null, "height", 1f);
float shift = parser.getAttributeFloatValue(null, "shift", 0f);
while ((status = parser.next()) != XmlResourceParser.END_TAG)
{
if (status == XmlResourceParser.START_TAG)
{
String tag = parser.getName();
if (tag.equals("key"))
keys.add(Key.parse(parser));
else
throw new Exception("Unknow row tag: " + tag);
}
}
return new Row(keys, h, shift);
}
public Row removeKeys(MapKeys f)
{
ArrayList<Key> keys_ = new ArrayList<Key>();
for (Key k : keys)
keys_.add(k.removeKeys(f));
return new Row(keys_, height, shift);
}
}
public static class Key
{
/*
** 1 2
** 0
** 3 4
*/
public final KeyValue key0;
public final KeyValue key1;
public final KeyValue key2;
public final KeyValue key3;
public final KeyValue key4;
/* Key width in relative unit. */
public final float width;
/* Extra empty space on the left of the key. */
public final float shift;
public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s)
{
key0 = k0;
key1 = k1;
key2 = k2;
key3 = k3;
key4 = k4;
width = w;
shift = s;
}
public static Key parse(XmlResourceParser parser) throws Exception
{
KeyValue k0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
KeyValue k1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
KeyValue k2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
KeyValue k3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
KeyValue k4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
float width = parser.getAttributeFloatValue(null, "width", 1f);
float shift = parser.getAttributeFloatValue(null, "shift", 0.f);
while (parser.next() != XmlResourceParser.END_TAG)
continue ;
return new Key(k0, k1, k2, k3, k4, width, shift);
}
public Key removeKeys(MapKeys f)
{
return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width, shift);
}
}
public static abstract interface MapKeys
{
public abstract KeyValue map(KeyValue k);
}
public static class RemoveKeysByFlags implements MapKeys
{
private final int _flags;
public RemoveKeysByFlags(int flags) { _flags = flags; }
public KeyValue map(KeyValue k)
{
return (k == null || (k.flags & _flags) != 0) ? null : k;
}
}
public static class RemoveKeysByEvent implements MapKeys
{
private final int _eventCode;
public RemoveKeysByEvent(int ev) { _eventCode = ev; }
public KeyValue map(KeyValue k)
{
return (k == null || k.eventCode == _eventCode) ? null : k;
}
}
}
|