/
Flow.java
1424 lines (1301 loc) · 56.2 KB
/
Flow.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
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.constraintlayout.core.widgets;
import androidx.constraintlayout.core.LinearSystem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import static androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.AT_MOST;
import static androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.EXACTLY;
import static androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.UNSPECIFIED;
/**
* Implements the Flow virtual layout.
*/
public class Flow extends VirtualLayout {
public static final int HORIZONTAL_ALIGN_START = 0;
public static final int HORIZONTAL_ALIGN_END = 1;
public static final int HORIZONTAL_ALIGN_CENTER = 2;
public static final int VERTICAL_ALIGN_TOP = 0;
public static final int VERTICAL_ALIGN_BOTTOM = 1;
public static final int VERTICAL_ALIGN_CENTER = 2;
public static final int VERTICAL_ALIGN_BASELINE = 3;
public static final int WRAP_NONE = 0;
public static final int WRAP_CHAIN = 1;
public static final int WRAP_ALIGNED = 2;
public static final int WRAP_CHAIN_NEW = 3;
private int mHorizontalStyle = UNKNOWN;
private int mVerticalStyle = UNKNOWN;
private int mFirstHorizontalStyle = UNKNOWN;
private int mFirstVerticalStyle = UNKNOWN;
private int mLastHorizontalStyle = UNKNOWN;
private int mLastVerticalStyle = UNKNOWN;
private float mHorizontalBias = 0.5f;
private float mVerticalBias = 0.5f;
private float mFirstHorizontalBias = 0.5f;
private float mFirstVerticalBias = 0.5f;
private float mLastHorizontalBias = 0.5f;
private float mLastVerticalBias = 0.5f;
private int mHorizontalGap = 0;
private int mVerticalGap = 0;
private int mHorizontalAlign = HORIZONTAL_ALIGN_CENTER;
private int mVerticalAlign = VERTICAL_ALIGN_CENTER;
private int mWrapMode = WRAP_NONE;
private int mMaxElementsWrap = UNKNOWN;
private int mOrientation = HORIZONTAL;
private ArrayList<WidgetsList> mChainList = new ArrayList<>();
// Aligned management
private ConstraintWidget[] mAlignedBiggestElementsInRows = null;
private ConstraintWidget[] mAlignedBiggestElementsInCols = null;
private int[] mAlignedDimensions = null;
private ConstraintWidget[] mDisplayedWidgets;
private int mDisplayedWidgetsCount = 0;
@Override
public void copy(ConstraintWidget src, HashMap<ConstraintWidget,ConstraintWidget> map) {
super.copy(src, map);
Flow srcFLow = (Flow) src;
mHorizontalStyle = srcFLow.mHorizontalStyle;
mVerticalStyle = srcFLow.mVerticalStyle;
mFirstHorizontalStyle = srcFLow.mFirstHorizontalStyle;
mFirstVerticalStyle = srcFLow.mFirstVerticalStyle;
mLastHorizontalStyle = srcFLow.mLastHorizontalStyle;
mLastVerticalStyle = srcFLow.mLastVerticalStyle;
mHorizontalBias = srcFLow.mHorizontalBias;
mVerticalBias = srcFLow.mVerticalBias;
mFirstHorizontalBias = srcFLow.mFirstHorizontalBias;
mFirstVerticalBias = srcFLow.mFirstVerticalBias;
mLastHorizontalBias = srcFLow.mLastHorizontalBias;
mLastVerticalBias = srcFLow.mLastVerticalBias;
mHorizontalGap = srcFLow.mHorizontalGap;
mVerticalGap = srcFLow.mVerticalGap;
mHorizontalAlign = srcFLow.mHorizontalAlign;
mVerticalAlign = srcFLow.mVerticalAlign;
mWrapMode = srcFLow.mWrapMode;
mMaxElementsWrap = srcFLow.mMaxElementsWrap;
mOrientation = srcFLow.mOrientation;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Accessors
/////////////////////////////////////////////////////////////////////////////////////////////
public void setOrientation(int value) {
mOrientation = value;
}
public void setFirstHorizontalStyle(int value) { mFirstHorizontalStyle = value; }
public void setFirstVerticalStyle(int value) { mFirstVerticalStyle = value; }
public void setLastHorizontalStyle(int value) { mLastHorizontalStyle = value; }
public void setLastVerticalStyle(int value) { mLastVerticalStyle = value; }
public void setHorizontalStyle(int value) {
mHorizontalStyle = value;
}
public void setVerticalStyle(int value) {
mVerticalStyle = value;
}
public void setHorizontalBias(float value) {
mHorizontalBias = value;
}
public void setVerticalBias(float value) {
mVerticalBias = value;
}
public void setFirstHorizontalBias(float value) { mFirstHorizontalBias = value; }
public void setFirstVerticalBias(float value) { mFirstVerticalBias = value; }
public void setLastHorizontalBias(float value) { mLastHorizontalBias = value; }
public void setLastVerticalBias(float value) { mLastVerticalBias = value; }
public void setHorizontalAlign(int value) {
mHorizontalAlign = value;
}
public void setVerticalAlign(int value) {
mVerticalAlign = value;
}
public void setWrapMode(int value) {
mWrapMode = value;
}
public void setHorizontalGap(int value) { mHorizontalGap = value; }
public void setVerticalGap(int value) { mVerticalGap = value; }
public void setMaxElementsWrap(int value) { mMaxElementsWrap = value; }
public float getMaxElementsWrap() {
return mMaxElementsWrap;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Utility methods
/////////////////////////////////////////////////////////////////////////////////////////////
final private int getWidgetWidth(ConstraintWidget widget, int max) {
if (widget == null) {
return 0;
}
if (widget.getHorizontalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
if (widget.mMatchConstraintDefaultWidth == MATCH_CONSTRAINT_SPREAD) {
return 0;
} else if (widget.mMatchConstraintDefaultWidth == MATCH_CONSTRAINT_PERCENT) {
int value = (int) (widget.mMatchConstraintPercentWidth * max);
if (value != widget.getWidth()) {
widget.setMeasureRequested(true);
measure(widget, DimensionBehaviour.FIXED, value, widget.getVerticalDimensionBehaviour(), widget.getHeight());
}
return value;
} else if (widget.mMatchConstraintDefaultWidth == MATCH_CONSTRAINT_WRAP) {
return widget.getWidth();
} else if (widget.mMatchConstraintDefaultWidth == MATCH_CONSTRAINT_RATIO) {
return (int) (widget.getHeight() * widget.mDimensionRatio + 0.5f);
}
}
return widget.getWidth();
}
final private int getWidgetHeight(ConstraintWidget widget, int max) {
if (widget == null) {
return 0;
}
if (widget.getVerticalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
if (widget.mMatchConstraintDefaultHeight == MATCH_CONSTRAINT_SPREAD) {
return 0;
} else if (widget.mMatchConstraintDefaultHeight== MATCH_CONSTRAINT_PERCENT) {
int value = (int) (widget.mMatchConstraintPercentHeight * max);
if (value != widget.getHeight()) {
widget.setMeasureRequested(true);
measure(widget, widget.getHorizontalDimensionBehaviour(), widget.getWidth(), DimensionBehaviour.FIXED, value);
}
return value;
} else if (widget.mMatchConstraintDefaultHeight == MATCH_CONSTRAINT_WRAP) {
return widget.getHeight();
} else if (widget.mMatchConstraintDefaultHeight == MATCH_CONSTRAINT_RATIO) {
return (int) (widget.getWidth() * widget.mDimensionRatio + 0.5f);
}
}
return widget.getHeight();
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Measure
/////////////////////////////////////////////////////////////////////////////////////////////
public void measure(int widthMode, int widthSize, int heightMode, int heightSize) {
if (mWidgetsCount > 0 && !measureChildren()) {
setMeasure(0, 0);
needsCallbackFromSolver(false);
return;
}
int width = 0;
int height = 0;
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int measured[] = new int[2];
int max = widthSize - paddingLeft - paddingRight;
if (mOrientation == VERTICAL) {
max = heightSize - paddingTop - paddingBottom;
}
if (mOrientation == HORIZONTAL) {
if (mHorizontalStyle == UNKNOWN) {
mHorizontalStyle = CHAIN_SPREAD;
}
if (mVerticalStyle == UNKNOWN) {
mVerticalStyle = CHAIN_SPREAD;
}
} else {
if (mHorizontalStyle == UNKNOWN) {
mHorizontalStyle = CHAIN_SPREAD;
}
if (mVerticalStyle == UNKNOWN) {
mVerticalStyle = CHAIN_SPREAD;
}
}
ConstraintWidget[] widgets = mWidgets;
int gone = 0;
for (int i = 0; i <mWidgetsCount; i++) {
ConstraintWidget widget = mWidgets[i];
if (widget.getVisibility() == GONE) {
gone++;
}
}
int count = mWidgetsCount;
if (gone > 0) {
widgets = new ConstraintWidget[mWidgetsCount - gone];
int j = 0;
for (int i = 0; i <mWidgetsCount; i++) {
ConstraintWidget widget = mWidgets[i];
if (widget.getVisibility() != GONE) {
widgets[j] = widget;
j++;
}
}
count = j;
}
mDisplayedWidgets = widgets;
mDisplayedWidgetsCount = count;
switch (mWrapMode) {
case WRAP_ALIGNED: {
measureAligned(widgets, count, mOrientation, max, measured);
}
break;
case WRAP_CHAIN: {
measureChainWrap(widgets, count, mOrientation, max, measured);
}
break;
case WRAP_NONE: {
measureNoWrap(widgets, count, mOrientation, max, measured);
}
break;
case WRAP_CHAIN_NEW: {
measureChainWrap_new(widgets, count, mOrientation, max, measured);
}
break;
}
width = measured[HORIZONTAL] + paddingLeft + paddingRight;
height = measured[VERTICAL] + paddingTop + paddingBottom;
int measuredWidth = 0;
int measuredHeight = 0;
if (widthMode == EXACTLY) {
measuredWidth = widthSize;
} else if (widthMode == AT_MOST) {
measuredWidth = Math.min(width, widthSize);
} else if (widthMode == UNSPECIFIED) {
measuredWidth = width;
}
if (heightMode == EXACTLY) {
measuredHeight = heightSize;
} else if (heightMode == AT_MOST) {
measuredHeight = Math.min(height, heightSize);
} else if (heightMode == UNSPECIFIED) {
measuredHeight = height;
}
setMeasure(measuredWidth, measuredHeight);
setWidth(measuredWidth);
setHeight(measuredHeight);
needsCallbackFromSolver(mWidgetsCount > 0);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Utility class representing a single chain
/////////////////////////////////////////////////////////////////////////////////////////////
private class WidgetsList {
private int mOrientation = HORIZONTAL;
private ConstraintWidget biggest = null;
int biggestDimension = 0;
private ConstraintAnchor mLeft;
private ConstraintAnchor mTop;
private ConstraintAnchor mRight;
private ConstraintAnchor mBottom;
private int mPaddingLeft = 0;
private int mPaddingTop = 0;
private int mPaddingRight = 0;
private int mPaddingBottom = 0;
private int mWidth = 0;
private int mHeight = 0;
private int mStartIndex = 0;
private int mCount = 0;
private int mNbMatchConstraintsWidgets = 0;
private int mMax = 0;
public WidgetsList(int orientation,
ConstraintAnchor left, ConstraintAnchor top,
ConstraintAnchor right, ConstraintAnchor bottom,
int max) {
mOrientation = orientation;
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
mPaddingLeft = getPaddingLeft();
mPaddingTop = getPaddingTop();
mPaddingRight = getPaddingRight();
mPaddingBottom = getPaddingBottom();
mMax = max;
}
public void setup(int orientation, ConstraintAnchor left, ConstraintAnchor top,
ConstraintAnchor right, ConstraintAnchor bottom,
int paddingLeft, int paddingTop, int paddingRight, int paddingBottom,
int max) {
mOrientation = orientation;
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
mPaddingLeft = paddingLeft;
mPaddingTop = paddingTop;
mPaddingRight = paddingRight;
mPaddingBottom = paddingBottom;
mMax = max;
}
public void clear() {
biggestDimension = 0;
biggest = null;
mWidth = 0;
mHeight = 0;
mStartIndex = 0;
mCount = 0;
mNbMatchConstraintsWidgets = 0;
}
public void setStartIndex(int value) { mStartIndex = value; }
public int getWidth() {
if (mOrientation == HORIZONTAL) {
return mWidth - mHorizontalGap;
}
return mWidth;
}
public int getHeight() {
if (mOrientation == VERTICAL) {
return mHeight - mVerticalGap;
}
return mHeight;
}
public void add(ConstraintWidget widget) {
if (mOrientation == HORIZONTAL) {
int width = getWidgetWidth(widget, mMax);
if (widget.getHorizontalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
mNbMatchConstraintsWidgets++;
width = 0;
}
int gap = mHorizontalGap;
if (widget.getVisibility() == GONE) {
gap = 0;
}
mWidth += width + gap;
int height = getWidgetHeight(widget, mMax);
if (biggest == null || biggestDimension < height) {
biggest = widget;
biggestDimension = height;
mHeight = height;
}
} else {
int width = getWidgetWidth(widget, mMax);
int height = getWidgetHeight(widget, mMax);
if (widget.getVerticalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
mNbMatchConstraintsWidgets++;
height = 0;
}
int gap = mVerticalGap;
if (widget.getVisibility() == GONE) {
gap = 0;
}
mHeight += height + gap;
if (biggest == null || biggestDimension < width) {
biggest = widget;
biggestDimension = width;
mWidth = width;
}
}
mCount++;
}
public void createConstraints(boolean isInRtl, int chainIndex, boolean isLastChain) {
final int count = mCount;
for (int i = 0; i < count; i++) {
if (mStartIndex + i >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + i];
if (widget != null) {
widget.resetAnchors();
}
}
if (count == 0 || biggest == null) {
return;
}
boolean singleChain = isLastChain && chainIndex == 0;
int firstVisible = -1;
int lastVisible = -1;
for (int i = 0; i < count; i++) {
int index = i;
if (isInRtl) {
index = count - 1 - i;
}
if (mStartIndex + index >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + index];
if (widget != null && widget.getVisibility() == VISIBLE) {
if (firstVisible == -1) {
firstVisible = i;
}
lastVisible = i;
}
}
ConstraintWidget previous = null;
if (mOrientation == HORIZONTAL) {
ConstraintWidget verticalWidget = biggest;
verticalWidget.setVerticalChainStyle(mVerticalStyle);
int padding = mPaddingTop;
if (chainIndex > 0) {
padding += mVerticalGap;
}
verticalWidget.mTop.connect(mTop, padding);
if (isLastChain) {
verticalWidget.mBottom.connect(mBottom, mPaddingBottom);
}
if (chainIndex > 0) {
ConstraintAnchor bottom = mTop.mOwner.mBottom;
bottom.connect(verticalWidget.mTop, 0);
}
ConstraintWidget baselineVerticalWidget = verticalWidget;
if (mVerticalAlign == VERTICAL_ALIGN_BASELINE && !verticalWidget.hasBaseline()) {
for (int i = 0; i < count; i++) {
int index = i;
if (isInRtl) {
index = count - 1 - i;
}
if (mStartIndex + index >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + index];
if (widget.hasBaseline()) {
baselineVerticalWidget = widget;
break;
}
}
}
for (int i = 0; i < count; i++) {
int index = i;
if (isInRtl) {
index = count - 1 - i;
}
if (mStartIndex + index >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + index];
if (widget == null) {
continue;
}
if (i == 0) {
widget.connect(widget.mLeft, mLeft, mPaddingLeft);
}
// ChainHead is always based on index, not i.
// E.g. RTL would have head at the right most widget.
if (index == 0) {
int style = mHorizontalStyle;
float bias = isInRtl ? (1 - mHorizontalBias) : mHorizontalBias;
if (mStartIndex == 0 && mFirstHorizontalStyle != UNKNOWN) {
style = mFirstHorizontalStyle;
bias = isInRtl ? (1 - mFirstHorizontalBias) : mFirstHorizontalBias;
} else if (isLastChain && mLastHorizontalStyle != UNKNOWN) {
style = mLastHorizontalStyle;
bias = isInRtl ? (1 - mLastHorizontalBias) : mLastHorizontalBias;
}
widget.setHorizontalChainStyle(style);
widget.setHorizontalBiasPercent(bias);
}
if (i == count - 1) {
widget.connect(widget.mRight, mRight, mPaddingRight);
}
if (previous != null) {
widget.mLeft.connect(previous.mRight, mHorizontalGap);
if (i == firstVisible) {
widget.mLeft.setGoneMargin(mPaddingLeft);
}
previous.mRight.connect(widget.mLeft, 0);
if (i == lastVisible + 1) {
previous.mRight.setGoneMargin(mPaddingRight);
}
}
if (widget != verticalWidget) {
if (mVerticalAlign == VERTICAL_ALIGN_BASELINE
&& baselineVerticalWidget.hasBaseline()
&& widget != baselineVerticalWidget
&& widget.hasBaseline()) {
widget.mBaseline.connect(baselineVerticalWidget.mBaseline, 0);
} else {
switch (mVerticalAlign) {
case VERTICAL_ALIGN_TOP: {
widget.mTop.connect(verticalWidget.mTop, 0);
}
break;
case VERTICAL_ALIGN_BOTTOM: {
widget.mBottom.connect(verticalWidget.mBottom, 0);
}
break;
case VERTICAL_ALIGN_CENTER:
default: {
if (singleChain) {
widget.mTop.connect(mTop, mPaddingTop);
widget.mBottom.connect(mBottom, mPaddingBottom);
} else {
widget.mTop.connect(verticalWidget.mTop, 0);
widget.mBottom.connect(verticalWidget.mBottom, 0);
}
}
}
}
}
previous = widget;
}
} else {
ConstraintWidget horizontalWidget = biggest;
horizontalWidget.setHorizontalChainStyle(mHorizontalStyle);
int padding = mPaddingLeft;
if (chainIndex > 0) {
padding += mHorizontalGap;
}
if (isInRtl) {
horizontalWidget.mRight.connect(mRight, padding);
if (isLastChain) {
horizontalWidget.mLeft.connect(mLeft, mPaddingRight);
}
if (chainIndex > 0) {
ConstraintAnchor left = mRight.mOwner.mLeft;
left.connect(horizontalWidget.mRight, 0);
}
} else {
horizontalWidget.mLeft.connect(mLeft, padding);
if (isLastChain) {
horizontalWidget.mRight.connect(mRight, mPaddingRight);
}
if (chainIndex > 0) {
ConstraintAnchor right = mLeft.mOwner.mRight;
right.connect(horizontalWidget.mLeft, 0);
}
}
for (int i = 0; i < count; i++) {
if (mStartIndex + i >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + i];
if (widget == null) {
continue;
}
if (i == 0) {
widget.connect(widget.mTop, mTop, mPaddingTop);
int style = mVerticalStyle;
float bias = mVerticalBias;
if (mStartIndex == 0 && mFirstVerticalStyle != UNKNOWN) {
style = mFirstVerticalStyle;
bias = mFirstVerticalBias;
} else if (isLastChain && mLastVerticalStyle != UNKNOWN) {
style = mLastVerticalStyle;
bias = mLastVerticalBias;
}
widget.setVerticalChainStyle(style);
widget.setVerticalBiasPercent(bias);
}
if (i == count - 1) {
widget.connect(widget.mBottom, mBottom, mPaddingBottom);
}
if (previous != null) {
widget.mTop.connect(previous.mBottom, mVerticalGap);
if (i == firstVisible) {
widget.mTop.setGoneMargin(mPaddingTop);
}
previous.mBottom.connect(widget.mTop, 0);
if (i == lastVisible + 1) {
previous.mBottom.setGoneMargin(mPaddingBottom);
}
}
if (widget != horizontalWidget) {
if (isInRtl) {
switch (mHorizontalAlign) {
case HORIZONTAL_ALIGN_START: {
widget.mRight.connect(horizontalWidget.mRight, 0);
}
break;
case HORIZONTAL_ALIGN_CENTER: {
widget.mLeft.connect(horizontalWidget.mLeft, 0);
widget.mRight.connect(horizontalWidget.mRight, 0);
}
break;
case HORIZONTAL_ALIGN_END: {
widget.mLeft.connect(horizontalWidget.mLeft, 0);
}
break;
}
} else {
switch (mHorizontalAlign) {
case HORIZONTAL_ALIGN_START: {
widget.mLeft.connect(horizontalWidget.mLeft, 0);
}
break;
case HORIZONTAL_ALIGN_CENTER: {
if (singleChain) {
widget.mLeft.connect(mLeft, mPaddingLeft);
widget.mRight.connect(mRight, mPaddingRight);
} else {
widget.mLeft.connect(horizontalWidget.mLeft, 0);
widget.mRight.connect(horizontalWidget.mRight, 0);
}
}
break;
case HORIZONTAL_ALIGN_END: {
widget.mRight.connect(horizontalWidget.mRight, 0);
}
break;
}
}
}
previous = widget;
}
}
}
public void measureMatchConstraints(int availableSpace) {
if (mNbMatchConstraintsWidgets == 0) {
return;
}
final int count = mCount;
int widgetSize = availableSpace / mNbMatchConstraintsWidgets; // that's completely incorrect and only works for spread with no weights?
for (int i = 0; i < count; i++) {
if (mStartIndex + i >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + i];
if (mOrientation == HORIZONTAL) {
if (widget != null && widget.getHorizontalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
if (widget.mMatchConstraintDefaultWidth == MATCH_CONSTRAINT_SPREAD) {
measure(widget, DimensionBehaviour.FIXED, widgetSize, widget.getVerticalDimensionBehaviour(), widget.getHeight());
}
}
} else {
if (widget != null && widget.getVerticalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
if (widget.mMatchConstraintDefaultHeight == MATCH_CONSTRAINT_SPREAD) {
measure(widget, widget.getHorizontalDimensionBehaviour(), widget.getWidth(), DimensionBehaviour.FIXED, widgetSize);
}
}
}
}
recomputeDimensions();
}
private void recomputeDimensions() {
mWidth = 0;
mHeight = 0;
biggest = null;
biggestDimension = 0;
final int count = mCount;
for (int i = 0; i < count; i++) {
if (mStartIndex + i >= mDisplayedWidgetsCount) {
break;
}
ConstraintWidget widget = mDisplayedWidgets[mStartIndex + i];
if (mOrientation == HORIZONTAL) {
int width = widget.getWidth();
int gap = mHorizontalGap;
if (widget.getVisibility() == GONE) {
gap = 0;
}
mWidth += width + gap;
int height = getWidgetHeight(widget, mMax);
if (biggest == null || biggestDimension < height) {
biggest = widget;
biggestDimension = height;
mHeight = height;
}
} else {
int width = getWidgetWidth(widget, mMax);
int height = getWidgetHeight(widget, mMax);
int gap = mVerticalGap;
if (widget.getVisibility() == GONE) {
gap = 0;
}
mHeight += height + gap;
if (biggest == null || biggestDimension < width) {
biggest = widget;
biggestDimension = width;
mWidth = width;
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Measure Chain Wrap
/////////////////////////////////////////////////////////////////////////////////////////////
/**
* Measure the virtual layout using a list of chains for the children
* @param widgets list of widgets
* @param count
* @param orientation the layout orientation (horizontal or vertical)
* @param max the maximum available space
* @param measured output parameters -- will contain the resulting measure
*/
private void measureChainWrap(ConstraintWidget[] widgets, int count, int orientation, int max, int[] measured) {
if (count == 0) {
return;
}
mChainList.clear();
WidgetsList list = new WidgetsList(orientation, mLeft, mTop, mRight, mBottom, max);
mChainList.add(list);
int nbMatchConstraintsWidgets = 0;
if (orientation == HORIZONTAL) {
int width = 0;
for (int i = 0; i < count; i++) {
ConstraintWidget widget = widgets[i];
int w = getWidgetWidth(widget, max);
if (widget.getHorizontalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
nbMatchConstraintsWidgets++;
}
boolean doWrap = (width == max || (width + mHorizontalGap + w) > max) && list.biggest != null;
if (!doWrap && i > 0 && mMaxElementsWrap > 0 && (i % mMaxElementsWrap == 0)) {
doWrap = true;
}
if (doWrap) {
width = w;
list = new WidgetsList(orientation, mLeft, mTop, mRight, mBottom, max);
list.setStartIndex(i);
mChainList.add(list);
} else {
if (i > 0) {
width += mHorizontalGap + w;
} else {
width = w;
}
}
list.add(widget);
}
} else {
int height = 0;
for (int i = 0; i < count; i++) {
ConstraintWidget widget = widgets[i];
int h = getWidgetHeight(widget, max);
if (widget.getVerticalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
nbMatchConstraintsWidgets++;
}
boolean doWrap = (height == max || (height + mVerticalGap + h ) > max) && list.biggest != null;
if (!doWrap && i > 0 && mMaxElementsWrap > 0 && (i % mMaxElementsWrap == 0)) {
doWrap = true;
}
if (doWrap) {
height = h;
list = new WidgetsList(orientation, mLeft, mTop, mRight, mBottom, max);
list.setStartIndex(i);
mChainList.add(list);
} else {
if (i > 0) {
height += mVerticalGap + h;
} else {
height = h;
}
}
list.add(widget);
}
}
final int listCount = mChainList.size();
ConstraintAnchor left = mLeft;
ConstraintAnchor top = mTop;
ConstraintAnchor right = mRight;
ConstraintAnchor bottom = mBottom;
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
int maxWidth = 0;
int maxHeight = 0;
boolean needInternalMeasure =
getHorizontalDimensionBehaviour() == DimensionBehaviour.WRAP_CONTENT
|| getVerticalDimensionBehaviour() == DimensionBehaviour.WRAP_CONTENT;
if (nbMatchConstraintsWidgets > 0 && needInternalMeasure) {
// we have to remeasure them.
for (int i = 0; i < listCount; i++) {
WidgetsList current = mChainList.get(i);
if (orientation == HORIZONTAL) {
current.measureMatchConstraints(max - current.getWidth());
} else {
current.measureMatchConstraints(max - current.getHeight());
}
}
}
for (int i = 0; i < listCount; i++) {
WidgetsList current = mChainList.get(i);
if (orientation == HORIZONTAL) {
if (i < listCount - 1) {
WidgetsList next = mChainList.get(i + 1);
bottom = next.biggest.mTop;
paddingBottom = 0;
} else {
bottom = mBottom;
paddingBottom = getPaddingBottom();
}
ConstraintAnchor currentBottom = current.biggest.mBottom;
current.setup(orientation, left, top, right, bottom,
paddingLeft, paddingTop, paddingRight, paddingBottom, max);
top = currentBottom;
paddingTop = 0;
maxWidth = Math.max(maxWidth, current.getWidth());
maxHeight += current.getHeight();
if (i > 0) {
maxHeight += mVerticalGap;
}
} else {
if (i < listCount - 1) {
WidgetsList next = mChainList.get(i + 1);
right = next.biggest.mLeft;
paddingRight = 0;
} else {
right = mRight;
paddingRight = getPaddingRight();
}
ConstraintAnchor currentRight = current.biggest.mRight;
current.setup(orientation, left, top, right, bottom,
paddingLeft, paddingTop, paddingRight, paddingBottom, max);
left = currentRight;
paddingLeft = 0;
maxWidth += current.getWidth();
maxHeight = Math.max(maxHeight, current.getHeight());
if (i > 0) {
maxWidth += mHorizontalGap;
}
}
}
measured[HORIZONTAL] = maxWidth;
measured[VERTICAL] = maxHeight;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Measure Chain Wrap new
/////////////////////////////////////////////////////////////////////////////////////////////
/**
* Measure the virtual layout using a list of chains for the children in new "fixed way"
* @param widgets list of widgets
* @param count
* @param orientation the layout orientation (horizontal or vertical)
* @param max the maximum available space
* @param measured output parameters -- will contain the resulting measure
*/
private void measureChainWrap_new(ConstraintWidget[] widgets, int count, int orientation, int max, int[] measured) {
if (count == 0) {
return;
}
mChainList.clear();
WidgetsList list = new WidgetsList(orientation, mLeft, mTop, mRight, mBottom, max);
mChainList.add(list);
int nbMatchConstraintsWidgets = 0;
if (orientation == HORIZONTAL) {
int width = 0;
int col = 0;
for (int i = 0; i < count; i++) {
col++;
ConstraintWidget widget = widgets[i];
int w = getWidgetWidth(widget, max);
if (widget.getHorizontalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
nbMatchConstraintsWidgets++;
}
boolean doWrap = (width == max || (width + mHorizontalGap + w) > max) && list.biggest != null;
if (!doWrap && i > 0 && mMaxElementsWrap > 0 && (col > mMaxElementsWrap)) {
doWrap = true;
}
if (doWrap) {
width = w;
list = new WidgetsList(orientation, mLeft, mTop, mRight, mBottom, max);
list.setStartIndex(i);
mChainList.add(list);
} else {
col = 0;
if (i > 0) {
width += mHorizontalGap + w;
} else {
width = w;
}
}
list.add(widget);
}
} else {
int height = 0;
int row = 0;
for (int i = 0; i < count; i++) {
ConstraintWidget widget = widgets[i];
int h = getWidgetHeight(widget, max);
if (widget.getVerticalDimensionBehaviour() == DimensionBehaviour.MATCH_CONSTRAINT) {
nbMatchConstraintsWidgets++;
}
boolean doWrap = (height == max || (height + mVerticalGap + h ) > max) && list.biggest != null;
if (!doWrap && i > 0 && mMaxElementsWrap > 0 && (row > mMaxElementsWrap)) {
doWrap = true;
}
if (doWrap) {
height = h;
list = new WidgetsList(orientation, mLeft, mTop, mRight, mBottom, max);
list.setStartIndex(i);