Clover Coverage Report - AnCal Coverage Report
Coverage timestamp: gio dic 18 2014 12:18:42 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
128   341   44   7,11
46   248   0,34   9
18     2,44  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  KeyboardWidgetView       Line # 14 128 44 96,4% 0.9635417
  KeyboardWidgetView.OnKeyClick       Line # 17 0 0 - -1.0
 
No Tests
 
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   
 
14    public class KeyboardWidgetView extends View
15    {
16    //types
 
17    public interface OnKeyClick
18    {
19    public void OnKeyClicked(KeyItem key, boolean bCapital);
20    }
21   
22    //fields
23    protected OnKeyClick mEventOnKeyClick = null;
24   
25    //fields
26    private Drawable bkgKeyboard = null;
27   
28    //fields
29    private static final int iKeyboardWidth = 284;
30    private static final int iKeyboardHeight = 214;
31   
32    //fields
33    public final static int iMarginTB = 3;
34    public final static int iKeyHeight = 48;
35   
36    //fields
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    //fields
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    //fields
51    private KeyItem activeKey = null;
52    private KeyItemLayout keyLayout = new KeyItemLayout();
53    public HintEdit editText = null;
54   
55    //fields
56    private Paint pt = new Paint();
57   
58    //methods
 
59  5 toggle public KeyboardWidgetView(Context context, int iDialogWidth)
60    {
61  5 super(context);
62  5 bkgKeyboard = getResources().getDrawable(R.drawable.keyboard);
63   
64    //calculate sizes
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    //get default symbol size
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   
 
87  5 toggle public int getKeyboardWidth()
88    {
89  5 return (10 * iKeyWidth);
90    }
91   
 
92  10 toggle public int getKeyboardHeight()
93    {
94  10 return 4 * (iMarginTB + iKeyHeight + iMarginTB);
95    }
96   
 
97  5 toggle public int getTotalHeight()
98    {
99  5 return getKeyboardHeight();
100    }
101   
 
102  5 toggle public void setKeyClickEvent(OnKeyClick keyClickEvent)
103    {
104  5 this.mEventOnKeyClick = keyClickEvent;
105    }
106   
 
107  25 toggle 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   
 
115  5 toggle private void initializeLayoutSpecKeys(Paint pt, int iTop)
116    {
117  5 final int iSpace = ((iDialogWidth - (10 * iKeyWidth)) >> 1);
118   
119    //get symbol size
120  5 pt.setUnderlineText(false);
121  5 pt.setFakeBoldText(true);
122  5 pt.setTypeface(tfMono);
123  5 pt.setTextSize(fTextSizeSmall);
124   
125    //third row
126  5 initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeyMode, iSpace, iTop, iKeyModeWidth);
127  5 initializeLayoutSpecKey(pt, KeyItemLayout.iSpecKeyDel, iDialogWidth - iSpace - iKeyModeWidth, iTop, iKeyModeWidth);
128   
129    //fourth row
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    //space key
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   
 
141  30 toggle 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   
 
153  5 toggle public void initializeLayouts(Paint pt)
154    {
155    //misc sizes
156  5 int iTop = iMarginTB;
157   
158    //first row
159  5 initializeLayoutRow(keyLayout.alphaRow1, iTop);
160  5 initializeLayoutRow(keyLayout.digitRow1, iTop);
161   
162    //second row
163  5 iTop += iKeyHeight + iMarginTB + iMarginTB;
164  5 initializeLayoutRow(keyLayout.alphaRow2, iTop);
165  5 initializeLayoutRow(keyLayout.digitRow2, iTop);
166   
167    //third row
168  5 iTop += iKeyHeight + iMarginTB + iMarginTB;
169  5 initializeLayoutRow(keyLayout.alphaRow3, iTop);
170  5 initializeLayoutRow(keyLayout.digitRow3, iTop);
171   
172    //spec keys
173  5 initializeLayoutSpecKeys(pt, iTop);
174    }
175   
 
176  2976 toggle 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    //draw active key over keyboard background
182  2976 if (key.bIsActive)
183    {
184    //draw focus frame
185  41 pt.setColor(bDefaultKey?0xffffbb77:0xff77bbff);
186  41 canvas.drawRoundRect(key.rectFocus, 3, 3, pt);
187    //draw background
188  41 pt.setColor(bDefaultKey?0xffffffff:0xff5a78a0);
189  41 canvas.drawRoundRect(key.rectFrame, 2, 2, pt);
190    }
191   
192    //draw symbol
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   
 
204  96 toggle @Override
205    protected void onDraw(Canvas canvas)
206    {
207  96 super.onDraw(canvas);
208   
209    //draw keyboard background
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    //draw keyboard layout
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    //iterate keys
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   
 
232  72 toggle private KeyItem getActiveKey(MotionEvent event)
233    {
234  72 final ArrayList<KeyItem> layout = keyLayout.getLayout();
235   
236    //iterate keys
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   
 
246  144 toggle private void setKeyActive(boolean bIsActive)
247    {
248  144 if (activeKey != null)
249    {
250  122 activeKey.bIsActive = bIsActive;
251    }
252    }
253   
 
254  67 toggle 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   
 
263  31 toggle private void hideKeyHint()
264    {
265  31 if ((editText != null) && (activeKey != null))
266  26 editText.hideSymbolHint();
267    }
268   
 
269  103 toggle @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   
 
315  31 toggle private void doButtonClick()
316    {
317  31 if ((activeKey != null) && (activeKey.bIsActive))
318    {
319   
320    //mode key: switch layout
321  26 if (activeKey.isModeKey())
322    {
323  1 keyLayout.cycleLayoutMode();
324  1 return;
325    }
326   
327    //call event
328  25 if (mEventOnKeyClick != null)
329    {
330  25 mEventOnKeyClick.OnKeyClicked(activeKey, keyLayout.isCapitalMode());
331    }
332   
333    }
334    }
335   
 
336  25 toggle public KeyItemLayout getKeyLayout()
337    {
338  25 return keyLayout;
339    }
340   
341    }