/
TiUIView.java
2166 lines (1924 loc) Β· 66.1 KB
/
TiUIView.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-2014 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.view;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollObject;
import org.appcelerator.kroll.KrollPropertyChange;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.KrollProxyListener;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiAnimationBuilder;
import org.appcelerator.titanium.util.TiAnimationBuilder.TiMatrixAnimation;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutParams;
import org.appcelerator.titanium.view.TiGradientDrawable.GradientType;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.Pair;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import com.nineoldandroids.view.ViewHelper;
/**
* This class is for Titanium View implementations, that correspond with TiViewProxy.
* A TiUIView is responsible for creating and maintaining a native Android View instance.
*/
@SuppressWarnings("deprecation")
public abstract class TiUIView
implements KrollProxyListener, OnFocusChangeListener
{
private static final boolean HONEYCOMB_OR_GREATER = (Build.VERSION.SDK_INT >= 11);
private static final boolean LOLLIPOP_OR_GREATER = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
private static final boolean LOWER_THAN_JELLYBEAN = (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2);
private static final int LAYER_TYPE_SOFTWARE = 1;
private static final String TAG = "TiUIView";
private static AtomicInteger idGenerator;
// When distinguishing twofingertap and pinch events, minimum motion (in pixels)
// to qualify as a scale event.
private static final float SCALE_THRESHOLD = 6.0f;
public static final int SOFT_KEYBOARD_DEFAULT_ON_FOCUS = 0;
public static final int SOFT_KEYBOARD_HIDE_ON_FOCUS = 1;
public static final int SOFT_KEYBOARD_SHOW_ON_FOCUS = 2;
public static final int TRANSITION_NONE = 0;
public static final int TRANSITION_EXPLODE = 1;
public static final int TRANSITION_FADE_IN = 2;
public static final int TRANSITION_FADE_OUT = 3;
public static final int TRANSITION_SLIDE_TOP = 4;
public static final int TRANSITION_SLIDE_RIGHT = 5;
public static final int TRANSITION_SLIDE_BOTTOM = 6;
public static final int TRANSITION_SLIDE_LEFT = 7;
public static final int TRANSITION_CHANGE_BOUNDS = 8;
public static final int TRANSITION_CHANGE_CLIP_BOUNDS = 9;
public static final int TRANSITION_CHANGE_TRANSFORM = 10;
public static final int TRANSITION_CHANGE_IMAGE_TRANSFORM = 11;
protected View nativeView; // Native View object
protected TiViewProxy proxy;
protected TiViewProxy parent;
protected ArrayList<TiUIView> children = new ArrayList<TiUIView>();
protected LayoutParams layoutParams;
protected TiAnimationBuilder animBuilder;
protected TiBackgroundDrawable background;
protected KrollDict additionalEventData;
// Since Android doesn't have a property to check to indicate
// the current animated x/y scale (from a scale animation), we track it here
// so if another scale animation is done we can gleen the fromX and fromY values
// rather than starting the next animation always from scale 1.0f (i.e., normal scale).
// This gives us parity with iPhone for scale animations that use the 2-argument variant
// of Ti2DMatrix.scale().
private Pair<Float, Float> animatedScaleValues = Pair.create(Float.valueOf(1f), Float.valueOf(1f)); // default = full size (1f)
// Same for rotation animation and for alpha animation.
private float animatedRotationDegrees = 0f; // i.e., no rotation.
private float animatedAlpha = Float.MIN_VALUE; // i.e., no animated alpha.
protected KrollDict lastUpEvent = new KrollDict(2);
// In the case of heavy-weight windows, the "nativeView" is null,
// so this holds a reference to the view which is used for touching,
// i.e., the view passed to registerForTouch.
private WeakReference<View> touchView = null;
private Method mSetLayerTypeMethod = null; // Honeycomb, for turning off hw acceleration.
private boolean zIndexChanged = false;
private TiBorderWrapperView borderView;
// For twofingertap detection
private boolean didScale = false;
//to maintain sync visibility between borderview and view. Default is visible
private int visibility = View.VISIBLE;
private int hiddenBehavior = View.INVISIBLE;
protected GestureDetector detector = null;
private AtomicBoolean bLayoutPending = new AtomicBoolean();
private AtomicBoolean bTransformPending = new AtomicBoolean();
/**
* Constructs a TiUIView object with the associated proxy.
* @param proxy the associated proxy.
* @module.api
*/
public TiUIView(TiViewProxy proxy)
{
if (idGenerator == null) {
idGenerator = new AtomicInteger(0);
}
this.proxy = proxy;
this.layoutParams = new TiCompositeLayout.LayoutParams();
}
/**
* Adds a child view into the ViewGroup.
* @param child the view to be added.
*/
public void add(TiUIView child)
{
add(child, -1);
}
/**
* Adds a child view into the ViewGroup in specific position.
* @param child the view to be added.
* @param position position the view to be added.
*/
public void insertAt(TiUIView child, int position)
{
add(child, position);
}
private void add(TiUIView child, int childIndex)
{
if (child != null) {
View cv = child.getOuterView();
if (cv != null) {
View nv = getNativeView();
if (nv instanceof ViewGroup) {
if (cv.getParent() == null) {
if (childIndex != -1) {
((ViewGroup) nv).addView(cv, childIndex, child.getLayoutParams());
} else {
((ViewGroup) nv).addView(cv, child.getLayoutParams());
}
}
if(children.contains(child)) {
children.remove(child);
}
if(childIndex == -1) {
children.add(child);
} else {
children.add(childIndex, child);
}
child.parent = proxy;
}
}
}
}
private int findChildIndex(TiUIView child)
{
int idxChild = -1;
if (child != null) {
View cv = child.getOuterView();
if (cv != null) {
View nv = getNativeView();
if (nv instanceof ViewGroup) {
idxChild = ((ViewGroup) nv).indexOfChild(cv);
}
}
}
return idxChild;
}
/**
* Removes the child view from the ViewGroup, if child exists.
* @param child the view to be removed.
*/
public void remove(TiUIView child)
{
if (child != null) {
View cv = child.getOuterView();
if (cv != null) {
View nv = getNativeView();
if (nv instanceof ViewGroup) {
((ViewGroup) nv).removeView(cv);
children.remove(child);
child.parent = null;
}
}
}
}
public void setAdditionalEventData(KrollDict dict) {
additionalEventData = dict;
}
public KrollDict getAdditionalEventData() {
return additionalEventData;
}
/**
* @return list of views added.
*/
public List<TiUIView> getChildren()
{
return children;
}
/**
* @return the view proxy.
* @module.api
*/
public TiViewProxy getProxy()
{
return proxy;
}
/**
* Sets the view proxy.
* @param proxy the proxy to set.
* @module.api
*/
public void setProxy(TiViewProxy proxy)
{
this.proxy = proxy;
}
public TiViewProxy getParent()
{
return parent;
}
public void setParent(TiViewProxy parent)
{
this.parent = parent;
}
/**
* @return the view's layout params.
* @module.api
*/
public LayoutParams getLayoutParams()
{
return layoutParams;
}
/**
* @return the Android native view.
* @module.api
*/
public View getNativeView()
{
return nativeView;
}
/**
* Sets the nativeView to view.
* @param view the view to set
* @module.api
*/
protected void setNativeView(View view)
{
if (view.getId() == View.NO_ID) {
view.setId(idGenerator.incrementAndGet());
}
this.nativeView = view;
boolean clickable = true;
if (proxy.hasProperty(TiC.PROPERTY_TOUCH_ENABLED)) {
clickable = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED), true);
}
doSetClickable(nativeView, clickable);
nativeView.setOnFocusChangeListener(this);
applyAccessibilityProperties();
}
protected void setLayoutParams(LayoutParams layoutParams)
{
this.layoutParams = layoutParams;
}
/**
* Animates the view if there are pending animations.
*/
public void animate()
{
View outerView = getOuterView();
if (outerView == null || bTransformPending.get()) {
return;
}
// Pre-honeycomb, if one animation clobbers another you get a problem whereby the background of the
// animated view's parent (or the grandparent) bleeds through. It seems to improve if you cancel and clear
// the older animation. So here we cancel and clear, then re-queue the desired animation.
if (Build.VERSION.SDK_INT < TiC.API_LEVEL_HONEYCOMB) {
Animation currentAnimation = outerView.getAnimation();
if (currentAnimation != null && currentAnimation.hasStarted() && !currentAnimation.hasEnded()) {
// Cancel existing animation and
// re-queue desired animation.
currentAnimation.cancel();
outerView.clearAnimation();
proxy.handlePendingAnimation(true);
return;
}
}
TiAnimationBuilder builder = proxy.getPendingAnimation();
if (builder == null) {
return;
}
proxy.clearAnimation(builder);
// If a view is "visible" but not currently seen (such as because it's covered or
// its position is currently set to be fully outside its parent's region),
// then Android might not animate it immediately because by default it animates
// "on first frame" and apparently "first frame" won't happen right away if the
// view has no visible rectangle on screen. In that case invalidate its parent, which will
// kick off the pending animation.
boolean invalidateParent = false;
ViewParent viewParent = outerView.getParent();
if (this.visibility == View.VISIBLE && viewParent instanceof View) {
int width = outerView.getWidth();
int height = outerView.getHeight();
if (width == 0 || height == 0) {
// Could be animating from nothing to something
invalidateParent = true;
} else {
Rect r = new Rect(0, 0, width, height);
Point p = new Point(0, 0);
invalidateParent = !(viewParent.getChildVisibleRect(outerView, r, p));
}
}
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "starting animation", Log.DEBUG_MODE);
}
builder.start(proxy, outerView);
if (invalidateParent) {
((View) viewParent).postInvalidate();
}
}
public void listenerAdded(String type, int count, KrollProxy proxy) {
}
public void listenerRemoved(String type, int count, KrollProxy proxy){
}
private boolean hasImage(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_IMAGE)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_IMAGE)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_FOCUSED_IMAGE)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_DISABLED_IMAGE);
}
private boolean hasRepeat(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_REPEAT);
}
private boolean hasGradient(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_GRADIENT);
}
private boolean hasBorder(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_COLOR)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_RADIUS)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_WIDTH);
}
private boolean hasColorState(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_FOCUSED_COLOR)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_FOCUSED_COLOR);
}
public float[] getPreTranslationValue(float[] points)
{
if (layoutParams.optionTransform != null) {
TiMatrixAnimation matrixAnimation = animBuilder.createMatrixAnimation(layoutParams.optionTransform);
int width = getNativeView().getWidth();
int height = getNativeView().getHeight();
Matrix m = matrixAnimation.getFinalMatrix(width, height);
// Get the translation values
float[] values = new float[9];
m.getValues(values);
points[0] = points[0] - values[2];
points[1] = points[1] - values[5];
}
return points;
}
protected void applyTransform(Ti2DMatrix matrix)
{
layoutParams.optionTransform = matrix;
if (animBuilder == null) {
animBuilder = new TiAnimationBuilder();
}
View outerView = getOuterView();
if (outerView == null) {
return;
}
boolean clearTransform = (matrix == null);
Ti2DMatrix matrixApply = matrix; // To not change original.
if (clearTransform) {
outerView.clearAnimation();
// Since we may have used property animators, which
// do not set the animation property of a view,
// we should also quickly apply a matrix with
// no rotation, no rotation and scale of 1.
matrixApply = (new Ti2DMatrix()).rotate(new Object[] { 0d })
.translate(0d, 0d).scale(new Object[] { 1d, 1d });
}
HashMap<String, Object> options = new HashMap<String, Object>(2);
options.put(TiC.PROPERTY_TRANSFORM, matrixApply);
options.put(TiC.PROPERTY_DURATION, 1);
animBuilder.applyOptions(options);
// When using Honeycomb+ property Animators, we can only use absolute values to specify the anchor point, eg. "50px".
// Therefore, we must start the transformation after the layout pass when we get the height and width of the view.
if (animBuilder.isUsingPropertyAnimators()) {
startTransformAfterLayout(outerView);
//If the layout already done, the above call won't trigger the change, force layout change.
outerView.requestLayout();
} else {
animBuilder.start(this.proxy, outerView);
}
}
/**
* When using Honeycomb+ property Animators, we start the transformation after the layout pass.
* @param v the view to animate
*/
protected void startTransformAfterLayout(final View v)
{
final TiViewProxy p = this.proxy;
bTransformPending.set(true);
OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
public void onGlobalLayout()
{
animBuilder.setCallback(new KrollFunction() {
public Object call(KrollObject krollObject, HashMap args) {
return null;
}
public Object call(KrollObject krollObject, Object[] args) {
return null;
}
public void callAsync(KrollObject krollObject, HashMap args) {}
public void callAsync(KrollObject krollObject, Object[] args) {
bTransformPending.set(false);
proxy.handlePendingAnimation(true);
}
});
animBuilder.start(p, v);
try {
if (Build.VERSION.SDK_INT < TiC.API_LEVEL_JELLY_BEAN) {
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
} catch (IllegalStateException e) {
if (Log.isDebugModeEnabled()) {
Log.w(TAG, "Unable to remove the OnGlobalLayoutListener.", e.getMessage());
}
}
}
};
v.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
// On Jelly Bean+, the view will visibly transform if the transformation starts after the layout pass,
// so we add OnPreDrawListener to skip the drawing pass before the animation is ended.
// This mechanism only works for Honeycomb+ property Animators. Because if we use pre-Honeycomb view
// animations and skip the drawing pass, the AnimationListener will not be triggered so
// TiAnimationBuilder.isAnimationRunningFor(view) will always return true.
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_JELLY_BEAN) {
final OnPreDrawListener preDrawListener = new OnPreDrawListener()
{
public boolean onPreDraw()
{
if (TiAnimationBuilder.isAnimationRunningFor(v)) {
// Skip the current drawing pass.
return false;
}
try {
v.getViewTreeObserver().removeOnPreDrawListener(this);
} catch (IllegalStateException e) {
if (Log.isDebugModeEnabled()) {
Log.w(TAG, "Unable to remove the OnPreDrawListener.", e.getMessage());
}
}
return true;
}
};
v.getViewTreeObserver().addOnPreDrawListener(preDrawListener);
}
}
public void forceLayoutNativeView(boolean informParent)
{
layoutNativeView(informParent);
}
protected void layoutNativeView()
{
layoutNativeView(false);
}
public boolean isLayoutPending()
{
return bLayoutPending.get();
}
protected void layoutNativeView(boolean informParent)
{
if (nativeView != null) {
Animation a = nativeView.getAnimation();
if (a != null && a instanceof TiMatrixAnimation) {
TiMatrixAnimation matrixAnimation = (TiMatrixAnimation) a;
matrixAnimation.invalidateWithMatrix(nativeView);
}
if (informParent) {
if (parent != null) {
TiUIView uiv = parent.peekView();
if (uiv != null) {
uiv.resort();
}
}
}
final View v = getOuterView();
if (v != null) {
bLayoutPending.set(true);
OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
bLayoutPending.set(false);
try {
if (Build.VERSION.SDK_INT < TiC.API_LEVEL_JELLY_BEAN) {
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
} catch (IllegalStateException e) {
if (Log.isDebugModeEnabled()) {
Log.w(TAG, "Unable to remove the OnGlobalLayoutListener.", e.getMessage());
}
}
}
};
v.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
}
nativeView.requestLayout();
}
}
public void resort()
{
View v = getNativeView();
if (v instanceof TiCompositeLayout) {
((TiCompositeLayout) v).resort();
}
}
public boolean iszIndexChanged()
{
return zIndexChanged;
}
public void setzIndexChanged(boolean zIndexChanged)
{
this.zIndexChanged = zIndexChanged;
}
/**
* On Honeycomb+ devices, we use property animations, which may affect
* translation values. We need to reset translationX when 'left', 'right'
* or 'center' property is changed.
*/
private void resetTranslationX() {
if (HONEYCOMB_OR_GREATER && nativeView != null) {
nativeView.setTranslationX(0);
}
}
/**
* On Honeycomb+ devices, we use property animations, which may affect
* translation values. We need to reset translationX when 'top', 'bottom'
* or 'center' property is changed.
*/
private void resetTranslationY() {
if (HONEYCOMB_OR_GREATER && nativeView != null) {
nativeView.setTranslationY(0);
}
}
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{
if (key.equals(TiC.PROPERTY_LEFT)) {
resetPostAnimationValues();
resetTranslationX();
if (newValue != null) {
layoutParams.optionLeft = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_LEFT);
} else {
layoutParams.optionLeft = null;
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_TOP)) {
resetPostAnimationValues();
resetTranslationY();
if (newValue != null) {
layoutParams.optionTop = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_TOP);
} else {
layoutParams.optionTop = null;
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_CENTER)) {
resetPostAnimationValues();
resetTranslationX();
resetTranslationY();
TiConvert.updateLayoutCenter(newValue, layoutParams);
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_RIGHT)) {
resetPostAnimationValues();
resetTranslationX();
if (newValue != null) {
layoutParams.optionRight = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_RIGHT);
} else {
layoutParams.optionRight = null;
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_BOTTOM)) {
resetPostAnimationValues();
resetTranslationY();
if (newValue != null) {
layoutParams.optionBottom = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_BOTTOM);
} else {
layoutParams.optionBottom = null;
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_SIZE)) {
if (newValue instanceof HashMap) {
@SuppressWarnings("unchecked")
HashMap<String, Object> d = (HashMap<String, Object>) newValue;
propertyChanged(TiC.PROPERTY_WIDTH, oldValue, d.get(TiC.PROPERTY_WIDTH), proxy);
propertyChanged(TiC.PROPERTY_HEIGHT, oldValue, d.get(TiC.PROPERTY_HEIGHT), proxy);
}else if (newValue != null){
Log.w(TAG, "Unsupported property type ("+(newValue.getClass().getSimpleName())+") for key: " + key+". Must be an object/dictionary");
}
} else if (key.equals(TiC.PROPERTY_HEIGHT)) {
resetPostAnimationValues();
if (newValue != null) {
layoutParams.optionHeight = null;
layoutParams.sizeOrFillHeightEnabled = true;
if (newValue.equals(TiC.LAYOUT_SIZE)) {
layoutParams.autoFillsHeight = false;
} else if (newValue.equals(TiC.LAYOUT_FILL)) {
layoutParams.autoFillsHeight = true;
} else if (!newValue.equals(TiC.SIZE_AUTO)) {
layoutParams.optionHeight = TiConvert.toTiDimension(TiConvert.toString(newValue),
TiDimension.TYPE_HEIGHT);
layoutParams.sizeOrFillHeightEnabled = false;
}
} else {
layoutParams.optionHeight = null;
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_HORIZONTAL_WRAP)) {
if (nativeView instanceof TiCompositeLayout) {
((TiCompositeLayout) nativeView).setEnableHorizontalWrap(TiConvert.toBoolean(newValue,true));
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_WIDTH)) {
resetPostAnimationValues();
if (newValue != null) {
layoutParams.optionWidth = null;
layoutParams.sizeOrFillWidthEnabled = true;
if (newValue.equals(TiC.LAYOUT_SIZE)) {
layoutParams.autoFillsWidth = false;
} else if (newValue.equals(TiC.LAYOUT_FILL)) {
layoutParams.autoFillsWidth = true;
} else if (!newValue.equals(TiC.SIZE_AUTO)) {
layoutParams.optionWidth = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_WIDTH);
layoutParams.sizeOrFillWidthEnabled = false;
}
} else {
layoutParams.optionWidth = null;
}
layoutNativeView();
} else if (key.equals(TiC.PROPERTY_ZINDEX)) {
if (newValue != null) {
layoutParams.optionZIndex = TiConvert.toInt(newValue);
} else {
layoutParams.optionZIndex = 0;
}
layoutNativeView(true);
} else if (key.equals(TiC.PROPERTY_FOCUSABLE) && newValue != null) {
registerForKeyPress(nativeView, TiConvert.toBoolean(newValue, false));
} else if (key.equals(TiC.PROPERTY_TOUCH_ENABLED)) {
nativeView.setEnabled(TiConvert.toBoolean(newValue));
doSetClickable(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_VISIBLE)) {
newValue = (newValue == null) ? false : newValue;
this.setVisibility(TiConvert.toBoolean(newValue) ? View.VISIBLE : View.INVISIBLE);
} else if (key.equals(TiC.PROPERTY_ENABLED)) {
nativeView.setEnabled(TiConvert.toBoolean(newValue));
} else if (key.startsWith(TiC.PROPERTY_BACKGROUND_PADDING)) {
Log.i(TAG, key + " not yet implemented.");
} else if (key.equals(TiC.PROPERTY_OPACITY)
|| key.equals(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR)
|| key.equals(TiC.PROPERTY_TOUCH_FEEDBACK)
|| key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX)
|| key.startsWith(TiC.PROPERTY_BORDER_PREFIX)) {
// Update first before querying.
proxy.setProperty(key, newValue);
KrollDict d = proxy.getProperties();
boolean hasImage = hasImage(d);
boolean hasRepeat = hasRepeat(d);
boolean hasColorState = hasColorState(d);
boolean hasBorder = hasBorder(d);
boolean hasGradient = hasGradient(d);
boolean nativeViewNull = (nativeView == null);
boolean requiresCustomBackground = hasImage || hasColorState || hasBorder || hasGradient;
// PROPERTY_BACKGROUND_REPEAT is implicitly passed as false though not used in JS. So check the truth value and proceed.
if (!requiresCustomBackground) {
requiresCustomBackground = requiresCustomBackground && d.optBoolean(TiC.PROPERTY_BACKGROUND_REPEAT, false);
}
if (!requiresCustomBackground) {
if (background != null) {
background.releaseDelegate();
background.setCallback(null);
background = null;
}
if (d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_COLOR)) {
Integer bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR);
if (!nativeViewNull) {
if (canApplyTouchFeedback(d)) {
applyTouchFeedback(bgColor, d.containsKey(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR) ? TiConvert.toColor(d, TiC.PROPERTY_TOUCH_FEEDBACK_COLOR) : null);
} else {
nativeView.setBackgroundColor(bgColor);
// A bug only on Android 2.3 (TIMOB-14311).
if (Build.VERSION.SDK_INT < TiC.API_LEVEL_HONEYCOMB && proxy.hasProperty(TiC.PROPERTY_OPACITY)) {
setOpacity(TiConvert.toFloat(proxy.getProperty(TiC.PROPERTY_OPACITY), 1f));
}
}
nativeView.postInvalidate();
}
} else {
if (key.equals(TiC.PROPERTY_OPACITY)) {
setOpacity(TiConvert.toFloat(newValue, 1f));
}
if (!nativeViewNull) {
nativeView.setBackgroundDrawable(null);
nativeView.postInvalidate();
}
}
} else {
boolean newBackground = background == null;
if (newBackground) {
background = new TiBackgroundDrawable();
}
Integer bgColor = null;
if (!hasColorState && !hasGradient) {
if (d.get(TiC.PROPERTY_BACKGROUND_COLOR) != null) {
bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR);
if (newBackground || (key.equals(TiC.PROPERTY_OPACITY) || key.equals(TiC.PROPERTY_BACKGROUND_COLOR))) {
background.setBackgroundColor(bgColor);
}
}
}
if (hasImage || hasRepeat || hasColorState || hasGradient) {
if (newBackground || key.equals(TiC.PROPERTY_OPACITY) || key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX)) {
handleBackgroundImage(d);
}
}
if (hasBorder) {
if (borderView == null && parent != null) {
// Since we have to create a new border wrapper view, we need to remove this view, and re-add
// it.
// This will ensure the border wrapper view is added correctly.
TiUIView parentView = parent.getOrCreateView();
int removedChildIndex = parentView.findChildIndex(this);
parentView.remove(this);
initializeBorder(d, bgColor);
if (removedChildIndex == -1) {
parentView.add(this);
} else {
parentView.add(this, removedChildIndex);
}
} else if (key.startsWith(TiC.PROPERTY_BORDER_PREFIX)) {
handleBorderProperty(key, newValue);
}
}
applyCustomBackground();
if (key.equals(TiC.PROPERTY_OPACITY)) {
setOpacity(TiConvert.toFloat(newValue, 1f));
} else if (Build.VERSION.SDK_INT < TiC.API_LEVEL_HONEYCOMB && proxy.hasProperty(TiC.PROPERTY_OPACITY)) {
// A bug only on Android 2.3 (TIMOB-14311).
setOpacity(TiConvert.toFloat(proxy.getProperty(TiC.PROPERTY_OPACITY), 1f));
}
}
if (!nativeViewNull) {
nativeView.postInvalidate();
}
} else if (key.equals(TiC.PROPERTY_SOFT_KEYBOARD_ON_FOCUS)) {
Log.w(TAG, "Focus state changed to " + TiConvert.toString(newValue) + " not honored until next focus event.",
Log.DEBUG_MODE);
} else if (key.equals(TiC.PROPERTY_TRANSFORM)) {
if (nativeView != null) {
applyTransform((Ti2DMatrix)newValue);
}
} else if (key.equals(TiC.PROPERTY_KEEP_SCREEN_ON)) {
if (nativeView != null) {
nativeView.setKeepScreenOn(TiConvert.toBoolean(newValue));
}
} else if (key.indexOf("accessibility") == 0 && !key.equals(TiC.PROPERTY_ACCESSIBILITY_HIDDEN)) {
applyContentDescription();
} else if (key.equals(TiC.PROPERTY_ACCESSIBILITY_HIDDEN)) {
applyAccessibilityHidden(newValue);
} else if (key.equals(TiC.PROPERTY_ELEVATION)) {
if (getOuterView() != null) {
ViewCompat.setElevation(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_TRANSLATION_X)) {
if (getOuterView() != null) {
ViewCompat.setTranslationX(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_TRANSLATION_Y)) {
if (getOuterView() != null) {
ViewCompat.setTranslationY(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_TRANSLATION_Z)) {
if (getOuterView() != null) {
ViewCompat.setTranslationZ(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_TRANSITION_NAME)) {
if (LOLLIPOP_OR_GREATER && (nativeView != null)) {
ViewCompat.setTransitionName(nativeView, TiConvert.toString(newValue));
}
} else if (key.equals(TiC.PROPERTY_SCALE_X)) {
if (getOuterView() != null) {
ViewCompat.setScaleX(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_SCALE_Y)) {
if (getOuterView() != null) {
ViewCompat.setScaleY(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_ROTATION)) {
if (getOuterView() != null) {
ViewCompat.setRotation(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_ROTATION_X)) {
if (getOuterView() != null) {
ViewCompat.setRotationX(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_ROTATION_Y)) {
if (getOuterView() != null) {
ViewCompat.setRotationY(getOuterView(), TiConvert.toFloat(newValue));
}
} else if (key.equals(TiC.PROPERTY_HIDDEN_BEHAVIOR)) {
hiddenBehavior = TiConvert.toInt(newValue, View.INVISIBLE);
} else if (Log.isDebugModeEnabled()) {
Log.d(TAG, "Unhandled property key: " + key, Log.DEBUG_MODE);
}
}
public void processProperties(KrollDict d)
{
boolean nativeViewNull = false;
if (nativeView == null) {
nativeViewNull = true;
Log.d(TAG, "Nativeview is null", Log.DEBUG_MODE);
}
if (d.containsKey(TiC.PROPERTY_LAYOUT)) {
String layout = TiConvert.toString(d, TiC.PROPERTY_LAYOUT);
if (nativeView instanceof TiCompositeLayout) {
((TiCompositeLayout)nativeView).setLayoutArrangement(layout);
}
}
if (TiConvert.fillLayout(d, layoutParams) && !nativeViewNull) {
nativeView.requestLayout();
}
if (d.containsKey(TiC.PROPERTY_HORIZONTAL_WRAP)) {
if (nativeView instanceof TiCompositeLayout) {
((TiCompositeLayout) nativeView).setEnableHorizontalWrap(TiConvert.toBoolean(d,TiC.PROPERTY_HORIZONTAL_WRAP,true));
}
}
Integer bgColor = null;
// Default background processing.
// Prefer image to color.
if (hasImage(d) || hasColorState(d) || hasGradient(d)) {
handleBackgroundImage(d);
} else if (d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR) && !nativeViewNull) {
bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR);