2015-07-31 20:48:19 +02:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
import android.content.res.Resources;
|
2015-07-31 20:48:19 +02:00
|
|
|
import android.content.res.XmlResourceParser;
|
|
|
|
import java.util.ArrayList;
|
2022-02-07 00:55:32 +01:00
|
|
|
import java.util.HashMap;
|
2021-04-15 23:23:31 +02:00
|
|
|
import java.util.List;
|
2022-02-07 00:55:32 +01:00
|
|
|
import java.util.Map;
|
2022-03-05 18:15:36 +01:00
|
|
|
import java.util.function.Function;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
|
|
|
class KeyboardData
|
|
|
|
{
|
2021-12-19 19:44:27 +01:00
|
|
|
public final List<Row> rows;
|
2021-04-29 00:59:19 +02:00
|
|
|
/* Total width of the keyboard. Unit is abstract. */
|
|
|
|
public final float keysWidth;
|
|
|
|
/* Total height of the keyboard. Unit is abstract. */
|
|
|
|
public final float keysHeight;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
public KeyboardData replaceKeys(MapKeys f)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
ArrayList<Row> rows_ = new ArrayList<Row>();
|
|
|
|
for (Row r : rows)
|
|
|
|
rows_.add(r.replaceKeys(f));
|
2022-02-13 13:20:22 +01:00
|
|
|
return new KeyboardData(rows_, keysWidth);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
private static Row _bottomRow = null;
|
|
|
|
private static Map<Integer, KeyboardData> _layoutCache = new HashMap<Integer, KeyboardData>();
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
public static KeyboardData load(Resources res, int id)
|
|
|
|
{
|
|
|
|
KeyboardData l = _layoutCache.get(id);
|
|
|
|
if (l == null)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
try
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
if (_bottomRow == null)
|
|
|
|
_bottomRow = parse_bottom_row(res.getXml(R.xml.bottom_row));
|
|
|
|
l = parse_keyboard(res.getXml(id));
|
|
|
|
_layoutCache.put(id, l);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 00:55:32 +01:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static KeyboardData parse_keyboard(XmlResourceParser parser) throws Exception
|
|
|
|
{
|
|
|
|
if (!expect_tag(parser, "keyboard"))
|
|
|
|
throw new Exception("Empty layout file");
|
|
|
|
boolean bottom_row = parser.getAttributeBooleanValue(null, "bottom_row", true);
|
|
|
|
ArrayList<Row> rows = new ArrayList<Row>();
|
|
|
|
while (expect_tag(parser, "row"))
|
|
|
|
rows.add(Row.parse(parser));
|
2022-02-13 13:20:22 +01:00
|
|
|
float kw = compute_max_width(rows);
|
2022-02-07 00:55:32 +01:00
|
|
|
if (bottom_row)
|
2022-02-13 13:20:22 +01:00
|
|
|
rows.add(_bottomRow.updateWidth(kw));
|
|
|
|
return new KeyboardData(rows, kw);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static float compute_max_width(List<Row> rows)
|
|
|
|
{
|
|
|
|
float w = 0.f;
|
|
|
|
for (Row r : rows)
|
|
|
|
w = Math.max(w, r.keysWidth);
|
|
|
|
return w;
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
private static Row parse_bottom_row(XmlResourceParser parser) throws Exception
|
2021-04-13 02:00:08 +02:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
if (!expect_tag(parser, "row"))
|
|
|
|
throw new Exception("Failed to parse bottom row");
|
|
|
|
return Row.parse(parser);
|
|
|
|
}
|
|
|
|
|
2022-02-13 13:20:22 +01:00
|
|
|
protected KeyboardData(List<Row> rows_, float kw)
|
2022-02-07 00:55:32 +01:00
|
|
|
{
|
|
|
|
float kh = 0.f;
|
|
|
|
for (Row r : rows_)
|
|
|
|
kh += r.height + r.shift;
|
|
|
|
rows = rows_;
|
|
|
|
keysWidth = kw;
|
|
|
|
keysHeight = kh;
|
2021-04-13 02:00:08 +02:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
public static class Row
|
|
|
|
{
|
2021-04-29 00:59:19 +02:00
|
|
|
public final List<Key> keys;
|
2022-02-13 13:20:22 +01:00
|
|
|
/* Height of the row, without 'shift'. Unit is abstract. */
|
2021-04-29 00:59:19 +02:00
|
|
|
public final float height;
|
|
|
|
/* Extra empty space on the top. */
|
|
|
|
public final float shift;
|
2022-02-13 13:20:22 +01:00
|
|
|
/* Total width of the row. Unit is abstract. */
|
2021-12-19 19:44:27 +01:00
|
|
|
private final float keysWidth;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
protected Row(List<Key> keys_, float h, float s)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
|
|
|
float kw = 0.f;
|
2021-04-29 00:59:19 +02:00
|
|
|
for (Key k : keys_) kw += k.width + k.shift;
|
|
|
|
keys = keys_;
|
|
|
|
height = h;
|
|
|
|
shift = s;
|
|
|
|
keysWidth = kw;
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
public static Row parse(XmlResourceParser parser) throws Exception
|
|
|
|
{
|
2021-04-15 23:23:31 +02:00
|
|
|
ArrayList<Key> keys = new ArrayList<Key>();
|
2021-12-19 19:44:27 +01:00
|
|
|
int status;
|
2021-04-29 00:59:19 +02:00
|
|
|
float h = parser.getAttributeFloatValue(null, "height", 1f);
|
|
|
|
float shift = parser.getAttributeFloatValue(null, "shift", 0f);
|
2022-02-07 00:55:32 +01:00
|
|
|
while (expect_tag(parser, "key"))
|
|
|
|
keys.add(Key.parse(parser));
|
2021-04-29 00:59:19 +02:00
|
|
|
return new Row(keys, h, shift);
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-01-09 20:26:06 +01:00
|
|
|
public Row replaceKeys(MapKeys f)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2021-04-29 00:59:19 +02:00
|
|
|
ArrayList<Key> keys_ = new ArrayList<Key>();
|
|
|
|
for (Key k : keys)
|
2022-01-09 20:26:06 +01:00
|
|
|
keys_.add(k.replaceKeys(f));
|
2021-04-29 00:59:19 +02:00
|
|
|
return new Row(keys_, height, shift);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
2022-02-13 13:20:22 +01:00
|
|
|
|
|
|
|
/** Change the width of every keys so that the row is 's' units wide. */
|
|
|
|
public Row updateWidth(float newWidth)
|
|
|
|
{
|
|
|
|
float s = newWidth / keysWidth;
|
|
|
|
ArrayList<Key> keys_ = new ArrayList<Key>();
|
|
|
|
for (Key k : keys)
|
|
|
|
keys_.add(k.scaleWidth(s));
|
|
|
|
return new Row(keys_, height, shift);
|
|
|
|
}
|
2021-12-19 19:44:27 +01: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;
|
2022-02-02 21:46:23 +01:00
|
|
|
/* Put keys 1 to 4 on the edges instead of the corners. */
|
|
|
|
public final boolean edgekeys;
|
2021-04-15 23:23:31 +02:00
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
protected Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s, boolean e)
|
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;
|
2022-02-02 21:46:23 +01:00
|
|
|
edgekeys = e;
|
|
|
|
}
|
|
|
|
|
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);
|
2022-02-02 21:46:23 +01:00
|
|
|
boolean edgekeys = parser.getAttributeBooleanValue(null, "edgekeys", false);
|
2021-04-15 23:23:31 +02:00
|
|
|
while (parser.next() != XmlResourceParser.END_TAG)
|
|
|
|
continue ;
|
2022-02-02 21:46:23 +01:00
|
|
|
return new Key(k0, k1, k2, k3, k4, width, shift, edgekeys);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-09 20:26:06 +01:00
|
|
|
public Key replaceKeys(MapKeys f)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2022-03-05 18:15:36 +01:00
|
|
|
return new Key(f.apply(key0), f.apply(key1), f.apply(key2),
|
|
|
|
f.apply(key3), f.apply(key4), width, shift, edgekeys);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
2022-02-13 13:20:22 +01:00
|
|
|
|
|
|
|
/** New key with the width multiplied by 's'. */
|
|
|
|
public Key scaleWidth(float s)
|
|
|
|
{
|
|
|
|
return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys);
|
|
|
|
}
|
2022-03-19 15:39:20 +01:00
|
|
|
|
2022-05-08 16:38:44 +02:00
|
|
|
/*
|
|
|
|
* See Pointers.onTouchMove() for the represented direction.
|
|
|
|
*/
|
|
|
|
public KeyValue getAtDirection(int direction)
|
2022-03-19 15:39:20 +01:00
|
|
|
{
|
2022-05-08 01:18:53 +02:00
|
|
|
if (edgekeys)
|
|
|
|
{
|
2022-05-06 18:38:43 +02:00
|
|
|
// \ 1 /
|
|
|
|
// \ /
|
|
|
|
// 3 0 2
|
|
|
|
// / \
|
|
|
|
// / 4 \
|
2022-05-07 00:08:20 +02:00
|
|
|
switch (direction)
|
2022-05-06 18:38:43 +02:00
|
|
|
{
|
2022-05-08 01:18:53 +02:00
|
|
|
case 2: case 3: return key1;
|
|
|
|
case 4: case 5: return key2;
|
|
|
|
case 6: case 7: return key4;
|
|
|
|
case 8: case 1: return key3;
|
2022-05-06 18:38:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-03-19 15:39:20 +01:00
|
|
|
{
|
2022-05-06 18:38:43 +02:00
|
|
|
// 1 | 2
|
|
|
|
// |
|
|
|
|
// --0--
|
2022-05-08 01:18:53 +02:00
|
|
|
// |
|
2022-05-06 18:38:43 +02:00
|
|
|
// 3 | 4
|
2022-05-07 00:08:20 +02:00
|
|
|
switch (direction)
|
2022-05-06 18:38:43 +02:00
|
|
|
{
|
2022-05-08 01:18:53 +02:00
|
|
|
case 1: case 2: return key1;
|
|
|
|
case 3: case 4: return key2;
|
|
|
|
case 5: case 6: return key4;
|
|
|
|
case 7: case 8: return key3;
|
2022-05-06 18:38:43 +02:00
|
|
|
}
|
2022-03-19 15:39:20 +01:00
|
|
|
}
|
2022-05-08 01:18:53 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 23:36:54 +02:00
|
|
|
// Not using Function<KeyValue, KeyValue> to keep compatibility with Android 6.
|
|
|
|
public static abstract interface MapKeys {
|
|
|
|
public KeyValue apply(KeyValue kv);
|
|
|
|
}
|
2022-02-07 00:55:32 +01:00
|
|
|
|
|
|
|
/** Parsing utils */
|
|
|
|
|
|
|
|
/** Returns [false] on [END_DOCUMENT] or [END_TAG], [true] otherwise. */
|
|
|
|
private static boolean expect_tag(XmlResourceParser parser, String name) throws Exception
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = parser.next();
|
|
|
|
if (status == XmlResourceParser.END_DOCUMENT || status == XmlResourceParser.END_TAG)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (status != XmlResourceParser.START_TAG);
|
|
|
|
if (!parser.getName().equals(name))
|
|
|
|
throw new Exception("Unknow tag: " + parser.getName());
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|