-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathIOSImplementation.java
More file actions
9532 lines (8189 loc) · 324 KB
/
IOSImplementation.java
File metadata and controls
9532 lines (8189 loc) · 324 KB
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.ios;
import com.codename1.background.BackgroundFetch;
import com.codename1.capture.VideoCaptureConstraints;
import com.codename1.codescan.CodeScanner;
import com.codename1.codescan.ScanResult;
import com.codename1.contacts.Address;
import com.codename1.contacts.Contact;
import com.codename1.db.Database;
import com.codename1.impl.CodenameOneImplementation;
import com.codename1.location.Location;
import com.codename1.ui.Component;
import com.codename1.ui.Display;
import com.codename1.ui.Font;
import com.codename1.ui.Image;
import com.codename1.ui.PeerComponent;
import com.codename1.ui.Sheet;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import java.util.StringTokenizer;
import com.codename1.io.BufferedInputStream;
import com.codename1.io.BufferedOutputStream;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.FileSystemStorage;
import com.codename1.io.Storage;
import com.codename1.io.Util;
import com.codename1.l10n.L10NManager;
import com.codename1.location.LocationListener;
import com.codename1.location.LocationManager;
import com.codename1.media.Media;
import com.codename1.messaging.Message;
import com.codename1.payment.Purchase;
import com.codename1.payment.PurchaseCallback;
import com.codename1.push.PushCallback;
import com.codename1.push.PushActionsProvider;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.util.EventDispatcher;
import com.codename1.ui.util.ImageIO;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;
import com.codename1.io.Cookie;
import com.codename1.io.Log;
import com.codename1.io.Preferences;
import com.codename1.location.Geofence;
import com.codename1.location.GeofenceListener;
import com.codename1.location.LocationRequest;
import com.codename1.media.AbstractMedia;
import com.codename1.media.AudioBuffer;
import com.codename1.media.MediaManager;
import com.codename1.media.MediaRecorderBuilder;
import com.codename1.notifications.LocalNotification;
import com.codename1.notifications.LocalNotificationCallback;
import com.codename1.payment.RestoreCallback;
import com.codename1.push.PushAction;
import com.codename1.push.PushActionCategory;
import com.codename1.push.PushContent;
import com.codename1.ui.Accessor;
import com.codename1.ui.CN;
import com.codename1.ui.Container;
import com.codename1.ui.Dialog;
import com.codename1.ui.Graphics;
import com.codename1.ui.geom.GeneralPath;
import com.codename1.ui.Stroke;
import com.codename1.ui.Transform;
import com.codename1.ui.geom.PathIterator;
import com.codename1.ui.geom.Shape;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.spinner.Picker;
import com.codename1.util.AsyncResource;
import com.codename1.util.Callback;
import com.codename1.util.StringUtil;
import com.codename1.util.SuccessCallback;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import com.codename1.ui.plaf.DefaultLookAndFeel;
/**
*
* @author Shai Almog
*/
public class IOSImplementation extends CodenameOneImplementation {
// Flag to indicate if the current openGallery process is selecting multiple files
private boolean disableUIWebView=true;
private static boolean gallerySelectMultiple;
public static IOSNative nativeInstance = new IOSNative();
private static LocalNotificationCallback localNotificationCallback;
private static PurchaseCallback purchaseCallback;
private static RestoreCallback restoreCallback;
private int timeout = 120000;
private static final Object CONNECTIONS_LOCK = new Object();
private ArrayList<NetworkConnection> connections = new ArrayList<NetworkConnection>();
private NativeFont defaultFont;
private NativeGraphics currentlyDrawingOn;
//private NativeImage backBuffer;
private NativeGraphics globalGraphics;
static IOSImplementation instance;
private TextArea currentEditing;
private static boolean initialized;
private Lifecycle life;
private CodeScannerImpl scannerInstance;
private static boolean minimized;
private String userAgent;
private TextureCache textureCache = new TextureCache();
private static boolean dropEvents;
private static boolean callInterruptionActive;
private NativePathRenderer globalPathRenderer;
private NativePathStroker globalPathStroker;
private boolean isActive=false;
private final ArrayList<Runnable> onActiveListeners = new ArrayList<Runnable>();
private static BackgroundFetch backgroundFetchCallback;
private boolean useContentBasedRTLStringDetection = false;
/**
* A pool that will cause java objects to be retained if they are passed
* to a non-managed thread via a mechanism like dispatch_async
*/
private static ArrayList autoreleasePool = new ArrayList();
static void retain(Object o){
if (o != null){
autoreleasePool.add(o);
}
}
static void release(Object o){
if (o != null){
autoreleasePool.remove(o);
}
}
public void initEDT() {
while(!initialized) {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}
}
if(globalGraphics == null) {
globalGraphics = new GlobalGraphics();
}
}
private static Runnable callback;
public static void callback() {
initialized = true;
Display.getInstance().callSerially(callback);
}
public void postInit() {
nativeInstance.initVM();
super.postInit();
}
@Override
protected void initDefaultUserAgent() {
String ue = getProperty("User-Agent", null);
if(ue != null) {
ConnectionRequest.setDefaultUserAgent(Display.getInstance().getProperty("User-Agent", ue));
}
}
public void init(Object m) {
instance = this;
setUseNativeCookieStore(false);
Display.getInstance().setTransitionYield(10);
Display.getInstance().setDefaultVirtualKeyboard(new IOSVirtualKeyboard(this));
callback = (Runnable)m;
if(m instanceof Lifecycle) {
life = (Lifecycle)m;
}
VideoCaptureConstraints.init(new IOSVideoCaptureConstraintsCompiler());
if("true".equals(Display.getInstance().getProperty("DisableScreenshots", ""))) {
nativeInstance.setDisableScreenshots(true);
}
}
public void setThreadPriority(Thread t, int p) {
}
public int getDisplayWidth() {
return nativeInstance.getDisplayWidth();
}
public int getDisplayHeight() {
return nativeInstance.getDisplayHeight();
}
public int getActualDisplayHeight() {
return nativeInstance.getDisplayHeight();
}
public static void displaySafeAreaChanged(final boolean revalidate) {
if (!CN.isEdt()) {
CN.callSerially(new Runnable() {
public void run() {
displaySafeAreaChanged(revalidate);
}
});
return;
}
Form f = CN.getCurrentForm();
if (f != null) {
f.setSafeAreaChanged();
f.revalidateWithAnimationSafety();
}
}
@Override
public Rectangle getDisplaySafeArea(Rectangle rect) {
if (rect == null) {
rect = new Rectangle();
}
try {
int x = nativeInstance.getDisplaySafeInsetLeft();
int y = nativeInstance.getDisplaySafeInsetTop();
int w = getDisplayWidth() - nativeInstance.getDisplaySafeInsetRight() - x;
int h = getDisplayHeight() - nativeInstance.getDisplaySafeInsetBottom() - y;
rect.setBounds(x, y, w, h);
} catch (NullPointerException err) {
Log.p("Invalid bounds in getDisplaySafeArea, if this message repeats frequently please let us know...");
}
return rect;
}
public boolean isNativeInputImmediate() {
return true;
}
@Override
protected int getDragAutoActivationThreshold() {
return 1000000;
}
public boolean isNativeInputSupported() {
return true;
}
public void exitApplication() {
System.exit(0);
}
public boolean isTablet() {
return nativeInstance.isTablet();
}
@Override
public void addCookie(Cookie c) {
if(isUseNativeCookieStore()) {
nativeInstance.addCookie(c.getName(), c.getValue(), c.getDomain(), c.getPath(), c.isSecure(), c.isHttpOnly(), c.getExpires());
} else {
super.addCookie(c);
}
}
private static SuccessCallback<Image> screenshotCallback;
@Override
public void screenshot(final SuccessCallback<Image> callback) {
if (callback == null) {
return;
}
if (screenshotCallback != null) {
Log.p("Screenshot request ignored: another capture is already in progress.");
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.onSucess(null);
}
});
return;
}
screenshotCallback = callback;
try {
nativeInstance.screenshot();
} catch (Throwable t) {
screenshotCallback = null;
Log.e(t);
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.onSucess(null);
}
});
}
}
static void onScreenshot(final byte[] imageData) {
final SuccessCallback<Image> callback = screenshotCallback;
screenshotCallback = null;
if (callback == null) {
return;
}
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
if (imageData != null && imageData.length > 0) {
try {
Image image = Image.createImage(imageData, 0, imageData.length);
if (image != null) {
if (image.getGraphics() == null) {
int width = Math.max(1, image.getWidth());
int height = Math.max(1, image.getHeight());
try {
int[] rgb = image.getRGB();
if (rgb != null && rgb.length >= width * height) {
Image mutable = Image.createImage(rgb, width, height);
if (mutable != null && mutable.getGraphics() != null) {
image = mutable;
}
}
} catch (OutOfMemoryError oom) {
Log.e(oom);
} catch (Throwable t) {
Log.e(t);
}
}
if (image != null && image.getGraphics() != null) {
callback.onSucess(image);
return;
}
}
} catch (Throwable t) {
Log.e(t);
}
}
callback.onSucess(null);
}
});
}
/**
* Used to enable/disable native cookies from native code.
* @param cookiesArray
*/
static void setUseNativeCookiesNativeCallback(boolean useNative){
instance.setUseNativeCookieStore(useNative);
}
static boolean isUseNativeCookiesNativeCallback(){
return instance.isUseNativeCookieStore();
}
@Override
public void clearNativeCookies() {
nativeInstance.clearNativeCookies();
}
/**
*
* {@inheritDoc }
*/
@Override
public boolean isNativeCookieSharingSupported() {
return true;
}
@Override
public void addCookie(Cookie[] cookiesArray) {
if(isUseNativeCookieStore()) {
int len = cookiesArray.length;
for(int i = 0 ; i < len ; i++){
addCookie(cookiesArray[i]);
}
} else {
super.addCookie(cookiesArray);
}
}
@Override
public Vector getCookiesForURL(String url) {
if(isUseNativeCookieStore()) {
Vector v = new Vector();
nativeInstance.getCookiesForURL(url, v);
return v;
}
return super.getCookiesForURL(url);
}
public void setPlatformHint(String key, String value) {
if ("platformHint.ios.useContentBasedRTLStringDetection".equals(key)) {
useContentBasedRTLStringDetection = Boolean.parseBoolean(value);
}
}
private boolean textEditorHidden;
@Override
public boolean isAsyncEditMode() {
return nativeInstance.isAsyncEditMode();
}
// This is a bit of a hack to work around the fact that setScrollY() automatically
// calls hideTextEditor when async editing is enabled. Sometimes we want to
// just scroll the text field into view and don't want this to happen.
private int doNotHideTextEditorSemaphore=0;
/**
* A way to get the *actual* root content pane of a form without exposing Form.getActualPane().
* @param f The form whose root pane we want.
* @return The root pane of the form. If there is no layered pane, then this should just
* return the content pane. Otherwise it may return the parent of the layered pane and content pane.
*/
private static Container getRootPane(Form f) {
Container root = f.getContentPane();
Container parent = null;
while ((parent = root.getParent()) != null && parent != f) {
root = parent;
}
return root;
}
@Override
public void hideTextEditor() {
if (doNotHideTextEditorSemaphore > 0) {
return;
}
if(textEditorHidden) {
return;
}
Form current = getCurrentForm();
if(nativeInstance.isAsyncEditMode() && current.isFormBottomPaddingEditingMode() && getRootPane(current).getUnselectedStyle().getPaddingBottom()> 0) {
getRootPane(current).getUnselectedStyle().setPadding(Component.BOTTOM, 0);
current.forceRevalidate();
}
nativeInstance.hideTextEditing();
textEditorHidden = true;
repaintTextEditor(false);
}
private boolean pendingEditingText;
@Override
public boolean isEditingText(Component c) {
if(textEditorHidden) {
return false;
}
if (pendingEditingText) {
return false;
}
//return c == currentEditing;
return super.isEditingText(c);
}
@Override
public boolean isEditingText() {
/*if(textEditorHidden) {
return false;
}*/
//return currentEditing != null;
return super.isEditingText();
}
@Override
public void stopTextEditing() {
if (isAsyncEditMode()) {
foldKeyboard();
} else {
if (currentEditing != null) {
editingUpdate(currentEditing.getText(), currentEditing.getCursorPosition(), true);
nativeInstance.foldVKB();
}
}
}
public static void foldKeyboard() {
if(instance.isAsyncEditMode()) {
Form f = Display.getInstance().getCurrent();
final Component cmp = f == null ? null : f.getFocused();
instance.callHideTextEditor();
nativeInstance.foldVKB();
// after folding the keyboard the screen layout might shift
Display.getInstance().callSerially(new Runnable() {
public void run() {
if(cmp != null) {
Form f = Display.getInstance().getCurrent();
if(f == cmp.getComponentForm()) {
cmp.requestFocus();
}
if(nativeInstance.isAsyncEditMode() && f.isFormBottomPaddingEditingMode() && getRootPane(f).getUnselectedStyle().getPaddingBottom() > 0) {
getRootPane(f).getUnselectedStyle().setPadding(Component.BOTTOM, 0);
f.forceRevalidate();
return;
}
// revalidate even if we transition to a different form since the
// spacing might have remained during the transition
f.revalidate();
}
}
});
}
}
private void callHideTextEditor() {
super.hideTextEditor();
}
/**
* Invoked from native do not remove
*/
static void showTextEditorAgain() {
instance.textEditorHidden = false;
instance.repaintTextEditor(true);
}
// A flag to override the invisible area under VKB. This
// is used when hiding the keyboard, but the keyboard may still
// be visible so that we can perform revalidation of the form
// using a supposed state.
private int areaUnderVKBOverride=-1;
@Override
public int getInvisibleAreaUnderVKB() {
if (areaUnderVKBOverride >= 0) {
return areaUnderVKBOverride;
}
if(isAsyncEditMode()) {
return nativeInstance.getVKBHeight();
}
return 0;
}
private static final String LAST_UPDATED_EDITOR_BOUNDS_KEY = "$$ios.updateNativeTextEditorFrame.lastUpdatedBounds";
private static void updateNativeTextEditorFrame() {
updateNativeTextEditorFrame(true);
}
private static void updateNativeTextEditorFrame(boolean requestFocus) {
if (instance.currentEditing != null) {
TextArea cmp = instance.currentEditing;
Form form = cmp.getComponentForm();
if (form == null || form != CN.getCurrentForm() ) {
instance.stopTextEditing();
return;
}
int x = cmp.getAbsoluteX() + cmp.getScrollX();
int y = cmp.getAbsoluteY() + cmp.getScrollY();
int w = cmp.getWidth();
int h = cmp.getHeight();
String key = LAST_UPDATED_EDITOR_BOUNDS_KEY;
Rectangle lastUpdatedBounds = (Rectangle)cmp.getClientProperty(key);
if (lastUpdatedBounds != null) {
if (lastUpdatedBounds.getX() == x && lastUpdatedBounds.getY() == y && lastUpdatedBounds.getWidth() == w && lastUpdatedBounds.getHeight() == h) {
return;
}
lastUpdatedBounds.setBounds(x, y, w, h);
} else {
lastUpdatedBounds = new Rectangle(x, y, w, h);
cmp.putClientProperty(key, lastUpdatedBounds);
}
final Style stl = cmp.getStyle();
final boolean rtl = UIManager.getInstance().getLookAndFeel().isRTL();
if (requestFocus) {
instance.doNotHideTextEditorSemaphore++;
try {
instance.currentEditing.requestFocus();
} finally {
instance.doNotHideTextEditorSemaphore--;
}
}
x = cmp.getAbsoluteX() + cmp.getScrollX();
y = cmp.getAbsoluteY() + cmp.getScrollY();
w = cmp.getWidth();
h = cmp.getHeight();
int pt = stl.getPaddingTop();
int pb = stl.getPaddingBottom();
int pl = stl.getPaddingLeft(rtl);
int pr = stl.getPaddingRight(rtl);
/*
if(cmp.isSingleLineTextArea()) {
switch(cmp.getVerticalAlignment()) {
case TextArea.CENTER:
if(h > cmp.getPreferredH()) {
y += (h / 2 - cmp.getPreferredH() / 2);
}
break;
case TextArea.BOTTOM:
if(h > cmp.getPreferredH()) {
y += (h - cmp.getPreferredH());
}
break;
}
}
*/
Container contentPane = form.getContentPane();
if (!contentPane.contains(cmp)) {
contentPane = form;
}
Style contentPaneStyle = contentPane.getStyle();
int minY = contentPane.getAbsoluteY() + contentPane.getScrollY() + contentPaneStyle.getPaddingTop();
int maxH = Display.getInstance().getDisplayHeight() - minY - nativeInstance.getVKBHeight();
if (y < minY) {
h -= (minY - y);
y = minY;
}
if (h > maxH ) {
// For text areas, we don't want the keyboard to cover part of the
// typing region. So we will try to size the component to
// to only go up to the top edge of the keyboard
// that should allow the OS to enable scrolling properly.... at least
// in theory.
h = maxH;
}
if (h < 0) {
// There isn't room for the editor at all.
Log.p("No room for text editor. h="+h);
instance.stopTextEditing();
return;
}
if (x < 0 || y < 0 || w <= 0 || h <= 0) {
instance.stopTextEditing();
return;
}
nativeInstance.resizeNativeTextView(x,
y,
w,
h,
pt,
pr,
pb,
pl
);
}
}
boolean keyboardShowing;
/**
* Callback for native. Called when keyboard is shown. Used for async editing
* with formBottomPaddingEditingMode.
*/
static void keyboardWillBeShown(){
instance.keyboardShowing = true;
if(nativeInstance.isAsyncEditMode()) {
// revalidate the parent since the size of form is now larger due to the vkb
final Form current = Display.getInstance().getCurrent();
//final Component currentEditingFinal = instance.currentEditing;
if (current != null) {
if(current.isFormBottomPaddingEditingMode()) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (current != null) {
getRootPane(current).getUnselectedStyle().setPaddingUnit(new byte[] {Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS});
getRootPane(current).getUnselectedStyle().setPadding(Component.BOTTOM, nativeInstance.getVKBHeight());
current.revalidate();
Display.getInstance().callSerially(new Runnable() {
public void run() {
updateNativeTextEditorFrame();
}
});
}
}
});
} else {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (current != null) {
if (instance.currentEditing != null) {
instance.doNotHideTextEditorSemaphore++;
try {
instance.currentEditing.requestFocus();
} finally {
instance.doNotHideTextEditorSemaphore--;
}
current.revalidate();
Display.getInstance().callSerially(new Runnable() {
public void run() {
updateNativeTextEditorFrame();
}
});
}
}
}
});
}
}
}
Display.getInstance().fireVirtualKeyboardEvent(true);
}
/**
* Callback for native. Called when keyboard is hidden. Used for async editing
* with formBottomPaddingEditingMode.
*/
static void keyboardWillBeHidden(){
instance.keyboardShowing = false;
Display.getInstance().callSerially(new Runnable(){
@Override
public void run() {
Form current = Display.getInstance().getCurrent();
if (current != null) {
instance.areaUnderVKBOverride = 0;
try {
current.revalidate();
//Now that screen size is changed, the scroll positions may
// be caught in a negative state, leaving a gap at the
// top.
//https://github.com/codenameone/CodenameOne/issues/2476
Accessor.fixNegativeScrolls(current);
} finally {
instance.areaUnderVKBOverride = -1;
}
}
}
});
Display.getInstance().fireVirtualKeyboardEvent(false);
}
public void setCurrentForm(Form f) {
if (isEditingText()) {
stopTextEditing();
}
super.setCurrentForm(f);
}
@Override
public void afterComponentPaint(Component c, Graphics g) {
super.afterComponentPaint(c, g);
if (isEditingText(c)) {
updateNativeTextEditorFrame(false);
}
}
private static final Object EDITING_LOCK = new Object();
private static boolean editNext;
public void editString(final Component cmp, final int maxSize, final int constraint, final String text, final int i) {
// The very first time we try to edit a string, let's determine if the
// system default is to do async editing. If the system default
// is not yet set, we set it here, and it will be used as the default from now on
// We do this because the nativeInstance.isAsyncEditMode() value changes
// to reflect the currently edited field so it isn't a good way to keep a
// system default.
pendingEditingText = false;
String defaultAsyncEditingSetting = Display.getInstance().getProperty("ios.VKBAlwaysOpen", null);
if (defaultAsyncEditingSetting == null) {
defaultAsyncEditingSetting = nativeInstance.isAsyncEditMode() ? "true" : "false";
Display.getInstance().setProperty("ios.VKBAlwaysOpen", defaultAsyncEditingSetting);
}
boolean asyncEdit = "true".equals(defaultAsyncEditingSetting) ? true : false;
//Log.p("Application default for async editing is "+asyncEdit);
try {
if (currentEditing != cmp && currentEditing != null && currentEditing instanceof TextArea) {
Display.getInstance().onEditingComplete(currentEditing, ((TextArea)currentEditing).getText());
currentEditing = null;
callHideTextEditor();
if (nativeInstance.isAsyncEditMode()) {
nativeInstance.setNativeEditingComponentVisible(false);
}
synchronized(EDITING_LOCK) {
EDITING_LOCK.notify();
}
Display.getInstance().callSerially(new Runnable() {
public void run() {
pendingEditingText = true;
Display.getInstance().editString(cmp, maxSize, constraint, text, i);
}
});
return;
}
if(cmp.isFocusable() && !cmp.hasFocus()) {
doNotHideTextEditorSemaphore++;
try {
cmp.requestFocus();
} finally {
doNotHideTextEditorSemaphore--;
}
// Notice here that we are checking isAsyncEditMode() which looks
// at the previously edited text area. Not the async mode
// of our upcoming field.
if(isAsyncEditMode()) {
// flush the EDT so the focus will work...
Display.getInstance().callSerially(new Runnable() {
public void run() {
pendingEditingText = true;
Display.getInstance().editString(cmp, maxSize, constraint, text, i);
}
});
return;
}
}
// Check if the form has any setting for asyncEditing that should override
// the application defaults.
Form parentForm = cmp.getComponentForm();
if (parentForm == null) {
//Log.p("Attempt to edit text area that is not on a form. This is not supported");
return;
}
if (parentForm.getClientProperty("asyncEditing") != null) {
Object async = parentForm.getClientProperty("asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean)async).booleanValue();
//Log.p("Form overriding asyncEdit due to asyncEditing client property: "+asyncEdit);
}
}
if (parentForm.getClientProperty("ios.asyncEditing") != null) {
Object async = parentForm.getClientProperty("ios.asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean)async).booleanValue();
//Log.p("Form overriding asyncEdit due to ios.asyncEditing client property: "+asyncEdit);
}
}
// If the system default is to use async editing, we need to check
// the form to make sure that it is scrollable. If it is not
// scrollable, then this field should default to Non-async
// editing - and should instead revert to legacy editing mode.
if(asyncEdit && !parentForm.isFormBottomPaddingEditingMode()) {
Container p = cmp.getParent();
// A crude estimate of how far the component needs to be able to scroll to make
// async editing viable. We start with half-way down the screen.
int keyboardClippingThresholdY = Display.getInstance().getDisplayWidth() / 2;
while(p != null) {
if(Accessor.scrollableYFlag(p) && p.getAbsoluteY() < keyboardClippingThresholdY) {
break;
}
p = p.getParent();
}
// no scrollabel parent automatically configure the text field for legacy mode
//nativeInstance.setAsyncEditMode(p != null);
asyncEdit = p != null;
//Log.p("Overriding asyncEdit due to form scrollability: "+asyncEdit);
} else if (parentForm.isFormBottomPaddingEditingMode()){
// If form uses bottom padding mode, then we will always
// use async edit (unless the field explicitly overrides it).
asyncEdit = true;
//Log.p("Overriding asyncEdit due to form bottom padding edit mode: "+asyncEdit);
}
// If the field itself explicitly sets async editing behaviour
// then this will override all other settings.
if (cmp.getClientProperty("asyncEditing") != null) {
Object async = cmp.getClientProperty("asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean)async).booleanValue();
//Log.p("Overriding asyncEdit due to field asyncEditing client property: "+asyncEdit);
}
}
if (cmp.getClientProperty("ios.asyncEditing") != null) {
Object async = cmp.getClientProperty("ios.asyncEditing");
if (async instanceof Boolean) {
asyncEdit = ((Boolean)async).booleanValue();
//Log.p("Overriding asyncEdit due to field ios.asyncEditing client property: "+asyncEdit);
}
}
// Finally we set the async edit mode for this field.
//System.out.println("Async edit mode is "+asyncEdit);
nativeInstance.setAsyncEditMode(asyncEdit);
textEditorHidden = false;
currentEditing = (TextArea)cmp;
//register the edited TextArea to support moving to the next field
TextEditUtil.setCurrentEditComponent(cmp);
final NativeFont fnt = f(cmp.getStyle().getFont().getNativeFont());
boolean forceSlideUpTmp = false;
final Form current = Display.getInstance().getCurrent();
if(current instanceof Dialog && !isTablet()) {
// special case, if we are editing a small dialog we want to move it
// so the bottom of the dialog shows within the screen. This is
// described in issue 505
Dialog dlg = (Dialog)current;
Component c = dlg.getDialogComponent();
if(c.getHeight() < Display.getInstance().getDisplayHeight() / 2 &&
c.getAbsoluteY() + c.getHeight() > Display.getInstance().getDisplayHeight() / 2) {
forceSlideUpTmp = true;
}
}
final boolean forceSlideUp = forceSlideUpTmp;
cmp.repaint();
// give the repaint one cycle to "do its magic...
final Style stl = currentEditing.getStyle();
final boolean rtl = UIManager.getInstance().getLookAndFeel().isRTL();
final Style hintStyle = currentEditing.getHintLabel() != null ? currentEditing.getHintLabel().getStyle() : stl;
if (current != null) {
Component nextComponent = current.getNextComponent(cmp);
TextEditUtil.setNextEditComponent(nextComponent);
}
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
int x = cmp.getAbsoluteX() + cmp.getScrollX();
int y = cmp.getAbsoluteY() + cmp.getScrollY();
int w = cmp.getWidth();
int h = cmp.getHeight();
int pt = stl.getPaddingTop();
int pb = stl.getPaddingBottom();
int pl = stl.getPaddingLeft(rtl);
int pr = stl.getPaddingRight(rtl);
/*
if(currentEditing != null && currentEditing.isSingleLineTextArea()) {
switch(currentEditing.getVerticalAlignment()) {
case TextArea.CENTER:
if(h > cmp.getPreferredH()) {
y += (h / 2 - cmp.getPreferredH() / 2);
}
break;
case TextArea.BOTTOM:
if(h > cmp.getPreferredH()) {
y += (h - cmp.getPreferredH());
}
break;
}
}
*/
String hint = null;
if(currentEditing != null && currentEditing.getUIManager().isThemeConstant("nativeHintBool", true) && currentEditing.getHint() != null) {
hint = currentEditing.getHint();
}
int hintColor = hintStyle.getFgColor();