2015-07-31 20:48:19 +02:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
|
|
|
import android.content.res.XmlResourceParser;
|
|
|
|
import java.util.ArrayList;
|
2021-04-15 23:23:31 +02:00
|
|
|
import java.util.List;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
|
|
|
class KeyboardData
|
|
|
|
{
|
2021-04-15 23:23:31 +02:00
|
|
|
private final List<Row> _rows;
|
2021-04-17 23:56:51 +02:00
|
|
|
private final float _keysWidth;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2021-04-17 23:56:51 +02:00
|
|
|
public KeyboardData(List<Row> rows)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2021-04-17 23:56:51 +02:00
|
|
|
float kpr = 0.f;
|
|
|
|
for (Row r : rows) kpr = Math.max(kpr, r.keysWidth());
|
2021-04-15 23:23:31 +02:00
|
|
|
_rows = rows;
|
2021-04-17 23:56:51 +02:00
|
|
|
_keysWidth = kpr;
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static KeyboardData parse(XmlResourceParser parser)
|
2015-07-31 20:48:19 +02:00
|
|
|
{
|
|
|
|
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"))
|
2021-04-15 23:23:31 +02:00
|
|
|
rows.add(Row.parse(parser));
|
2015-07-31 20:48:19 +02:00
|
|
|
else
|
|
|
|
throw new Exception("Unknow keyboard tag: " + tag);
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
return new KeyboardData(rows);
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
return new KeyboardData(rows);
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|
|
|
|
|
2021-04-17 23:56:51 +02:00
|
|
|
public List<Row> getRows() { return _rows; }
|
|
|
|
|
|
|
|
public float getKeysWidth() { return _keysWidth; }
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2021-04-15 23:23:31 +02:00
|
|
|
public KeyboardData removeKeys(MapKeys f)
|
2021-04-13 02:00:08 +02:00
|
|
|
{
|
2021-04-15 23:23:31 +02:00
|
|
|
ArrayList<Row> rows = new ArrayList<Row>();
|
2021-04-13 02:00:08 +02:00
|
|
|
for (Row r : _rows)
|
2021-04-15 23:23:31 +02:00
|
|
|
rows.add(r.removeKeys(f));
|
|
|
|
return new KeyboardData(rows);
|
2021-04-13 02:00:08 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 23:23:31 +02:00
|
|
|
public static class Row
|
2015-07-31 20:48:19 +02:00
|
|
|
{
|
2021-04-15 23:23:31 +02:00
|
|
|
private final List<Key> _keys;
|
2021-04-17 23:56:51 +02:00
|
|
|
/* Total width of very keys. Unit is abstract. */
|
2021-04-15 23:23:31 +02:00
|
|
|
private final float _keysWidth;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2021-04-15 23:23:31 +02:00
|
|
|
public Row(List<Key> keys)
|
|
|
|
{
|
|
|
|
float kw = 0.f;
|
2021-04-17 23:56:51 +02:00
|
|
|
for (Key k : keys) kw += k.width + k.shift;
|
2021-04-15 23:23:31 +02:00
|
|
|
_keys = keys;
|
|
|
|
_keysWidth = kw;
|
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2021-04-15 23:23:31 +02:00
|
|
|
public static Row parse(XmlResourceParser parser) throws Exception
|
|
|
|
{
|
|
|
|
ArrayList<Key> keys = new ArrayList<Key>();
|
2015-07-31 20:48:19 +02:00
|
|
|
int status;
|
|
|
|
while ((status = parser.next()) != XmlResourceParser.END_TAG)
|
|
|
|
{
|
|
|
|
if (status == XmlResourceParser.START_TAG)
|
|
|
|
{
|
|
|
|
String tag = parser.getName();
|
|
|
|
if (tag.equals("key"))
|
2021-04-15 23:23:31 +02:00
|
|
|
keys.add(Key.parse(parser));
|
2015-07-31 20:48:19 +02:00
|
|
|
else
|
|
|
|
throw new Exception("Unknow row tag: " + tag);
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
return new Row(keys);
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 23:23:31 +02:00
|
|
|
public List<Key> getKeys() { return _keys; }
|
|
|
|
|
2021-04-17 23:56:51 +02:00
|
|
|
public float keysWidth() { return _keysWidth; }
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2021-04-15 23:23:31 +02:00
|
|
|
public Row removeKeys(MapKeys f)
|
|
|
|
{
|
|
|
|
ArrayList<Key> keys = new ArrayList<Key>();
|
|
|
|
for (Key k : _keys)
|
|
|
|
keys.add(k.removeKeys(f));
|
|
|
|
return new Row(keys);
|
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-04-17 23:56:51 +02:00
|
|
|
/* Key width in relative unit. */
|
2021-04-15 23:23:31 +02:00
|
|
|
public final float width;
|
2021-04-17 23:56:51 +02:00
|
|
|
/* Extra empty space on the left of the key. */
|
|
|
|
public final float shift;
|
2021-04-15 23:23:31 +02:00
|
|
|
|
2021-04-17 23:56:51 +02:00
|
|
|
public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
|
|
|
key0 = k0;
|
|
|
|
key1 = k1;
|
|
|
|
key2 = k2;
|
|
|
|
key3 = k3;
|
|
|
|
key4 = k4;
|
|
|
|
width = w;
|
2021-04-17 23:56:51 +02:00
|
|
|
shift = s;
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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"));
|
2021-04-17 23:56:51 +02:00
|
|
|
float width = parser.getAttributeFloatValue(null, "width", 1f);
|
|
|
|
float shift = parser.getAttributeFloatValue(null, "shift", 0.f);
|
2021-04-15 23:23:31 +02:00
|
|
|
while (parser.next() != XmlResourceParser.END_TAG)
|
|
|
|
continue ;
|
2021-04-17 23:56:51 +02:00
|
|
|
return new Key(k0, k1, k2, k3, k4, width, shift);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Key removeKeys(MapKeys f)
|
|
|
|
{
|
2021-04-17 23:56:51 +02:00
|
|
|
return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width, shift);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.getFlags() & _flags) != 0) ? null : k;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|