forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiViewProxy.java
1126 lines (982 loc) · 29 KB
/
TiViewProxy.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
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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package org.appcelerator.titanium.proxy;
import java.lang.ref.WeakReference;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.AsyncResult;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.util.TiAnimationBuilder;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUrl;
import org.appcelerator.titanium.view.TiAnimation;
import org.appcelerator.titanium.view.TiUIView;
import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
/**
* The parent class of view proxies.
*/
@Kroll.proxy(propertyAccessors={
// background properties
"backgroundImage", "backgroundRepeat", "backgroundSelectedImage",
"backgroundFocusedImage", "backgroundDisabledImage", "backgroundColor",
"backgroundSelectedColor", "backgroundFocusedColor", "backgroundDisabledColor",
"backgroundPadding", "backgroundGradient",
// border properties
"borderColor", "borderRadius", "borderWidth",
// layout / dimension (size/width/height have custom accessors)
"left", "top", "right", "bottom", "layout", "zIndex",
// accessibility
TiC.PROPERTY_ACCESSIBILITY_HINT, TiC.PROPERTY_ACCESSIBILITY_LABEL, TiC.PROPERTY_ACCESSIBILITY_VALUE,
TiC.PROPERTY_ACCESSIBILITY_HIDDEN,
// others
"focusable", "touchEnabled", "visible", "enabled", "opacity",
"softKeyboardOnFocus", "transform"
})
public abstract class TiViewProxy extends KrollProxy implements Handler.Callback
{
private static final String TAG = "TiViewProxy";
private static final int MSG_FIRST_ID = KrollProxy.MSG_LAST_ID + 1;
private static final int MSG_GETVIEW = MSG_FIRST_ID + 100;
private static final int MSG_ADD_CHILD = MSG_FIRST_ID + 102;
private static final int MSG_REMOVE_CHILD = MSG_FIRST_ID + 103;
private static final int MSG_BLUR = MSG_FIRST_ID + 104;
private static final int MSG_FOCUS = MSG_FIRST_ID + 105;
private static final int MSG_SHOW = MSG_FIRST_ID + 106;
private static final int MSG_HIDE = MSG_FIRST_ID + 107;
private static final int MSG_ANIMATE = MSG_FIRST_ID + 108;
private static final int MSG_TOIMAGE = MSG_FIRST_ID + 109;
private static final int MSG_GETSIZE = MSG_FIRST_ID + 110;
private static final int MSG_GETRECT = MSG_FIRST_ID + 111;
private static final int MSG_FINISH_LAYOUT = MSG_FIRST_ID + 112;
private static final int MSG_UPDATE_LAYOUT = MSG_FIRST_ID + 113;
private static final int MSG_TRANSFER_VIEW = MSG_FIRST_ID + 114;
protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;
protected ArrayList<TiViewProxy> children;
protected WeakReference<TiViewProxy> parent;
protected TiUIView view;
protected Object pendingAnimationLock;
protected TiAnimationBuilder pendingAnimation;
private boolean isDecorView = false;
// TODO: Deprecated since Release 3.0.0
@Deprecated private AtomicBoolean layoutStarted = new AtomicBoolean();
/**
* Constructs a new TiViewProxy instance.
* @module.api
*/
public TiViewProxy()
{
pendingAnimationLock = new Object();
defaultValues.put(TiC.PROPERTY_BACKGROUND_REPEAT, false);
}
@Override
public void handleCreationDict(KrollDict options)
{
options = handleStyleOptions(options);
super.handleCreationDict(options);
//TODO eventManager.addOnEventChangeListener(this);
}
private static HashMap<TiUrl,String> styleSheetUrlCache = new HashMap<TiUrl,String>(5);
protected String getBaseUrlForStylesheet()
{
TiUrl creationUrl = getCreationUrl();
if (styleSheetUrlCache.containsKey(creationUrl)) {
return styleSheetUrlCache.get(creationUrl);
}
String baseUrl = creationUrl.baseUrl;
if (baseUrl == null || (baseUrl.equals("app://") && creationUrl.url.equals(""))) {
baseUrl = "app://app.js";
} else {
baseUrl = creationUrl.resolve();
}
int idx = baseUrl.lastIndexOf("/");
if (idx != -1) {
baseUrl = baseUrl.substring(idx + 1).replace(".js", "");
}
styleSheetUrlCache.put(creationUrl,baseUrl);
return baseUrl;
}
protected KrollDict handleStyleOptions(KrollDict options)
{
String viewId = getProxyId();
TreeSet<String> styleClasses = new TreeSet<String>();
// TODO styleClasses.add(getShortAPIName().toLowerCase());
if (options.containsKey(TiC.PROPERTY_ID)) {
viewId = TiConvert.toString(options, TiC.PROPERTY_ID);
}
if (options.containsKey(TiC.PROPERTY_CLASS_NAME)) {
String className = TiConvert.toString(options, TiC.PROPERTY_CLASS_NAME);
for (String clazz : className.split(" ")) {
styleClasses.add(clazz);
}
}
if (options.containsKey(TiC.PROPERTY_CLASS_NAMES)) {
Object c = options.get(TiC.PROPERTY_CLASS_NAMES);
if (c.getClass().isArray()) {
int length = Array.getLength(c);
for (int i = 0; i < length; i++) {
Object clazz = Array.get(c, i);
if (clazz != null) {
styleClasses.add(clazz.toString());
}
}
}
}
String baseUrl = getBaseUrlForStylesheet();
KrollDict dict = TiApplication.getInstance().getStylesheet(baseUrl, styleClasses, viewId);
if (dict == null || dict.isEmpty()) {
return options;
}
extend(dict);
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "trying to get stylesheet for base:" + baseUrl + ",classes:" + styleClasses + ",id:" + viewId + ",dict:"
+ dict, Log.DEBUG_MODE);
}
// merge in our stylesheet details to the passed in dictionary
// our passed in dictionary takes precedence over the stylesheet
dict.putAll(options);
return dict;
}
public TiAnimationBuilder getPendingAnimation()
{
synchronized(pendingAnimationLock) {
return pendingAnimation;
}
}
public void clearAnimation(TiAnimationBuilder builder)
{
synchronized(pendingAnimationLock) {
if (pendingAnimation != null && pendingAnimation == builder) {
pendingAnimation = null;
}
}
}
//This handler callback is tied to the UI thread.
public boolean handleMessage(Message msg)
{
switch(msg.what) {
case MSG_TRANSFER_VIEW:{
handleTransfer((TiViewProxy) msg.obj);
return true;
}
case MSG_GETVIEW : {
AsyncResult result = (AsyncResult) msg.obj;
result.setResult(handleGetView());
return true;
}
case MSG_ADD_CHILD : {
AsyncResult result = (AsyncResult) msg.obj;
handleAdd((TiViewProxy) result.getArg());
result.setResult(null); //Signal added.
return true;
}
case MSG_REMOVE_CHILD : {
AsyncResult result = (AsyncResult) msg.obj;
handleRemove((TiViewProxy) result.getArg());
result.setResult(null); //Signal removed.
return true;
}
case MSG_BLUR : {
handleBlur();
return true;
}
case MSG_FOCUS : {
handleFocus();
return true;
}
case MSG_SHOW : {
handleShow((KrollDict) msg.obj);
return true;
}
case MSG_HIDE : {
handleHide((KrollDict) msg.obj);
return true;
}
case MSG_ANIMATE : {
handleAnimate();
return true;
}
case MSG_TOIMAGE: {
AsyncResult result = (AsyncResult) msg.obj;
result.setResult(handleToImage());
return true;
}
case MSG_GETSIZE : {
AsyncResult result = (AsyncResult) msg.obj;
KrollDict d = null;
d = new KrollDict();
d.put(TiC.PROPERTY_X, 0);
d.put(TiC.PROPERTY_Y, 0);
if (view != null) {
View v = view.getNativeView();
if (v != null) {
TiDimension nativeWidth = new TiDimension(v.getWidth(), TiDimension.TYPE_WIDTH);
TiDimension nativeHeight = new TiDimension(v.getHeight(), TiDimension.TYPE_HEIGHT);
// TiDimension needs a view to grab the window manager, so we'll just use the decorview of the current window
if (TiApplication.getAppCurrentActivity() != null && TiApplication.getAppCurrentActivity().getWindow() != null) {
View decorView = TiApplication.getAppCurrentActivity().getWindow().getDecorView();
d.put(TiC.PROPERTY_WIDTH, nativeWidth.getAsDefault(decorView));
d.put(TiC.PROPERTY_HEIGHT, nativeHeight.getAsDefault(decorView));
}
}
}
if (!d.containsKey(TiC.PROPERTY_WIDTH)) {
d.put(TiC.PROPERTY_WIDTH, 0);
d.put(TiC.PROPERTY_HEIGHT, 0);
}
result.setResult(d);
return true;
}
case MSG_GETRECT: {
AsyncResult result = (AsyncResult) msg.obj;
KrollDict d = null;
d = new KrollDict();
if (view != null) {
View v = view.getOuterView();
if (v != null) {
TiDimension nativeWidth = new TiDimension(v.getWidth(), TiDimension.TYPE_WIDTH);
TiDimension nativeHeight = new TiDimension(v.getHeight(), TiDimension.TYPE_HEIGHT);
TiDimension nativeLeft = new TiDimension(v.getLeft(), TiDimension.TYPE_LEFT);
TiDimension nativeTop = new TiDimension(v.getTop(), TiDimension.TYPE_TOP);
// TiDimension needs a view to grab the window manager, so we'll just use the decorview of the current window
View decorView = TiApplication.getAppCurrentActivity().getWindow().getDecorView();
d.put(TiC.PROPERTY_WIDTH, nativeWidth.getAsDefault(decorView));
d.put(TiC.PROPERTY_HEIGHT, nativeHeight.getAsDefault(decorView));
d.put(TiC.PROPERTY_X, nativeLeft.getAsDefault(decorView));
d.put(TiC.PROPERTY_Y, nativeTop.getAsDefault(decorView));
}
}
if (!d.containsKey(TiC.PROPERTY_WIDTH)) {
d.put(TiC.PROPERTY_WIDTH, 0);
d.put(TiC.PROPERTY_HEIGHT, 0);
d.put(TiC.PROPERTY_X, 0);
d.put(TiC.PROPERTY_Y, 0);
}
result.setResult(d);
return true;
}
case MSG_FINISH_LAYOUT : {
handleFinishLayout();
return true;
}
case MSG_UPDATE_LAYOUT : {
handleUpdateLayout((HashMap) msg.obj);
return true;
}
}
return super.handleMessage(msg);
}
/*
public Context getContext()
{
return getActivity();
}
*/
@Kroll.getProperty @Kroll.method
public KrollDict getRect()
{
return (KrollDict) TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_GETRECT), getActivity());
}
@Kroll.getProperty @Kroll.method
public KrollDict getSize()
{
return (KrollDict) TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_GETSIZE), getActivity());
}
@Kroll.getProperty @Kroll.method
public Object getWidth()
{
if (hasProperty(TiC.PROPERTY_WIDTH)) {
return getProperty(TiC.PROPERTY_WIDTH);
}
return KrollRuntime.UNDEFINED;
}
@Kroll.method
public void transferChild(TiViewProxy newProxy)
{
getMainHandler().obtainMessage(MSG_TRANSFER_VIEW, newProxy).sendToTarget();
}
@Kroll.setProperty(retain=false) @Kroll.method
public void setWidth(Object width)
{
setPropertyAndFire(TiC.PROPERTY_WIDTH, width);
}
@Kroll.getProperty @Kroll.method
public Object getHeight()
{
if (hasProperty(TiC.PROPERTY_HEIGHT)) {
return getProperty(TiC.PROPERTY_HEIGHT);
}
return KrollRuntime.UNDEFINED;
}
@Kroll.setProperty(retain=false) @Kroll.method
public void setHeight(Object height)
{
setPropertyAndFire(TiC.PROPERTY_HEIGHT, height);
}
@Kroll.getProperty @Kroll.method
public Object getCenter()
{
Object dict = KrollRuntime.UNDEFINED;
if (hasProperty(TiC.PROPERTY_CENTER)) {
dict = getProperty(TiC.PROPERTY_CENTER);
}
return dict;
}
public void clearView()
{
if (view != null) {
view.release();
}
view = null;
}
/**
* @return the TiUIView associated with this proxy.
* @module.api
*/
public TiUIView peekView()
{
return view;
}
public void setView(TiUIView view)
{
this.view = view;
}
public TiUIView forceCreateView()
{
view = null;
return getOrCreateView();
}
/**
* Transfer an existing view to this view proxy.
* Special use in tableView. Do not use anywhere else.
* Called from TiTableViewRowProxyItem.java
* @param transferview - The view to transfer
* @param oldProxy - The currentProxy of the view
*/
public void transferView(TiUIView transferview, TiViewProxy oldProxy) {
if(oldProxy != null) {
oldProxy.setView(null);
oldProxy.setModelListener(null);
}
view = transferview;
modelListener = transferview;
view.setProxy(this);
}
/**
* Creates or retrieves the view associated with this proxy.
* @return a TiUIView instance.
* @module.api
*/
public TiUIView getOrCreateView()
{
if (activity == null || view != null) {
return view;
}
if (TiApplication.isUIThread()) {
return handleGetView();
}
return (TiUIView) TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_GETVIEW), 0);
}
protected TiUIView handleGetView()
{
if (view == null) {
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "getView: " + getClass().getSimpleName(), Log.DEBUG_MODE);
}
Activity activity = getActivity();
view = createView(activity);
if (isDecorView) {
if (activity != null) {
((TiBaseActivity)activity).setViewProxy(view.getProxy());
} else {
Log.w(TAG, "Activity is null", Log.DEBUG_MODE);
}
}
realizeViews(view);
view.registerForTouch();
view.registerForKeyPress();
}
return view;
}
public void realizeViews(TiUIView view)
{
setModelListener(view);
// Use a copy so bundle can be modified as it passes up the inheritance
// tree. Allows defaults to be added and keys removed.
if (children != null) {
try {
for (TiViewProxy p : children) {
TiUIView cv = p.getOrCreateView();
view.add(cv);
}
} catch (ConcurrentModificationException e) {
Log.e(TAG, e.getMessage(), e);
}
}
synchronized(pendingAnimationLock) {
if (pendingAnimation != null) {
handlePendingAnimation(true);
}
}
}
public void releaseViews()
{
if (view != null) {
if (children != null) {
for (TiViewProxy p : children) {
p.releaseViews();
}
}
view.release();
view = null;
}
setModelListener(null);
KrollRuntime.suggestGC();
}
/**
* Implementing classes should use this method to create and return the appropriate view.
* @param activity the context activity.
* @return a TiUIView instance.
* @module.api
*/
public abstract TiUIView createView(Activity activity);
/**
* Adds a child to this view proxy.
* @param child The child view proxy to add.
* @module.api
*/
@Kroll.method
public void add(TiViewProxy child)
{
if (child == null) {
Log.e(TAG, "Add called with a null child");
return;
}
if (children == null) {
children = new ArrayList<TiViewProxy>();
}
if (peekView() != null) {
if (TiApplication.isUIThread()) {
handleAdd(child);
return;
}
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_ADD_CHILD), child);
} else {
children.add(child);
child.parent = new WeakReference<TiViewProxy>(this);
}
//TODO zOrder
}
private void handleTransfer(TiViewProxy newProxy){
TiViewProxy oldParent = newProxy.getParent();
oldParent.handleRemoveNotDestructive(newProxy);
add(newProxy);
}
public void handleAdd(TiViewProxy child)
{
children.add(child);
child.parent = new WeakReference<TiViewProxy>(this);
if (view != null) {
child.setActivity(getActivity());
if (this instanceof DecorViewProxy) {
child.isDecorView = true;
}
TiUIView cv = child.getOrCreateView();
view.add(cv);
}
}
/**
* Removes a view from this view proxy, releasing the underlying native view if it exists.
* @param child The child to remove.
* @module.api
*/
@Kroll.method
public void remove(TiViewProxy child)
{
if (child == null) {
Log.e(TAG, "Add called with null child");
return;
}
if (peekView() != null) {
if (TiApplication.isUIThread()) {
handleRemove(child);
return;
}
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_REMOVE_CHILD), child);
} else {
if (children != null) {
children.remove(child);
if (child.parent != null && child.parent.get() == this) {
child.parent = null;
}
}
}
}
/**
* Removes all children views.
* @module.api
*/
@Kroll.method
public void removeAllChildren()
{
if (children != null) {
//children might be altered while we loop through it (threading)
//so we first copy children as it was when asked to remove all children
ArrayList<TiViewProxy> childViews = new ArrayList<TiViewProxy>();
childViews.addAll(children);
for (TiViewProxy child : childViews) {
remove(child);
}
}
}
public void handleRemoveNotDestructive(TiViewProxy child)
{
if (children != null) {
children.remove(child);
if (view != null) {
view.remove(child.peekView());
}
}
}
public void handleRemove(TiViewProxy child)
{
if (children != null) {
children.remove(child);
if (view != null) {
view.remove(child.peekView());
}
if (child != null) {
child.releaseViews();
}
}
}
@Kroll.method
public void show(@Kroll.argument(optional=true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleShow(options);
} else {
getMainHandler().obtainMessage(MSG_SHOW, options).sendToTarget();
}
}
protected void handleShow(KrollDict options)
{
if (view != null) {
view.show();
setProperty(TiC.PROPERTY_VISIBLE, true);
}
}
@Kroll.method
public void hide(@Kroll.argument(optional=true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleHide(options);
} else {
getMainHandler().obtainMessage(MSG_HIDE, options).sendToTarget();
}
}
protected void handleHide(KrollDict options)
{
if (view != null) {
synchronized(pendingAnimationLock) {
if (pendingAnimation != null) {
handlePendingAnimation(false);
}
}
view.hide();
setProperty(TiC.PROPERTY_VISIBLE, false);
}
}
@Kroll.method
public void animate(Object arg, @Kroll.argument(optional=true) KrollFunction callback)
{
synchronized (pendingAnimationLock) {
if (arg instanceof HashMap) {
HashMap options = (HashMap) arg;
pendingAnimation = new TiAnimationBuilder();
pendingAnimation.applyOptions(options);
} else if (arg instanceof TiAnimation) {
TiAnimation anim = (TiAnimation) arg;
pendingAnimation = new TiAnimationBuilder();
pendingAnimation.applyAnimation(anim);
} else {
throw new IllegalArgumentException("Unhandled argument to animate: " + arg.getClass().getSimpleName());
}
if (callback != null) {
pendingAnimation.setCallback(callback);
}
handlePendingAnimation(false);
}
}
public void handlePendingAnimation(boolean forceQueue)
{
if (pendingAnimation != null && peekView() != null) {
if (forceQueue || !(TiApplication.isUIThread())) {
if (Build.VERSION.SDK_INT < TiC.API_LEVEL_HONEYCOMB) {
// Even this very small delay can help eliminate the bug
// whereby the animated view's parent suddenly becomes
// transparent (pre-honeycomb). cf. TIMOB-9813.
getMainHandler().sendEmptyMessageDelayed(MSG_ANIMATE, 10);
} else {
getMainHandler().sendEmptyMessage(MSG_ANIMATE);
}
} else {
handleAnimate();
}
}
}
protected void handleAnimate()
{
TiUIView tiv = peekView();
if (tiv != null) {
tiv.animate();
}
}
@Kroll.method
public void blur()
{
if (TiApplication.isUIThread()) {
handleBlur();
} else {
getMainHandler().sendEmptyMessage(MSG_BLUR);
}
}
protected void handleBlur()
{
if (view != null) {
view.blur();
}
}
@Kroll.method
public void focus()
{
if (TiApplication.isUIThread()) {
handleFocus();
} else {
getMainHandler().sendEmptyMessage(MSG_FOCUS);
}
}
protected void handleFocus()
{
if (view != null) {
view.focus();
}
}
@Kroll.method
public KrollDict toImage()
{
if (TiApplication.isUIThread()) {
return handleToImage();
} else {
return (KrollDict) TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_TOIMAGE), getActivity());
}
}
protected KrollDict handleToImage()
{
TiUIView view = getOrCreateView();
if (view == null) {
return null;
}
return view.toImage();
}
/**
* Fires an event that can optionally be "bubbled" to the parent view.
*
* @param eventName event to get dispatched to listeners
* @param data data to include in the event
* @param bubbles if true will send the event to the parent view after it has been dispatched to this view's listeners.
* @return true if the event was handled
*/
public boolean fireEvent(String eventName, Object data, boolean bubbles)
{
if (data == null) {
data = new KrollDict();
}
// Set the "bubbles" property to indicate if the event needs to be bubbled.
if (data instanceof HashMap) {
((HashMap)data).put(TiC.PROPERTY_BUBBLES, bubbles);
}
// Dispatch the event to JavaScript which takes care of the bubbling.
return super.fireEvent(eventName, data);
}
/**
* Fires an event that will be bubbled to the parent view.
*/
@Override
public boolean fireEvent(String eventName, Object data)
{
// To remain compatible this override of fireEvent will always
// bubble the event to the parent view. It should eventually be deprecated
// in favor of using the fireEvent(String, Object, boolean) method.
return fireEvent(eventName, data, true);
}
/**
* @return The parent view proxy of this view proxy.
* @module.api
*/
@Kroll.getProperty @Kroll.method
public TiViewProxy getParent()
{
if (this.parent == null) {
return null;
}
return this.parent.get();
}
public void setParent(TiViewProxy parent)
{
if (parent == null) {
this.parent = null;
return;
}
this.parent = new WeakReference<TiViewProxy>(parent);
}
@Override
public KrollProxy getParentForBubbling()
{
return getParent();
}
@Override
public void setActivity(Activity activity)
{
super.setActivity(activity);
if (children != null) {
for (TiViewProxy child : children) {
child.setActivity(activity);
}
}
}
/**
* @return An array of the children view proxies of this view.
* @module.api
*/
@Kroll.getProperty @Kroll.method
public TiViewProxy[] getChildren()
{
if (children == null) return new TiViewProxy[0];
return children.toArray(new TiViewProxy[children.size()]);
}
@Override
public void eventListenerAdded(String eventName, int count, KrollProxy proxy)
{
super.eventListenerAdded(eventName, count, proxy);
if (eventName.equals(TiC.EVENT_CLICK) && proxy.equals(this) && count == 1 && !(proxy instanceof TiWindowProxy)) {
if (!proxy.hasProperty(TiC.PROPERTY_TOUCH_ENABLED)
|| TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED))) {
setClickable(true);
}
}
}
@Override
public void eventListenerRemoved(String eventName, int count, KrollProxy proxy)
{
super.eventListenerRemoved(eventName, count, proxy);
if (eventName.equals(TiC.EVENT_CLICK) && count == 0 && proxy.equals(this) && !(proxy instanceof TiWindowProxy)) {
if (proxy.hasProperty(TiC.PROPERTY_TOUCH_ENABLED)
&& !TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED))) {
setClickable(false);
}
}
}
public void setClickable(boolean clickable)
{
TiUIView v = peekView();
if (v != null) {
View nv = v.getNativeView();
if (nv != null) {
nv.setClickable(clickable);
}
}
}
@Kroll.method
public void addClass(Object[] classNames)
{
// This is a pretty naive implementation right now,
// but it will work for our current needs
String baseUrl = getBaseUrlForStylesheet();
ArrayList<String> classes = new ArrayList<String>();
for (Object c : classNames) {
classes.add(TiConvert.toString(c));
}
KrollDict options = TiApplication.getInstance().getStylesheet(baseUrl, classes, null);
extend(options);
}
@Kroll.method @Kroll.getProperty
public boolean getKeepScreenOn()
{
Boolean keepScreenOn = null;
TiUIView v = peekView();
if (v != null) {
View nv = v.getNativeView();
if (nv != null) {
keepScreenOn = nv.getKeepScreenOn();
}
}
//Keep the proxy in the correct state
Object current = getProperty(TiC.PROPERTY_KEEP_SCREEN_ON);
if (current != null) {
boolean currentValue = TiConvert.toBoolean(current);
if (keepScreenOn == null) {
keepScreenOn = currentValue;
} else {
if (currentValue != keepScreenOn) {
setProperty(TiC.PROPERTY_KEEP_SCREEN_ON, keepScreenOn);
} else {
keepScreenOn = currentValue;
}
}
} else {
if (keepScreenOn == null) {
keepScreenOn = false; // Android default
}
setProperty(TiC.PROPERTY_KEEP_SCREEN_ON, keepScreenOn);
}
return keepScreenOn;
}
@Kroll.method @Kroll.setProperty(retain=false)
public void setKeepScreenOn(boolean keepScreenOn)
{
setPropertyAndFire(TiC.PROPERTY_KEEP_SCREEN_ON, keepScreenOn);
}
@Kroll.method
public KrollDict convertPointToView(KrollDict point, TiViewProxy dest)
{
if (point == null) {
throw new IllegalArgumentException("convertPointToView: point must not be null");
}
if (dest == null) {
throw new IllegalArgumentException("convertPointToView: destinationView must not be null");
}
if (!point.containsKey(TiC.PROPERTY_X)) {
throw new IllegalArgumentException("convertPointToView: required property \"x\" not found in point");
}
if (!point.containsKey(TiC.PROPERTY_Y)) {
throw new IllegalArgumentException("convertPointToView: required property \"y\" not found in point");
}