abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/KeyboardData.java
blob: b52a172c1952cc597cf9c94e06ab7342f774041f (plain)
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
package juloo.keyboard2;

import android.content.res.XmlResourceParser;
import java.util.ArrayList;

class KeyboardData
{
	private ArrayList<Row>	_rows;

	public KeyboardData(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(new Row(parser));
					else
						throw new Exception("Unknow keyboard tag: " + tag);
				}
			}
			_rows = rows;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public ArrayList<Row>	getRows()
	{
		return (_rows);
	}

  // Remove every keys that has the given flags.
  public void removeKeysByFlag(int flags)
  {
    for (Row r : _rows)
    {
      for (Key k : r)
      {
        k.key0 = _removeKeyValueFlag(k.key0, flags);
        k.key1 = _removeKeyValueFlag(k.key1, flags);
        k.key2 = _removeKeyValueFlag(k.key2, flags);
        k.key3 = _removeKeyValueFlag(k.key3, flags);
        k.key4 = _removeKeyValueFlag(k.key4, flags);
      }
    }
  }

  private KeyValue _removeKeyValueFlag(KeyValue v, int flags)
  {
    return (v != null && (v.getFlags() & flags) != 0) ? null : v;
  }

	public class Row extends ArrayList<Key>
	{
		private float		_keysWidth;

		public Row(XmlResourceParser parser) throws Exception
		{
			super();

			int status;
			_keysWidth = 0;
			while ((status = parser.next()) != XmlResourceParser.END_TAG)
			{
				if (status == XmlResourceParser.START_TAG)
				{
					String tag = parser.getName();
					if (tag.equals("key"))
					{
						Key k = new Key(parser);
						_keysWidth += k.width;
						add(k);
					}
					else
						throw new Exception("Unknow row tag: " + tag);
				}
			}
		}

		public float		getWidth(float keyWidth)
		{
			return (keyWidth * _keysWidth);
		}
	}

	public class Key
	{
		/*
		** 1   2
		**   0
		** 3   4
		*/
		public KeyValue		key0;
		public KeyValue		key1;
		public KeyValue		key2;
		public KeyValue		key3;
		public KeyValue		key4;

		public float		width;

		public Key(XmlResourceParser parser) throws Exception
		{
			key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
			key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
			key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
			key3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
			key4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
			try
			{
				width = parser.getAttributeFloatValue(null, "width", 1f);
			}
			catch (Exception e)
			{
				width = 1f;
			}
			while (parser.next() != XmlResourceParser.END_TAG)
				continue ;
		}
	}
}