| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import static android.view.Gravity.AXIS_PULL_AFTER; |
| 20 |
import static android.view.Gravity.AXIS_PULL_BEFORE; |
| 21 |
import static android.view.Gravity.AXIS_SPECIFIED; |
| 22 |
import static android.view.Gravity.AXIS_X_SHIFT; |
| 23 |
import static android.view.Gravity.AXIS_Y_SHIFT; |
| 24 |
import static android.view.Gravity.HORIZONTAL_GRAVITY_MASK; |
| 25 |
import static android.view.Gravity.RELATIVE_LAYOUT_DIRECTION; |
| 26 |
import static android.view.Gravity.VERTICAL_GRAVITY_MASK; |
| 27 |
import static android.view.View.MeasureSpec.EXACTLY; |
| 28 |
import static android.view.View.MeasureSpec.makeMeasureSpec; |
| 29 |
|
| 30 |
import static java.lang.Math.max; |
| 31 |
import static java.lang.Math.min; |
| 32 |
|
| 33 |
import android.annotation.IntDef; |
| 34 |
import android.annotation.Nullable; |
| 35 |
import android.compat.annotation.UnsupportedAppUsage; |
| 36 |
import android.content.Context; |
| 37 |
import android.content.res.TypedArray; |
| 38 |
import android.graphics.Canvas; |
| 39 |
import android.graphics.Color; |
| 40 |
import android.graphics.Insets; |
| 41 |
import android.graphics.Paint; |
| 42 |
import android.os.Build; |
| 43 |
import android.util.AttributeSet; |
| 44 |
import android.util.Log; |
| 45 |
import android.util.LogPrinter; |
| 46 |
import android.util.Pair; |
| 47 |
import android.util.Printer; |
| 48 |
import android.view.Gravity; |
| 49 |
import android.view.RemotableViewMethod; |
| 50 |
import android.view.View; |
| 51 |
import android.view.ViewGroup; |
| 52 |
import android.view.inspector.InspectableProperty; |
| 53 |
import android.widget.RemoteViews.RemoteView; |
| 54 |
|
| 55 |
import com.android.internal.R; |
| 56 |
|
| 57 |
import java.lang.annotation.Retention; |
| 58 |
import java.lang.annotation.RetentionPolicy; |
| 59 |
import java.lang.reflect.Array; |
| 60 |
import java.util.ArrayList; |
| 61 |
import java.util.Arrays; |
| 62 |
import java.util.HashMap; |
| 63 |
import java.util.List; |
| 64 |
import java.util.Map; |
| 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 |
@RemoteView |
| 175 |
public class GridLayout extends ViewGroup { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@IntDef(prefix = { "HORIZONTAL", "VERTICAL" }, value = { |
| 181 |
HORIZONTAL, |
| 182 |
VERTICAL |
| 183 |
}) |
| 184 |
@Retention(RetentionPolicy.SOURCE) |
| 185 |
public @interface Orientation {} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static final int HORIZONTAL = LinearLayout.HORIZONTAL; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static final int VERTICAL = LinearLayout.VERTICAL; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public static final int UNDEFINED = Integer.MIN_VALUE; |
| 207 |
|
| 208 |
|
| 209 |
@IntDef(prefix = { "ALIGN_" }, value = { |
| 210 |
ALIGN_BOUNDS, |
| 211 |
ALIGN_MARGINS |
| 212 |
}) |
| 213 |
@Retention(RetentionPolicy.SOURCE) |
| 214 |
public @interface AlignmentMode {} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public static final int ALIGN_BOUNDS = 0; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public static final int ALIGN_MARGINS = 1; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static final int MAX_SIZE = 100000; |
| 252 |
static final int DEFAULT_CONTAINER_MARGIN = 0; |
| 253 |
static final int UNINITIALIZED_HASH = 0; |
| 254 |
static final Printer LOG_PRINTER = new LogPrinter(Log.DEBUG, GridLayout.class.getName()); |
| 255 |
static final Printer NO_PRINTER = new Printer() { |
| 256 |
@Override |
| 257 |
public void println(String x) { |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
private static final int DEFAULT_ORIENTATION = HORIZONTAL; |
| 264 |
private static final int DEFAULT_COUNT = UNDEFINED; |
| 265 |
private static final boolean DEFAULT_USE_DEFAULT_MARGINS = false; |
| 266 |
private static final boolean DEFAULT_ORDER_PRESERVED = true; |
| 267 |
private static final int DEFAULT_ALIGNMENT_MODE = ALIGN_MARGINS; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
private static final int ORIENTATION = R.styleable.GridLayout_orientation; |
| 272 |
private static final int ROW_COUNT = R.styleable.GridLayout_rowCount; |
| 273 |
private static final int COLUMN_COUNT = R.styleable.GridLayout_columnCount; |
| 274 |
private static final int USE_DEFAULT_MARGINS = R.styleable.GridLayout_useDefaultMargins; |
| 275 |
private static final int ALIGNMENT_MODE = R.styleable.GridLayout_alignmentMode; |
| 276 |
private static final int ROW_ORDER_PRESERVED = R.styleable.GridLayout_rowOrderPreserved; |
| 277 |
private static final int COLUMN_ORDER_PRESERVED = R.styleable.GridLayout_columnOrderPreserved; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
final Axis mHorizontalAxis = new Axis(true); |
| 282 |
final Axis mVerticalAxis = new Axis(false); |
| 283 |
int mOrientation = DEFAULT_ORIENTATION; |
| 284 |
boolean mUseDefaultMargins = DEFAULT_USE_DEFAULT_MARGINS; |
| 285 |
int mAlignmentMode = DEFAULT_ALIGNMENT_MODE; |
| 286 |
int mDefaultGap; |
| 287 |
int mLastLayoutParamsHashCode = UNINITIALIZED_HASH; |
| 288 |
Printer mPrinter = LOG_PRINTER; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public GridLayout(Context context) { |
| 293 |
this(context, null); |
| 294 |
} |
| 295 |
|
| 296 |
public GridLayout(Context context, AttributeSet attrs) { |
| 297 |
this(context, attrs, 0); |
| 298 |
} |
| 299 |
|
| 300 |
public GridLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 301 |
this(context, attrs, defStyleAttr, 0); |
| 302 |
} |
| 303 |
|
| 304 |
public GridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 305 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 306 |
mDefaultGap = context.getResources().getDimensionPixelOffset(R.dimen.default_gap); |
| 307 |
final TypedArray a = context.obtainStyledAttributes( |
| 308 |
attrs, R.styleable.GridLayout, defStyleAttr, defStyleRes); |
| 309 |
saveAttributeDataForStyleable(context, R.styleable.GridLayout, |
| 310 |
attrs, a, defStyleAttr, defStyleRes); |
| 311 |
try { |
| 312 |
setRowCount(a.getInt(ROW_COUNT, DEFAULT_COUNT)); |
| 313 |
setColumnCount(a.getInt(COLUMN_COUNT, DEFAULT_COUNT)); |
| 314 |
setOrientation(a.getInt(ORIENTATION, DEFAULT_ORIENTATION)); |
| 315 |
setUseDefaultMargins(a.getBoolean(USE_DEFAULT_MARGINS, DEFAULT_USE_DEFAULT_MARGINS)); |
| 316 |
setAlignmentMode(a.getInt(ALIGNMENT_MODE, DEFAULT_ALIGNMENT_MODE)); |
| 317 |
setRowOrderPreserved(a.getBoolean(ROW_ORDER_PRESERVED, DEFAULT_ORDER_PRESERVED)); |
| 318 |
setColumnOrderPreserved(a.getBoolean(COLUMN_ORDER_PRESERVED, DEFAULT_ORDER_PRESERVED)); |
| 319 |
} finally { |
| 320 |
a.recycle(); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
@Orientation |
| 336 |
@InspectableProperty(enumMapping = { |
| 337 |
@InspectableProperty.EnumEntry(value = HORIZONTAL, name = "horizontal"), |
| 338 |
@InspectableProperty.EnumEntry(value = VERTICAL, name = "vertical") |
| 339 |
}) |
| 340 |
public int getOrientation() { |
| 341 |
return mOrientation; |
| 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 |
public void setOrientation(@Orientation int orientation) { |
| 381 |
if (this.mOrientation != orientation) { |
| 382 |
this.mOrientation = orientation; |
| 383 |
invalidateStructure(); |
| 384 |
requestLayout(); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
@InspectableProperty |
| 401 |
public int getRowCount() { |
| 402 |
return mVerticalAxis.getCount(); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
@RemotableViewMethod |
| 417 |
public void setRowCount(int rowCount) { |
| 418 |
mVerticalAxis.setCount(rowCount); |
| 419 |
invalidateStructure(); |
| 420 |
requestLayout(); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
@InspectableProperty |
| 436 |
public int getColumnCount() { |
| 437 |
return mHorizontalAxis.getCount(); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
@RemotableViewMethod |
| 452 |
public void setColumnCount(int columnCount) { |
| 453 |
mHorizontalAxis.setCount(columnCount); |
| 454 |
invalidateStructure(); |
| 455 |
requestLayout(); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
@InspectableProperty |
| 469 |
public boolean getUseDefaultMargins() { |
| 470 |
return mUseDefaultMargins; |
| 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 |
public void setUseDefaultMargins(boolean useDefaultMargins) { |
| 500 |
this.mUseDefaultMargins = useDefaultMargins; |
| 501 |
requestLayout(); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
@AlignmentMode |
| 517 |
@InspectableProperty(enumMapping = { |
| 518 |
@InspectableProperty.EnumEntry(value = ALIGN_BOUNDS, name = "alignBounds"), |
| 519 |
@InspectableProperty.EnumEntry(value = ALIGN_MARGINS, name = "alignMargins"), |
| 520 |
}) |
| 521 |
public int getAlignmentMode() { |
| 522 |
return mAlignmentMode; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
@RemotableViewMethod |
| 541 |
public void setAlignmentMode(@AlignmentMode int alignmentMode) { |
| 542 |
this.mAlignmentMode = alignmentMode; |
| 543 |
requestLayout(); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
@InspectableProperty |
| 557 |
public boolean isRowOrderPreserved() { |
| 558 |
return mVerticalAxis.isOrderPreserved(); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
public void setRowOrderPreserved(boolean rowOrderPreserved) { |
| 578 |
mVerticalAxis.setOrderPreserved(rowOrderPreserved); |
| 579 |
invalidateStructure(); |
| 580 |
requestLayout(); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
@InspectableProperty |
| 594 |
public boolean isColumnOrderPreserved() { |
| 595 |
return mHorizontalAxis.isOrderPreserved(); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
public void setColumnOrderPreserved(boolean columnOrderPreserved) { |
| 615 |
mHorizontalAxis.setOrderPreserved(columnOrderPreserved); |
| 616 |
invalidateStructure(); |
| 617 |
requestLayout(); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public Printer getPrinter() { |
| 630 |
return mPrinter; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public void setPrinter(Printer printer) { |
| 644 |
this.mPrinter = (printer == null) ? NO_PRINTER : printer; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
static int max2(int[] a, int valueIfEmpty) { |
| 650 |
int result = valueIfEmpty; |
| 651 |
for (int i = 0, N = a.length; i < N; i++) { |
| 652 |
result = Math.max(result, a[i]); |
| 653 |
} |
| 654 |
return result; |
| 655 |
} |
| 656 |
|
| 657 |
@SuppressWarnings("unchecked") |
| 658 |
static <T> T[] append(T[] a, T[] b) { |
| 659 |
T[] result = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length); |
| 660 |
System.arraycopy(a, 0, result, 0, a.length); |
| 661 |
System.arraycopy(b, 0, result, a.length, b.length); |
| 662 |
return result; |
| 663 |
} |
| 664 |
|
| 665 |
static Alignment getAlignment(int gravity, boolean horizontal) { |
| 666 |
int mask = horizontal ? HORIZONTAL_GRAVITY_MASK : VERTICAL_GRAVITY_MASK; |
| 667 |
int shift = horizontal ? AXIS_X_SHIFT : AXIS_Y_SHIFT; |
| 668 |
int flags = (gravity & mask) >> shift; |
| 669 |
switch (flags) { |
| 670 |
case (AXIS_SPECIFIED | AXIS_PULL_BEFORE): |
| 671 |
return horizontal ? LEFT : TOP; |
| 672 |
case (AXIS_SPECIFIED | AXIS_PULL_AFTER): |
| 673 |
return horizontal ? RIGHT : BOTTOM; |
| 674 |
case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | AXIS_PULL_AFTER): |
| 675 |
return FILL; |
| 676 |
case AXIS_SPECIFIED: |
| 677 |
return CENTER; |
| 678 |
case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | RELATIVE_LAYOUT_DIRECTION): |
| 679 |
return START; |
| 680 |
case (AXIS_SPECIFIED | AXIS_PULL_AFTER | RELATIVE_LAYOUT_DIRECTION): |
| 681 |
return END; |
| 682 |
default: |
| 683 |
return UNDEFINED_ALIGNMENT; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
private int getDefaultMargin(View c, boolean horizontal, boolean leading) { |
| 689 |
if (c.getClass() == Space.class) { |
| 690 |
return 0; |
| 691 |
} |
| 692 |
return mDefaultGap / 2; |
| 693 |
} |
| 694 |
|
| 695 |
private int getDefaultMargin(View c, boolean isAtEdge, boolean horizontal, boolean leading) { |
| 696 |
return getDefaultMargin(c, horizontal, leading); |
| 697 |
} |
| 698 |
|
| 699 |
private int getDefaultMargin(View c, LayoutParams p, boolean horizontal, boolean leading) { |
| 700 |
if (!mUseDefaultMargins) { |
| 701 |
return 0; |
| 702 |
} |
| 703 |
Spec spec = horizontal ? p.columnSpec : p.rowSpec; |
| 704 |
Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
| 705 |
Interval span = spec.span; |
| 706 |
boolean leading1 = (horizontal && isLayoutRtl()) ? !leading : leading; |
| 707 |
boolean isAtEdge = leading1 ? (span.min == 0) : (span.max == axis.getCount()); |
| 708 |
|
| 709 |
return getDefaultMargin(c, isAtEdge, horizontal, leading); |
| 710 |
} |
| 711 |
|
| 712 |
int getMargin1(View view, boolean horizontal, boolean leading) { |
| 713 |
LayoutParams lp = getLayoutParams(view); |
| 714 |
int margin = horizontal ? |
| 715 |
(leading ? lp.leftMargin : lp.rightMargin) : |
| 716 |
(leading ? lp.topMargin : lp.bottomMargin); |
| 717 |
return margin == UNDEFINED ? getDefaultMargin(view, lp, horizontal, leading) : margin; |
| 718 |
} |
| 719 |
|
| 720 |
private int getMargin(View view, boolean horizontal, boolean leading) { |
| 721 |
if (mAlignmentMode == ALIGN_MARGINS) { |
| 722 |
return getMargin1(view, horizontal, leading); |
| 723 |
} else { |
| 724 |
Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
| 725 |
int[] margins = leading ? axis.getLeadingMargins() : axis.getTrailingMargins(); |
| 726 |
LayoutParams lp = getLayoutParams(view); |
| 727 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 728 |
int index = leading ? spec.span.min : spec.span.max; |
| 729 |
return margins[index]; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
private int getTotalMargin(View child, boolean horizontal) { |
| 734 |
return getMargin(child, horizontal, true) + getMargin(child, horizontal, false); |
| 735 |
} |
| 736 |
|
| 737 |
private static boolean fits(int[] a, int value, int start, int end) { |
| 738 |
if (end > a.length) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
for (int i = start; i < end; i++) { |
| 742 |
if (a[i] > value) { |
| 743 |
return false; |
| 744 |
} |
| 745 |
} |
| 746 |
return true; |
| 747 |
} |
| 748 |
|
| 749 |
private static void procrusteanFill(int[] a, int start, int end, int value) { |
| 750 |
int length = a.length; |
| 751 |
Arrays.fill(a, Math.min(start, length), Math.min(end, length), value); |
| 752 |
} |
| 753 |
|
| 754 |
private static void setCellGroup(LayoutParams lp, int row, int rowSpan, int col, int colSpan) { |
| 755 |
lp.setRowSpecSpan(new Interval(row, row + rowSpan)); |
| 756 |
lp.setColumnSpecSpan(new Interval(col, col + colSpan)); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
private static int clip(Interval minorRange, boolean minorWasDefined, int count) { |
| 761 |
int size = minorRange.size(); |
| 762 |
if (count == 0) { |
| 763 |
return size; |
| 764 |
} |
| 765 |
int min = minorWasDefined ? min(minorRange.min, count) : 0; |
| 766 |
return min(size, count - min); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
private void validateLayoutParams() { |
| 771 |
final boolean horizontal = (mOrientation == HORIZONTAL); |
| 772 |
final Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
| 773 |
final int count = (axis.definedCount != UNDEFINED) ? axis.definedCount : 0; |
| 774 |
|
| 775 |
int major = 0; |
| 776 |
int minor = 0; |
| 777 |
int[] maxSizes = new int[count]; |
| 778 |
|
| 779 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 780 |
LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); |
| 781 |
|
| 782 |
final Spec majorSpec = horizontal ? lp.rowSpec : lp.columnSpec; |
| 783 |
final Interval majorRange = majorSpec.span; |
| 784 |
final boolean majorWasDefined = majorSpec.startDefined; |
| 785 |
final int majorSpan = majorRange.size(); |
| 786 |
if (majorWasDefined) { |
| 787 |
major = majorRange.min; |
| 788 |
} |
| 789 |
|
| 790 |
final Spec minorSpec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 791 |
final Interval minorRange = minorSpec.span; |
| 792 |
final boolean minorWasDefined = minorSpec.startDefined; |
| 793 |
final int minorSpan = clip(minorRange, minorWasDefined, count); |
| 794 |
if (minorWasDefined) { |
| 795 |
minor = minorRange.min; |
| 796 |
} |
| 797 |
|
| 798 |
if (count != 0) { |
| 799 |
|
| 800 |
if (!majorWasDefined || !minorWasDefined) { |
| 801 |
while (!fits(maxSizes, major, minor, minor + minorSpan)) { |
| 802 |
if (minorWasDefined) { |
| 803 |
major++; |
| 804 |
} else { |
| 805 |
if (minor + minorSpan <= count) { |
| 806 |
minor++; |
| 807 |
} else { |
| 808 |
minor = 0; |
| 809 |
major++; |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
procrusteanFill(maxSizes, minor, minor + minorSpan, major + majorSpan); |
| 815 |
} |
| 816 |
|
| 817 |
if (horizontal) { |
| 818 |
setCellGroup(lp, major, majorSpan, minor, minorSpan); |
| 819 |
} else { |
| 820 |
setCellGroup(lp, minor, minorSpan, major, majorSpan); |
| 821 |
} |
| 822 |
|
| 823 |
minor = minor + minorSpan; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
private void invalidateStructure() { |
| 828 |
mLastLayoutParamsHashCode = UNINITIALIZED_HASH; |
| 829 |
mHorizontalAxis.invalidateStructure(); |
| 830 |
mVerticalAxis.invalidateStructure(); |
| 831 |
|
| 832 |
invalidateValues(); |
| 833 |
} |
| 834 |
|
| 835 |
private void invalidateValues() { |
| 836 |
|
| 837 |
|
| 838 |
if (mHorizontalAxis != null && mVerticalAxis != null) { |
| 839 |
mHorizontalAxis.invalidateValues(); |
| 840 |
mVerticalAxis.invalidateValues(); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
@Override |
| 846 |
protected void onSetLayoutParams(View child, ViewGroup.LayoutParams layoutParams) { |
| 847 |
super.onSetLayoutParams(child, layoutParams); |
| 848 |
|
| 849 |
if (!checkLayoutParams(layoutParams)) { |
| 850 |
handleInvalidParams("supplied LayoutParams are of the wrong type"); |
| 851 |
} |
| 852 |
|
| 853 |
invalidateStructure(); |
| 854 |
} |
| 855 |
|
| 856 |
final LayoutParams getLayoutParams(View c) { |
| 857 |
return (LayoutParams) c.getLayoutParams(); |
| 858 |
} |
| 859 |
|
| 860 |
private static void handleInvalidParams(String msg) { |
| 861 |
throw new IllegalArgumentException(msg + ". "); |
| 862 |
} |
| 863 |
|
| 864 |
private void checkLayoutParams(LayoutParams lp, boolean horizontal) { |
| 865 |
String groupName = horizontal ? "column" : "row"; |
| 866 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 867 |
Interval span = spec.span; |
| 868 |
if (span.min != UNDEFINED && span.min < 0) { |
| 869 |
handleInvalidParams(groupName + " indices must be positive"); |
| 870 |
} |
| 871 |
Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
| 872 |
int count = axis.definedCount; |
| 873 |
if (count != UNDEFINED) { |
| 874 |
if (span.max > count) { |
| 875 |
handleInvalidParams(groupName + |
| 876 |
" indices (start + span) mustn't exceed the " + groupName + " count"); |
| 877 |
} |
| 878 |
if (span.size() > count) { |
| 879 |
handleInvalidParams(groupName + " span mustn't exceed the " + groupName + " count"); |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
@Override |
| 885 |
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 886 |
if (!(p instanceof LayoutParams)) { |
| 887 |
return false; |
| 888 |
} |
| 889 |
LayoutParams lp = (LayoutParams) p; |
| 890 |
|
| 891 |
checkLayoutParams(lp, true); |
| 892 |
checkLayoutParams(lp, false); |
| 893 |
|
| 894 |
return true; |
| 895 |
} |
| 896 |
|
| 897 |
@Override |
| 898 |
protected LayoutParams generateDefaultLayoutParams() { |
| 899 |
return new LayoutParams(); |
| 900 |
} |
| 901 |
|
| 902 |
@Override |
| 903 |
public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 904 |
return new LayoutParams(getContext(), attrs); |
| 905 |
} |
| 906 |
|
| 907 |
@Override |
| 908 |
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 909 |
if (sPreserveMarginParamsInLayoutParamConversion) { |
| 910 |
if (lp instanceof LayoutParams) { |
| 911 |
return new LayoutParams((LayoutParams) lp); |
| 912 |
} else if (lp instanceof MarginLayoutParams) { |
| 913 |
return new LayoutParams((MarginLayoutParams) lp); |
| 914 |
} |
| 915 |
} |
| 916 |
return new LayoutParams(lp); |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
private void drawLine(Canvas graphics, int x1, int y1, int x2, int y2, Paint paint) { |
| 922 |
if (isLayoutRtl()) { |
| 923 |
int width = getWidth(); |
| 924 |
graphics.drawLine(width - x1, y1, width - x2, y2, paint); |
| 925 |
} else { |
| 926 |
graphics.drawLine(x1, y1, x2, y2, paint); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
@Override |
| 934 |
protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
| 935 |
|
| 936 |
LayoutParams lp = new LayoutParams(); |
| 937 |
for (int i = 0; i < getChildCount(); i++) { |
| 938 |
View c = getChildAt(i); |
| 939 |
lp.setMargins( |
| 940 |
getMargin1(c, true, true), |
| 941 |
getMargin1(c, false, true), |
| 942 |
getMargin1(c, true, false), |
| 943 |
getMargin1(c, false, false)); |
| 944 |
lp.onDebugDraw(c, canvas, paint); |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
@Override |
| 952 |
protected void onDebugDraw(Canvas canvas) { |
| 953 |
Paint paint = new Paint(); |
| 954 |
paint.setStyle(Paint.Style.STROKE); |
| 955 |
paint.setColor(Color.argb(50, 255, 255, 255)); |
| 956 |
|
| 957 |
Insets insets = getOpticalInsets(); |
| 958 |
|
| 959 |
int top = getPaddingTop() + insets.top; |
| 960 |
int left = getPaddingLeft() + insets.left; |
| 961 |
int right = getWidth() - getPaddingRight() - insets.right; |
| 962 |
int bottom = getHeight() - getPaddingBottom() - insets.bottom; |
| 963 |
|
| 964 |
int[] xs = mHorizontalAxis.locations; |
| 965 |
if (xs != null) { |
| 966 |
for (int i = 0, length = xs.length; i < length; i++) { |
| 967 |
int x = left + xs[i]; |
| 968 |
drawLine(canvas, x, top, x, bottom, paint); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
int[] ys = mVerticalAxis.locations; |
| 973 |
if (ys != null) { |
| 974 |
for (int i = 0, length = ys.length; i < length; i++) { |
| 975 |
int y = top + ys[i]; |
| 976 |
drawLine(canvas, left, y, right, y, paint); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
super.onDebugDraw(canvas); |
| 981 |
} |
| 982 |
|
| 983 |
@Override |
| 984 |
public void onViewAdded(View child) { |
| 985 |
super.onViewAdded(child); |
| 986 |
invalidateStructure(); |
| 987 |
} |
| 988 |
|
| 989 |
@Override |
| 990 |
public void onViewRemoved(View child) { |
| 991 |
super.onViewRemoved(child); |
| 992 |
invalidateStructure(); |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
@Override |
| 1002 |
protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
| 1003 |
super.onChildVisibilityChanged(child, oldVisibility, newVisibility); |
| 1004 |
if (oldVisibility == GONE || newVisibility == GONE) { |
| 1005 |
invalidateStructure(); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
private int computeLayoutParamsHashCode() { |
| 1010 |
int result = 1; |
| 1011 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1012 |
View c = getChildAt(i); |
| 1013 |
if (c.getVisibility() == View.GONE) continue; |
| 1014 |
LayoutParams lp = (LayoutParams) c.getLayoutParams(); |
| 1015 |
result = 31 * result + lp.hashCode(); |
| 1016 |
} |
| 1017 |
return result; |
| 1018 |
} |
| 1019 |
|
| 1020 |
private void consistencyCheck() { |
| 1021 |
if (mLastLayoutParamsHashCode == UNINITIALIZED_HASH) { |
| 1022 |
validateLayoutParams(); |
| 1023 |
mLastLayoutParamsHashCode = computeLayoutParamsHashCode(); |
| 1024 |
} else if (mLastLayoutParamsHashCode != computeLayoutParamsHashCode()) { |
| 1025 |
mPrinter.println("The fields of some layout parameters were modified in between " |
| 1026 |
+ "layout operations. Check the javadoc for GridLayout.LayoutParams#rowSpec."); |
| 1027 |
invalidateStructure(); |
| 1028 |
consistencyCheck(); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
private void measureChildWithMargins2(View child, int parentWidthSpec, int parentHeightSpec, |
| 1036 |
int childWidth, int childHeight) { |
| 1037 |
int childWidthSpec = getChildMeasureSpec(parentWidthSpec, |
| 1038 |
getTotalMargin(child, true), childWidth); |
| 1039 |
int childHeightSpec = getChildMeasureSpec(parentHeightSpec, |
| 1040 |
getTotalMargin(child, false), childHeight); |
| 1041 |
child.measure(childWidthSpec, childHeightSpec); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
private void measureChildrenWithMargins(int widthSpec, int heightSpec, boolean firstPass) { |
| 1046 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1047 |
View c = getChildAt(i); |
| 1048 |
if (c.getVisibility() == View.GONE) continue; |
| 1049 |
LayoutParams lp = getLayoutParams(c); |
| 1050 |
if (firstPass) { |
| 1051 |
measureChildWithMargins2(c, widthSpec, heightSpec, lp.width, lp.height); |
| 1052 |
} else { |
| 1053 |
boolean horizontal = (mOrientation == HORIZONTAL); |
| 1054 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1055 |
if (spec.getAbsoluteAlignment(horizontal) == FILL) { |
| 1056 |
Interval span = spec.span; |
| 1057 |
Axis axis = horizontal ? mHorizontalAxis : mVerticalAxis; |
| 1058 |
int[] locations = axis.getLocations(); |
| 1059 |
int cellSize = locations[span.max] - locations[span.min]; |
| 1060 |
int viewSize = cellSize - getTotalMargin(c, horizontal); |
| 1061 |
if (horizontal) { |
| 1062 |
measureChildWithMargins2(c, widthSpec, heightSpec, viewSize, lp.height); |
| 1063 |
} else { |
| 1064 |
measureChildWithMargins2(c, widthSpec, heightSpec, lp.width, viewSize); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
static int adjust(int measureSpec, int delta) { |
| 1072 |
return makeMeasureSpec( |
| 1073 |
MeasureSpec.getSize(measureSpec + delta), MeasureSpec.getMode(measureSpec)); |
| 1074 |
} |
| 1075 |
|
| 1076 |
@Override |
| 1077 |
protected void onMeasure(int widthSpec, int heightSpec) { |
| 1078 |
consistencyCheck(); |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
invalidateValues(); |
| 1083 |
|
| 1084 |
int hPadding = getPaddingLeft() + getPaddingRight(); |
| 1085 |
int vPadding = getPaddingTop() + getPaddingBottom(); |
| 1086 |
|
| 1087 |
int widthSpecSansPadding = adjust( widthSpec, -hPadding); |
| 1088 |
int heightSpecSansPadding = adjust(heightSpec, -vPadding); |
| 1089 |
|
| 1090 |
measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, true); |
| 1091 |
|
| 1092 |
int widthSansPadding; |
| 1093 |
int heightSansPadding; |
| 1094 |
|
| 1095 |
|
| 1096 |
if (mOrientation == HORIZONTAL) { |
| 1097 |
widthSansPadding = mHorizontalAxis.getMeasure(widthSpecSansPadding); |
| 1098 |
measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, false); |
| 1099 |
heightSansPadding = mVerticalAxis.getMeasure(heightSpecSansPadding); |
| 1100 |
} else { |
| 1101 |
heightSansPadding = mVerticalAxis.getMeasure(heightSpecSansPadding); |
| 1102 |
measureChildrenWithMargins(widthSpecSansPadding, heightSpecSansPadding, false); |
| 1103 |
widthSansPadding = mHorizontalAxis.getMeasure(widthSpecSansPadding); |
| 1104 |
} |
| 1105 |
|
| 1106 |
int measuredWidth = Math.max(widthSansPadding + hPadding, getSuggestedMinimumWidth()); |
| 1107 |
int measuredHeight = Math.max(heightSansPadding + vPadding, getSuggestedMinimumHeight()); |
| 1108 |
|
| 1109 |
setMeasuredDimension( |
| 1110 |
resolveSizeAndState(measuredWidth, widthSpec, 0), |
| 1111 |
resolveSizeAndState(measuredHeight, heightSpec, 0)); |
| 1112 |
} |
| 1113 |
|
| 1114 |
private int getMeasurement(View c, boolean horizontal) { |
| 1115 |
return horizontal ? c.getMeasuredWidth() : c.getMeasuredHeight(); |
| 1116 |
} |
| 1117 |
|
| 1118 |
final int getMeasurementIncludingMargin(View c, boolean horizontal) { |
| 1119 |
if (c.getVisibility() == View.GONE) { |
| 1120 |
return 0; |
| 1121 |
} |
| 1122 |
return getMeasurement(c, horizontal) + getTotalMargin(c, horizontal); |
| 1123 |
} |
| 1124 |
|
| 1125 |
@Override |
| 1126 |
public void requestLayout() { |
| 1127 |
super.requestLayout(); |
| 1128 |
invalidateValues(); |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
@Override |
| 1146 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 1147 |
consistencyCheck(); |
| 1148 |
|
| 1149 |
int targetWidth = right - left; |
| 1150 |
int targetHeight = bottom - top; |
| 1151 |
|
| 1152 |
int paddingLeft = getPaddingLeft(); |
| 1153 |
int paddingTop = getPaddingTop(); |
| 1154 |
int paddingRight = getPaddingRight(); |
| 1155 |
int paddingBottom = getPaddingBottom(); |
| 1156 |
|
| 1157 |
mHorizontalAxis.layout(targetWidth - paddingLeft - paddingRight); |
| 1158 |
mVerticalAxis.layout(targetHeight - paddingTop - paddingBottom); |
| 1159 |
|
| 1160 |
int[] hLocations = mHorizontalAxis.getLocations(); |
| 1161 |
int[] vLocations = mVerticalAxis.getLocations(); |
| 1162 |
|
| 1163 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1164 |
View c = getChildAt(i); |
| 1165 |
if (c.getVisibility() == View.GONE) continue; |
| 1166 |
LayoutParams lp = getLayoutParams(c); |
| 1167 |
Spec columnSpec = lp.columnSpec; |
| 1168 |
Spec rowSpec = lp.rowSpec; |
| 1169 |
|
| 1170 |
Interval colSpan = columnSpec.span; |
| 1171 |
Interval rowSpan = rowSpec.span; |
| 1172 |
|
| 1173 |
int x1 = hLocations[colSpan.min]; |
| 1174 |
int y1 = vLocations[rowSpan.min]; |
| 1175 |
|
| 1176 |
int x2 = hLocations[colSpan.max]; |
| 1177 |
int y2 = vLocations[rowSpan.max]; |
| 1178 |
|
| 1179 |
int cellWidth = x2 - x1; |
| 1180 |
int cellHeight = y2 - y1; |
| 1181 |
|
| 1182 |
int pWidth = getMeasurement(c, true); |
| 1183 |
int pHeight = getMeasurement(c, false); |
| 1184 |
|
| 1185 |
Alignment hAlign = columnSpec.getAbsoluteAlignment(true); |
| 1186 |
Alignment vAlign = rowSpec.getAbsoluteAlignment(false); |
| 1187 |
|
| 1188 |
Bounds boundsX = mHorizontalAxis.getGroupBounds().getValue(i); |
| 1189 |
Bounds boundsY = mVerticalAxis.getGroupBounds().getValue(i); |
| 1190 |
|
| 1191 |
|
| 1192 |
int gravityOffsetX = hAlign.getGravityOffset(c, cellWidth - boundsX.size(true)); |
| 1193 |
int gravityOffsetY = vAlign.getGravityOffset(c, cellHeight - boundsY.size(true)); |
| 1194 |
|
| 1195 |
int leftMargin = getMargin(c, true, true); |
| 1196 |
int topMargin = getMargin(c, false, true); |
| 1197 |
int rightMargin = getMargin(c, true, false); |
| 1198 |
int bottomMargin = getMargin(c, false, false); |
| 1199 |
|
| 1200 |
int sumMarginsX = leftMargin + rightMargin; |
| 1201 |
int sumMarginsY = topMargin + bottomMargin; |
| 1202 |
|
| 1203 |
|
| 1204 |
int alignmentOffsetX = boundsX.getOffset(this, c, hAlign, pWidth + sumMarginsX, true); |
| 1205 |
int alignmentOffsetY = boundsY.getOffset(this, c, vAlign, pHeight + sumMarginsY, false); |
| 1206 |
|
| 1207 |
int width = hAlign.getSizeInCell(c, pWidth, cellWidth - sumMarginsX); |
| 1208 |
int height = vAlign.getSizeInCell(c, pHeight, cellHeight - sumMarginsY); |
| 1209 |
|
| 1210 |
int dx = x1 + gravityOffsetX + alignmentOffsetX; |
| 1211 |
|
| 1212 |
int cx = !isLayoutRtl() ? paddingLeft + leftMargin + dx : |
| 1213 |
targetWidth - width - paddingRight - rightMargin - dx; |
| 1214 |
int cy = paddingTop + y1 + gravityOffsetY + alignmentOffsetY + topMargin; |
| 1215 |
|
| 1216 |
if (width != c.getMeasuredWidth() || height != c.getMeasuredHeight()) { |
| 1217 |
c.measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY)); |
| 1218 |
} |
| 1219 |
c.layout(cx, cy, cx + width, cy + height); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
@Override |
| 1224 |
public CharSequence getAccessibilityClassName() { |
| 1225 |
return GridLayout.class.getName(); |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
final class Axis { |
| 1237 |
private static final int NEW = 0; |
| 1238 |
private static final int PENDING = 1; |
| 1239 |
private static final int COMPLETE = 2; |
| 1240 |
|
| 1241 |
public final boolean horizontal; |
| 1242 |
|
| 1243 |
public int definedCount = UNDEFINED; |
| 1244 |
private int maxIndex = UNDEFINED; |
| 1245 |
|
| 1246 |
PackedMap<Spec, Bounds> groupBounds; |
| 1247 |
public boolean groupBoundsValid = false; |
| 1248 |
|
| 1249 |
PackedMap<Interval, MutableInt> forwardLinks; |
| 1250 |
public boolean forwardLinksValid = false; |
| 1251 |
|
| 1252 |
PackedMap<Interval, MutableInt> backwardLinks; |
| 1253 |
public boolean backwardLinksValid = false; |
| 1254 |
|
| 1255 |
public int[] leadingMargins; |
| 1256 |
public boolean leadingMarginsValid = false; |
| 1257 |
|
| 1258 |
public int[] trailingMargins; |
| 1259 |
public boolean trailingMarginsValid = false; |
| 1260 |
|
| 1261 |
public Arc[] arcs; |
| 1262 |
public boolean arcsValid = false; |
| 1263 |
|
| 1264 |
public int[] locations; |
| 1265 |
public boolean locationsValid = false; |
| 1266 |
|
| 1267 |
public boolean hasWeights; |
| 1268 |
public boolean hasWeightsValid = false; |
| 1269 |
public int[] deltas; |
| 1270 |
|
| 1271 |
boolean orderPreserved = DEFAULT_ORDER_PRESERVED; |
| 1272 |
|
| 1273 |
private MutableInt parentMin = new MutableInt(0); |
| 1274 |
private MutableInt parentMax = new MutableInt(-MAX_SIZE); |
| 1275 |
|
| 1276 |
private Axis(boolean horizontal) { |
| 1277 |
this.horizontal = horizontal; |
| 1278 |
} |
| 1279 |
|
| 1280 |
private int calculateMaxIndex() { |
| 1281 |
|
| 1282 |
int result = -1; |
| 1283 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1284 |
View c = getChildAt(i); |
| 1285 |
LayoutParams params = getLayoutParams(c); |
| 1286 |
Spec spec = horizontal ? params.columnSpec : params.rowSpec; |
| 1287 |
Interval span = spec.span; |
| 1288 |
result = max(result, span.min); |
| 1289 |
result = max(result, span.max); |
| 1290 |
result = max(result, span.size()); |
| 1291 |
} |
| 1292 |
return result == -1 ? UNDEFINED : result; |
| 1293 |
} |
| 1294 |
|
| 1295 |
private int getMaxIndex() { |
| 1296 |
if (maxIndex == UNDEFINED) { |
| 1297 |
maxIndex = max(0, calculateMaxIndex()); |
| 1298 |
} |
| 1299 |
return maxIndex; |
| 1300 |
} |
| 1301 |
|
| 1302 |
public int getCount() { |
| 1303 |
return max(definedCount, getMaxIndex()); |
| 1304 |
} |
| 1305 |
|
| 1306 |
public void setCount(int count) { |
| 1307 |
if (count != UNDEFINED && count < getMaxIndex()) { |
| 1308 |
handleInvalidParams((horizontal ? "column" : "row") + |
| 1309 |
"Count must be greater than or equal to the maximum of all grid indices " + |
| 1310 |
"(and spans) defined in the LayoutParams of each child"); |
| 1311 |
} |
| 1312 |
this.definedCount = count; |
| 1313 |
} |
| 1314 |
|
| 1315 |
public boolean isOrderPreserved() { |
| 1316 |
return orderPreserved; |
| 1317 |
} |
| 1318 |
|
| 1319 |
public void setOrderPreserved(boolean orderPreserved) { |
| 1320 |
this.orderPreserved = orderPreserved; |
| 1321 |
invalidateStructure(); |
| 1322 |
} |
| 1323 |
|
| 1324 |
private PackedMap<Spec, Bounds> createGroupBounds() { |
| 1325 |
Assoc<Spec, Bounds> assoc = Assoc.of(Spec.class, Bounds.class); |
| 1326 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1327 |
View c = getChildAt(i); |
| 1328 |
|
| 1329 |
LayoutParams lp = getLayoutParams(c); |
| 1330 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1331 |
Bounds bounds = spec.getAbsoluteAlignment(horizontal).getBounds(); |
| 1332 |
assoc.put(spec, bounds); |
| 1333 |
} |
| 1334 |
return assoc.pack(); |
| 1335 |
} |
| 1336 |
|
| 1337 |
private void computeGroupBounds() { |
| 1338 |
Bounds[] values = groupBounds.values; |
| 1339 |
for (int i = 0; i < values.length; i++) { |
| 1340 |
values[i].reset(); |
| 1341 |
} |
| 1342 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1343 |
View c = getChildAt(i); |
| 1344 |
|
| 1345 |
LayoutParams lp = getLayoutParams(c); |
| 1346 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1347 |
int size = getMeasurementIncludingMargin(c, horizontal) + |
| 1348 |
((spec.weight == 0) ? 0 : getDeltas()[i]); |
| 1349 |
groupBounds.getValue(i).include(GridLayout.this, c, spec, this, size); |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
public PackedMap<Spec, Bounds> getGroupBounds() { |
| 1354 |
if (groupBounds == null) { |
| 1355 |
groupBounds = createGroupBounds(); |
| 1356 |
} |
| 1357 |
if (!groupBoundsValid) { |
| 1358 |
computeGroupBounds(); |
| 1359 |
groupBoundsValid = true; |
| 1360 |
} |
| 1361 |
return groupBounds; |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
private PackedMap<Interval, MutableInt> createLinks(boolean min) { |
| 1366 |
Assoc<Interval, MutableInt> result = Assoc.of(Interval.class, MutableInt.class); |
| 1367 |
Spec[] keys = getGroupBounds().keys; |
| 1368 |
for (int i = 0, N = keys.length; i < N; i++) { |
| 1369 |
Interval span = min ? keys[i].span : keys[i].span.inverse(); |
| 1370 |
result.put(span, new MutableInt()); |
| 1371 |
} |
| 1372 |
return result.pack(); |
| 1373 |
} |
| 1374 |
|
| 1375 |
private void computeLinks(PackedMap<Interval, MutableInt> links, boolean min) { |
| 1376 |
MutableInt[] spans = links.values; |
| 1377 |
for (int i = 0; i < spans.length; i++) { |
| 1378 |
spans[i].reset(); |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
Bounds[] bounds = getGroupBounds().values; |
| 1383 |
for (int i = 0; i < bounds.length; i++) { |
| 1384 |
int size = bounds[i].size(min); |
| 1385 |
MutableInt valueHolder = links.getValue(i); |
| 1386 |
|
| 1387 |
valueHolder.value = max(valueHolder.value, min ? size : -size); |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
private PackedMap<Interval, MutableInt> getForwardLinks() { |
| 1392 |
if (forwardLinks == null) { |
| 1393 |
forwardLinks = createLinks(true); |
| 1394 |
} |
| 1395 |
if (!forwardLinksValid) { |
| 1396 |
computeLinks(forwardLinks, true); |
| 1397 |
forwardLinksValid = true; |
| 1398 |
} |
| 1399 |
return forwardLinks; |
| 1400 |
} |
| 1401 |
|
| 1402 |
private PackedMap<Interval, MutableInt> getBackwardLinks() { |
| 1403 |
if (backwardLinks == null) { |
| 1404 |
backwardLinks = createLinks(false); |
| 1405 |
} |
| 1406 |
if (!backwardLinksValid) { |
| 1407 |
computeLinks(backwardLinks, false); |
| 1408 |
backwardLinksValid = true; |
| 1409 |
} |
| 1410 |
return backwardLinks; |
| 1411 |
} |
| 1412 |
|
| 1413 |
private void include(List<Arc> arcs, Interval key, MutableInt size, |
| 1414 |
boolean ignoreIfAlreadyPresent) { |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
if (key.size() == 0) { |
| 1422 |
return; |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
if (ignoreIfAlreadyPresent) { |
| 1427 |
for (Arc arc : arcs) { |
| 1428 |
Interval span = arc.span; |
| 1429 |
if (span.equals(key)) { |
| 1430 |
return; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
} |
| 1434 |
arcs.add(new Arc(key, size)); |
| 1435 |
} |
| 1436 |
|
| 1437 |
private void include(List<Arc> arcs, Interval key, MutableInt size) { |
| 1438 |
include(arcs, key, size, true); |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
Arc[][] groupArcsByFirstVertex(Arc[] arcs) { |
| 1444 |
int N = getCount() + 1; |
| 1445 |
Arc[][] result = new Arc[N][]; |
| 1446 |
int[] sizes = new int[N]; |
| 1447 |
for (Arc arc : arcs) { |
| 1448 |
sizes[arc.span.min]++; |
| 1449 |
} |
| 1450 |
for (int i = 0; i < sizes.length; i++) { |
| 1451 |
result[i] = new Arc[sizes[i]]; |
| 1452 |
} |
| 1453 |
|
| 1454 |
Arrays.fill(sizes, 0); |
| 1455 |
for (Arc arc : arcs) { |
| 1456 |
int i = arc.span.min; |
| 1457 |
result[i][sizes[i]++] = arc; |
| 1458 |
} |
| 1459 |
|
| 1460 |
return result; |
| 1461 |
} |
| 1462 |
|
| 1463 |
private Arc[] topologicalSort(final Arc[] arcs) { |
| 1464 |
return new Object() { |
| 1465 |
Arc[] result = new Arc[arcs.length]; |
| 1466 |
int cursor = result.length - 1; |
| 1467 |
Arc[][] arcsByVertex = groupArcsByFirstVertex(arcs); |
| 1468 |
int[] visited = new int[getCount() + 1]; |
| 1469 |
|
| 1470 |
void walk(int loc) { |
| 1471 |
switch (visited[loc]) { |
| 1472 |
case NEW: { |
| 1473 |
visited[loc] = PENDING; |
| 1474 |
for (Arc arc : arcsByVertex[loc]) { |
| 1475 |
walk(arc.span.max); |
| 1476 |
result[cursor--] = arc; |
| 1477 |
} |
| 1478 |
visited[loc] = COMPLETE; |
| 1479 |
break; |
| 1480 |
} |
| 1481 |
case PENDING: { |
| 1482 |
|
| 1483 |
assert false; |
| 1484 |
break; |
| 1485 |
} |
| 1486 |
case COMPLETE: { |
| 1487 |
break; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
Arc[] sort() { |
| 1493 |
for (int loc = 0, N = arcsByVertex.length; loc < N; loc++) { |
| 1494 |
walk(loc); |
| 1495 |
} |
| 1496 |
assert cursor == -1; |
| 1497 |
return result; |
| 1498 |
} |
| 1499 |
}.sort(); |
| 1500 |
} |
| 1501 |
|
| 1502 |
private Arc[] topologicalSort(List<Arc> arcs) { |
| 1503 |
return topologicalSort(arcs.toArray(new Arc[arcs.size()])); |
| 1504 |
} |
| 1505 |
|
| 1506 |
private void addComponentSizes(List<Arc> result, PackedMap<Interval, MutableInt> links) { |
| 1507 |
for (int i = 0; i < links.keys.length; i++) { |
| 1508 |
Interval key = links.keys[i]; |
| 1509 |
include(result, key, links.values[i], false); |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
private Arc[] createArcs() { |
| 1514 |
List<Arc> mins = new ArrayList<Arc>(); |
| 1515 |
List<Arc> maxs = new ArrayList<Arc>(); |
| 1516 |
|
| 1517 |
|
| 1518 |
addComponentSizes(mins, getForwardLinks()); |
| 1519 |
|
| 1520 |
addComponentSizes(maxs, getBackwardLinks()); |
| 1521 |
|
| 1522 |
|
| 1523 |
if (orderPreserved) { |
| 1524 |
|
| 1525 |
for (int i = 0; i < getCount(); i++) { |
| 1526 |
include(mins, new Interval(i, i + 1), new MutableInt(0)); |
| 1527 |
} |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
int N = getCount(); |
| 1533 |
include(mins, new Interval(0, N), parentMin, false); |
| 1534 |
include(maxs, new Interval(N, 0), parentMax, false); |
| 1535 |
|
| 1536 |
|
| 1537 |
Arc[] sMins = topologicalSort(mins); |
| 1538 |
Arc[] sMaxs = topologicalSort(maxs); |
| 1539 |
|
| 1540 |
return append(sMins, sMaxs); |
| 1541 |
} |
| 1542 |
|
| 1543 |
private void computeArcs() { |
| 1544 |
|
| 1545 |
getForwardLinks(); |
| 1546 |
getBackwardLinks(); |
| 1547 |
} |
| 1548 |
|
| 1549 |
public Arc[] getArcs() { |
| 1550 |
if (arcs == null) { |
| 1551 |
arcs = createArcs(); |
| 1552 |
} |
| 1553 |
if (!arcsValid) { |
| 1554 |
computeArcs(); |
| 1555 |
arcsValid = true; |
| 1556 |
} |
| 1557 |
return arcs; |
| 1558 |
} |
| 1559 |
|
| 1560 |
private boolean relax(int[] locations, Arc entry) { |
| 1561 |
if (!entry.valid) { |
| 1562 |
return false; |
| 1563 |
} |
| 1564 |
Interval span = entry.span; |
| 1565 |
int u = span.min; |
| 1566 |
int v = span.max; |
| 1567 |
int value = entry.value.value; |
| 1568 |
int candidate = locations[u] + value; |
| 1569 |
if (candidate > locations[v]) { |
| 1570 |
locations[v] = candidate; |
| 1571 |
return true; |
| 1572 |
} |
| 1573 |
return false; |
| 1574 |
} |
| 1575 |
|
| 1576 |
private void init(int[] locations) { |
| 1577 |
Arrays.fill(locations, 0); |
| 1578 |
} |
| 1579 |
|
| 1580 |
private String arcsToString(List<Arc> arcs) { |
| 1581 |
String var = horizontal ? "x" : "y"; |
| 1582 |
StringBuilder result = new StringBuilder(); |
| 1583 |
boolean first = true; |
| 1584 |
for (Arc arc : arcs) { |
| 1585 |
if (first) { |
| 1586 |
first = false; |
| 1587 |
} else { |
| 1588 |
result = result.append(", "); |
| 1589 |
} |
| 1590 |
int src = arc.span.min; |
| 1591 |
int dst = arc.span.max; |
| 1592 |
int value = arc.value.value; |
| 1593 |
result.append((src < dst) ? |
| 1594 |
var + dst + "-" + var + src + ">=" + value : |
| 1595 |
var + src + "-" + var + dst + "<=" + -value); |
| 1596 |
|
| 1597 |
} |
| 1598 |
return result.toString(); |
| 1599 |
} |
| 1600 |
|
| 1601 |
private void logError(String axisName, Arc[] arcs, boolean[] culprits0) { |
| 1602 |
List<Arc> culprits = new ArrayList<Arc>(); |
| 1603 |
List<Arc> removed = new ArrayList<Arc>(); |
| 1604 |
for (int c = 0; c < arcs.length; c++) { |
| 1605 |
Arc arc = arcs[c]; |
| 1606 |
if (culprits0[c]) { |
| 1607 |
culprits.add(arc); |
| 1608 |
} |
| 1609 |
if (!arc.valid) { |
| 1610 |
removed.add(arc); |
| 1611 |
} |
| 1612 |
} |
| 1613 |
mPrinter.println(axisName + " constraints: " + arcsToString(culprits) + |
| 1614 |
" are inconsistent; permanently removing: " + arcsToString(removed) + ". "); |
| 1615 |
} |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
private boolean solve(Arc[] arcs, int[] locations) { |
| 1638 |
return solve(arcs, locations, true); |
| 1639 |
} |
| 1640 |
|
| 1641 |
private boolean solve(Arc[] arcs, int[] locations, boolean modifyOnError) { |
| 1642 |
String axisName = horizontal ? "horizontal" : "vertical"; |
| 1643 |
int N = getCount() + 1; |
| 1644 |
boolean[] originalCulprits = null; |
| 1645 |
|
| 1646 |
for (int p = 0; p < arcs.length; p++) { |
| 1647 |
init(locations); |
| 1648 |
|
| 1649 |
|
| 1650 |
for (int i = 0; i < N; i++) { |
| 1651 |
boolean changed = false; |
| 1652 |
for (int j = 0, length = arcs.length; j < length; j++) { |
| 1653 |
changed |= relax(locations, arcs[j]); |
| 1654 |
} |
| 1655 |
if (!changed) { |
| 1656 |
if (originalCulprits != null) { |
| 1657 |
logError(axisName, arcs, originalCulprits); |
| 1658 |
} |
| 1659 |
return true; |
| 1660 |
} |
| 1661 |
} |
| 1662 |
|
| 1663 |
if (!modifyOnError) { |
| 1664 |
return false; |
| 1665 |
} |
| 1666 |
|
| 1667 |
boolean[] culprits = new boolean[arcs.length]; |
| 1668 |
for (int i = 0; i < N; i++) { |
| 1669 |
for (int j = 0, length = arcs.length; j < length; j++) { |
| 1670 |
culprits[j] |= relax(locations, arcs[j]); |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
if (p == 0) { |
| 1675 |
originalCulprits = culprits; |
| 1676 |
} |
| 1677 |
|
| 1678 |
for (int i = 0; i < arcs.length; i++) { |
| 1679 |
if (culprits[i]) { |
| 1680 |
Arc arc = arcs[i]; |
| 1681 |
|
| 1682 |
if (arc.span.min < arc.span.max) { |
| 1683 |
continue; |
| 1684 |
} |
| 1685 |
arc.valid = false; |
| 1686 |
break; |
| 1687 |
} |
| 1688 |
} |
| 1689 |
} |
| 1690 |
return true; |
| 1691 |
} |
| 1692 |
|
| 1693 |
private void computeMargins(boolean leading) { |
| 1694 |
int[] margins = leading ? leadingMargins : trailingMargins; |
| 1695 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1696 |
View c = getChildAt(i); |
| 1697 |
if (c.getVisibility() == View.GONE) continue; |
| 1698 |
LayoutParams lp = getLayoutParams(c); |
| 1699 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1700 |
Interval span = spec.span; |
| 1701 |
int index = leading ? span.min : span.max; |
| 1702 |
margins[index] = max(margins[index], getMargin1(c, horizontal, leading)); |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
public int[] getLeadingMargins() { |
| 1709 |
if (leadingMargins == null) { |
| 1710 |
leadingMargins = new int[getCount() + 1]; |
| 1711 |
} |
| 1712 |
if (!leadingMarginsValid) { |
| 1713 |
computeMargins(true); |
| 1714 |
leadingMarginsValid = true; |
| 1715 |
} |
| 1716 |
return leadingMargins; |
| 1717 |
} |
| 1718 |
|
| 1719 |
public int[] getTrailingMargins() { |
| 1720 |
if (trailingMargins == null) { |
| 1721 |
trailingMargins = new int[getCount() + 1]; |
| 1722 |
} |
| 1723 |
if (!trailingMarginsValid) { |
| 1724 |
computeMargins(false); |
| 1725 |
trailingMarginsValid = true; |
| 1726 |
} |
| 1727 |
return trailingMargins; |
| 1728 |
} |
| 1729 |
|
| 1730 |
private boolean solve(int[] a) { |
| 1731 |
return solve(getArcs(), a); |
| 1732 |
} |
| 1733 |
|
| 1734 |
private boolean computeHasWeights() { |
| 1735 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1736 |
final View child = getChildAt(i); |
| 1737 |
if (child.getVisibility() == View.GONE) { |
| 1738 |
continue; |
| 1739 |
} |
| 1740 |
LayoutParams lp = getLayoutParams(child); |
| 1741 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1742 |
if (spec.weight != 0) { |
| 1743 |
return true; |
| 1744 |
} |
| 1745 |
} |
| 1746 |
return false; |
| 1747 |
} |
| 1748 |
|
| 1749 |
private boolean hasWeights() { |
| 1750 |
if (!hasWeightsValid) { |
| 1751 |
hasWeights = computeHasWeights(); |
| 1752 |
hasWeightsValid = true; |
| 1753 |
} |
| 1754 |
return hasWeights; |
| 1755 |
} |
| 1756 |
|
| 1757 |
public int[] getDeltas() { |
| 1758 |
if (deltas == null) { |
| 1759 |
deltas = new int[getChildCount()]; |
| 1760 |
} |
| 1761 |
return deltas; |
| 1762 |
} |
| 1763 |
|
| 1764 |
private void shareOutDelta(int totalDelta, float totalWeight) { |
| 1765 |
Arrays.fill(deltas, 0); |
| 1766 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1767 |
final View c = getChildAt(i); |
| 1768 |
if (c.getVisibility() == View.GONE) { |
| 1769 |
continue; |
| 1770 |
} |
| 1771 |
LayoutParams lp = getLayoutParams(c); |
| 1772 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1773 |
float weight = spec.weight; |
| 1774 |
if (weight != 0) { |
| 1775 |
int delta = Math.round((weight * totalDelta / totalWeight)); |
| 1776 |
deltas[i] = delta; |
| 1777 |
|
| 1778 |
|
| 1779 |
totalDelta -= delta; |
| 1780 |
totalWeight -= weight; |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
|
| 1785 |
private void solveAndDistributeSpace(int[] a) { |
| 1786 |
Arrays.fill(getDeltas(), 0); |
| 1787 |
solve(a); |
| 1788 |
int deltaMax = parentMin.value * getChildCount() + 1; |
| 1789 |
if (deltaMax < 2) { |
| 1790 |
return; |
| 1791 |
} |
| 1792 |
int deltaMin = 0; |
| 1793 |
|
| 1794 |
float totalWeight = calculateTotalWeight(); |
| 1795 |
|
| 1796 |
int validDelta = -1; |
| 1797 |
boolean validSolution = true; |
| 1798 |
|
| 1799 |
while(deltaMin < deltaMax) { |
| 1800 |
|
| 1801 |
final int delta = (int) (((long) deltaMin + deltaMax) / 2); |
| 1802 |
invalidateValues(); |
| 1803 |
shareOutDelta(delta, totalWeight); |
| 1804 |
validSolution = solve(getArcs(), a, false); |
| 1805 |
if (validSolution) { |
| 1806 |
validDelta = delta; |
| 1807 |
deltaMin = delta + 1; |
| 1808 |
} else { |
| 1809 |
deltaMax = delta; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
if (validDelta > 0 && !validSolution) { |
| 1813 |
|
| 1814 |
invalidateValues(); |
| 1815 |
shareOutDelta(validDelta, totalWeight); |
| 1816 |
solve(a); |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
private float calculateTotalWeight() { |
| 1821 |
float totalWeight = 0f; |
| 1822 |
for (int i = 0, N = getChildCount(); i < N; i++) { |
| 1823 |
View c = getChildAt(i); |
| 1824 |
if (c.getVisibility() == View.GONE) { |
| 1825 |
continue; |
| 1826 |
} |
| 1827 |
LayoutParams lp = getLayoutParams(c); |
| 1828 |
Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
| 1829 |
totalWeight += spec.weight; |
| 1830 |
} |
| 1831 |
return totalWeight; |
| 1832 |
} |
| 1833 |
|
| 1834 |
private void computeLocations(int[] a) { |
| 1835 |
if (!hasWeights()) { |
| 1836 |
solve(a); |
| 1837 |
} else { |
| 1838 |
solveAndDistributeSpace(a); |
| 1839 |
} |
| 1840 |
if (!orderPreserved) { |
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
int a0 = a[0]; |
| 1848 |
for (int i = 0, N = a.length; i < N; i++) { |
| 1849 |
a[i] = a[i] - a0; |
| 1850 |
} |
| 1851 |
} |
| 1852 |
} |
| 1853 |
|
| 1854 |
public int[] getLocations() { |
| 1855 |
if (locations == null) { |
| 1856 |
int N = getCount() + 1; |
| 1857 |
locations = new int[N]; |
| 1858 |
} |
| 1859 |
if (!locationsValid) { |
| 1860 |
computeLocations(locations); |
| 1861 |
locationsValid = true; |
| 1862 |
} |
| 1863 |
return locations; |
| 1864 |
} |
| 1865 |
|
| 1866 |
private int size(int[] locations) { |
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
return locations[getCount()]; |
| 1871 |
} |
| 1872 |
|
| 1873 |
private void setParentConstraints(int min, int max) { |
| 1874 |
parentMin.value = min; |
| 1875 |
parentMax.value = -max; |
| 1876 |
locationsValid = false; |
| 1877 |
} |
| 1878 |
|
| 1879 |
private int getMeasure(int min, int max) { |
| 1880 |
setParentConstraints(min, max); |
| 1881 |
return size(getLocations()); |
| 1882 |
} |
| 1883 |
|
| 1884 |
public int getMeasure(int measureSpec) { |
| 1885 |
int mode = MeasureSpec.getMode(measureSpec); |
| 1886 |
int size = MeasureSpec.getSize(measureSpec); |
| 1887 |
switch (mode) { |
| 1888 |
case MeasureSpec.UNSPECIFIED: { |
| 1889 |
return getMeasure(0, MAX_SIZE); |
| 1890 |
} |
| 1891 |
case MeasureSpec.EXACTLY: { |
| 1892 |
return getMeasure(size, size); |
| 1893 |
} |
| 1894 |
case MeasureSpec.AT_MOST: { |
| 1895 |
return getMeasure(0, size); |
| 1896 |
} |
| 1897 |
default: { |
| 1898 |
assert false; |
| 1899 |
return 0; |
| 1900 |
} |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
public void layout(int size) { |
| 1905 |
setParentConstraints(size, size); |
| 1906 |
getLocations(); |
| 1907 |
} |
| 1908 |
|
| 1909 |
public void invalidateStructure() { |
| 1910 |
maxIndex = UNDEFINED; |
| 1911 |
|
| 1912 |
groupBounds = null; |
| 1913 |
forwardLinks = null; |
| 1914 |
backwardLinks = null; |
| 1915 |
|
| 1916 |
leadingMargins = null; |
| 1917 |
trailingMargins = null; |
| 1918 |
arcs = null; |
| 1919 |
|
| 1920 |
locations = null; |
| 1921 |
|
| 1922 |
deltas = null; |
| 1923 |
hasWeightsValid = false; |
| 1924 |
|
| 1925 |
invalidateValues(); |
| 1926 |
} |
| 1927 |
|
| 1928 |
public void invalidateValues() { |
| 1929 |
groupBoundsValid = false; |
| 1930 |
forwardLinksValid = false; |
| 1931 |
backwardLinksValid = false; |
| 1932 |
|
| 1933 |
leadingMarginsValid = false; |
| 1934 |
trailingMarginsValid = false; |
| 1935 |
arcsValid = false; |
| 1936 |
|
| 1937 |
locationsValid = false; |
| 1938 |
} |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
public static class LayoutParams extends MarginLayoutParams { |
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
private static final int DEFAULT_WIDTH = WRAP_CONTENT; |
| 2025 |
private static final int DEFAULT_HEIGHT = WRAP_CONTENT; |
| 2026 |
private static final int DEFAULT_MARGIN = UNDEFINED; |
| 2027 |
private static final int DEFAULT_ROW = UNDEFINED; |
| 2028 |
private static final int DEFAULT_COLUMN = UNDEFINED; |
| 2029 |
private static final Interval DEFAULT_SPAN = new Interval(UNDEFINED, UNDEFINED + 1); |
| 2030 |
private static final int DEFAULT_SPAN_SIZE = DEFAULT_SPAN.size(); |
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
private static final int MARGIN = R.styleable.ViewGroup_MarginLayout_layout_margin; |
| 2035 |
private static final int LEFT_MARGIN = R.styleable.ViewGroup_MarginLayout_layout_marginLeft; |
| 2036 |
private static final int TOP_MARGIN = R.styleable.ViewGroup_MarginLayout_layout_marginTop; |
| 2037 |
private static final int RIGHT_MARGIN = |
| 2038 |
R.styleable.ViewGroup_MarginLayout_layout_marginRight; |
| 2039 |
private static final int BOTTOM_MARGIN = |
| 2040 |
R.styleable.ViewGroup_MarginLayout_layout_marginBottom; |
| 2041 |
private static final int COLUMN = R.styleable.GridLayout_Layout_layout_column; |
| 2042 |
private static final int COLUMN_SPAN = R.styleable.GridLayout_Layout_layout_columnSpan; |
| 2043 |
private static final int COLUMN_WEIGHT = R.styleable.GridLayout_Layout_layout_columnWeight; |
| 2044 |
|
| 2045 |
private static final int ROW = R.styleable.GridLayout_Layout_layout_row; |
| 2046 |
private static final int ROW_SPAN = R.styleable.GridLayout_Layout_layout_rowSpan; |
| 2047 |
private static final int ROW_WEIGHT = R.styleable.GridLayout_Layout_layout_rowWeight; |
| 2048 |
|
| 2049 |
private static final int GRAVITY = R.styleable.GridLayout_Layout_layout_gravity; |
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
public Spec rowSpec = Spec.UNDEFINED; |
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
public Spec columnSpec = Spec.UNDEFINED; |
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
private LayoutParams( |
| 2084 |
int width, int height, |
| 2085 |
int left, int top, int right, int bottom, |
| 2086 |
Spec rowSpec, Spec columnSpec) { |
| 2087 |
super(width, height); |
| 2088 |
setMargins(left, top, right, bottom); |
| 2089 |
this.rowSpec = rowSpec; |
| 2090 |
this.columnSpec = columnSpec; |
| 2091 |
} |
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
public LayoutParams(Spec rowSpec, Spec columnSpec) { |
| 2102 |
this(DEFAULT_WIDTH, DEFAULT_HEIGHT, |
| 2103 |
DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, |
| 2104 |
rowSpec, columnSpec); |
| 2105 |
} |
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
public LayoutParams() { |
| 2111 |
this(Spec.UNDEFINED, Spec.UNDEFINED); |
| 2112 |
} |
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
public LayoutParams(ViewGroup.LayoutParams params) { |
| 2120 |
super(params); |
| 2121 |
} |
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
public LayoutParams(MarginLayoutParams params) { |
| 2127 |
super(params); |
| 2128 |
} |
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
public LayoutParams(LayoutParams source) { |
| 2137 |
super(source); |
| 2138 |
|
| 2139 |
this.rowSpec = source.rowSpec; |
| 2140 |
this.columnSpec = source.columnSpec; |
| 2141 |
} |
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
public LayoutParams(Context context, AttributeSet attrs) { |
| 2152 |
super(context, attrs); |
| 2153 |
reInitSuper(context, attrs); |
| 2154 |
init(context, attrs); |
| 2155 |
} |
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
private void reInitSuper(Context context, AttributeSet attrs) { |
| 2169 |
TypedArray a = |
| 2170 |
context.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 2171 |
try { |
| 2172 |
int margin = a.getDimensionPixelSize(MARGIN, DEFAULT_MARGIN); |
| 2173 |
|
| 2174 |
this.leftMargin = a.getDimensionPixelSize(LEFT_MARGIN, margin); |
| 2175 |
this.topMargin = a.getDimensionPixelSize(TOP_MARGIN, margin); |
| 2176 |
this.rightMargin = a.getDimensionPixelSize(RIGHT_MARGIN, margin); |
| 2177 |
this.bottomMargin = a.getDimensionPixelSize(BOTTOM_MARGIN, margin); |
| 2178 |
} finally { |
| 2179 |
a.recycle(); |
| 2180 |
} |
| 2181 |
} |
| 2182 |
|
| 2183 |
private void init(Context context, AttributeSet attrs) { |
| 2184 |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridLayout_Layout); |
| 2185 |
try { |
| 2186 |
int gravity = a.getInt(GRAVITY, Gravity.NO_GRAVITY); |
| 2187 |
|
| 2188 |
int column = a.getInt(COLUMN, DEFAULT_COLUMN); |
| 2189 |
int colSpan = a.getInt(COLUMN_SPAN, DEFAULT_SPAN_SIZE); |
| 2190 |
float colWeight = a.getFloat(COLUMN_WEIGHT, Spec.DEFAULT_WEIGHT); |
| 2191 |
this.columnSpec = spec(column, colSpan, getAlignment(gravity, true), colWeight); |
| 2192 |
|
| 2193 |
int row = a.getInt(ROW, DEFAULT_ROW); |
| 2194 |
int rowSpan = a.getInt(ROW_SPAN, DEFAULT_SPAN_SIZE); |
| 2195 |
float rowWeight = a.getFloat(ROW_WEIGHT, Spec.DEFAULT_WEIGHT); |
| 2196 |
this.rowSpec = spec(row, rowSpan, getAlignment(gravity, false), rowWeight); |
| 2197 |
} finally { |
| 2198 |
a.recycle(); |
| 2199 |
} |
| 2200 |
} |
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
public void setGravity(int gravity) { |
| 2211 |
rowSpec = rowSpec.copyWriteAlignment(getAlignment(gravity, false)); |
| 2212 |
columnSpec = columnSpec.copyWriteAlignment(getAlignment(gravity, true)); |
| 2213 |
} |
| 2214 |
|
| 2215 |
@Override |
| 2216 |
protected void setBaseAttributes(TypedArray attributes, int widthAttr, int heightAttr) { |
| 2217 |
this.width = attributes.getLayoutDimension(widthAttr, DEFAULT_WIDTH); |
| 2218 |
this.height = attributes.getLayoutDimension(heightAttr, DEFAULT_HEIGHT); |
| 2219 |
} |
| 2220 |
|
| 2221 |
final void setRowSpecSpan(Interval span) { |
| 2222 |
rowSpec = rowSpec.copyWriteSpan(span); |
| 2223 |
} |
| 2224 |
|
| 2225 |
final void setColumnSpecSpan(Interval span) { |
| 2226 |
columnSpec = columnSpec.copyWriteSpan(span); |
| 2227 |
} |
| 2228 |
|
| 2229 |
@Override |
| 2230 |
public boolean equals(@Nullable Object o) { |
| 2231 |
if (this == o) return true; |
| 2232 |
if (o == null || getClass() != o.getClass()) return false; |
| 2233 |
|
| 2234 |
LayoutParams that = (LayoutParams) o; |
| 2235 |
|
| 2236 |
if (!columnSpec.equals(that.columnSpec)) return false; |
| 2237 |
if (!rowSpec.equals(that.rowSpec)) return false; |
| 2238 |
|
| 2239 |
return true; |
| 2240 |
} |
| 2241 |
|
| 2242 |
@Override |
| 2243 |
public int hashCode() { |
| 2244 |
int result = rowSpec.hashCode(); |
| 2245 |
result = 31 * result + columnSpec.hashCode(); |
| 2246 |
return result; |
| 2247 |
} |
| 2248 |
} |
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
final static class Arc { |
| 2255 |
public final Interval span; |
| 2256 |
public final MutableInt value; |
| 2257 |
public boolean valid = true; |
| 2258 |
|
| 2259 |
public Arc(Interval span, MutableInt value) { |
| 2260 |
this.span = span; |
| 2261 |
this.value = value; |
| 2262 |
} |
| 2263 |
|
| 2264 |
@Override |
| 2265 |
public String toString() { |
| 2266 |
return span + " " + (!valid ? "+>" : "->") + " " + value; |
| 2267 |
} |
| 2268 |
} |
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
final static class MutableInt { |
| 2273 |
public int value; |
| 2274 |
|
| 2275 |
public MutableInt() { |
| 2276 |
reset(); |
| 2277 |
} |
| 2278 |
|
| 2279 |
public MutableInt(int value) { |
| 2280 |
this.value = value; |
| 2281 |
} |
| 2282 |
|
| 2283 |
public void reset() { |
| 2284 |
value = Integer.MIN_VALUE; |
| 2285 |
} |
| 2286 |
|
| 2287 |
@Override |
| 2288 |
public String toString() { |
| 2289 |
return Integer.toString(value); |
| 2290 |
} |
| 2291 |
} |
| 2292 |
|
| 2293 |
final static class Assoc<K, V> extends ArrayList<Pair<K, V>> { |
| 2294 |
private final Class<K> keyType; |
| 2295 |
private final Class<V> valueType; |
| 2296 |
|
| 2297 |
private Assoc(Class<K> keyType, Class<V> valueType) { |
| 2298 |
this.keyType = keyType; |
| 2299 |
this.valueType = valueType; |
| 2300 |
} |
| 2301 |
|
| 2302 |
public static <K, V> Assoc<K, V> of(Class<K> keyType, Class<V> valueType) { |
| 2303 |
return new Assoc<K, V>(keyType, valueType); |
| 2304 |
} |
| 2305 |
|
| 2306 |
public void put(K key, V value) { |
| 2307 |
add(Pair.create(key, value)); |
| 2308 |
} |
| 2309 |
|
| 2310 |
@SuppressWarnings(value = "unchecked") |
| 2311 |
public PackedMap<K, V> pack() { |
| 2312 |
int N = size(); |
| 2313 |
K[] keys = (K[]) Array.newInstance(keyType, N); |
| 2314 |
V[] values = (V[]) Array.newInstance(valueType, N); |
| 2315 |
for (int i = 0; i < N; i++) { |
| 2316 |
keys[i] = get(i).first; |
| 2317 |
values[i] = get(i).second; |
| 2318 |
} |
| 2319 |
return new PackedMap<K, V>(keys, values); |
| 2320 |
} |
| 2321 |
} |
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
@SuppressWarnings(value = "unchecked") |
| 2338 |
final static class PackedMap<K, V> { |
| 2339 |
public final int[] index; |
| 2340 |
public final K[] keys; |
| 2341 |
public final V[] values; |
| 2342 |
|
| 2343 |
private PackedMap(K[] keys, V[] values) { |
| 2344 |
this.index = createIndex(keys); |
| 2345 |
|
| 2346 |
this.keys = compact(keys, index); |
| 2347 |
this.values = compact(values, index); |
| 2348 |
} |
| 2349 |
|
| 2350 |
public V getValue(int i) { |
| 2351 |
return values[index[i]]; |
| 2352 |
} |
| 2353 |
|
| 2354 |
private static <K> int[] createIndex(K[] keys) { |
| 2355 |
int size = keys.length; |
| 2356 |
int[] result = new int[size]; |
| 2357 |
|
| 2358 |
Map<K, Integer> keyToIndex = new HashMap<K, Integer>(); |
| 2359 |
for (int i = 0; i < size; i++) { |
| 2360 |
K key = keys[i]; |
| 2361 |
Integer index = keyToIndex.get(key); |
| 2362 |
if (index == null) { |
| 2363 |
index = keyToIndex.size(); |
| 2364 |
keyToIndex.put(key, index); |
| 2365 |
} |
| 2366 |
result[i] = index; |
| 2367 |
} |
| 2368 |
return result; |
| 2369 |
} |
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
private static <K> K[] compact(K[] a, int[] index) { |
| 2375 |
int size = a.length; |
| 2376 |
Class<?> componentType = a.getClass().getComponentType(); |
| 2377 |
K[] result = (K[]) Array.newInstance(componentType, max2(index, -1) + 1); |
| 2378 |
|
| 2379 |
|
| 2380 |
for (int i = 0; i < size; i++) { |
| 2381 |
result[index[i]] = a[i]; |
| 2382 |
} |
| 2383 |
return result; |
| 2384 |
} |
| 2385 |
} |
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
static class Bounds { |
| 2399 |
public int before; |
| 2400 |
public int after; |
| 2401 |
public int flexibility; |
| 2402 |
|
| 2403 |
private Bounds() { |
| 2404 |
reset(); |
| 2405 |
} |
| 2406 |
|
| 2407 |
protected void reset() { |
| 2408 |
before = Integer.MIN_VALUE; |
| 2409 |
after = Integer.MIN_VALUE; |
| 2410 |
flexibility = CAN_STRETCH; |
| 2411 |
} |
| 2412 |
|
| 2413 |
protected void include(int before, int after) { |
| 2414 |
this.before = max(this.before, before); |
| 2415 |
this.after = max(this.after, after); |
| 2416 |
} |
| 2417 |
|
| 2418 |
protected int size(boolean min) { |
| 2419 |
if (!min) { |
| 2420 |
if (canStretch(flexibility)) { |
| 2421 |
return MAX_SIZE; |
| 2422 |
} |
| 2423 |
} |
| 2424 |
return before + after; |
| 2425 |
} |
| 2426 |
|
| 2427 |
protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean horizontal) { |
| 2428 |
return before - a.getAlignmentValue(c, size, gl.getLayoutMode()); |
| 2429 |
} |
| 2430 |
|
| 2431 |
protected final void include(GridLayout gl, View c, Spec spec, Axis axis, int size) { |
| 2432 |
this.flexibility &= spec.getFlexibility(); |
| 2433 |
boolean horizontal = axis.horizontal; |
| 2434 |
Alignment alignment = spec.getAbsoluteAlignment(axis.horizontal); |
| 2435 |
|
| 2436 |
int before = alignment.getAlignmentValue(c, size, gl.getLayoutMode()); |
| 2437 |
include(before, size - before); |
| 2438 |
} |
| 2439 |
|
| 2440 |
@Override |
| 2441 |
public String toString() { |
| 2442 |
return "Bounds{" + |
| 2443 |
"before=" + before + |
| 2444 |
", after=" + after + |
| 2445 |
'}'; |
| 2446 |
} |
| 2447 |
} |
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
final static class Interval { |
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
public final int min; |
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
public final int max; |
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
public Interval(int min, int max) { |
| 2482 |
this.min = min; |
| 2483 |
this.max = max; |
| 2484 |
} |
| 2485 |
|
| 2486 |
int size() { |
| 2487 |
return max - min; |
| 2488 |
} |
| 2489 |
|
| 2490 |
Interval inverse() { |
| 2491 |
return new Interval(max, min); |
| 2492 |
} |
| 2493 |
|
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
@Override |
| 2505 |
public boolean equals(@Nullable Object that) { |
| 2506 |
if (this == that) { |
| 2507 |
return true; |
| 2508 |
} |
| 2509 |
if (that == null || getClass() != that.getClass()) { |
| 2510 |
return false; |
| 2511 |
} |
| 2512 |
|
| 2513 |
Interval interval = (Interval) that; |
| 2514 |
|
| 2515 |
if (max != interval.max) { |
| 2516 |
return false; |
| 2517 |
} |
| 2518 |
|
| 2519 |
if (min != interval.min) { |
| 2520 |
return false; |
| 2521 |
} |
| 2522 |
|
| 2523 |
return true; |
| 2524 |
} |
| 2525 |
|
| 2526 |
@Override |
| 2527 |
public int hashCode() { |
| 2528 |
int result = min; |
| 2529 |
result = 31 * result + max; |
| 2530 |
return result; |
| 2531 |
} |
| 2532 |
|
| 2533 |
@Override |
| 2534 |
public String toString() { |
| 2535 |
return "[" + min + ", " + max + "]"; |
| 2536 |
} |
| 2537 |
} |
| 2538 |
|
| 2539 |
|
| 2540 |
|
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
public static class Spec { |
| 2566 |
static final Spec UNDEFINED = spec(GridLayout.UNDEFINED); |
| 2567 |
static final float DEFAULT_WEIGHT = 0; |
| 2568 |
|
| 2569 |
final boolean startDefined; |
| 2570 |
final Interval span; |
| 2571 |
final Alignment alignment; |
| 2572 |
final float weight; |
| 2573 |
|
| 2574 |
private Spec(boolean startDefined, Interval span, Alignment alignment, float weight) { |
| 2575 |
this.startDefined = startDefined; |
| 2576 |
this.span = span; |
| 2577 |
this.alignment = alignment; |
| 2578 |
this.weight = weight; |
| 2579 |
} |
| 2580 |
|
| 2581 |
private Spec(boolean startDefined, int start, int size, Alignment alignment, float weight) { |
| 2582 |
this(startDefined, new Interval(start, start + size), alignment, weight); |
| 2583 |
} |
| 2584 |
|
| 2585 |
private Alignment getAbsoluteAlignment(boolean horizontal) { |
| 2586 |
if (alignment != UNDEFINED_ALIGNMENT) { |
| 2587 |
return alignment; |
| 2588 |
} |
| 2589 |
if (weight == 0f) { |
| 2590 |
return horizontal ? START : BASELINE; |
| 2591 |
} |
| 2592 |
return FILL; |
| 2593 |
} |
| 2594 |
|
| 2595 |
final Spec copyWriteSpan(Interval span) { |
| 2596 |
return new Spec(startDefined, span, alignment, weight); |
| 2597 |
} |
| 2598 |
|
| 2599 |
final Spec copyWriteAlignment(Alignment alignment) { |
| 2600 |
return new Spec(startDefined, span, alignment, weight); |
| 2601 |
} |
| 2602 |
|
| 2603 |
final int getFlexibility() { |
| 2604 |
return (alignment == UNDEFINED_ALIGNMENT && weight == 0) ? INFLEXIBLE : CAN_STRETCH; |
| 2605 |
} |
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
@Override |
| 2618 |
public boolean equals(@Nullable Object that) { |
| 2619 |
if (this == that) { |
| 2620 |
return true; |
| 2621 |
} |
| 2622 |
if (that == null || getClass() != that.getClass()) { |
| 2623 |
return false; |
| 2624 |
} |
| 2625 |
|
| 2626 |
Spec spec = (Spec) that; |
| 2627 |
|
| 2628 |
if (!alignment.equals(spec.alignment)) { |
| 2629 |
return false; |
| 2630 |
} |
| 2631 |
|
| 2632 |
if (!span.equals(spec.span)) { |
| 2633 |
return false; |
| 2634 |
} |
| 2635 |
|
| 2636 |
return true; |
| 2637 |
} |
| 2638 |
|
| 2639 |
@Override |
| 2640 |
public int hashCode() { |
| 2641 |
int result = span.hashCode(); |
| 2642 |
result = 31 * result + alignment.hashCode(); |
| 2643 |
return result; |
| 2644 |
} |
| 2645 |
} |
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
public static Spec spec(int start, int size, Alignment alignment, float weight) { |
| 2663 |
return new Spec(start != UNDEFINED, start, size, alignment, weight); |
| 2664 |
} |
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
public static Spec spec(int start, Alignment alignment, float weight) { |
| 2674 |
return spec(start, 1, alignment, weight); |
| 2675 |
} |
| 2676 |
|
| 2677 |
|
| 2678 |
|
| 2679 |
|
| 2680 |
|
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
public static Spec spec(int start, int size, float weight) { |
| 2687 |
return spec(start, size, UNDEFINED_ALIGNMENT, weight); |
| 2688 |
} |
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
public static Spec spec(int start, float weight) { |
| 2697 |
return spec(start, 1, weight); |
| 2698 |
} |
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
public static Spec spec(int start, int size, Alignment alignment) { |
| 2708 |
return spec(start, size, alignment, Spec.DEFAULT_WEIGHT); |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
public static Spec spec(int start, Alignment alignment) { |
| 2726 |
return spec(start, 1, alignment); |
| 2727 |
} |
| 2728 |
|
| 2729 |
|
| 2730 |
|
| 2731 |
|
| 2732 |
|
| 2733 |
|
| 2734 |
|
| 2735 |
|
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
|
| 2741 |
|
| 2742 |
public static Spec spec(int start, int size) { |
| 2743 |
return spec(start, size, UNDEFINED_ALIGNMENT); |
| 2744 |
} |
| 2745 |
|
| 2746 |
|
| 2747 |
|
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
public static Spec spec(int start) { |
| 2759 |
return spec(start, 1); |
| 2760 |
} |
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
|
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
public static abstract class Alignment { |
| 2784 |
Alignment() { |
| 2785 |
} |
| 2786 |
|
| 2787 |
abstract int getGravityOffset(View view, int cellDelta); |
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
abstract int getAlignmentValue(View view, int viewSize, int mode); |
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
int getSizeInCell(View view, int viewSize, int cellSize) { |
| 2815 |
return viewSize; |
| 2816 |
} |
| 2817 |
|
| 2818 |
Bounds getBounds() { |
| 2819 |
return new Bounds(); |
| 2820 |
} |
| 2821 |
} |
| 2822 |
|
| 2823 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2824 |
static final Alignment UNDEFINED_ALIGNMENT = new Alignment() { |
| 2825 |
@Override |
| 2826 |
int getGravityOffset(View view, int cellDelta) { |
| 2827 |
return UNDEFINED; |
| 2828 |
} |
| 2829 |
|
| 2830 |
@Override |
| 2831 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2832 |
return UNDEFINED; |
| 2833 |
} |
| 2834 |
}; |
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 2839 |
|
| 2840 |
private static final Alignment LEADING = new Alignment() { |
| 2841 |
@Override |
| 2842 |
int getGravityOffset(View view, int cellDelta) { |
| 2843 |
return 0; |
| 2844 |
} |
| 2845 |
|
| 2846 |
@Override |
| 2847 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2848 |
return 0; |
| 2849 |
} |
| 2850 |
}; |
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
private static final Alignment TRAILING = new Alignment() { |
| 2857 |
@Override |
| 2858 |
int getGravityOffset(View view, int cellDelta) { |
| 2859 |
return cellDelta; |
| 2860 |
} |
| 2861 |
|
| 2862 |
@Override |
| 2863 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2864 |
return viewSize; |
| 2865 |
} |
| 2866 |
}; |
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
public static final Alignment TOP = LEADING; |
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
public static final Alignment BOTTOM = TRAILING; |
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
public static final Alignment START = LEADING; |
| 2885 |
|
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
public static final Alignment END = TRAILING; |
| 2891 |
|
| 2892 |
private static Alignment createSwitchingAlignment(final Alignment ltr, final Alignment rtl) { |
| 2893 |
return new Alignment() { |
| 2894 |
@Override |
| 2895 |
int getGravityOffset(View view, int cellDelta) { |
| 2896 |
return (!view.isLayoutRtl() ? ltr : rtl).getGravityOffset(view, cellDelta); |
| 2897 |
} |
| 2898 |
|
| 2899 |
@Override |
| 2900 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2901 |
return (!view.isLayoutRtl() ? ltr : rtl).getAlignmentValue(view, viewSize, mode); |
| 2902 |
} |
| 2903 |
}; |
| 2904 |
} |
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
public static final Alignment LEFT = createSwitchingAlignment(START, END); |
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
public static final Alignment RIGHT = createSwitchingAlignment(END, START); |
| 2917 |
|
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
public static final Alignment CENTER = new Alignment() { |
| 2924 |
@Override |
| 2925 |
int getGravityOffset(View view, int cellDelta) { |
| 2926 |
return cellDelta >> 1; |
| 2927 |
} |
| 2928 |
|
| 2929 |
@Override |
| 2930 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2931 |
return viewSize >> 1; |
| 2932 |
} |
| 2933 |
}; |
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
|
| 2942 |
public static final Alignment BASELINE = new Alignment() { |
| 2943 |
@Override |
| 2944 |
int getGravityOffset(View view, int cellDelta) { |
| 2945 |
return 0; |
| 2946 |
} |
| 2947 |
|
| 2948 |
@Override |
| 2949 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 2950 |
if (view.getVisibility() == GONE) { |
| 2951 |
return 0; |
| 2952 |
} |
| 2953 |
int baseline = view.getBaseline(); |
| 2954 |
return baseline == -1 ? UNDEFINED : baseline; |
| 2955 |
} |
| 2956 |
|
| 2957 |
@Override |
| 2958 |
public Bounds getBounds() { |
| 2959 |
return new Bounds() { |
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
|
| 2966 |
private int size; |
| 2967 |
|
| 2968 |
@Override |
| 2969 |
protected void reset() { |
| 2970 |
super.reset(); |
| 2971 |
size = Integer.MIN_VALUE; |
| 2972 |
} |
| 2973 |
|
| 2974 |
@Override |
| 2975 |
protected void include(int before, int after) { |
| 2976 |
super.include(before, after); |
| 2977 |
size = max(size, before + after); |
| 2978 |
} |
| 2979 |
|
| 2980 |
@Override |
| 2981 |
protected int size(boolean min) { |
| 2982 |
return max(super.size(min), size); |
| 2983 |
} |
| 2984 |
|
| 2985 |
@Override |
| 2986 |
protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean hrz) { |
| 2987 |
return max(0, super.getOffset(gl, c, a, size, hrz)); |
| 2988 |
} |
| 2989 |
}; |
| 2990 |
} |
| 2991 |
}; |
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
|
| 2998 |
public static final Alignment FILL = new Alignment() { |
| 2999 |
@Override |
| 3000 |
int getGravityOffset(View view, int cellDelta) { |
| 3001 |
return 0; |
| 3002 |
} |
| 3003 |
|
| 3004 |
@Override |
| 3005 |
public int getAlignmentValue(View view, int viewSize, int mode) { |
| 3006 |
return UNDEFINED; |
| 3007 |
} |
| 3008 |
|
| 3009 |
@Override |
| 3010 |
public int getSizeInCell(View view, int viewSize, int cellSize) { |
| 3011 |
return cellSize; |
| 3012 |
} |
| 3013 |
}; |
| 3014 |
|
| 3015 |
static boolean canStretch(int flexibility) { |
| 3016 |
return (flexibility & CAN_STRETCH) != 0; |
| 3017 |
} |
| 3018 |
|
| 3019 |
private static final int INFLEXIBLE = 0; |
| 3020 |
private static final int CAN_STRETCH = 2; |
| 3021 |
} |