1 |
|
|
2 |
|
package pl.magot.vetch.widgets; |
3 |
|
|
4 |
|
|
5 |
|
import java.util.ArrayList; |
6 |
|
import android.content.Context; |
7 |
|
import android.view.*; |
8 |
|
import android.widget.LinearLayout.LayoutParams; |
9 |
|
import android.graphics.*; |
10 |
|
import android.graphics.drawable.*; |
11 |
|
import pl.magot.vetch.ancal.*; |
12 |
|
|
13 |
|
|
|
|
| 96,4% |
Uncovered Elements: 7 (192) |
Complexity: 44 |
Complexity Density: 0,34 |
|
14 |
|
public class KeyboardWidgetView extends View |
15 |
|
{ |
16 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
17 |
|
public interface OnKeyClick |
18 |
|
{ |
19 |
|
public void OnKeyClicked(KeyItem key, boolean bCapital); |
20 |
|
} |
21 |
|
|
22 |
|
|
23 |
|
protected OnKeyClick mEventOnKeyClick = null; |
24 |
|
|
25 |
|
|
26 |
|
private Drawable bkgKeyboard = null; |
27 |
|
|
28 |
|
|
29 |
|
private static final int iKeyboardWidth = 284; |
30 |
|
private static final int iKeyboardHeight = 214; |
31 |
|
|
32 |
|
|
33 |
|
public final static int iMarginTB = 3; |
34 |
|
public final static int iKeyHeight = 48; |
35 |
|
|
36 |
|
|
37 |
|
private final static Typeface tfMono = Typeface.create(Typeface.MONOSPACE, Typeface.NORMAL); |
38 |
|
private final static float fTextSize = 20; |
39 |
|
private final static float fTextSizeSmall = 12; |
40 |
|
|
41 |
|
|
42 |
|
private int iDialogWidth = 0; |
43 |
|
private int iKeyWidth = 0; |
44 |
|
private int iKeyModeWidth = 0; |
45 |
|
private int iKeySpecialWidth = 0; |
46 |
|
private int iDefaultSymbolWidth = 0; |
47 |
|
private int iDefaultSymbolHeight = 0; |
48 |
|
private int iDefaultSmallSymbolHeight = 0; |
49 |
|
|
50 |
|
|
51 |
|
private KeyItem activeKey = null; |
52 |
|
private KeyItemLayout keyLayout = new KeyItemLayout(); |
53 |
|
public HintEdit editText = null; |
54 |
|
|
55 |
|
|
56 |
|
private Paint pt = new Paint(); |
57 |
|
|
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0,06 |
|
59 |
5
|
public KeyboardWidgetView(Context context, int iDialogWidth)... |
60 |
|
{ |
61 |
5
|
super(context); |
62 |
5
|
bkgKeyboard = getResources().getDrawable(R.drawable.keyboard); |
63 |
|
|
64 |
|
|
65 |
5
|
this.iDialogWidth = iDialogWidth; |
66 |
5
|
this.iKeyWidth = (iDialogWidth / 10); |
67 |
5
|
this.iKeyModeWidth = (int)(iKeyWidth * 1.4F); |
68 |
5
|
this.iKeySpecialWidth = (iKeyWidth * 2); |
69 |
|
|
70 |
|
|
71 |
5
|
pt.setUnderlineText(false); |
72 |
5
|
pt.setFakeBoldText(true); |
73 |
5
|
pt.setTypeface(tfMono); |
74 |
|
|
75 |
5
|
pt.setTextSize(fTextSize); |
76 |
5
|
iDefaultSymbolWidth = (int)pt.measureText("W"); |
77 |
5
|
iDefaultSymbolHeight = (int)(-pt.ascent() + pt.descent()); |
78 |
|
|
79 |
5
|
pt.setTextSize(fTextSizeSmall); |
80 |
5
|
iDefaultSmallSymbolHeight = (int)(-pt.ascent() + pt.descent()); |
81 |
|
|
82 |
5
|
setLayoutParams(new LayoutParams(getKeyboardWidth(), getKeyboardHeight())); |
83 |
|
|
84 |
5
|
initializeLayouts(pt); |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
87 |
5
|
public int getKeyboardWidth()... |
88 |
|
{ |
89 |
5
|
return (10 * iKeyWidth); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
10
|
public int getKeyboardHeight()... |
93 |
|
{ |
94 |
10
|
return 4 * (iMarginTB + iKeyHeight + iMarginTB); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
5
|
public int getTotalHeight()... |
98 |
|
{ |
99 |
5
|
return getKeyboardHeight(); |
100 |
|
} |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
5
|
public void setKeyClickEvent(OnKeyClick keyClickEvent) ... |
103 |
|
{ |
104 |
5
|
this.mEventOnKeyClick = keyClickEvent; |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
107 |
25
|
public void initializeLayoutSpecKey(Paint pt, int iSpecKeyIndex, int iLeft, int iTop, int iKeyWidth)... |
108 |
|
{ |
109 |
25
|
KeyItem item = keyLayout.specialKeys[iSpecKeyIndex]; |
110 |
25
|
int iSymbolWidth = (int)pt.measureText(item.sChar); |
111 |
25
|
item.setRectangle(iLeft, iTop, iKeyWidth, iKeyHeight); |
112 |
25
|
item.setSymbolSize(iSymbolWidth, iDefaultSmallSymbolHeight); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0,08 |
|
115 |
5
|
private void initializeLayoutSpecKeys(Paint pt, int iTop)... |
116 |
|
{ |
117 |
5
|
final int iSpace = ((iDialogWidth - (10 * iKeyWidth)) >> 1); |
118 |
|
|
119 |
|
|
120 |
5
|
pt.setUnderlineText(false); |
121 |
5
|
pt.setFakeBoldText(true); |
122 |
5
|
pt.setTypeface(tfMono); |
123 |
5
|
pt.setTextSize(fTextSizeSmall); |
124 |
|
|
125 |
|
|
126 |
5
|
initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeyMode, iSpace, iTop, iKeyModeWidth); |
127 |
5
|
initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeyDel, iDialogWidth - iSpace - iKeyModeWidth, iTop, iKeyModeWidth); |
128 |
|
|
129 |
|
|
130 |
5
|
iTop += iKeyHeight + iMarginTB + iMarginTB; |
131 |
|
|
132 |
5
|
initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeyCycle, iSpace, iTop, iKeySpecialWidth); |
133 |
5
|
initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeyReturn, iDialogWidth - iSpace - iKeySpecialWidth, iTop, iKeySpecialWidth); |
134 |
|
|
135 |
|
|
136 |
5
|
int iLeftX = iSpace + iKeySpecialWidth + 5; |
137 |
5
|
int iRightX = iDialogWidth - iSpace - iKeySpecialWidth - 5; |
138 |
5
|
initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeySpace, iLeftX, iTop, iRightX - iLeftX); |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
141 |
30
|
public void initializeLayoutRow(KeyItem[] map, int iTop)... |
142 |
|
{ |
143 |
30
|
int iSpace = ((iDialogWidth - (map.length * iKeyWidth)) >> 1); |
144 |
290
|
for (int index = 0; index < map.length; index++) |
145 |
|
{ |
146 |
260
|
KeyItem mapItem = map[index]; |
147 |
260
|
mapItem.setRectangle(iSpace, iTop, iKeyWidth, iKeyHeight); |
148 |
260
|
mapItem.setSymbolSize(iDefaultSymbolWidth, iDefaultSymbolHeight); |
149 |
260
|
iSpace += iKeyWidth; |
150 |
|
} |
151 |
|
} |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0,1 |
|
153 |
5
|
public void initializeLayouts(Paint pt)... |
154 |
|
{ |
155 |
|
|
156 |
5
|
int iTop = iMarginTB; |
157 |
|
|
158 |
|
|
159 |
5
|
initializeLayoutRow(keyLayout.alphaRow1, iTop); |
160 |
5
|
initializeLayoutRow(keyLayout.digitRow1, iTop); |
161 |
|
|
162 |
|
|
163 |
5
|
iTop += iKeyHeight + iMarginTB + iMarginTB; |
164 |
5
|
initializeLayoutRow(keyLayout.alphaRow2, iTop); |
165 |
5
|
initializeLayoutRow(keyLayout.digitRow2, iTop); |
166 |
|
|
167 |
|
|
168 |
5
|
iTop += iKeyHeight + iMarginTB + iMarginTB; |
169 |
5
|
initializeLayoutRow(keyLayout.alphaRow3, iTop); |
170 |
5
|
initializeLayoutRow(keyLayout.digitRow3, iTop); |
171 |
|
|
172 |
|
|
173 |
5
|
initializeLayoutSpecKeys(pt, iTop); |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 7 |
Complexity Density: 0,5 |
|
176 |
2976
|
private void drawKey(Canvas canvas, Paint pt, KeyItem key)... |
177 |
|
{ |
178 |
2976
|
final boolean bDefaultKey = key.isDefaultKey(); |
179 |
2976
|
final boolean bSpaceKey = key.isSpaceKey(); |
180 |
|
|
181 |
|
|
182 |
2976
|
if (key.bIsActive) |
183 |
|
{ |
184 |
|
|
185 |
41
|
pt.setColor(bDefaultKey?0xffffbb77:0xff77bbff); |
186 |
41
|
canvas.drawRoundRect(key.rectFocus, 3, 3, pt); |
187 |
|
|
188 |
41
|
pt.setColor(bDefaultKey?0xffffffff:0xff5a78a0); |
189 |
41
|
canvas.drawRoundRect(key.rectFrame, 2, 2, pt); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
2976
|
pt.setColor(bDefaultKey?0xff000000:0xffddeeff); |
194 |
2976
|
pt.setTextSize(bDefaultKey?fTextSize:fTextSizeSmall); |
195 |
2976
|
if (bSpaceKey) |
196 |
96
|
pt.setTextSize(fTextSizeSmall); |
197 |
|
|
198 |
2976
|
final int iSymbolPosX = (int)key.rectFrame.left + (((int)key.rectFrame.width() >> 1) - (key.iSymbolWidth >> 1)); |
199 |
2976
|
final int iSymbolPosY = (int)key.rectFrame.top + (((int)key.rectFrame.height() >> 1) - (key.iSymbolHeight >> 1)) - (int)pt.ascent(); |
200 |
|
|
201 |
2976
|
canvas.drawText(keyLayout.getKeySymbol(key), iSymbolPosX, iSymbolPosY, pt); |
202 |
|
} |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 2 |
Complexity Density: 0,14 |
|
204 |
96
|
@Override... |
205 |
|
protected void onDraw(Canvas canvas) |
206 |
|
{ |
207 |
96
|
super.onDraw(canvas); |
208 |
|
|
209 |
|
|
210 |
96
|
final int iPosX = -2; |
211 |
96
|
final int iPosY = 1; |
212 |
96
|
bkgKeyboard.setBounds(iPosX, iPosY, iPosX + iKeyboardWidth, iPosY + iKeyboardHeight); |
213 |
96
|
bkgKeyboard.draw(canvas); |
214 |
|
|
215 |
|
|
216 |
96
|
final ArrayList<KeyItem> layout = keyLayout.getLayout(); |
217 |
|
|
218 |
96
|
pt.setAntiAlias(true); |
219 |
96
|
pt.setShader(null); |
220 |
96
|
pt.setUnderlineText(false); |
221 |
96
|
pt.setFakeBoldText(true); |
222 |
96
|
pt.setTypeface(tfMono); |
223 |
|
|
224 |
|
|
225 |
3072
|
for (int i = 0; i < layout.size(); i++) |
226 |
|
{ |
227 |
2976
|
final KeyItem key = layout.get(i); |
228 |
2976
|
drawKey(canvas, pt, key); |
229 |
|
} |
230 |
|
} |
231 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0,5 |
|
232 |
72
|
private KeyItem getActiveKey(MotionEvent event)... |
233 |
|
{ |
234 |
72
|
final ArrayList<KeyItem> layout = keyLayout.getLayout(); |
235 |
|
|
236 |
|
|
237 |
1457
|
for (int i = 0; i < layout.size(); i++) |
238 |
|
{ |
239 |
1446
|
final KeyItem key = layout.get(i); |
240 |
1446
|
if (key.rectFrame.contains((int)event.getX(), (int)event.getY())) |
241 |
61
|
return key; |
242 |
|
} |
243 |
11
|
return null; |
244 |
|
} |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
246 |
144
|
private void setKeyActive(boolean bIsActive)... |
247 |
|
{ |
248 |
144
|
if (activeKey != null) |
249 |
|
{ |
250 |
122
|
activeKey.bIsActive = bIsActive; |
251 |
|
} |
252 |
|
} |
253 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
254 |
67
|
private void showKeyHint()... |
255 |
|
{ |
256 |
67
|
if ((editText != null) && (activeKey != null)) |
257 |
|
{ |
258 |
61
|
final String sKey = keyLayout.getKeySymbol(activeKey); |
259 |
61
|
editText.showSymbolHint(sKey, activeKey.isDefaultKey()); |
260 |
|
} |
261 |
|
} |
262 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 3 |
Complexity Density: 1,5 |
|
263 |
31
|
private void hideKeyHint()... |
264 |
|
{ |
265 |
31
|
if ((editText != null) && (activeKey != null)) |
266 |
26
|
editText.hideSymbolHint(); |
267 |
|
} |
268 |
|
|
|
|
| 85,4% |
Uncovered Elements: 6 (41) |
Complexity: 8 |
Complexity Density: 0,3 |
|
269 |
103
|
@Override... |
270 |
|
public boolean onTouchEvent(MotionEvent event) |
271 |
|
{ |
272 |
103
|
boolean bHandled = false; |
273 |
|
|
274 |
103
|
if (event.getAction() == MotionEvent.ACTION_DOWN) |
275 |
|
{ |
276 |
31
|
activeKey = getActiveKey(event); |
277 |
31
|
setKeyActive(true); |
278 |
31
|
bHandled = true; |
279 |
31
|
showKeyHint(); |
280 |
|
} |
281 |
103
|
if (event.getAction() == MotionEvent.ACTION_CANCEL) |
282 |
|
{ |
283 |
0
|
setKeyActive(false); |
284 |
0
|
bHandled = true; |
285 |
0
|
hideKeyHint(); |
286 |
|
} |
287 |
103
|
if (event.getAction() == MotionEvent.ACTION_UP) |
288 |
|
{ |
289 |
31
|
doButtonClick(); |
290 |
31
|
setKeyActive(false); |
291 |
31
|
bHandled = true; |
292 |
31
|
hideKeyHint(); |
293 |
|
} |
294 |
103
|
if (event.getAction() == MotionEvent.ACTION_MOVE) |
295 |
|
{ |
296 |
41
|
setKeyActive(false); |
297 |
41
|
activeKey = getActiveKey(event); |
298 |
41
|
setKeyActive(true); |
299 |
41
|
bHandled = true; |
300 |
41
|
if (activeKey == null) |
301 |
|
{ |
302 |
5
|
if (editText != null) |
303 |
5
|
editText.hideSymbolHint(); |
304 |
|
} else { |
305 |
36
|
showKeyHint(); |
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
103
|
if (bHandled) |
310 |
103
|
invalidate(); |
311 |
|
|
312 |
103
|
return bHandled; |
313 |
|
} |
314 |
|
|
|
|
| 91,7% |
Uncovered Elements: 1 (12) |
Complexity: 5 |
Complexity Density: 0,83 |
|
315 |
31
|
private void doButtonClick()... |
316 |
|
{ |
317 |
31
|
if ((activeKey != null) && (activeKey.bIsActive)) |
318 |
|
{ |
319 |
|
|
320 |
|
|
321 |
26
|
if (activeKey.isModeKey()) |
322 |
|
{ |
323 |
1
|
keyLayout.cycleLayoutMode(); |
324 |
1
|
return; |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
25
|
if (mEventOnKeyClick != null) |
329 |
|
{ |
330 |
25
|
mEventOnKeyClick.OnKeyClicked(activeKey, keyLayout.isCapitalMode()); |
331 |
|
} |
332 |
|
|
333 |
|
} |
334 |
|
} |
335 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
336 |
25
|
public KeyItemLayout getKeyLayout()... |
337 |
|
{ |
338 |
25
|
return keyLayout; |
339 |
|
} |
340 |
|
|
341 |
|
} |