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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
|
/*
* Copyright (C) 2016 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 android.app;
import android.graphics.Rect;
import android.os.Build;
import android.transition.Transition;
import android.transition.TransitionListenerAdapter;
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.util.ArrayMap;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import com.android.internal.view.OneShotPreDrawListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Contains the Fragment Transition functionality for both ordered and reordered
* Fragment Transactions. With reordered fragment transactions, all Views have been
* added to the View hierarchy prior to calling startTransitions. With ordered
* fragment transactions, Views will be removed and added after calling startTransitions.
*/
class FragmentTransition {
/**
* The inverse of all BackStackRecord operation commands. This assumes that
* REPLACE operations have already been replaced by add/remove operations.
*/
private static final int[] INVERSE_OPS = {
BackStackRecord.OP_NULL, // inverse of OP_NULL (error)
BackStackRecord.OP_REMOVE, // inverse of OP_ADD
BackStackRecord.OP_NULL, // inverse of OP_REPLACE (error)
BackStackRecord.OP_ADD, // inverse of OP_REMOVE
BackStackRecord.OP_SHOW, // inverse of OP_HIDE
BackStackRecord.OP_HIDE, // inverse of OP_SHOW
BackStackRecord.OP_ATTACH, // inverse of OP_DETACH
BackStackRecord.OP_DETACH, // inverse of OP_ATTACH
BackStackRecord.OP_UNSET_PRIMARY_NAV, // inverse of OP_SET_PRIMARY_NAV
BackStackRecord.OP_SET_PRIMARY_NAV, // inverse of OP_UNSET_PRIMARY_NAV
};
/**
* The main entry point for Fragment Transitions, this starts the transitions
* set on the leaving Fragment's {@link Fragment#getExitTransition()}, the
* entering Fragment's {@link Fragment#getEnterTransition()} and
* {@link Fragment#getSharedElementEnterTransition()}. When popping,
* the leaving Fragment's {@link Fragment#getReturnTransition()} and
* {@link Fragment#getSharedElementReturnTransition()} and the entering
* {@link Fragment#getReenterTransition()} will be run.
* <p>
* With reordered Fragment Transitions, all Views have been added to the
* View hierarchy prior to calling this method. The incoming Fragment's Views
* will be INVISIBLE. With ordered Fragment Transitions, this method
* is called before any change has been made to the hierarchy. That means
* that the added Fragments have not created their Views yet and the hierarchy
* is unknown.
*
* @param fragmentManager The executing FragmentManagerImpl
* @param records The list of transactions being executed.
* @param isRecordPop For each transaction, whether it is a pop transaction or not.
* @param startIndex The first index into records and isRecordPop to execute as
* part of this transition.
* @param endIndex One past the last index into records and isRecordPop to execute
* as part of this transition.
* @param isReordered true if this is a reordered transaction, meaning that the
* Views of incoming fragments have been added. false if the
* transaction has yet to be run and Views haven't been created.
*/
static void startTransitions(FragmentManagerImpl fragmentManager,
ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop,
int startIndex, int endIndex, boolean isReordered) {
if (fragmentManager.mCurState < Fragment.CREATED) {
return;
}
SparseArray<FragmentContainerTransition> transitioningFragments =
new SparseArray<>();
for (int i = startIndex; i < endIndex; i++) {
final BackStackRecord record = records.get(i);
final boolean isPop = isRecordPop.get(i);
if (isPop) {
calculatePopFragments(record, transitioningFragments, isReordered);
} else {
calculateFragments(record, transitioningFragments, isReordered);
}
}
if (transitioningFragments.size() != 0) {
final View nonExistentView = new View(fragmentManager.mHost.getContext());
final int numContainers = transitioningFragments.size();
for (int i = 0; i < numContainers; i++) {
int containerId = transitioningFragments.keyAt(i);
ArrayMap<String, String> nameOverrides = calculateNameOverrides(containerId,
records, isRecordPop, startIndex, endIndex);
FragmentContainerTransition containerTransition = transitioningFragments.valueAt(i);
if (isReordered) {
configureTransitionsReordered(fragmentManager, containerId,
containerTransition, nonExistentView, nameOverrides);
} else {
configureTransitionsOrdered(fragmentManager, containerId,
containerTransition, nonExistentView, nameOverrides);
}
}
}
}
/**
* Iterates through the transactions that affect a given fragment container
* and tracks the shared element names across transactions. This is most useful
* in pop transactions where the names of shared elements are known.
*
* @param containerId The container ID that is executing the transition.
* @param records The list of transactions being executed.
* @param isRecordPop For each transaction, whether it is a pop transaction or not.
* @param startIndex The first index into records and isRecordPop to execute as
* part of this transition.
* @param endIndex One past the last index into records and isRecordPop to execute
* as part of this transition.
* @return A map from the initial shared element name to the final shared element name
* before any onMapSharedElements is run.
*/
private static ArrayMap<String, String> calculateNameOverrides(int containerId,
ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop,
int startIndex, int endIndex) {
ArrayMap<String, String> nameOverrides = new ArrayMap<>();
for (int recordNum = endIndex - 1; recordNum >= startIndex; recordNum--) {
final BackStackRecord record = records.get(recordNum);
if (!record.interactsWith(containerId)) {
continue;
}
final boolean isPop = isRecordPop.get(recordNum);
if (record.mSharedElementSourceNames != null) {
final int numSharedElements = record.mSharedElementSourceNames.size();
final ArrayList<String> sources;
final ArrayList<String> targets;
if (isPop) {
targets = record.mSharedElementSourceNames;
sources = record.mSharedElementTargetNames;
} else {
sources = record.mSharedElementSourceNames;
targets = record.mSharedElementTargetNames;
}
for (int i = 0; i < numSharedElements; i++) {
String sourceName = sources.get(i);
String targetName = targets.get(i);
String previousTarget = nameOverrides.remove(targetName);
if (previousTarget != null) {
nameOverrides.put(sourceName, previousTarget);
} else {
nameOverrides.put(sourceName, targetName);
}
}
}
}
return nameOverrides;
}
/**
* Configures a transition for a single fragment container for which the transaction was
* reordered. That means that all Fragment Views have been added and incoming fragment
* Views are marked invisible.
*
* @param fragmentManager The executing FragmentManagerImpl
* @param containerId The container ID that is executing the transition.
* @param fragments A structure holding the transitioning fragments in this container.
* @param nonExistentView A View that does not exist in the hierarchy. This is used to
* prevent transitions from acting on other Views when there is no
* other target.
* @param nameOverrides A map of the shared element names from the starting fragment to
* the final fragment's Views as given in
* {@link FragmentTransaction#addSharedElement(View, String)}.
*/
private static void configureTransitionsReordered(FragmentManagerImpl fragmentManager,
int containerId, FragmentContainerTransition fragments,
View nonExistentView, ArrayMap<String, String> nameOverrides) {
ViewGroup sceneRoot = null;
if (fragmentManager.mContainer.onHasView()) {
sceneRoot = fragmentManager.mContainer.onFindViewById(containerId);
}
if (sceneRoot == null) {
return;
}
final Fragment inFragment = fragments.lastIn;
final Fragment outFragment = fragments.firstOut;
final boolean inIsPop = fragments.lastInIsPop;
final boolean outIsPop = fragments.firstOutIsPop;
ArrayList<View> sharedElementsIn = new ArrayList<>();
ArrayList<View> sharedElementsOut = new ArrayList<>();
Transition enterTransition = getEnterTransition(inFragment, inIsPop);
Transition exitTransition = getExitTransition(outFragment, outIsPop);
TransitionSet sharedElementTransition = configureSharedElementsReordered(sceneRoot,
nonExistentView, nameOverrides, fragments, sharedElementsOut, sharedElementsIn,
enterTransition, exitTransition);
if (enterTransition == null && sharedElementTransition == null &&
exitTransition == null) {
return; // no transitions!
}
ArrayList<View> exitingViews = configureEnteringExitingViews(exitTransition,
outFragment, sharedElementsOut, nonExistentView);
ArrayList<View> enteringViews = configureEnteringExitingViews(enterTransition,
inFragment, sharedElementsIn, nonExistentView);
setViewVisibility(enteringViews, View.INVISIBLE);
Transition transition = mergeTransitions(enterTransition, exitTransition,
sharedElementTransition, inFragment, inIsPop);
if (transition != null) {
replaceHide(exitTransition, outFragment, exitingViews);
transition.setNameOverrides(nameOverrides);
scheduleRemoveTargets(transition,
enterTransition, enteringViews, exitTransition, exitingViews,
sharedElementTransition, sharedElementsIn);
TransitionManager.beginDelayedTransition(sceneRoot, transition);
setViewVisibility(enteringViews, View.VISIBLE);
// Swap the shared element targets
if (sharedElementTransition != null) {
sharedElementTransition.getTargets().clear();
sharedElementTransition.getTargets().addAll(sharedElementsIn);
replaceTargets(sharedElementTransition, sharedElementsOut, sharedElementsIn);
}
}
}
/**
* Configures a transition for a single fragment container for which the transaction was
* ordered. That means that the transaction has not been executed yet, so incoming
* Views are not yet known.
*
* @param fragmentManager The executing FragmentManagerImpl
* @param containerId The container ID that is executing the transition.
* @param fragments A structure holding the transitioning fragments in this container.
* @param nonExistentView A View that does not exist in the hierarchy. This is used to
* prevent transitions from acting on other Views when there is no
* other target.
* @param nameOverrides A map of the shared element names from the starting fragment to
* the final fragment's Views as given in
* {@link FragmentTransaction#addSharedElement(View, String)}.
*/
private static void configureTransitionsOrdered(FragmentManagerImpl fragmentManager,
int containerId, FragmentContainerTransition fragments,
View nonExistentView, ArrayMap<String, String> nameOverrides) {
ViewGroup sceneRoot = null;
if (fragmentManager.mContainer.onHasView()) {
sceneRoot = fragmentManager.mContainer.onFindViewById(containerId);
}
if (sceneRoot == null) {
return;
}
final Fragment inFragment = fragments.lastIn;
final Fragment outFragment = fragments.firstOut;
final boolean inIsPop = fragments.lastInIsPop;
final boolean outIsPop = fragments.firstOutIsPop;
Transition enterTransition = getEnterTransition(inFragment, inIsPop);
Transition exitTransition = getExitTransition(outFragment, outIsPop);
ArrayList<View> sharedElementsOut = new ArrayList<>();
ArrayList<View> sharedElementsIn = new ArrayList<>();
TransitionSet sharedElementTransition = configureSharedElementsOrdered(sceneRoot,
nonExistentView, nameOverrides, fragments, sharedElementsOut, sharedElementsIn,
enterTransition, exitTransition);
if (enterTransition == null && sharedElementTransition == null &&
exitTransition == null) {
return; // no transitions!
}
ArrayList<View> exitingViews = configureEnteringExitingViews(exitTransition,
outFragment, sharedElementsOut, nonExistentView);
if (exitingViews == null || exitingViews.isEmpty()) {
exitTransition = null;
}
if (enterTransition != null) {
// Ensure the entering transition doesn't target anything until the views are made
// visible
enterTransition.addTarget(nonExistentView);
}
Transition transition = mergeTransitions(enterTransition, exitTransition,
sharedElementTransition, inFragment, fragments.lastInIsPop);
if (transition != null) {
transition.setNameOverrides(nameOverrides);
final ArrayList<View> enteringViews = new ArrayList<>();
scheduleRemoveTargets(transition,
enterTransition, enteringViews, exitTransition, exitingViews,
sharedElementTransition, sharedElementsIn);
scheduleTargetChange(sceneRoot, inFragment, nonExistentView, sharedElementsIn,
enterTransition, enteringViews, exitTransition, exitingViews);
TransitionManager.beginDelayedTransition(sceneRoot, transition);
}
}
/**
* Replace hide operations with visibility changes on the exiting views. Instead of making
* the entire fragment's view GONE, make each exiting view INVISIBLE. At the end of the
* transition, make the fragment's view GONE.
*/
private static void replaceHide(Transition exitTransition, Fragment exitingFragment,
final ArrayList<View> exitingViews) {
if (exitingFragment != null && exitTransition != null && exitingFragment.mAdded
&& exitingFragment.mHidden && exitingFragment.mHiddenChanged) {
exitingFragment.setHideReplaced(true);
final View fragmentView = exitingFragment.getView();
OneShotPreDrawListener.add(exitingFragment.mContainer, () -> {
setViewVisibility(exitingViews, View.INVISIBLE);
});
exitTransition.addListener(new TransitionListenerAdapter() {
@Override
public void onTransitionEnd(Transition transition) {
transition.removeListener(this);
fragmentView.setVisibility(View.GONE);
setViewVisibility(exitingViews, View.VISIBLE);
}
});
}
}
/**
* This method is used for fragment transitions for ordered transactions to change the
* enter and exit transition targets after the call to
* {@link TransitionManager#beginDelayedTransition(ViewGroup, Transition)}. The exit transition
* must ensure that it does not target any Views and the enter transition must start targeting
* the Views of the incoming Fragment.
*
* @param sceneRoot The fragment container View
* @param inFragment The last fragment that is entering
* @param nonExistentView A view that does not exist in the hierarchy that is used as a
* transition target to ensure no View is targeted.
* @param sharedElementsIn The shared element Views of the incoming fragment
* @param enterTransition The enter transition of the incoming fragment
* @param enteringViews The entering Views of the incoming fragment
* @param exitTransition The exit transition of the outgoing fragment
* @param exitingViews The exiting views of the outgoing fragment
*/
private static void scheduleTargetChange(final ViewGroup sceneRoot,
final Fragment inFragment, final View nonExistentView,
final ArrayList<View> sharedElementsIn,
final Transition enterTransition, final ArrayList<View> enteringViews,
final Transition exitTransition, final ArrayList<View> exitingViews) {
OneShotPreDrawListener.add(sceneRoot, () -> {
if (enterTransition != null) {
enterTransition.removeTarget(nonExistentView);
ArrayList<View> views = configureEnteringExitingViews(
enterTransition, inFragment, sharedElementsIn, nonExistentView);
enteringViews.addAll(views);
}
if (exitingViews != null) {
if (exitTransition != null) {
ArrayList<View> tempExiting = new ArrayList<>();
tempExiting.add(nonExistentView);
replaceTargets(exitTransition, exitingViews, tempExiting);
}
exitingViews.clear();
exitingViews.add(nonExistentView);
}
});
}
/**
* Returns a TransitionSet containing the shared element transition. The wrapping TransitionSet
* targets all shared elements to ensure that no other Views are targeted. The shared element
* transition can then target any or all shared elements without worrying about accidentally
* targeting entering or exiting Views.
*
* @param inFragment The incoming fragment
* @param outFragment the outgoing fragment
* @param isPop True if this is a pop transaction or false if it is a normal (add) transaction.
* @return A TransitionSet wrapping the shared element transition or null if no such transition
* exists.
*/
private static TransitionSet getSharedElementTransition(Fragment inFragment,
Fragment outFragment, boolean isPop) {
if (inFragment == null || outFragment == null) {
return null;
}
Transition transition = cloneTransition(isPop
? outFragment.getSharedElementReturnTransition()
: inFragment.getSharedElementEnterTransition());
if (transition == null) {
return null;
}
TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(transition);
return transitionSet;
}
/**
* Returns a clone of the enter transition or null if no such transition exists.
*/
private static Transition getEnterTransition(Fragment inFragment, boolean isPop) {
if (inFragment == null) {
return null;
}
return cloneTransition(isPop ? inFragment.getReenterTransition() :
inFragment.getEnterTransition());
}
/**
* Returns a clone of the exit transition or null if no such transition exists.
*/
private static Transition getExitTransition(Fragment outFragment, boolean isPop) {
if (outFragment == null) {
return null;
}
return cloneTransition(isPop ? outFragment.getReturnTransition() :
outFragment.getExitTransition());
}
/**
* Returns a clone of a transition or null if it is null
*/
private static Transition cloneTransition(Transition transition) {
if (transition != null) {
transition = transition.clone();
}
return transition;
}
/**
* Configures the shared elements of an reordered fragment transaction's transition.
* This retrieves the shared elements of the outgoing and incoming fragments, maps the
* views, and sets up the epicenter on the transitions.
* <p>
* The epicenter of exit and shared element transitions is the first shared element
* in the outgoing fragment. The epicenter of the entering transition is the first shared
* element in the incoming fragment.
*
* @param sceneRoot The fragment container View
* @param nonExistentView A View that does not exist in the hierarchy. This is used to
* prevent transitions from acting on other Views when there is no
* other target.
* @param nameOverrides A map of the shared element names from the starting fragment to
* the final fragment's Views as given in
* {@link FragmentTransaction#addSharedElement(View, String)}.
* @param fragments A structure holding the transitioning fragments in this container.
* @param sharedElementsOut A list modified to contain the shared elements in the outgoing
* fragment
* @param sharedElementsIn A list modified to contain the shared elements in the incoming
* fragment
* @param enterTransition The transition used for entering Views, modified by applying the
* epicenter
* @param exitTransition The transition used for exiting Views, modified by applying the
* epicenter
* @return The shared element transition or null if no shared elements exist
*/
private static TransitionSet configureSharedElementsReordered(final ViewGroup sceneRoot,
final View nonExistentView, ArrayMap<String, String> nameOverrides,
final FragmentContainerTransition fragments,
final ArrayList<View> sharedElementsOut,
final ArrayList<View> sharedElementsIn,
final Transition enterTransition, final Transition exitTransition) {
final Fragment inFragment = fragments.lastIn;
final Fragment outFragment = fragments.firstOut;
if (inFragment != null) {
inFragment.getView().setVisibility(View.VISIBLE);
}
if (inFragment == null || outFragment == null) {
return null; // no shared element without a fragment
}
final boolean inIsPop = fragments.lastInIsPop;
TransitionSet sharedElementTransition = nameOverrides.isEmpty() ? null
: getSharedElementTransition(inFragment, outFragment, inIsPop);
ArrayMap<String, View> outSharedElements = captureOutSharedElements(nameOverrides,
sharedElementTransition, fragments);
ArrayMap<String, View> inSharedElements = captureInSharedElements(nameOverrides,
sharedElementTransition, fragments);
if (nameOverrides.isEmpty()) {
sharedElementTransition = null;
if (outSharedElements != null) {
outSharedElements.clear();
}
if (inSharedElements != null) {
inSharedElements.clear();
}
} else {
addSharedElementsWithMatchingNames(sharedElementsOut, outSharedElements,
nameOverrides.keySet());
addSharedElementsWithMatchingNames(sharedElementsIn, inSharedElements,
nameOverrides.values());
}
if (enterTransition == null && exitTransition == null && sharedElementTransition == null) {
// don't call onSharedElementStart/End since there is no transition
return null;
}
callSharedElementStartEnd(inFragment, outFragment, inIsPop, outSharedElements, true);
final Rect epicenter;
final View epicenterView;
if (sharedElementTransition != null) {
sharedElementsIn.add(nonExistentView);
setSharedElementTargets(sharedElementTransition, nonExistentView, sharedElementsOut);
final boolean outIsPop = fragments.firstOutIsPop;
final BackStackRecord outTransaction = fragments.firstOutTransaction;
setOutEpicenter(sharedElementTransition, exitTransition, outSharedElements, outIsPop,
outTransaction);
epicenter = new Rect();
epicenterView = getInEpicenterView(inSharedElements, fragments,
enterTransition, inIsPop);
if (epicenterView != null) {
enterTransition.setEpicenterCallback(new Transition.EpicenterCallback() {
@Override
public Rect onGetEpicenter(Transition transition) {
return epicenter;
}
});
}
} else {
epicenter = null;
epicenterView = null;
}
OneShotPreDrawListener.add(sceneRoot, () -> {
callSharedElementStartEnd(inFragment, outFragment, inIsPop,
inSharedElements, false);
if (epicenterView != null) {
epicenterView.getBoundsOnScreen(epicenter);
}
});
return sharedElementTransition;
}
/**
* Add Views from sharedElements into views that have the transitionName in the
* nameOverridesSet.
*
* @param views Views list to add shared elements to
* @param sharedElements List of shared elements
* @param nameOverridesSet The transition names for all views to be copied from
* sharedElements to views.
*/
private static void addSharedElementsWithMatchingNames(ArrayList<View> views,
ArrayMap<String, View> sharedElements, Collection<String> nameOverridesSet) {
for (int i = sharedElements.size() - 1; i >= 0; i--) {
View view = sharedElements.valueAt(i);
if (view != null && nameOverridesSet.contains(view.getTransitionName())) {
views.add(view);
}
}
}
/**
* Configures the shared elements of an ordered fragment transaction's transition.
* This retrieves the shared elements of the incoming fragments, and schedules capturing
* the incoming fragment's shared elements. It also maps the views, and sets up the epicenter
* on the transitions.
* <p>
* The epicenter of exit and shared element transitions is the first shared element
* in the outgoing fragment. The epicenter of the entering transition is the first shared
* element in the incoming fragment.
*
* @param sceneRoot The fragment container View
* @param nonExistentView A View that does not exist in the hierarchy. This is used to
* prevent transitions from acting on other Views when there is no
* other target.
* @param nameOverrides A map of the shared element names from the starting fragment to
* the final fragment's Views as given in
* {@link FragmentTransaction#addSharedElement(View, String)}.
* @param fragments A structure holding the transitioning fragments in this container.
* @param sharedElementsOut A list modified to contain the shared elements in the outgoing
* fragment
* @param sharedElementsIn A list modified to contain the shared elements in the incoming
* fragment
* @param enterTransition The transition used for entering Views, modified by applying the
* epicenter
* @param exitTransition The transition used for exiting Views, modified by applying the
* epicenter
* @return The shared element transition or null if no shared elements exist
*/
private static TransitionSet configureSharedElementsOrdered(final ViewGroup sceneRoot,
final View nonExistentView, ArrayMap<String, String> nameOverrides,
final FragmentContainerTransition fragments,
final ArrayList<View> sharedElementsOut,
final ArrayList<View> sharedElementsIn,
final Transition enterTransition, final Transition exitTransition) {
final Fragment inFragment = fragments.lastIn;
final Fragment outFragment = fragments.firstOut;
if (inFragment == null || outFragment == null) {
return null; // no transition
}
final boolean inIsPop = fragments.lastInIsPop;
TransitionSet sharedElementTransition = nameOverrides.isEmpty() ? null
: getSharedElementTransition(inFragment, outFragment, inIsPop);
ArrayMap<String, View> outSharedElements = captureOutSharedElements(nameOverrides,
sharedElementTransition, fragments);
if (nameOverrides.isEmpty()) {
sharedElementTransition = null;
} else {
sharedElementsOut.addAll(outSharedElements.values());
}
if (enterTransition == null && exitTransition == null && sharedElementTransition == null) {
// don't call onSharedElementStart/End since there is no transition
return null;
}
callSharedElementStartEnd(inFragment, outFragment, inIsPop, outSharedElements, true);
final Rect inEpicenter;
if (sharedElementTransition != null) {
inEpicenter = new Rect();
setSharedElementTargets(sharedElementTransition, nonExistentView, sharedElementsOut);
final boolean outIsPop = fragments.firstOutIsPop;
final BackStackRecord outTransaction = fragments.firstOutTransaction;
setOutEpicenter(sharedElementTransition, exitTransition, outSharedElements, outIsPop,
outTransaction);
if (enterTransition != null) {
enterTransition.setEpicenterCallback(new Transition.EpicenterCallback() {
@Override
public Rect onGetEpicenter(Transition transition) {
if (inEpicenter.isEmpty()) {
return null;
}
return inEpicenter;
}
});
}
} else {
inEpicenter = null;
}
TransitionSet finalSharedElementTransition = sharedElementTransition;
OneShotPreDrawListener.add(sceneRoot, () -> {
ArrayMap<String, View> inSharedElements = captureInSharedElements(
nameOverrides, finalSharedElementTransition, fragments);
if (inSharedElements != null) {
sharedElementsIn.addAll(inSharedElements.values());
sharedElementsIn.add(nonExistentView);
}
callSharedElementStartEnd(inFragment, outFragment, inIsPop,
inSharedElements, false);
if (finalSharedElementTransition != null) {
finalSharedElementTransition.getTargets().clear();
finalSharedElementTransition.getTargets().addAll(sharedElementsIn);
replaceTargets(finalSharedElementTransition, sharedElementsOut,
sharedElementsIn);
final View inEpicenterView = getInEpicenterView(inSharedElements,
fragments, enterTransition, inIsPop);
if (inEpicenterView != null) {
inEpicenterView.getBoundsOnScreen(inEpicenter);
}
}
});
return sharedElementTransition;
}
/**
* Finds the shared elements in the outgoing fragment. It also calls
* {@link SharedElementCallback#onMapSharedElements(List, Map)} to allow more control
* of the shared element mapping. {@code nameOverrides} is updated to match the
* actual transition name of the mapped shared elements.
*
* @param nameOverrides A map of the shared element names from the starting fragment to
* the final fragment's Views as given in
* {@link FragmentTransaction#addSharedElement(View, String)}.
* @param sharedElementTransition The shared element transition
* @param fragments A structure holding the transitioning fragments in this container.
* @return The mapping of shared element names to the Views in the hierarchy or null
* if there is no shared element transition.
*/
private static ArrayMap<String, View> captureOutSharedElements(
ArrayMap<String, String> nameOverrides, TransitionSet sharedElementTransition,
FragmentContainerTransition fragments) {
if (nameOverrides.isEmpty() || sharedElementTransition == null) {
nameOverrides.clear();
return null;
}
final Fragment outFragment = fragments.firstOut;
final ArrayMap<String, View> outSharedElements = new ArrayMap<>();
outFragment.getView().findNamedViews(outSharedElements);
final SharedElementCallback sharedElementCallback;
final ArrayList<String> names;
final BackStackRecord outTransaction = fragments.firstOutTransaction;
if (fragments.firstOutIsPop) {
sharedElementCallback = outFragment.getEnterTransitionCallback();
names = outTransaction.mSharedElementTargetNames;
} else {
sharedElementCallback = outFragment.getExitTransitionCallback();
names = outTransaction.mSharedElementSourceNames;
}
outSharedElements.retainAll(names);
if (sharedElementCallback != null) {
sharedElementCallback.onMapSharedElements(names, outSharedElements);
for (int i = names.size() - 1; i >= 0; i--) {
String name = names.get(i);
View view = outSharedElements.get(name);
if (view == null) {
nameOverrides.remove(name);
} else if (!name.equals(view.getTransitionName())) {
String targetValue = nameOverrides.remove(name);
nameOverrides.put(view.getTransitionName(), targetValue);
}
}
} else {
nameOverrides.retainAll(outSharedElements.keySet());
}
return outSharedElements;
}
/**
* Finds the shared elements in the incoming fragment. It also calls
* {@link SharedElementCallback#onMapSharedElements(List, Map)} to allow more control
* of the shared element mapping. {@code nameOverrides} is updated to match the
* actual transition name of the mapped shared elements.
*
* @param nameOverrides A map of the shared element names from the starting fragment to
* the final fragment's Views as given in
* {@link FragmentTransaction#addSharedElement(View, String)}.
* @param sharedElementTransition The shared element transition
* @param fragments A structure holding the transitioning fragments in this container.
* @return The mapping of shared element names to the Views in the hierarchy or null
* if there is no shared element transition.
*/
private static ArrayMap<String, View> captureInSharedElements(
ArrayMap<String, String> nameOverrides, TransitionSet sharedElementTransition,
FragmentContainerTransition fragments) {
Fragment inFragment = fragments.lastIn;
final View fragmentView = inFragment.getView();
if (nameOverrides.isEmpty() || sharedElementTransition == null || fragmentView == null) {
nameOverrides.clear();
return null;
}
final ArrayMap<String, View> inSharedElements = new ArrayMap<>();
fragmentView.findNamedViews(inSharedElements);
final SharedElementCallback sharedElementCallback;
final ArrayList<String> names;
final BackStackRecord inTransaction = fragments.lastInTransaction;
if (fragments.lastInIsPop) {
sharedElementCallback = inFragment.getExitTransitionCallback();
names = inTransaction.mSharedElementSourceNames;
} else {
sharedElementCallback = inFragment.getEnterTransitionCallback();
names = inTransaction.mSharedElementTargetNames;
}
if (names != null) {
inSharedElements.retainAll(names);
}
if (names != null && sharedElementCallback != null) {
sharedElementCallback.onMapSharedElements(names, inSharedElements);
for (int i = names.size() - 1; i >= 0; i--) {
String name = names.get(i);
View view = inSharedElements.get(name);
if (view == null) {
String key = findKeyForValue(nameOverrides, name);
if (key != null) {
nameOverrides.remove(key);
}
} else if (!name.equals(view.getTransitionName())) {
String key = findKeyForValue(nameOverrides, name);
if (key != null) {
nameOverrides.put(key, view.getTransitionName());
}
}
}
} else {
retainValues(nameOverrides, inSharedElements);
}
return inSharedElements;
}
/**
* Utility to find the String key in {@code map} that maps to {@code value}.
*/
private static String findKeyForValue(ArrayMap<String, String> map, String value) {
final int numElements = map.size();
for (int i = 0; i < numElements; i++) {
if (value.equals(map.valueAt(i))) {
return map.keyAt(i);
}
}
return null;
}
/**
* Returns the View in the incoming Fragment that should be used as the epicenter.
*
* @param inSharedElements The mapping of shared element names to Views in the
* incoming fragment.
* @param fragments A structure holding the transitioning fragments in this container.
* @param enterTransition The transition used for the incoming Fragment's views
* @param inIsPop Is the incoming fragment being added as a pop transaction?
*/
private static View getInEpicenterView(ArrayMap<String, View> inSharedElements,
FragmentContainerTransition fragments,
Transition enterTransition, boolean inIsPop) {
BackStackRecord inTransaction = fragments.lastInTransaction;
if (enterTransition != null && inSharedElements != null
&& inTransaction.mSharedElementSourceNames != null
&& !inTransaction.mSharedElementSourceNames.isEmpty()) {
final String targetName = inIsPop
? inTransaction.mSharedElementSourceNames.get(0)
: inTransaction.mSharedElementTargetNames.get(0);
return inSharedElements.get(targetName);
}
return null;
}
/**
* Sets the epicenter for the exit transition.
*
* @param sharedElementTransition The shared element transition
* @param exitTransition The transition for the outgoing fragment's views
* @param outSharedElements Shared elements in the outgoing fragment
* @param outIsPop Is the outgoing fragment being removed as a pop transaction?
* @param outTransaction The transaction that caused the fragment to be removed.
*/
private static void setOutEpicenter(TransitionSet sharedElementTransition,
Transition exitTransition, ArrayMap<String, View> outSharedElements, boolean outIsPop,
BackStackRecord outTransaction) {
if (outTransaction.mSharedElementSourceNames != null &&
!outTransaction.mSharedElementSourceNames.isEmpty()) {
final String sourceName = outIsPop
? outTransaction.mSharedElementTargetNames.get(0)
: outTransaction.mSharedElementSourceNames.get(0);
final View outEpicenterView = outSharedElements.get(sourceName);
setEpicenter(sharedElementTransition, outEpicenterView);
if (exitTransition != null) {
setEpicenter(exitTransition, outEpicenterView);
}
}
}
/**
* Sets a transition epicenter to the rectangle of a given View.
*/
private static void setEpicenter(Transition transition, View view) {
if (view != null) {
final Rect epicenter = new Rect();
view.getBoundsOnScreen(epicenter);
transition.setEpicenterCallback(new Transition.EpicenterCallback() {
@Override
public Rect onGetEpicenter(Transition transition) {
return epicenter;
}
});
}
}
/**
* A utility to retain only the mappings in {@code nameOverrides} that have a value
* that has a key in {@code namedViews}. This is a useful equivalent to
* {@link ArrayMap#retainAll(Collection)} for values.
*/
private static void retainValues(ArrayMap<String, String> nameOverrides,
ArrayMap<String, View> namedViews) {
for (int i = nameOverrides.size() - 1; i >= 0; i--) {
final String targetName = nameOverrides.valueAt(i);
if (!namedViews.containsKey(targetName)) {
nameOverrides.removeAt(i);
}
}
}
/**
* Calls the {@link SharedElementCallback#onSharedElementStart(List, List, List)} or
* {@link SharedElementCallback#onSharedElementEnd(List, List, List)} on the appropriate
* incoming or outgoing fragment.
*
* @param inFragment The incoming fragment
* @param outFragment The outgoing fragment
* @param isPop Is the incoming fragment part of a pop transaction?
* @param sharedElements The shared element Views
* @param isStart Call the start or end call on the SharedElementCallback
*/
private static void callSharedElementStartEnd(Fragment inFragment, Fragment outFragment,
boolean isPop, ArrayMap<String, View> sharedElements, boolean isStart) {
SharedElementCallback sharedElementCallback = isPop
? outFragment.getEnterTransitionCallback()
: inFragment.getEnterTransitionCallback();
if (sharedElementCallback != null) {
ArrayList<View> views = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
final int count = sharedElements == null ? 0 : sharedElements.size();
for (int i = 0; i < count; i++) {
names.add(sharedElements.keyAt(i));
views.add(sharedElements.valueAt(i));
}
if (isStart) {
sharedElementCallback.onSharedElementStart(names, views, null);
} else {
sharedElementCallback.onSharedElementEnd(names, views, null);
}
}
}
/**
* Finds all children of the shared elements and sets the wrapping TransitionSet
* targets to point to those. It also limits transitions that have no targets to the
* specific shared elements. This allows developers to target child views of the
* shared elements specifically, but this doesn't happen by default.
*/
private static void setSharedElementTargets(TransitionSet transition,
View nonExistentView, ArrayList<View> sharedViews) {
final List<View> views = transition.getTargets();
views.clear();
final int count = sharedViews.size();
for (int i = 0; i < count; i++) {
final View view = sharedViews.get(i);
bfsAddViewChildren(views, view);
}
views.add(nonExistentView);
sharedViews.add(nonExistentView);
addTargets(transition, sharedViews);
}
/**
* Uses a breadth-first scheme to add startView and all of its children to views.
* It won't add a child if it is already in views.
*/
private static void bfsAddViewChildren(final List<View> views, final View startView) {
final int startIndex = views.size();
if (containedBeforeIndex(views, startView, startIndex)) {
return; // This child is already in the list, so all its children are also.
}
views.add(startView);
for (int index = startIndex; index < views.size(); index++) {
final View view = views.get(index);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
final int childCount = viewGroup.getChildCount();
for (int childIndex = 0; childIndex < childCount; childIndex++) {
final View child = viewGroup.getChildAt(childIndex);
if (!containedBeforeIndex(views, child, startIndex)) {
views.add(child);
}
}
}
}
}
/**
* Does a linear search through views for view, limited to maxIndex.
*/
private static boolean containedBeforeIndex(final List<View> views, final View view,
final int maxIndex) {
for (int i = 0; i < maxIndex; i++) {
if (views.get(i) == view) {
return true;
}
}
return false;
}
/**
* After the transition has started, remove all targets that we added to the transitions
* so that the transitions are left in a clean state.
*/
private static void scheduleRemoveTargets(final Transition overalTransition,
final Transition enterTransition, final ArrayList<View> enteringViews,
final Transition exitTransition, final ArrayList<View> exitingViews,
final TransitionSet sharedElementTransition, final ArrayList<View> sharedElementsIn) {
overalTransition.addListener(new TransitionListenerAdapter() {
@Override
public void onTransitionStart(Transition transition) {
if (enterTransition != null) {
replaceTargets(enterTransition, enteringViews, null);
}
if (exitTransition != null) {
replaceTargets(exitTransition, exitingViews, null);
}
if (sharedElementTransition != null) {
replaceTargets(sharedElementTransition, sharedElementsIn, null);
}
}
@Override
public void onTransitionEnd(Transition transition) {
transition.removeListener(this);
}
});
}
/**
* This method removes the views from transitions that target ONLY those views and
* replaces them with the new targets list.
* The views list should match those added in addTargets and should contain
* one view that is not in the view hierarchy (state.nonExistentView).
*/
public static void replaceTargets(Transition transition, ArrayList<View> oldTargets,
ArrayList<View> newTargets) {
if (transition instanceof TransitionSet) {
TransitionSet set = (TransitionSet) transition;
int numTransitions = set.getTransitionCount();
for (int i = 0; i < numTransitions; i++) {
Transition child = set.getTransitionAt(i);
replaceTargets(child, oldTargets, newTargets);
}
} else if (!hasSimpleTarget(transition)) {
List<View> targets = transition.getTargets();
if (targets != null && targets.size() == oldTargets.size() &&
targets.containsAll(oldTargets)) {
// We have an exact match. We must have added these earlier in addTargets
final int targetCount = newTargets == null ? 0 : newTargets.size();
for (int i = 0; i < targetCount; i++) {
transition.addTarget(newTargets.get(i));
}
for (int i = oldTargets.size() - 1; i >= 0; i--) {
transition.removeTarget(oldTargets.get(i));
}
}
}
}
/**
* This method adds views as targets to the transition, but only if the transition
* doesn't already have a target. It is best for views to contain one View object
* that does not exist in the view hierarchy (state.nonExistentView) so that
* when they are removed later, a list match will suffice to remove the targets.
* Otherwise, if you happened to have targeted the exact views for the transition,
* the replaceTargets call will remove them unexpectedly.
*/
public static void addTargets(Transition transition, ArrayList<View> views) {
if (transition == null) {
return;
}
if (transition instanceof TransitionSet) {
TransitionSet set = (TransitionSet) transition;
int numTransitions = set.getTransitionCount();
for (int i = 0; i < numTransitions; i++) {
Transition child = set.getTransitionAt(i);
addTargets(child, views);
}
} else if (!hasSimpleTarget(transition)) {
List<View> targets = transition.getTargets();
if (isNullOrEmpty(targets)) {
// We can just add the target views
int numViews = views.size();
for (int i = 0; i < numViews; i++) {
transition.addTarget(views.get(i));
}
}
}
}
/**
* Returns true if there are any targets based on ID, transition or type.
*/
private static boolean hasSimpleTarget(Transition transition) {
return !isNullOrEmpty(transition.getTargetIds()) ||
!isNullOrEmpty(transition.getTargetNames()) ||
!isNullOrEmpty(transition.getTargetTypes());
}
/**
* Simple utility to detect if a list is null or has no elements.
*/
private static boolean isNullOrEmpty(List list) {
return list == null || list.isEmpty();
}
private static ArrayList<View> configureEnteringExitingViews(Transition transition,
Fragment fragment, ArrayList<View> sharedElements, View nonExistentView) {
ArrayList<View> viewList = null;
if (transition != null) {
viewList = new ArrayList<>();
View root = fragment.getView();
if (root != null) {
root.captureTransitioningViews(viewList);
}
if (sharedElements != null) {
viewList.removeAll(sharedElements);
}
if (!viewList.isEmpty()) {
viewList.add(nonExistentView);
addTargets(transition, viewList);
}
}
return viewList;
}
/**
* Sets the visibility of all Views in {@code views} to {@code visibility}.
*/
private static void setViewVisibility(ArrayList<View> views, @View.Visibility int visibility) {
if (views == null) {
return;
}
for (int i = views.size() - 1; i >= 0; i--) {
final View view = views.get(i);
view.setVisibility(visibility);
}
}
/**
* Merges exit, shared element, and enter transitions so that they act together or
* sequentially as defined in the fragments.
*/
private static Transition mergeTransitions(Transition enterTransition,
Transition exitTransition, Transition sharedElementTransition, Fragment inFragment,
boolean isPop) {
boolean overlap = true;
if (enterTransition != null && exitTransition != null && inFragment != null) {
overlap = isPop ? inFragment.getAllowReturnTransitionOverlap() :
inFragment.getAllowEnterTransitionOverlap();
}
// Wrap the transitions. Explicit targets like in enter and exit will cause the
// views to be targeted regardless of excluded views. If that happens, then the
// excluded fragments views (hidden fragments) will still be in the transition.
Transition transition;
if (overlap) {
// Regular transition -- do it all together
TransitionSet transitionSet = new TransitionSet();
if (enterTransition != null) {
transitionSet.addTransition(enterTransition);
}
if (exitTransition != null) {
transitionSet.addTransition(exitTransition);
}
if (sharedElementTransition != null) {
transitionSet.addTransition(sharedElementTransition);
}
transition = transitionSet;
} else {
// First do exit, then enter, but allow shared element transition to happen
// during both.
Transition staggered = null;
if (exitTransition != null && enterTransition != null) {
staggered = new TransitionSet()
.addTransition(exitTransition)
.addTransition(enterTransition)
.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
} else if (exitTransition != null) {
staggered = exitTransition;
} else if (enterTransition != null) {
staggered = enterTransition;
}
if (sharedElementTransition != null) {
TransitionSet together = new TransitionSet();
if (staggered != null) {
together.addTransition(staggered);
}
together.addTransition(sharedElementTransition);
transition = together;
} else {
transition = staggered;
}
}
return transition;
}
/**
* Finds the first removed fragment and last added fragments when going forward.
* If none of the fragments have transitions, then both lists will be empty.
*
* @param transitioningFragments Keyed on the container ID, the first fragments to be removed,
* and last fragments to be added. This will be modified by
* this method.
*/
public static void calculateFragments(BackStackRecord transaction,
SparseArray<FragmentContainerTransition> transitioningFragments,
boolean isReordered) {
final int numOps = transaction.mOps.size();
for (int opNum = 0; opNum < numOps; opNum++) {
final BackStackRecord.Op op = transaction.mOps.get(opNum);
addToFirstInLastOut(transaction, op, transitioningFragments, false, isReordered);
}
}
/**
* Finds the first removed fragment and last added fragments when popping the back stack.
* If none of the fragments have transitions, then both lists will be empty.
*
* @param transitioningFragments Keyed on the container ID, the first fragments to be removed,
* and last fragments to be added. This will be modified by
* this method.
*/
public static void calculatePopFragments(BackStackRecord transaction,
SparseArray<FragmentContainerTransition> transitioningFragments, boolean isReordered) {
if (!transaction.mManager.mContainer.onHasView()) {
return; // nothing to see, so no transitions
}
final int numOps = transaction.mOps.size();
for (int opNum = numOps - 1; opNum >= 0; opNum--) {
final BackStackRecord.Op op = transaction.mOps.get(opNum);
addToFirstInLastOut(transaction, op, transitioningFragments, true, isReordered);
}
}
/**
* Examines the {@code command} and may set the first out or last in fragment for the fragment's
* container.
*
* @param transaction The executing transaction
* @param op The operation being run.
* @param transitioningFragments A structure holding the first in and last out fragments
* for each fragment container.
* @param isPop Is the operation a pop?
* @param isReorderedTransaction True if the operations have been partially executed and the
* added fragments have Views in the hierarchy or false if the
* operations haven't been executed yet.
*/
@SuppressWarnings("ReferenceEquality")
private static void addToFirstInLastOut(BackStackRecord transaction, BackStackRecord.Op op,
SparseArray<FragmentContainerTransition> transitioningFragments, boolean isPop,
boolean isReorderedTransaction) {
final Fragment fragment = op.fragment;
if (fragment == null) {
return; // no fragment, no transition
}
final int containerId = fragment.mContainerId;
if (containerId == 0) {
return; // no container, no transition
}
final int command = isPop ? INVERSE_OPS[op.cmd] : op.cmd;
boolean setLastIn = false;
boolean wasRemoved = false;
boolean setFirstOut = false;
boolean wasAdded = false;
switch (command) {
case BackStackRecord.OP_SHOW:
if (isReorderedTransaction) {
setLastIn = fragment.mHiddenChanged && !fragment.mHidden &&
fragment.mAdded;
} else {
setLastIn = fragment.mHidden;
}
wasAdded = true;
break;
case BackStackRecord.OP_ADD:
case BackStackRecord.OP_ATTACH:
if (isReorderedTransaction) {
setLastIn = fragment.mIsNewlyAdded;
} else {
setLastIn = !fragment.mAdded && !fragment.mHidden;
}
wasAdded = true;
break;
case BackStackRecord.OP_HIDE:
if (isReorderedTransaction) {
setFirstOut = fragment.mHiddenChanged && fragment.mAdded &&
fragment.mHidden;
} else {
setFirstOut = fragment.mAdded && !fragment.mHidden;
}
wasRemoved = true;
break;
case BackStackRecord.OP_REMOVE:
case BackStackRecord.OP_DETACH:
if (isReorderedTransaction) {
setFirstOut = !fragment.mAdded && fragment.mView != null
&& fragment.mView.getVisibility() == View.VISIBLE
&& fragment.mView.getTransitionAlpha() > 0;
} else {
setFirstOut = fragment.mAdded && !fragment.mHidden;
}
wasRemoved = true;
break;
}
FragmentContainerTransition containerTransition = transitioningFragments.get(containerId);
if (setLastIn) {
containerTransition =
ensureContainer(containerTransition, transitioningFragments, containerId);
containerTransition.lastIn = fragment;
containerTransition.lastInIsPop = isPop;
containerTransition.lastInTransaction = transaction;
}
if (!isReorderedTransaction && wasAdded) {
if (containerTransition != null && containerTransition.firstOut == fragment) {
containerTransition.firstOut = null;
}
/*
* Ensure that fragments that are entering are at least at the CREATED state
* so that they may load Transitions using TransitionInflater.
*/
FragmentManagerImpl manager = transaction.mManager;
if (fragment.mState < Fragment.CREATED && manager.mCurState >= Fragment.CREATED &&
manager.mHost.getContext().getApplicationInfo().targetSdkVersion >=
Build.VERSION_CODES.N && !transaction.mReorderingAllowed) {
manager.makeActive(fragment);
manager.moveToState(fragment, Fragment.CREATED, 0, 0, false);
}
}
if (setFirstOut && (containerTransition == null || containerTransition.firstOut == null)) {
containerTransition =
ensureContainer(containerTransition, transitioningFragments, containerId);
containerTransition.firstOut = fragment;
containerTransition.firstOutIsPop = isPop;
containerTransition.firstOutTransaction = transaction;
}
if (!isReorderedTransaction && wasRemoved &&
(containerTransition != null && containerTransition.lastIn == fragment)) {
containerTransition.lastIn = null;
}
}
/**
* Ensures that a FragmentContainerTransition has been added to the SparseArray. If so,
* it returns the existing one. If not, one is created and added to the SparseArray and
* returned.
*/
private static FragmentContainerTransition ensureContainer(
FragmentContainerTransition containerTransition,
SparseArray<FragmentContainerTransition> transitioningFragments, int containerId) {
if (containerTransition == null) {
containerTransition = new FragmentContainerTransition();
transitioningFragments.put(containerId, containerTransition);
}
return containerTransition;
}
/**
* Tracks the last fragment added and first fragment removed for fragment transitions.
* This also tracks which fragments are changed by push or pop transactions.
*/
public static class FragmentContainerTransition {
/**
* The last fragment added/attached/shown in its container
*/
public Fragment lastIn;
/**
* true when lastIn was added during a pop transaction or false if added with a push
*/
public boolean lastInIsPop;
/**
* The transaction that included the last in fragment
*/
public BackStackRecord lastInTransaction;
/**
* The first fragment with a View that was removed/detached/hidden in its container.
*/
public Fragment firstOut;
/**
* true when firstOut was removed during a pop transaction or false otherwise
*/
public boolean firstOutIsPop;
/**
* The transaction that included the first out fragment
*/
public BackStackRecord firstOutTransaction;
}
}
|