-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathCodenameOneView.java
571 lines (508 loc) · 22.1 KB
/
CodenameOneView.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.impl.android;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.*;
import android.view.inputmethod.EditorInfo;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.PeerComponent;
import com.codename1.ui.TextArea;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.LayeredLayout;
import java.util.Collections;
import java.util.List;
/**
*
* @author Chen
*/
public class CodenameOneView {
int width = 1;
int height = 1;
Bitmap bitmap;
AndroidGraphics buffy = null;
private Canvas canvas;
private AndroidImplementation implementation = null;
private final KeyCharacterMap keyCharacterMap;
private final Rect bounds = new Rect();
private boolean fireKeyDown = false;
//private volatile boolean created = false;
private boolean drawing;
public CodenameOneView(Activity activity, View androidView, AndroidImplementation implementation, boolean drawing) {
this.implementation = implementation;
this.drawing = drawing;
androidView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
androidView.setFocusable(true);
androidView.setFocusableInTouchMode(true);
androidView.setEnabled(true);
androidView.setClickable(true);
androidView.setLongClickable(false);
/**
* tell the system that we do our own caching and it does not need to
* use an extra offscreen bitmap.
*/
if(!drawing) {
androidView.setWillNotCacheDrawing(false);
androidView.setWillNotDraw(true);
this.buffy = new AndroidGraphics(implementation, null, false);
}
this.keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
/**
* From the docs: "Change whether this view is one of the set of
* scrollable containers in its window. This will be used to determine
* whether the window can resize or must pan when a soft input area is
* open -- scrollable containers allow the window to use resize mode
* since the container will appropriately shrink. "
*/
androidView.setScrollContainer(true);
android.view.Display androidDisplay = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
width = androidDisplay.getWidth();
height = androidDisplay.getHeight();
initBitmaps(width, height);
}
public boolean isOpaque() {
return true;
}
public void onSurfaceChanged(final int w, final int h) {
if(!Display.isInitialized()) {
return;
}
Display.getInstance().callSerially(new Runnable() {
public void run() {
handleSizeChange(w, h);
}
});
}
public void onSurfaceCreated() {
this.visibilityChangedTo(true);
}
public void onSurfaceDestroyed() {
this.visibilityChangedTo(false);
}
private void initBitmaps(int w, int h) {
if(!drawing) {
this.bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
this.canvas = new Canvas(this.bitmap);
this.buffy.setCanvas(this.canvas);
}
}
public void visibilityChangedTo(boolean visible) {
boolean changed = visible;
if (this.implementation.getCurrentForm() != null && changed) {
if (visible) {
this.implementation.showNotifyPublic();
/**
* request a full repaint as our surfaceview is most likely
* black if this app comes back from the background.
*/
this.implementation.getCurrentForm().repaint();
//android.os.Debug.startMethodTracing("calc");
} else {
this.implementation.hideNotifyPublic();
//android.os.Debug.stopMethodTracing();
}
}
//flushGraphics();
}
public void handleSizeChange(int w, int h) {
if(!drawing) {
if ((this.width != w && (this.width < w || this.height < h))
|| (bitmap.getHeight() < h)) {
this.initBitmaps(w, h);
}
}
if (this.width == w && this.height == h) {
return;
}
this.width = w;
this.height = h;
Log.d("Codename One", "sizechanged: " + width + " " + height + " " + this);
if (this.implementation.getCurrentForm() == null) {
/**
* make sure a form has been set before we can send events to the
* EDT. if we send events before the form has been set we might
* deadlock!
*/
return;
}
if (InPlaceEditView.isEditing()) {
final Form f = this.implementation.getCurrentForm();
ActionListener sizeChanged = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
CodenameOneView.this.implementation.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
InPlaceEditView.reLayoutEdit();
InPlaceEditView.scrollActiveTextfieldToVisible();
}
});
f.removeSizeChangedListener(this);
}
};
f.addSizeChangedListener(sizeChanged);
}
Display.getInstance().sizeChanged(w, h);
}
//@Override
protected void d(Canvas canvas) {
if(!drawing) {
boolean empty = canvas.getClipBounds(bounds);
if (empty) {
// ??
canvas.drawBitmap(bitmap, 0, 0, null);
} else {
bounds.intersect(0, 0, width, height);
canvas.drawBitmap(bitmap, bounds, bounds, null);
}
}
}
/**
* some info from the MIDP docs about keycodes:
*
* "Applications receive keystroke events in which the individual keys are
* named within a space of key codes. Every key for which events are
* reported to MIDP applications is assigned a key code. The key code values
* are unique for each hardware key unless two keys are obvious synonyms for
* each other. MIDP defines the following key codes: KEY_NUM0, KEY_NUM1,
* KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8,
* KEY_NUM9, KEY_STAR, and KEY_POUND. (These key codes correspond to keys on
* a ITU-T standard telephone keypad.) Other keys may be present on the
* keyboard, and they will generally have key codes distinct from those list
* above. In order to guarantee portability, applications should use only
* the standard key codes.
*
* The standard key codes' values are equal to the Unicode encoding for the
* character that represents the key. If the device includes any other keys
* that have an obvious correspondence to a Unicode character, their key
* code values should equal the Unicode encoding for that character. For
* keys that have no corresponding Unicode character, the implementation
* must use negative values. Zero is defined to be an invalid key code."
*
* Because the MIDP implementation is our reference and that implementation
* does not interpret the given keycodes we behave alike and pass on the
* unicode values.
*/
final static int internalKeyCodeTranslate(int keyCode) {
/**
* make sure these important keys have a negative value when passed to
* Codename One or they might be interpreted as characters.
*/
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
return AndroidImplementation.DROID_IMPL_KEY_DOWN;
case KeyEvent.KEYCODE_DPAD_UP:
return AndroidImplementation.DROID_IMPL_KEY_UP;
case KeyEvent.KEYCODE_DPAD_LEFT:
return AndroidImplementation.DROID_IMPL_KEY_LEFT;
case KeyEvent.KEYCODE_DPAD_RIGHT:
return AndroidImplementation.DROID_IMPL_KEY_RIGHT;
case KeyEvent.KEYCODE_DPAD_CENTER:
return AndroidImplementation.DROID_IMPL_KEY_FIRE;
case KeyEvent.KEYCODE_MENU:
return AndroidImplementation.DROID_IMPL_KEY_MENU;
case KeyEvent.KEYCODE_CLEAR:
return AndroidImplementation.DROID_IMPL_KEY_CLEAR;
case KeyEvent.KEYCODE_DEL:
return AndroidImplementation.DROID_IMPL_KEY_BACKSPACE;
case KeyEvent.KEYCODE_BACK:
return AndroidImplementation.DROID_IMPL_KEY_BACK;
default:
return keyCode;
}
}
public boolean onKeyUpDown(boolean down, int keyCode, KeyEvent event) {
keyCode = this.internalKeyCodeTranslate(keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_SEARCH:
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
case KeyEvent.KEYCODE_ALT_LEFT:
case KeyEvent.KEYCODE_ALT_RIGHT:
case KeyEvent.KEYCODE_SYM:
case KeyEvent.KEYCODE_ENTER:
return false;
default:
}
if (event.getRepeatCount() > 0) {
// skip repeats
return true;
}
if (this.implementation.getCurrentForm() == null) {
/**
* make sure a form has been set before we can send events to the
* EDT. if we send events before the form has been set we might
* deadlock!
*/
return true;
}
if (keyCode == AndroidImplementation.DROID_IMPL_KEY_FIRE) {
this.fireKeyDown = down;
} else if (keyCode == AndroidImplementation.DROID_IMPL_KEY_DOWN
|| keyCode == AndroidImplementation.DROID_IMPL_KEY_UP
|| keyCode == AndroidImplementation.DROID_IMPL_KEY_LEFT
|| keyCode == AndroidImplementation.DROID_IMPL_KEY_RIGHT) {
if (this.fireKeyDown) {
/**
* we keep track of trackball press/release. while it is pressed
* we drop directional movements. these movements are most
* likely not intended. if the device has no trackball i see no
* situation where this additional behavior could hurt.
*/
return true;
}
}
switch (keyCode) {
case AndroidImplementation.DROID_IMPL_KEY_MENU:
//if the native commands are used don't handle the keycode
if (Display.getInstance().getCommandBehavior() == Display.COMMAND_BEHAVIOR_NATIVE) {
return false;
}
case AndroidImplementation.DROID_IMPL_KEY_BACK:
case AndroidImplementation.DROID_IMPL_KEY_DOWN:
case AndroidImplementation.DROID_IMPL_KEY_UP:
case AndroidImplementation.DROID_IMPL_KEY_LEFT:
case AndroidImplementation.DROID_IMPL_KEY_RIGHT:
case AndroidImplementation.DROID_IMPL_KEY_FIRE:
case AndroidImplementation.DROID_IMPL_KEY_CLEAR:
case AndroidImplementation.DROID_IMPL_KEY_BACKSPACE:
// directly pass to display.
if (down) {
Display.getInstance().keyPressed(keyCode);
} else {
Display.getInstance().keyReleased(keyCode);
}
return true;
default:
/**
* Codename One's TextField does not seem to work well if two
* keyup-keydown sequences of different keys are not strictly
* sequential. so we pass the up event of a character right
* after the down event. this is exactly the behavior of the
* BlackBerry implementation from this repository and has worked
* well for me. i guess this should be changed as soon as the
* TextField changes.
*/
int meta = 0;
if (event.isShiftPressed()) {
meta |= KeyEvent.META_SHIFT_ON;
}
if (event.isAltPressed()) {
meta |= KeyEvent.META_ALT_ON;
}
if (event.isSymPressed()) {
meta |= KeyEvent.META_SYM_ON;
}
final int nextchar = this.keyCharacterMap.get(keyCode, meta);
if (down) {
Display.getInstance().keyPressed(nextchar);
} else {
Display.getInstance().keyReleased(nextchar);
}
return true;
}
}
private boolean cn1GrabbedPointer = false;
//private boolean nativePeerGrabbedPointer = false;
public boolean onTouchEvent(MotionEvent event) {
if (this.implementation.getCurrentForm() == null) {
/**
* make sure a form has been set before we can send events to the
* EDT. if we send events before the form has been set we might
* deadlock!
*/
return true;
}
int[] x = null;
int[] y = null;
int size = event.getPointerCount();
if (size > 1) {
x = new int[size];
y = new int[size];
for (int i = 0; i < size; i++) {
x[i] = (int) event.getX(i);
y[i] = (int) event.getY(i);
}
}
/*
if (!cn1GrabbedPointer) {
if (x == null) {
Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)event.getX(), (int)event.getY());
if (componentAt != null && (componentAt instanceof PeerComponent)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//nativePeerGrabbedPointer = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//nativePeerGrabbedPointer = false;
}
return false;
}
} else {
Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)x[0], (int)y[0]);
if (componentAt != null && (componentAt instanceof PeerComponent)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
nativePeerGrabbedPointer = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
nativePeerGrabbedPointer = false;
}
return false;
}
}
}
*/
//if (nativePeerGrabbedPointer) {
// return false;
//}
Component componentAt;
if (x == null) {
componentAt = safetyGetComponentAt(this.implementation.getCurrentForm(), (int)event.getX(), (int)event.getY());
} else {
componentAt = safetyGetComponentAt(this.implementation.getCurrentForm(), (int)x[0], (int)y[0]);
}
boolean isPeer = (componentAt instanceof PeerComponent);
boolean consumeEvent = !isPeer || cn1GrabbedPointer;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x == null) {
this.implementation.pointerPressed((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerPressed(x, y);
}
if (!isPeer) cn1GrabbedPointer = true;
break;
case MotionEvent.ACTION_UP:
if (x == null) {
this.implementation.pointerReleased((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerReleased(x, y);
}
cn1GrabbedPointer = false;
break;
case MotionEvent.ACTION_CANCEL:
cn1GrabbedPointer = false;
break;
case MotionEvent.ACTION_MOVE:
if (x == null) {
this.implementation.pointerDragged((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerDragged(x, y);
}
break;
}
return consumeEvent;
}
private Component safetyGetComponentAt(final Container root, final int x, final int y){
if (!root.isVisible() || !root.contains(x, y)) {
return null;
}
List<Component> children = root.getChildrenAsList(true);
if (root.getLayout() instanceof LayeredLayout){
Collections.reverse(children);
}
int componentCount = root.getComponentCount();
int startingPoint = 0;
int endPoint = componentCount;
int direction = 1;
if(root.getLayout() instanceof LayeredLayout){
direction = -1;
endPoint = -1;
startingPoint = componentCount - 1;
}
for(int iter = startingPoint ; iter != endPoint ; iter += direction) {
Component currComponent = root.getComponentAt(iter);
if (currComponent.contains(x, y)) {
if (!currComponent.isBlockLead() && currComponent instanceof Container) {
currComponent = safetyGetComponentAt((Container)currComponent, x, y);
}
if (currComponent != null && currComponent.isVisible() && currComponent.isEnabled()) {
return currComponent;
}
}
}
if (root.isEnabled() && (root.isFocusable() || root.isGrabsPointerEvents() || root.isDraggable())){
return root;
}
return null;
}
public AndroidGraphics getGraphics() {
return buffy;
}
public int getViewHeight() {
return height;
}
public int getViewWidth() {
return width;
}
public void setInputType(EditorInfo editorInfo) {
/**
* do not use the enter key to fire some kind of action!
*/
// editorInfo.imeOptions |= EditorInfo.IME_ACTION_NONE;
Component txtCmp = Display.getInstance().getCurrent().getFocused();
if (txtCmp != null && txtCmp instanceof TextArea) {
TextArea txt = (TextArea) txtCmp;
if (txt.isSingleLineTextArea()) {
editorInfo.imeOptions |= EditorInfo.IME_ACTION_DONE;
} else {
editorInfo.imeOptions |= EditorInfo.IME_ACTION_NONE;
}
int inputType = 0;
int constraint = txt.getConstraint();
if ((constraint & TextArea.PASSWORD) == TextArea.PASSWORD) {
constraint = constraint ^ TextArea.PASSWORD;
}
switch (constraint) {
case TextArea.NUMERIC:
inputType = EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_SIGNED;
break;
case TextArea.DECIMAL:
inputType = EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_DECIMAL;
break;
case TextArea.PHONENUMBER:
inputType = EditorInfo.TYPE_CLASS_PHONE;
break;
case TextArea.EMAILADDR:
inputType = EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case TextArea.URL:
inputType = EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_URI;
break;
default:
inputType = EditorInfo.TYPE_CLASS_TEXT;
break;
}
editorInfo.inputType = inputType;
}
}
}