1 |
|
|
2 |
|
package pl.magot.vetch.widgets; |
3 |
|
|
4 |
|
|
5 |
|
import android.app.*; |
6 |
|
import android.content.Intent; |
7 |
|
import android.os.Bundle; |
8 |
|
import android.view.*; |
9 |
|
import android.widget.Button; |
10 |
|
import android.widget.LinearLayout; |
11 |
|
import android.widget.TextView; |
12 |
|
import android.widget.LinearLayout.LayoutParams; |
13 |
|
|
14 |
|
|
|
|
| 84,9% |
Uncovered Elements: 25 (166) |
Complexity: 36 |
Complexity Density: 0,3 |
|
15 |
|
public class KeyboardWidget extends Activity |
16 |
|
{ |
17 |
|
|
18 |
|
public static final int EDIT_TEXT_REQUEST = 113; |
19 |
|
private static final int iSmallButtonWidth = 100; |
20 |
|
|
21 |
|
|
22 |
|
public static final int iMaxWidth = 320 - (20 * 2); |
23 |
|
public static final int iMaxHeight = 480; |
24 |
|
|
25 |
|
|
26 |
|
private static String sStrTitle = "Enter text"; |
27 |
|
private static String sStrSet = "set"; |
28 |
|
private static String sStrCancel= "cancel"; |
29 |
|
|
30 |
|
|
31 |
|
private LinearLayout layContent = null; |
32 |
|
private KeyboardWidgetView keyboard = null; |
33 |
|
private HintEdit edit = null; |
34 |
|
private Button btnCancel = null; |
35 |
|
private Button btnSet = null; |
36 |
|
|
37 |
|
|
|
|
| 76,5% |
Uncovered Elements: 4 (17) |
Complexity: 4 |
Complexity Density: 0,36 |
|
38 |
5
|
@Override... |
39 |
|
public void onCreate(Bundle icicle) |
40 |
|
{ |
41 |
5
|
super.onCreate(icicle); |
42 |
|
|
43 |
5
|
setTitle(sStrTitle); |
44 |
|
|
45 |
|
|
46 |
5
|
String sText = ""; |
47 |
5
|
Bundle data = this.getIntent().getExtras(); |
48 |
5
|
if (data != null) |
49 |
|
{ |
50 |
5
|
if (data.containsKey("text")) |
51 |
5
|
sText = data.getString("text"); |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
5
|
if (icicle != null) |
56 |
|
{ |
57 |
0
|
sText = icicle.getString("freezeText"); |
58 |
|
} |
59 |
|
|
60 |
5
|
setContentView(generateContentView()); |
61 |
|
|
62 |
5
|
initializeEditing(sText); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
65 |
1
|
@Override... |
66 |
|
protected void onSaveInstanceState(Bundle outState) |
67 |
|
{ |
68 |
1
|
super.onSaveInstanceState(outState); |
69 |
|
|
70 |
1
|
outState.putString("freezeText", edit.getText().toString().trim()); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
5
|
@Override... |
74 |
|
public void onStart() |
75 |
|
{ |
76 |
5
|
super.onStart(); |
77 |
|
|
78 |
|
} |
79 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
0
|
public static void setStrings(String sStrTitle)... |
81 |
|
{ |
82 |
0
|
KeyboardWidget.sStrTitle = new String(sStrTitle); |
83 |
|
} |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
85 |
0
|
public static void setStrings(String sStrTitle, String sStrCancel, String strSet)... |
86 |
|
{ |
87 |
0
|
sStrTitle = new String(sStrTitle); |
88 |
0
|
sStrCancel = new String(sStrCancel); |
89 |
0
|
strSet = new String(strSet); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
92 |
5
|
public static void Open(Activity parentActivity, String sText)... |
93 |
|
{ |
94 |
5
|
Intent it = new Intent("android.intent.action.AnCal.ACTION_MODE_EDIT_TEXT"); |
95 |
5
|
Bundle data = new Bundle(); |
96 |
5
|
data.putString("text", sText); |
97 |
5
|
it.putExtras(data); |
98 |
5
|
parentActivity.startActivityForResult(it, EDIT_TEXT_REQUEST); |
99 |
|
} |
100 |
|
|
|
|
| 62,5% |
Uncovered Elements: 3 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
101 |
4
|
public static String GetTextOnActivityResult(int requestCode, int resultCode, Bundle extras)... |
102 |
|
{ |
103 |
4
|
if ((requestCode == KeyboardWidget.EDIT_TEXT_REQUEST) && (resultCode == RESULT_OK)) |
104 |
|
{ |
105 |
4
|
if (extras.containsKey("text")) |
106 |
|
{ |
107 |
4
|
return extras.getString("text"); |
108 |
|
} |
109 |
|
} |
110 |
0
|
return ""; |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
113 |
30
|
public LinearLayout createLayout(int iOrientation)... |
114 |
|
{ |
115 |
30
|
LinearLayout lay = new LinearLayout(this); |
116 |
30
|
lay.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); |
117 |
30
|
lay.setOrientation(iOrientation); |
118 |
30
|
return lay; |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
121 |
10
|
public Button createButton(String sText, int iWidth, int iHeight)... |
122 |
|
{ |
123 |
10
|
Button btn = new Button(this); |
124 |
10
|
btn.setText(sText); |
125 |
10
|
btn.setLayoutParams(new LayoutParams(iWidth, iHeight)); |
126 |
10
|
return btn; |
127 |
|
} |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
129 |
5
|
public TextView createLabel(String sText, int iWidth, int iHeight)... |
130 |
|
{ |
131 |
5
|
TextView label = new TextView(this); |
132 |
5
|
label.setText(sText); |
133 |
5
|
label.setLayoutParams(new LayoutParams(iWidth, iHeight)); |
134 |
5
|
return label; |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0,09 |
|
137 |
5
|
public void generateBottomButtons(LinearLayout layBottomControls)... |
138 |
|
{ |
139 |
5
|
TextView labMargin = createLabel("", 8, android.view.ViewGroup.LayoutParams.FILL_PARENT); |
140 |
|
|
141 |
5
|
btnCancel = createButton(sStrCancel, iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); |
142 |
5
|
btnCancel.setBackgroundResource(android.R.drawable.btn_default_small); |
143 |
|
|
144 |
5
|
btnSet = createButton(sStrSet, iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); |
145 |
5
|
btnSet.setBackgroundResource(android.R.drawable.btn_default_small); |
146 |
|
|
147 |
|
|
148 |
5
|
btnCancel.setOnClickListener(new Button.OnClickListener() |
149 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
1
|
public void onClick(View arg0) {... |
151 |
1
|
OnClose(false); |
152 |
|
}}); |
153 |
5
|
btnSet.setOnClickListener(new Button.OnClickListener() |
154 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
4
|
public void onClick(View arg0) { ... |
156 |
4
|
OnClose(true); |
157 |
|
}}); |
158 |
|
|
159 |
5
|
layBottomControls.setGravity(Gravity.CENTER_HORIZONTAL); |
160 |
5
|
layBottomControls.addView(btnCancel); |
161 |
5
|
layBottomControls.addView(labMargin); |
162 |
5
|
layBottomControls.addView(btnSet); |
163 |
|
} |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
165 |
5
|
public void OnClose(boolean bOK)... |
166 |
|
{ |
167 |
5
|
Bundle data = new Bundle(); |
168 |
5
|
data.putString("text", edit.getText().toString().trim()); |
169 |
|
|
170 |
5
|
Intent intentData = new Intent(""); |
171 |
5
|
intentData.putExtras(data); |
172 |
5
|
setResult(bOK?RESULT_OK:RESULT_CANCELED, intentData); |
173 |
|
|
174 |
5
|
this.finish(); |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
177 |
5
|
public HintEdit createEditBox()... |
178 |
|
{ |
179 |
5
|
edit = new HintEdit(this); |
180 |
5
|
edit.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); |
181 |
5
|
edit.setLines(3); |
182 |
5
|
edit.setGravity(Gravity.TOP); |
183 |
5
|
edit.setFocusable(true); |
184 |
|
|
185 |
5
|
return edit; |
186 |
|
} |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 1 |
Complexity Density: 0,05 |
|
188 |
5
|
private View generateContentView()... |
189 |
|
{ |
190 |
5
|
LinearLayout layMain = createLayout(LinearLayout.VERTICAL); |
191 |
5
|
layMain.setPadding(8, 8, 8, 8); |
192 |
|
|
193 |
5
|
LinearLayout layTopControls = createLayout(LinearLayout.HORIZONTAL); |
194 |
5
|
LinearLayout layMargin = createLayout(LinearLayout.HORIZONTAL); |
195 |
5
|
layMargin.getLayoutParams().height = 12; |
196 |
|
|
197 |
5
|
LinearLayout layContentBottom = createLayout(LinearLayout.HORIZONTAL); |
198 |
5
|
layContentBottom.getLayoutParams().height = 18; |
199 |
|
|
200 |
5
|
LinearLayout layBottomControls = createLayout(LinearLayout.HORIZONTAL); |
201 |
|
|
202 |
|
|
203 |
5
|
edit = createEditBox(); |
204 |
5
|
layTopControls.addView(edit); |
205 |
|
|
206 |
|
|
207 |
5
|
layContent = createLayout(LinearLayout.VERTICAL); |
208 |
|
|
209 |
5
|
keyboard = new KeyboardWidgetView(this, iMaxWidth); |
210 |
|
|
211 |
5
|
layContent.getLayoutParams().width = iMaxWidth; |
212 |
5
|
layContent.getLayoutParams().height = keyboard.getTotalHeight(); |
213 |
|
|
214 |
5
|
layContent.addView(keyboard); |
215 |
|
|
216 |
5
|
generateBottomButtons(layBottomControls); |
217 |
|
|
218 |
5
|
layMain.addView(layTopControls); |
219 |
5
|
layMain.addView(layMargin); |
220 |
5
|
layMain.addView(layContent); |
221 |
5
|
layMain.addView(layContentBottom); |
222 |
5
|
layMain.addView(layBottomControls); |
223 |
|
|
224 |
5
|
return layMain; |
225 |
|
} |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
227 |
5
|
public void initializeEditing(String sText)... |
228 |
|
{ |
229 |
5
|
keyboard.setKeyClickEvent(mOnKeyClick); |
230 |
5
|
keyboard.editText = edit; |
231 |
5
|
edit.setText(sText); |
232 |
5
|
edit.setSelectAllOnFocus(true); |
233 |
|
} |
234 |
|
|
235 |
|
public KeyboardWidgetView.OnKeyClick mOnKeyClick = new KeyboardWidgetView.OnKeyClick() |
236 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
237 |
25
|
public void OnKeyClicked(KeyItem key, boolean bCapital)... |
238 |
|
{ |
239 |
25
|
key.sendKey(edit, bCapital); |
240 |
25
|
autoChangeLayout(key); |
241 |
|
} |
242 |
|
}; |
243 |
|
|
|
|
| 72,1% |
Uncovered Elements: 12 (43) |
Complexity: 11 |
Complexity Density: 0,41 |
|
244 |
25
|
public void autoChangeLayout(KeyItem key)... |
245 |
|
{ |
246 |
25
|
KeyItemLayout keys = keyboard.getKeyLayout(); |
247 |
25
|
String sText = edit.getText().toString(); |
248 |
|
|
249 |
|
|
250 |
25
|
if (sText.length() == 0) |
251 |
|
{ |
252 |
0
|
keys.setLayoutBigCaps(); |
253 |
0
|
keyboard.invalidate(); |
254 |
0
|
return; |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
25
|
if (sText.length() == 1) |
259 |
|
{ |
260 |
2
|
keys.setLayoutSmallCaps(); |
261 |
2
|
keyboard.invalidate(); |
262 |
2
|
return; |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
23
|
if ((sText.length() > 0) && (key.iType == KeyItem.kAlphaCycle)) |
267 |
|
{ |
268 |
1
|
if (sText.charAt(sText.length() - 1) == '.') |
269 |
|
{ |
270 |
1
|
keys.setLayoutBigCaps(); |
271 |
1
|
keyboard.invalidate(); |
272 |
1
|
return; |
273 |
|
} |
274 |
0
|
if (sText.charAt(sText.length() - 1) == ',') |
275 |
|
{ |
276 |
0
|
keys.setLayoutSmallCaps(); |
277 |
0
|
keyboard.invalidate(); |
278 |
0
|
return; |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
22
|
int index = sText.lastIndexOf("."); |
284 |
22
|
if ((index != -1) && (sText.length() > 0)) |
285 |
|
{ |
286 |
10
|
sText = sText.substring(index + 1).trim(); |
287 |
10
|
if (sText.length() == 1) |
288 |
|
{ |
289 |
1
|
if (Character.isUpperCase(sText.charAt(0))) |
290 |
|
{ |
291 |
1
|
keys.setLayoutSmallCaps(); |
292 |
1
|
keyboard.invalidate(); |
293 |
1
|
return; |
294 |
|
} |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
} |
299 |
|
|
300 |
|
} |