Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 19 | import com.android.internal.util.FastPrintWriter; |
| 20 | |
| 21 | import android.graphics.Rect; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 24 | import android.text.TextUtils; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 25 | import android.transition.Transition; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 26 | import android.transition.TransitionManager; |
| 27 | import android.transition.TransitionSet; |
| 28 | import android.util.ArrayMap; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 30 | import android.util.LogWriter; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 31 | import android.util.SparseArray; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 34 | import android.view.ViewTreeObserver; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 35 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 36 | import java.io.FileDescriptor; |
| 37 | import java.io.PrintWriter; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 39 | import java.util.List; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 40 | |
| 41 | final class BackStackState implements Parcelable { |
| 42 | final int[] mOps; |
| 43 | final int mTransition; |
| 44 | final int mTransitionStyle; |
| 45 | final String mName; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 46 | final int mIndex; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 47 | final int mBreadCrumbTitleRes; |
| 48 | final CharSequence mBreadCrumbTitleText; |
| 49 | final int mBreadCrumbShortTitleRes; |
| 50 | final CharSequence mBreadCrumbShortTitleText; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 51 | final ArrayList<String> mSharedElementSourceNames; |
| 52 | final ArrayList<String> mSharedElementTargetNames; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 53 | |
| 54 | public BackStackState(FragmentManagerImpl fm, BackStackRecord bse) { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 55 | int numRemoved = 0; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 56 | BackStackRecord.Op op = bse.mHead; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 57 | while (op != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 58 | if (op.removed != null) { |
| 59 | numRemoved += op.removed.size(); |
| 60 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 61 | op = op.next; |
| 62 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 63 | mOps = new int[bse.mNumOp * 7 + numRemoved]; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 64 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 65 | if (!bse.mAddToBackStack) { |
| 66 | throw new IllegalStateException("Not on back stack"); |
| 67 | } |
| 68 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 69 | op = bse.mHead; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 70 | int pos = 0; |
| 71 | while (op != null) { |
| 72 | mOps[pos++] = op.cmd; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 73 | mOps[pos++] = op.fragment != null ? op.fragment.mIndex : -1; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 74 | mOps[pos++] = op.enterAnim; |
| 75 | mOps[pos++] = op.exitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 76 | mOps[pos++] = op.popEnterAnim; |
| 77 | mOps[pos++] = op.popExitAnim; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 78 | if (op.removed != null) { |
| 79 | final int N = op.removed.size(); |
| 80 | mOps[pos++] = N; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 81 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 82 | mOps[pos++] = op.removed.get(i).mIndex; |
| 83 | } |
| 84 | } else { |
| 85 | mOps[pos++] = 0; |
| 86 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 87 | op = op.next; |
| 88 | } |
| 89 | mTransition = bse.mTransition; |
| 90 | mTransitionStyle = bse.mTransitionStyle; |
| 91 | mName = bse.mName; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 92 | mIndex = bse.mIndex; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 93 | mBreadCrumbTitleRes = bse.mBreadCrumbTitleRes; |
| 94 | mBreadCrumbTitleText = bse.mBreadCrumbTitleText; |
| 95 | mBreadCrumbShortTitleRes = bse.mBreadCrumbShortTitleRes; |
| 96 | mBreadCrumbShortTitleText = bse.mBreadCrumbShortTitleText; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 97 | mSharedElementSourceNames = bse.mSharedElementSourceNames; |
| 98 | mSharedElementTargetNames = bse.mSharedElementTargetNames; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 99 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 100 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 101 | public BackStackState(Parcel in) { |
| 102 | mOps = in.createIntArray(); |
| 103 | mTransition = in.readInt(); |
| 104 | mTransitionStyle = in.readInt(); |
| 105 | mName = in.readString(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 106 | mIndex = in.readInt(); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 107 | mBreadCrumbTitleRes = in.readInt(); |
| 108 | mBreadCrumbTitleText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 109 | mBreadCrumbShortTitleRes = in.readInt(); |
| 110 | mBreadCrumbShortTitleText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 111 | mSharedElementSourceNames = in.createStringArrayList(); |
| 112 | mSharedElementTargetNames = in.createStringArrayList(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 113 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 114 | |
| 115 | public BackStackRecord instantiate(FragmentManagerImpl fm) { |
| 116 | BackStackRecord bse = new BackStackRecord(fm); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 117 | int pos = 0; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 118 | int num = 0; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 119 | while (pos < mOps.length) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 120 | BackStackRecord.Op op = new BackStackRecord.Op(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 121 | op.cmd = mOps[pos++]; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 122 | if (FragmentManagerImpl.DEBUG) { |
| 123 | Log.v(FragmentManagerImpl.TAG, |
| 124 | "Instantiate " + bse + " op #" + num + " base fragment #" + mOps[pos]); |
| 125 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 126 | int findex = mOps[pos++]; |
| 127 | if (findex >= 0) { |
| 128 | Fragment f = fm.mActive.get(findex); |
| 129 | op.fragment = f; |
| 130 | } else { |
| 131 | op.fragment = null; |
| 132 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 133 | op.enterAnim = mOps[pos++]; |
| 134 | op.exitAnim = mOps[pos++]; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 135 | op.popEnterAnim = mOps[pos++]; |
| 136 | op.popExitAnim = mOps[pos++]; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 137 | final int N = mOps[pos++]; |
| 138 | if (N > 0) { |
| 139 | op.removed = new ArrayList<Fragment>(N); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 140 | for (int i = 0; i < N; i++) { |
| 141 | if (FragmentManagerImpl.DEBUG) { |
| 142 | Log.v(FragmentManagerImpl.TAG, |
| 143 | "Instantiate " + bse + " set remove fragment #" + mOps[pos]); |
| 144 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 145 | Fragment r = fm.mActive.get(mOps[pos++]); |
| 146 | op.removed.add(r); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 149 | bse.addOp(op); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 150 | num++; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 151 | } |
| 152 | bse.mTransition = mTransition; |
| 153 | bse.mTransitionStyle = mTransitionStyle; |
| 154 | bse.mName = mName; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 155 | bse.mIndex = mIndex; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 156 | bse.mAddToBackStack = true; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 157 | bse.mBreadCrumbTitleRes = mBreadCrumbTitleRes; |
| 158 | bse.mBreadCrumbTitleText = mBreadCrumbTitleText; |
| 159 | bse.mBreadCrumbShortTitleRes = mBreadCrumbShortTitleRes; |
| 160 | bse.mBreadCrumbShortTitleText = mBreadCrumbShortTitleText; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 161 | bse.mSharedElementSourceNames = mSharedElementSourceNames; |
| 162 | bse.mSharedElementTargetNames = mSharedElementTargetNames; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 163 | bse.bumpBackStackNesting(1); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 164 | return bse; |
| 165 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 166 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 167 | public int describeContents() { |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | public void writeToParcel(Parcel dest, int flags) { |
| 172 | dest.writeIntArray(mOps); |
| 173 | dest.writeInt(mTransition); |
| 174 | dest.writeInt(mTransitionStyle); |
| 175 | dest.writeString(mName); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 176 | dest.writeInt(mIndex); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 177 | dest.writeInt(mBreadCrumbTitleRes); |
| 178 | TextUtils.writeToParcel(mBreadCrumbTitleText, dest, 0); |
| 179 | dest.writeInt(mBreadCrumbShortTitleRes); |
| 180 | TextUtils.writeToParcel(mBreadCrumbShortTitleText, dest, 0); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 181 | dest.writeStringList(mSharedElementSourceNames); |
| 182 | dest.writeStringList(mSharedElementTargetNames); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 183 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 184 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 185 | public static final Parcelable.Creator<BackStackState> CREATOR |
| 186 | = new Parcelable.Creator<BackStackState>() { |
| 187 | public BackStackState createFromParcel(Parcel in) { |
| 188 | return new BackStackState(in); |
| 189 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 190 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 191 | public BackStackState[] newArray(int size) { |
| 192 | return new BackStackState[size]; |
| 193 | } |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @hide Entry of an operation on the fragment back stack. |
| 199 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 200 | final class BackStackRecord extends FragmentTransaction implements |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 201 | FragmentManager.BackStackEntry, Runnable { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 202 | static final String TAG = FragmentManagerImpl.TAG; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 203 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 204 | final FragmentManagerImpl mManager; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 205 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 206 | static final int OP_NULL = 0; |
| 207 | static final int OP_ADD = 1; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 208 | static final int OP_REPLACE = 2; |
| 209 | static final int OP_REMOVE = 3; |
| 210 | static final int OP_HIDE = 4; |
| 211 | static final int OP_SHOW = 5; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 212 | static final int OP_DETACH = 6; |
| 213 | static final int OP_ATTACH = 7; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 214 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 215 | static final class Op { |
| 216 | Op next; |
| 217 | Op prev; |
| 218 | int cmd; |
| 219 | Fragment fragment; |
| 220 | int enterAnim; |
| 221 | int exitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 222 | int popEnterAnim; |
| 223 | int popExitAnim; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 224 | ArrayList<Fragment> removed; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 225 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 226 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 227 | Op mHead; |
| 228 | Op mTail; |
| 229 | int mNumOp; |
| 230 | int mEnterAnim; |
| 231 | int mExitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 232 | int mPopEnterAnim; |
| 233 | int mPopExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 234 | int mTransition; |
| 235 | int mTransitionStyle; |
| 236 | boolean mAddToBackStack; |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 237 | boolean mAllowAddToBackStack = true; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 238 | String mName; |
| 239 | boolean mCommitted; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 240 | int mIndex = -1; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 241 | |
| 242 | int mBreadCrumbTitleRes; |
| 243 | CharSequence mBreadCrumbTitleText; |
| 244 | int mBreadCrumbShortTitleRes; |
| 245 | CharSequence mBreadCrumbShortTitleText; |
| 246 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 247 | ArrayList<String> mSharedElementSourceNames; |
| 248 | ArrayList<String> mSharedElementTargetNames; |
| 249 | |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public String toString() { |
| 252 | StringBuilder sb = new StringBuilder(128); |
| 253 | sb.append("BackStackEntry{"); |
| 254 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 255 | if (mIndex >= 0) { |
| 256 | sb.append(" #"); |
| 257 | sb.append(mIndex); |
| 258 | } |
| 259 | if (mName != null) { |
| 260 | sb.append(" "); |
| 261 | sb.append(mName); |
| 262 | } |
| 263 | sb.append("}"); |
| 264 | return sb.toString(); |
| 265 | } |
| 266 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 267 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 268 | dump(prefix, writer, true); |
| 269 | } |
| 270 | |
| 271 | void dump(String prefix, PrintWriter writer, boolean full) { |
| 272 | if (full) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 273 | writer.print(prefix); |
| 274 | writer.print("mName="); |
| 275 | writer.print(mName); |
| 276 | writer.print(" mIndex="); |
| 277 | writer.print(mIndex); |
| 278 | writer.print(" mCommitted="); |
| 279 | writer.println(mCommitted); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 280 | if (mTransition != FragmentTransaction.TRANSIT_NONE) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 281 | writer.print(prefix); |
| 282 | writer.print("mTransition=#"); |
| 283 | writer.print(Integer.toHexString(mTransition)); |
| 284 | writer.print(" mTransitionStyle=#"); |
| 285 | writer.println(Integer.toHexString(mTransitionStyle)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 286 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 287 | if (mEnterAnim != 0 || mExitAnim != 0) { |
| 288 | writer.print(prefix); |
| 289 | writer.print("mEnterAnim=#"); |
| 290 | writer.print(Integer.toHexString(mEnterAnim)); |
| 291 | writer.print(" mExitAnim=#"); |
| 292 | writer.println(Integer.toHexString(mExitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 293 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 294 | if (mPopEnterAnim != 0 || mPopExitAnim != 0) { |
| 295 | writer.print(prefix); |
| 296 | writer.print("mPopEnterAnim=#"); |
| 297 | writer.print(Integer.toHexString(mPopEnterAnim)); |
| 298 | writer.print(" mPopExitAnim=#"); |
| 299 | writer.println(Integer.toHexString(mPopExitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 300 | } |
| 301 | if (mBreadCrumbTitleRes != 0 || mBreadCrumbTitleText != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 302 | writer.print(prefix); |
| 303 | writer.print("mBreadCrumbTitleRes=#"); |
| 304 | writer.print(Integer.toHexString(mBreadCrumbTitleRes)); |
| 305 | writer.print(" mBreadCrumbTitleText="); |
| 306 | writer.println(mBreadCrumbTitleText); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 307 | } |
| 308 | if (mBreadCrumbShortTitleRes != 0 || mBreadCrumbShortTitleText != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 309 | writer.print(prefix); |
| 310 | writer.print("mBreadCrumbShortTitleRes=#"); |
| 311 | writer.print(Integer.toHexString(mBreadCrumbShortTitleRes)); |
| 312 | writer.print(" mBreadCrumbShortTitleText="); |
| 313 | writer.println(mBreadCrumbShortTitleText); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 314 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (mHead != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 318 | writer.print(prefix); |
| 319 | writer.println("Operations:"); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 320 | String innerPrefix = prefix + " "; |
| 321 | Op op = mHead; |
| 322 | int num = 0; |
| 323 | while (op != null) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 324 | String cmdStr; |
| 325 | switch (op.cmd) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 326 | case OP_NULL: |
| 327 | cmdStr = "NULL"; |
| 328 | break; |
| 329 | case OP_ADD: |
| 330 | cmdStr = "ADD"; |
| 331 | break; |
| 332 | case OP_REPLACE: |
| 333 | cmdStr = "REPLACE"; |
| 334 | break; |
| 335 | case OP_REMOVE: |
| 336 | cmdStr = "REMOVE"; |
| 337 | break; |
| 338 | case OP_HIDE: |
| 339 | cmdStr = "HIDE"; |
| 340 | break; |
| 341 | case OP_SHOW: |
| 342 | cmdStr = "SHOW"; |
| 343 | break; |
| 344 | case OP_DETACH: |
| 345 | cmdStr = "DETACH"; |
| 346 | break; |
| 347 | case OP_ATTACH: |
| 348 | cmdStr = "ATTACH"; |
| 349 | break; |
| 350 | default: |
| 351 | cmdStr = "cmd=" + op.cmd; |
| 352 | break; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 353 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 354 | writer.print(prefix); |
| 355 | writer.print(" Op #"); |
| 356 | writer.print(num); |
| 357 | writer.print(": "); |
| 358 | writer.print(cmdStr); |
| 359 | writer.print(" "); |
| 360 | writer.println(op.fragment); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 361 | if (full) { |
| 362 | if (op.enterAnim != 0 || op.exitAnim != 0) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 363 | writer.print(innerPrefix); |
| 364 | writer.print("enterAnim=#"); |
| 365 | writer.print(Integer.toHexString(op.enterAnim)); |
| 366 | writer.print(" exitAnim=#"); |
| 367 | writer.println(Integer.toHexString(op.exitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 368 | } |
| 369 | if (op.popEnterAnim != 0 || op.popExitAnim != 0) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 370 | writer.print(innerPrefix); |
| 371 | writer.print("popEnterAnim=#"); |
| 372 | writer.print(Integer.toHexString(op.popEnterAnim)); |
| 373 | writer.print(" popExitAnim=#"); |
| 374 | writer.println(Integer.toHexString(op.popExitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 375 | } |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 376 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 377 | if (op.removed != null && op.removed.size() > 0) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 378 | for (int i = 0; i < op.removed.size(); i++) { |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 379 | writer.print(innerPrefix); |
| 380 | if (op.removed.size() == 1) { |
| 381 | writer.print("Removed: "); |
| 382 | } else { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 383 | if (i == 0) { |
| 384 | writer.println("Removed:"); |
| 385 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 386 | writer.print(innerPrefix); |
| 387 | writer.print(" #"); |
| 388 | writer.print(i); |
| 389 | writer.print(": "); |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 390 | } |
| 391 | writer.println(op.removed.get(i)); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | op = op.next; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 395 | num++; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 400 | public BackStackRecord(FragmentManagerImpl manager) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 401 | mManager = manager; |
| 402 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 403 | |
| 404 | public int getId() { |
| 405 | return mIndex; |
| 406 | } |
| 407 | |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 408 | public int getBreadCrumbTitleRes() { |
| 409 | return mBreadCrumbTitleRes; |
| 410 | } |
| 411 | |
| 412 | public int getBreadCrumbShortTitleRes() { |
| 413 | return mBreadCrumbShortTitleRes; |
| 414 | } |
| 415 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 416 | public CharSequence getBreadCrumbTitle() { |
| 417 | if (mBreadCrumbTitleRes != 0) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 418 | return mManager.mHost.getContext().getText(mBreadCrumbTitleRes); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 419 | } |
| 420 | return mBreadCrumbTitleText; |
| 421 | } |
| 422 | |
| 423 | public CharSequence getBreadCrumbShortTitle() { |
| 424 | if (mBreadCrumbShortTitleRes != 0) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 425 | return mManager.mHost.getContext().getText(mBreadCrumbShortTitleRes); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 426 | } |
| 427 | return mBreadCrumbShortTitleText; |
| 428 | } |
| 429 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 430 | void addOp(Op op) { |
| 431 | if (mHead == null) { |
| 432 | mHead = mTail = op; |
| 433 | } else { |
| 434 | op.prev = mTail; |
| 435 | mTail.next = op; |
| 436 | mTail = op; |
| 437 | } |
| 438 | op.enterAnim = mEnterAnim; |
| 439 | op.exitAnim = mExitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 440 | op.popEnterAnim = mPopEnterAnim; |
| 441 | op.popExitAnim = mPopExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 442 | mNumOp++; |
| 443 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 444 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 445 | public FragmentTransaction add(Fragment fragment, String tag) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 446 | doAddOp(0, fragment, tag, OP_ADD); |
| 447 | return this; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | public FragmentTransaction add(int containerViewId, Fragment fragment) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 451 | doAddOp(containerViewId, fragment, null, OP_ADD); |
| 452 | return this; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | public FragmentTransaction add(int containerViewId, Fragment fragment, String tag) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 456 | doAddOp(containerViewId, fragment, tag, OP_ADD); |
| 457 | return this; |
| 458 | } |
| 459 | |
| 460 | private void doAddOp(int containerViewId, Fragment fragment, String tag, int opcmd) { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 461 | fragment.mFragmentManager = mManager; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 462 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 463 | if (tag != null) { |
| 464 | if (fragment.mTag != null && !tag.equals(fragment.mTag)) { |
| 465 | throw new IllegalStateException("Can't change tag of fragment " |
| 466 | + fragment + ": was " + fragment.mTag |
| 467 | + " now " + tag); |
| 468 | } |
| 469 | fragment.mTag = tag; |
| 470 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 471 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 472 | if (containerViewId != 0) { |
Adam Powell | e01f595 | 2016-02-23 15:25:42 -0800 | [diff] [blame] | 473 | if (containerViewId == View.NO_ID) { |
| 474 | throw new IllegalArgumentException("Can't add fragment " |
| 475 | + fragment + " with tag " + tag + " to container view with no id"); |
| 476 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 477 | if (fragment.mFragmentId != 0 && fragment.mFragmentId != containerViewId) { |
| 478 | throw new IllegalStateException("Can't change container ID of fragment " |
| 479 | + fragment + ": was " + fragment.mFragmentId |
| 480 | + " now " + containerViewId); |
| 481 | } |
| 482 | fragment.mContainerId = fragment.mFragmentId = containerViewId; |
| 483 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 484 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 485 | Op op = new Op(); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 486 | op.cmd = opcmd; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 487 | op.fragment = fragment; |
| 488 | addOp(op); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | public FragmentTransaction replace(int containerViewId, Fragment fragment) { |
| 492 | return replace(containerViewId, fragment, null); |
| 493 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 494 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 495 | public FragmentTransaction replace(int containerViewId, Fragment fragment, String tag) { |
| 496 | if (containerViewId == 0) { |
| 497 | throw new IllegalArgumentException("Must use non-zero containerViewId"); |
| 498 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 499 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 500 | doAddOp(containerViewId, fragment, tag, OP_REPLACE); |
| 501 | return this; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 502 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 503 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 504 | public FragmentTransaction remove(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 505 | Op op = new Op(); |
| 506 | op.cmd = OP_REMOVE; |
| 507 | op.fragment = fragment; |
| 508 | addOp(op); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 509 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 510 | return this; |
| 511 | } |
| 512 | |
| 513 | public FragmentTransaction hide(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 514 | Op op = new Op(); |
| 515 | op.cmd = OP_HIDE; |
| 516 | op.fragment = fragment; |
| 517 | addOp(op); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 518 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 519 | return this; |
| 520 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 521 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 522 | public FragmentTransaction show(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 523 | Op op = new Op(); |
| 524 | op.cmd = OP_SHOW; |
| 525 | op.fragment = fragment; |
| 526 | addOp(op); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 527 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 528 | return this; |
| 529 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 530 | |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 531 | public FragmentTransaction detach(Fragment fragment) { |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 532 | Op op = new Op(); |
| 533 | op.cmd = OP_DETACH; |
| 534 | op.fragment = fragment; |
| 535 | addOp(op); |
| 536 | |
| 537 | return this; |
| 538 | } |
| 539 | |
| 540 | public FragmentTransaction attach(Fragment fragment) { |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 541 | Op op = new Op(); |
| 542 | op.cmd = OP_ATTACH; |
| 543 | op.fragment = fragment; |
| 544 | addOp(op); |
| 545 | |
| 546 | return this; |
| 547 | } |
| 548 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 549 | public FragmentTransaction setCustomAnimations(int enter, int exit) { |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 550 | return setCustomAnimations(enter, exit, 0, 0); |
| 551 | } |
| 552 | |
| 553 | public FragmentTransaction setCustomAnimations(int enter, int exit, |
| 554 | int popEnter, int popExit) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 555 | mEnterAnim = enter; |
| 556 | mExitAnim = exit; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 557 | mPopEnterAnim = popEnter; |
| 558 | mPopExitAnim = popExit; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 559 | return this; |
| 560 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 561 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 562 | public FragmentTransaction setTransition(int transition) { |
| 563 | mTransition = transition; |
| 564 | return this; |
| 565 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 566 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 567 | @Override |
George Mount | 448bace | 2014-08-18 16:27:43 -0700 | [diff] [blame] | 568 | public FragmentTransaction addSharedElement(View sharedElement, String name) { |
| 569 | String transitionName = sharedElement.getTransitionName(); |
| 570 | if (transitionName == null) { |
| 571 | throw new IllegalArgumentException("Unique transitionNames are required for all" + |
| 572 | " sharedElements"); |
| 573 | } |
| 574 | if (mSharedElementSourceNames == null) { |
| 575 | mSharedElementSourceNames = new ArrayList<String>(); |
| 576 | mSharedElementTargetNames = new ArrayList<String>(); |
| 577 | } |
| 578 | mSharedElementSourceNames.add(transitionName); |
| 579 | mSharedElementTargetNames.add(name); |
| 580 | return this; |
| 581 | } |
| 582 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 583 | public FragmentTransaction setTransitionStyle(int styleRes) { |
| 584 | mTransitionStyle = styleRes; |
| 585 | return this; |
| 586 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 587 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 588 | public FragmentTransaction addToBackStack(String name) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 589 | if (!mAllowAddToBackStack) { |
| 590 | throw new IllegalStateException( |
| 591 | "This FragmentTransaction is not allowed to be added to the back stack."); |
| 592 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 593 | mAddToBackStack = true; |
| 594 | mName = name; |
| 595 | return this; |
| 596 | } |
| 597 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 598 | public boolean isAddToBackStackAllowed() { |
| 599 | return mAllowAddToBackStack; |
| 600 | } |
| 601 | |
| 602 | public FragmentTransaction disallowAddToBackStack() { |
| 603 | if (mAddToBackStack) { |
| 604 | throw new IllegalStateException( |
| 605 | "This transaction is already being added to the back stack"); |
| 606 | } |
| 607 | mAllowAddToBackStack = false; |
| 608 | return this; |
| 609 | } |
| 610 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 611 | public FragmentTransaction setBreadCrumbTitle(int res) { |
| 612 | mBreadCrumbTitleRes = res; |
| 613 | mBreadCrumbTitleText = null; |
| 614 | return this; |
| 615 | } |
| 616 | |
| 617 | public FragmentTransaction setBreadCrumbTitle(CharSequence text) { |
| 618 | mBreadCrumbTitleRes = 0; |
| 619 | mBreadCrumbTitleText = text; |
| 620 | return this; |
| 621 | } |
| 622 | |
| 623 | public FragmentTransaction setBreadCrumbShortTitle(int res) { |
| 624 | mBreadCrumbShortTitleRes = res; |
| 625 | mBreadCrumbShortTitleText = null; |
| 626 | return this; |
| 627 | } |
| 628 | |
| 629 | public FragmentTransaction setBreadCrumbShortTitle(CharSequence text) { |
| 630 | mBreadCrumbShortTitleRes = 0; |
| 631 | mBreadCrumbShortTitleText = text; |
| 632 | return this; |
| 633 | } |
| 634 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 635 | void bumpBackStackNesting(int amt) { |
| 636 | if (!mAddToBackStack) { |
| 637 | return; |
| 638 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 639 | if (FragmentManagerImpl.DEBUG) { |
| 640 | Log.v(TAG, "Bump nesting in " + this |
| 641 | + " by " + amt); |
| 642 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 643 | Op op = mHead; |
| 644 | while (op != null) { |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 645 | if (op.fragment != null) { |
| 646 | op.fragment.mBackStackNesting += amt; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 647 | if (FragmentManagerImpl.DEBUG) { |
| 648 | Log.v(TAG, "Bump nesting of " |
| 649 | + op.fragment + " to " + op.fragment.mBackStackNesting); |
| 650 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 651 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 652 | if (op.removed != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 653 | for (int i = op.removed.size() - 1; i >= 0; i--) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 654 | Fragment r = op.removed.get(i); |
| 655 | r.mBackStackNesting += amt; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 656 | if (FragmentManagerImpl.DEBUG) { |
| 657 | Log.v(TAG, "Bump nesting of " |
| 658 | + r + " to " + r.mBackStackNesting); |
| 659 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | op = op.next; |
| 663 | } |
| 664 | } |
| 665 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 666 | public int commit() { |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 667 | return commitInternal(false); |
| 668 | } |
| 669 | |
| 670 | public int commitAllowingStateLoss() { |
| 671 | return commitInternal(true); |
| 672 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 673 | |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 674 | @Override |
| 675 | public void commitNow() { |
| 676 | disallowAddToBackStack(); |
| 677 | mManager.execSingleAction(this, false); |
| 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public void commitNowAllowingStateLoss() { |
| 682 | disallowAddToBackStack(); |
| 683 | mManager.execSingleAction(this, true); |
| 684 | } |
| 685 | |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 686 | int commitInternal(boolean allowStateLoss) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 687 | if (mCommitted) { |
| 688 | throw new IllegalStateException("commit already called"); |
| 689 | } |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 690 | if (FragmentManagerImpl.DEBUG) { |
| 691 | Log.v(TAG, "Commit: " + this); |
| 692 | LogWriter logw = new LogWriter(Log.VERBOSE, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 693 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 694 | dump(" ", null, pw, null); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 695 | pw.flush(); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 696 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 697 | mCommitted = true; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 698 | if (mAddToBackStack) { |
| 699 | mIndex = mManager.allocBackStackIndex(this); |
| 700 | } else { |
| 701 | mIndex = -1; |
| 702 | } |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 703 | mManager.enqueueAction(this, allowStateLoss); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 704 | return mIndex; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 705 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 706 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 707 | public void run() { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 708 | if (FragmentManagerImpl.DEBUG) { |
| 709 | Log.v(TAG, "Run: " + this); |
| 710 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 711 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 712 | if (mAddToBackStack) { |
| 713 | if (mIndex < 0) { |
| 714 | throw new IllegalStateException("addToBackStack() called after commit()"); |
| 715 | } |
| 716 | } |
| 717 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 718 | bumpBackStackNesting(1); |
| 719 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 720 | SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); |
| 721 | SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 722 | calculateFragments(firstOutFragments, lastInFragments); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 723 | beginTransition(firstOutFragments, lastInFragments, false); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 724 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 725 | Op op = mHead; |
| 726 | while (op != null) { |
| 727 | switch (op.cmd) { |
| 728 | case OP_ADD: { |
| 729 | Fragment f = op.fragment; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 730 | f.mNextAnim = op.enterAnim; |
| 731 | mManager.addFragment(f, false); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 732 | } |
| 733 | break; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 734 | case OP_REPLACE: { |
| 735 | Fragment f = op.fragment; |
Eddie Ringle | 22369d5 | 2014-06-29 16:43:28 -0400 | [diff] [blame] | 736 | int containerId = f.mContainerId; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 737 | if (mManager.mAdded != null) { |
Aurimas Liutikas | 61fa2d4 | 2016-01-05 15:58:29 -0800 | [diff] [blame] | 738 | for (int i = mManager.mAdded.size() - 1; i >= 0; i--) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 739 | Fragment old = mManager.mAdded.get(i); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 740 | if (FragmentManagerImpl.DEBUG) { |
| 741 | Log.v(TAG, |
| 742 | "OP_REPLACE: adding=" + f + " old=" + old); |
| 743 | } |
Eddie Ringle | 22369d5 | 2014-06-29 16:43:28 -0400 | [diff] [blame] | 744 | if (old.mContainerId == containerId) { |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 745 | if (old == f) { |
| 746 | op.fragment = f = null; |
| 747 | } else { |
| 748 | if (op.removed == null) { |
| 749 | op.removed = new ArrayList<Fragment>(); |
| 750 | } |
| 751 | op.removed.add(old); |
| 752 | old.mNextAnim = op.exitAnim; |
| 753 | if (mAddToBackStack) { |
| 754 | old.mBackStackNesting += 1; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 755 | if (FragmentManagerImpl.DEBUG) { |
| 756 | Log.v(TAG, "Bump nesting of " |
| 757 | + old + " to " + old.mBackStackNesting); |
| 758 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 759 | } |
| 760 | mManager.removeFragment(old, mTransition, mTransitionStyle); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 761 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 765 | if (f != null) { |
| 766 | f.mNextAnim = op.enterAnim; |
| 767 | mManager.addFragment(f, false); |
| 768 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 769 | } |
| 770 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 771 | case OP_REMOVE: { |
| 772 | Fragment f = op.fragment; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 773 | f.mNextAnim = op.exitAnim; |
| 774 | mManager.removeFragment(f, mTransition, mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 775 | } |
| 776 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 777 | case OP_HIDE: { |
| 778 | Fragment f = op.fragment; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 779 | f.mNextAnim = op.exitAnim; |
| 780 | mManager.hideFragment(f, mTransition, mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 781 | } |
| 782 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 783 | case OP_SHOW: { |
| 784 | Fragment f = op.fragment; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 785 | f.mNextAnim = op.enterAnim; |
| 786 | mManager.showFragment(f, mTransition, mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 787 | } |
| 788 | break; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 789 | case OP_DETACH: { |
| 790 | Fragment f = op.fragment; |
| 791 | f.mNextAnim = op.exitAnim; |
| 792 | mManager.detachFragment(f, mTransition, mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 793 | } |
| 794 | break; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 795 | case OP_ATTACH: { |
| 796 | Fragment f = op.fragment; |
| 797 | f.mNextAnim = op.enterAnim; |
| 798 | mManager.attachFragment(f, mTransition, mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 799 | } |
| 800 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 801 | default: { |
| 802 | throw new IllegalArgumentException("Unknown cmd: " + op.cmd); |
| 803 | } |
| 804 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 805 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 806 | op = op.next; |
| 807 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 808 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 809 | mManager.moveToState(mManager.mCurState, mTransition, |
| 810 | mTransitionStyle, true); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 811 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 812 | if (mAddToBackStack) { |
| 813 | mManager.addBackStackState(this); |
| 814 | } |
| 815 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 816 | |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 817 | private static void setFirstOut(SparseArray<Fragment> firstOutFragments, |
| 818 | SparseArray<Fragment> lastInFragments, Fragment fragment) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 819 | if (fragment != null) { |
| 820 | int containerId = fragment.mContainerId; |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 821 | if (containerId != 0 && !fragment.isHidden()) { |
| 822 | if (fragment.isAdded() && fragment.getView() != null |
| 823 | && firstOutFragments.get(containerId) == null) { |
| 824 | firstOutFragments.put(containerId, fragment); |
| 825 | } |
| 826 | if (lastInFragments.get(containerId) == fragment) { |
| 827 | lastInFragments.remove(containerId); |
| 828 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 829 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 830 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 831 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 832 | |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 833 | private void setLastIn(SparseArray<Fragment> firstOutFragments, |
| 834 | SparseArray<Fragment> lastInFragments, Fragment fragment) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 835 | if (fragment != null) { |
| 836 | int containerId = fragment.mContainerId; |
| 837 | if (containerId != 0) { |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 838 | if (!fragment.isAdded()) { |
| 839 | lastInFragments.put(containerId, fragment); |
| 840 | } |
| 841 | if (firstOutFragments.get(containerId) == fragment) { |
| 842 | firstOutFragments.remove(containerId); |
| 843 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Finds the first removed fragment and last added fragments when going forward. |
| 850 | * If none of the fragments have transitions, then both lists will be empty. |
| 851 | * |
| 852 | * @param firstOutFragments The list of first fragments to be removed, keyed on the |
| 853 | * container ID. This list will be modified by the method. |
| 854 | * @param lastInFragments The list of last fragments to be added, keyed on the |
| 855 | * container ID. This list will be modified by the method. |
| 856 | */ |
| 857 | private void calculateFragments(SparseArray<Fragment> firstOutFragments, |
| 858 | SparseArray<Fragment> lastInFragments) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 859 | if (!mManager.mContainer.onHasView()) { |
George Mount | 0b26e4d | 2014-09-17 16:36:42 -0700 | [diff] [blame] | 860 | return; // nothing to see, so no transitions |
| 861 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 862 | Op op = mHead; |
| 863 | while (op != null) { |
| 864 | switch (op.cmd) { |
| 865 | case OP_ADD: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 866 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 867 | break; |
| 868 | case OP_REPLACE: { |
| 869 | Fragment f = op.fragment; |
| 870 | if (mManager.mAdded != null) { |
| 871 | for (int i = 0; i < mManager.mAdded.size(); i++) { |
| 872 | Fragment old = mManager.mAdded.get(i); |
| 873 | if (f == null || old.mContainerId == f.mContainerId) { |
| 874 | if (old == f) { |
| 875 | f = null; |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 876 | lastInFragments.remove(old.mContainerId); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 877 | } else { |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 878 | setFirstOut(firstOutFragments, lastInFragments, old); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | } |
| 882 | } |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 883 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 884 | break; |
| 885 | } |
| 886 | case OP_REMOVE: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 887 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 888 | break; |
| 889 | case OP_HIDE: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 890 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 891 | break; |
| 892 | case OP_SHOW: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 893 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 894 | break; |
| 895 | case OP_DETACH: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 896 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 897 | break; |
| 898 | case OP_ATTACH: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 899 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 900 | break; |
| 901 | } |
| 902 | |
| 903 | op = op.next; |
| 904 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Finds the first removed fragment and last added fragments when popping the back stack. |
| 909 | * If none of the fragments have transitions, then both lists will be empty. |
| 910 | * |
| 911 | * @param firstOutFragments The list of first fragments to be removed, keyed on the |
| 912 | * container ID. This list will be modified by the method. |
| 913 | * @param lastInFragments The list of last fragments to be added, keyed on the |
| 914 | * container ID. This list will be modified by the method. |
| 915 | */ |
| 916 | public void calculateBackFragments(SparseArray<Fragment> firstOutFragments, |
| 917 | SparseArray<Fragment> lastInFragments) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 918 | if (!mManager.mContainer.onHasView()) { |
George Mount | 0b26e4d | 2014-09-17 16:36:42 -0700 | [diff] [blame] | 919 | return; // nothing to see, so no transitions |
| 920 | } |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 921 | Op op = mTail; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 922 | while (op != null) { |
| 923 | switch (op.cmd) { |
| 924 | case OP_ADD: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 925 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 926 | break; |
| 927 | case OP_REPLACE: |
| 928 | if (op.removed != null) { |
| 929 | for (int i = op.removed.size() - 1; i >= 0; i--) { |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 930 | setLastIn(firstOutFragments, lastInFragments, op.removed.get(i)); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 933 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 934 | break; |
| 935 | case OP_REMOVE: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 936 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 937 | break; |
| 938 | case OP_HIDE: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 939 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 940 | break; |
| 941 | case OP_SHOW: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 942 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 943 | break; |
| 944 | case OP_DETACH: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 945 | setLastIn(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 946 | break; |
| 947 | case OP_ATTACH: |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 948 | setFirstOut(firstOutFragments, lastInFragments, op.fragment); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 949 | break; |
| 950 | } |
| 951 | |
George Mount | 233c97c | 2015-12-04 18:00:55 +0000 | [diff] [blame] | 952 | op = op.prev; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 953 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /** |
| 957 | * When custom fragment transitions are used, this sets up the state for each transition |
| 958 | * and begins the transition. A different transition is started for each fragment container |
| 959 | * and consists of up to 3 different transitions: the exit transition, a shared element |
| 960 | * transition and an enter transition. |
| 961 | * |
| 962 | * <p>The exit transition operates against the leaf nodes of the first fragment |
| 963 | * with a view that was removed. If no such fragment was removed, then no exit |
| 964 | * transition is executed. The exit transition comes from the outgoing fragment.</p> |
| 965 | * |
| 966 | * <p>The enter transition operates against the last fragment that was added. If |
| 967 | * that fragment does not have a view or no fragment was added, then no enter |
| 968 | * transition is executed. The enter transition comes from the incoming fragment.</p> |
| 969 | * |
| 970 | * <p>The shared element transition operates against all views and comes either |
| 971 | * from the outgoing fragment or the incoming fragment, depending on whether this |
| 972 | * is going forward or popping the back stack. When going forward, the incoming |
| 973 | * fragment's enter shared element transition is used, but when going back, the |
| 974 | * outgoing fragment's return shared element transition is used. Shared element |
| 975 | * transitions only operate if there is both an incoming and outgoing fragment.</p> |
| 976 | * |
| 977 | * @param firstOutFragments The list of first fragments to be removed, keyed on the |
| 978 | * container ID. |
| 979 | * @param lastInFragments The list of last fragments to be added, keyed on the |
| 980 | * container ID. |
| 981 | * @param isBack true if this is popping the back stack or false if this is a |
| 982 | * forward operation. |
| 983 | * @return The TransitionState used to complete the operation of the transition |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 984 | * in {@link #setNameOverrides(android.app.BackStackRecord.TransitionState, java.util.ArrayList, |
| 985 | * java.util.ArrayList)}. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 986 | */ |
| 987 | private TransitionState beginTransition(SparseArray<Fragment> firstOutFragments, |
| 988 | SparseArray<Fragment> lastInFragments, boolean isBack) { |
George Mount | 128690c | 2015-12-03 14:43:33 -0800 | [diff] [blame] | 989 | ensureFragmentsAreInitialized(lastInFragments); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 990 | TransitionState state = new TransitionState(); |
| 991 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 992 | // Adding a non-existent target view makes sure that the transitions don't target |
| 993 | // any views by default. They'll only target the views we tell add. If we don't |
| 994 | // add any, then no views will be targeted. |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 995 | state.nonExistentView = new View(mManager.mHost.getContext()); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 996 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 997 | // Go over all leaving fragments. |
| 998 | for (int i = 0; i < firstOutFragments.size(); i++) { |
| 999 | int containerId = firstOutFragments.keyAt(i); |
| 1000 | configureTransitions(containerId, state, isBack, firstOutFragments, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1001 | lastInFragments); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1004 | // Now go over all entering fragments that didn't have a leaving fragment. |
| 1005 | for (int i = 0; i < lastInFragments.size(); i++) { |
| 1006 | int containerId = lastInFragments.keyAt(i); |
| 1007 | if (firstOutFragments.get(containerId) == null) { |
| 1008 | configureTransitions(containerId, state, isBack, firstOutFragments, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1009 | lastInFragments); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1012 | return state; |
| 1013 | } |
| 1014 | |
George Mount | 128690c | 2015-12-03 14:43:33 -0800 | [diff] [blame] | 1015 | /** |
| 1016 | * Ensure that fragments that are entering are at least at the CREATED state |
| 1017 | * so that they may load Transitions using TransitionInflater. |
| 1018 | */ |
| 1019 | private void ensureFragmentsAreInitialized(SparseArray<Fragment> lastInFragments) { |
| 1020 | final int count = lastInFragments.size(); |
| 1021 | for (int i = 0; i < count; i++) { |
| 1022 | final Fragment fragment = lastInFragments.valueAt(i); |
| 1023 | if (fragment.mState < Fragment.CREATED) { |
| 1024 | mManager.makeActive(fragment); |
| 1025 | mManager.moveToState(fragment, Fragment.CREATED, 0, 0, false); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1030 | private static Transition cloneTransition(Transition transition) { |
| 1031 | if (transition != null) { |
| 1032 | transition = transition.clone(); |
| 1033 | } |
| 1034 | return transition; |
| 1035 | } |
| 1036 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1037 | private static Transition getEnterTransition(Fragment inFragment, boolean isBack) { |
| 1038 | if (inFragment == null) { |
| 1039 | return null; |
| 1040 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1041 | return cloneTransition(isBack ? inFragment.getReenterTransition() : |
| 1042 | inFragment.getEnterTransition()); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1043 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1044 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1045 | private static Transition getExitTransition(Fragment outFragment, boolean isBack) { |
| 1046 | if (outFragment == null) { |
| 1047 | return null; |
| 1048 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1049 | return cloneTransition(isBack ? outFragment.getReturnTransition() : |
| 1050 | outFragment.getExitTransition()); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1053 | private static TransitionSet getSharedElementTransition(Fragment inFragment, |
| 1054 | Fragment outFragment, boolean isBack) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1055 | if (inFragment == null || outFragment == null) { |
| 1056 | return null; |
| 1057 | } |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1058 | Transition transition = cloneTransition(isBack |
| 1059 | ? outFragment.getSharedElementReturnTransition() |
| 1060 | : inFragment.getSharedElementEnterTransition()); |
| 1061 | if (transition == null) { |
| 1062 | return null; |
| 1063 | } |
| 1064 | TransitionSet transitionSet = new TransitionSet(); |
| 1065 | transitionSet.addTransition(transition); |
| 1066 | return transitionSet; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1069 | private static ArrayList<View> captureExitingViews(Transition exitTransition, |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1070 | Fragment outFragment, ArrayMap<String, View> namedViews, View nonExistentView) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1071 | ArrayList<View> viewList = null; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1072 | if (exitTransition != null) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1073 | viewList = new ArrayList<View>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1074 | View root = outFragment.getView(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1075 | root.captureTransitioningViews(viewList); |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1076 | if (namedViews != null) { |
| 1077 | viewList.removeAll(namedViews.values()); |
| 1078 | } |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1079 | if (!viewList.isEmpty()) { |
| 1080 | viewList.add(nonExistentView); |
| 1081 | addTargets(exitTransition, viewList); |
| 1082 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1083 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1084 | return viewList; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | private ArrayMap<String, View> remapSharedElements(TransitionState state, Fragment outFragment, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1088 | boolean isBack) { |
| 1089 | ArrayMap<String, View> namedViews = new ArrayMap<String, View>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1090 | if (mSharedElementSourceNames != null) { |
| 1091 | outFragment.getView().findNamedViews(namedViews); |
| 1092 | if (isBack) { |
| 1093 | namedViews.retainAll(mSharedElementTargetNames); |
| 1094 | } else { |
| 1095 | namedViews = remapNames(mSharedElementSourceNames, mSharedElementTargetNames, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1096 | namedViews); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1100 | if (isBack) { |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1101 | outFragment.mEnterTransitionCallback.onMapSharedElements( |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1102 | mSharedElementTargetNames, namedViews); |
| 1103 | setBackNameOverrides(state, namedViews, false); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1104 | } else { |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 1105 | outFragment.mExitTransitionCallback.onMapSharedElements( |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1106 | mSharedElementTargetNames, namedViews); |
| 1107 | setNameOverrides(state, namedViews, false); |
| 1108 | } |
| 1109 | |
| 1110 | return namedViews; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * Prepares the enter transition by adding a non-existent view to the transition's target list |
| 1115 | * and setting it epicenter callback. By adding a non-existent view to the target list, |
| 1116 | * we can prevent any view from being targeted at the beginning of the transition. |
| 1117 | * We will add to the views before the end state of the transition is captured so that the |
| 1118 | * views will appear. At the start of the transition, we clear the list of targets so that |
| 1119 | * we can restore the state of the transition and use it again. |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1120 | * |
| 1121 | * <p>The shared element transition maps its shared elements immediately prior to |
| 1122 | * capturing the final state of the Transition.</p> |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1123 | */ |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1124 | private ArrayList<View> addTransitionTargets(final TransitionState state, |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1125 | final Transition enterTransition, final TransitionSet sharedElementTransition, |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1126 | final Transition exitTransition, final Transition overallTransition, |
| 1127 | final View container, final Fragment inFragment, final Fragment outFragment, |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1128 | final ArrayList<View> hiddenFragmentViews, final boolean isBack, |
| 1129 | final ArrayList<View> sharedElementTargets) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1130 | if (enterTransition == null && sharedElementTransition == null && |
| 1131 | overallTransition == null) { |
| 1132 | return null; |
| 1133 | } |
| 1134 | final ArrayList<View> enteringViews = new ArrayList<View>(); |
| 1135 | container.getViewTreeObserver().addOnPreDrawListener( |
| 1136 | new ViewTreeObserver.OnPreDrawListener() { |
| 1137 | @Override |
| 1138 | public boolean onPreDraw() { |
| 1139 | container.getViewTreeObserver().removeOnPreDrawListener(this); |
| 1140 | |
| 1141 | // Don't include any newly-hidden fragments in the transition. |
George Mount | 13d7032 | 2015-07-21 14:30:24 -0700 | [diff] [blame] | 1142 | if (inFragment != null) { |
| 1143 | excludeHiddenFragments(hiddenFragmentViews, inFragment.mContainerId, |
| 1144 | overallTransition); |
| 1145 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1146 | |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1147 | ArrayMap<String, View> namedViews = null; |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1148 | if (sharedElementTransition != null) { |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1149 | namedViews = mapSharedElementsIn(state, isBack, inFragment); |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1150 | removeTargets(sharedElementTransition, sharedElementTargets); |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1151 | // keep the nonExistentView as excluded so the list doesn't get emptied |
| 1152 | sharedElementTargets.remove(state.nonExistentView); |
| 1153 | excludeViews(exitTransition, sharedElementTransition, |
| 1154 | sharedElementTargets, false); |
| 1155 | excludeViews(enterTransition, sharedElementTransition, |
| 1156 | sharedElementTargets, false); |
| 1157 | |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1158 | setSharedElementTargets(sharedElementTransition, |
| 1159 | state.nonExistentView, namedViews, sharedElementTargets); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1160 | |
| 1161 | setEpicenterIn(namedViews, state); |
| 1162 | |
| 1163 | callSharedElementEnd(state, inFragment, outFragment, isBack, |
| 1164 | namedViews); |
| 1165 | } |
| 1166 | |
| 1167 | if (enterTransition != null) { |
George Mount | ec3364c | 2015-06-03 15:47:44 -0700 | [diff] [blame] | 1168 | enterTransition.removeTarget(state.nonExistentView); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1169 | View view = inFragment.getView(); |
| 1170 | if (view != null) { |
| 1171 | view.captureTransitioningViews(enteringViews); |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1172 | if (namedViews != null) { |
| 1173 | enteringViews.removeAll(namedViews.values()); |
| 1174 | } |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1175 | enteringViews.add(state.nonExistentView); |
| 1176 | // We added this earlier to prevent any views being targeted. |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1177 | addTargets(enterTransition, enteringViews); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1178 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1179 | setSharedElementEpicenter(enterTransition, state); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1180 | } |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1181 | |
| 1182 | excludeViews(exitTransition, enterTransition, enteringViews, true); |
| 1183 | excludeViews(exitTransition, sharedElementTransition, sharedElementTargets, |
| 1184 | true); |
| 1185 | excludeViews(enterTransition, sharedElementTransition, sharedElementTargets, |
| 1186 | true); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1187 | return true; |
| 1188 | } |
| 1189 | }); |
| 1190 | return enteringViews; |
| 1191 | } |
| 1192 | |
| 1193 | private void callSharedElementEnd(TransitionState state, Fragment inFragment, |
| 1194 | Fragment outFragment, boolean isBack, ArrayMap<String, View> namedViews) { |
| 1195 | SharedElementCallback sharedElementCallback = isBack ? |
| 1196 | outFragment.mEnterTransitionCallback : |
| 1197 | inFragment.mEnterTransitionCallback; |
| 1198 | ArrayList<String> names = new ArrayList<String>(namedViews.keySet()); |
| 1199 | ArrayList<View> views = new ArrayList<View>(namedViews.values()); |
| 1200 | sharedElementCallback.onSharedElementEnd(names, views, null); |
| 1201 | } |
| 1202 | |
| 1203 | private void setEpicenterIn(ArrayMap<String, View> namedViews, TransitionState state) { |
| 1204 | if (mSharedElementTargetNames != null && !namedViews.isEmpty()) { |
| 1205 | // now we know the epicenter of the entering transition. |
| 1206 | View epicenter = namedViews |
| 1207 | .get(mSharedElementTargetNames.get(0)); |
| 1208 | if (epicenter != null) { |
| 1209 | state.enteringEpicenterView = epicenter; |
| 1210 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1214 | private ArrayMap<String, View> mapSharedElementsIn(TransitionState state, |
| 1215 | boolean isBack, Fragment inFragment) { |
| 1216 | // Now map the shared elements in the incoming fragment |
| 1217 | ArrayMap<String, View> namedViews = mapEnteringSharedElements(state, inFragment, isBack); |
| 1218 | |
| 1219 | // remap shared elements and set the name mapping used |
| 1220 | // in the shared element transition. |
| 1221 | if (isBack) { |
| 1222 | inFragment.mExitTransitionCallback.onMapSharedElements( |
| 1223 | mSharedElementTargetNames, namedViews); |
| 1224 | setBackNameOverrides(state, namedViews, true); |
| 1225 | } else { |
| 1226 | inFragment.mEnterTransitionCallback.onMapSharedElements( |
| 1227 | mSharedElementTargetNames, namedViews); |
| 1228 | setNameOverrides(state, namedViews, true); |
| 1229 | } |
| 1230 | return namedViews; |
| 1231 | } |
| 1232 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1233 | private static Transition mergeTransitions(Transition enterTransition, |
| 1234 | Transition exitTransition, Transition sharedElementTransition, Fragment inFragment, |
| 1235 | boolean isBack) { |
| 1236 | boolean overlap = true; |
George Mount | ec3364c | 2015-06-03 15:47:44 -0700 | [diff] [blame] | 1237 | if (enterTransition != null && exitTransition != null && inFragment != null) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1238 | overlap = isBack ? inFragment.getAllowReturnTransitionOverlap() : |
| 1239 | inFragment.getAllowEnterTransitionOverlap(); |
| 1240 | } |
| 1241 | |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1242 | // Wrap the transitions. Explicit targets like in enter and exit will cause the |
| 1243 | // views to be targeted regardless of excluded views. If that happens, then the |
| 1244 | // excluded fragments views (hidden fragments) will still be in the transition. |
| 1245 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1246 | Transition transition; |
| 1247 | if (overlap) { |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1248 | // Regular transition -- do it all together |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1249 | TransitionSet transitionSet = new TransitionSet(); |
| 1250 | if (enterTransition != null) { |
| 1251 | transitionSet.addTransition(enterTransition); |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1252 | } |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1253 | if (exitTransition != null) { |
| 1254 | transitionSet.addTransition(exitTransition); |
| 1255 | } |
| 1256 | if (sharedElementTransition != null) { |
| 1257 | transitionSet.addTransition(sharedElementTransition); |
| 1258 | } |
| 1259 | transition = transitionSet; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1260 | } else { |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1261 | // First do exit, then enter, but allow shared element transition to happen |
| 1262 | // during both. |
| 1263 | Transition staggered = null; |
| 1264 | if (exitTransition != null && enterTransition != null) { |
| 1265 | staggered = new TransitionSet() |
| 1266 | .addTransition(exitTransition) |
| 1267 | .addTransition(enterTransition) |
| 1268 | .setOrdering(TransitionSet.ORDERING_SEQUENTIAL); |
| 1269 | } else if (exitTransition != null) { |
| 1270 | staggered = exitTransition; |
| 1271 | } else if (enterTransition != null) { |
| 1272 | staggered = enterTransition; |
| 1273 | } |
| 1274 | if (sharedElementTransition != null) { |
| 1275 | TransitionSet together = new TransitionSet(); |
| 1276 | if (staggered != null) { |
| 1277 | together.addTransition(staggered); |
| 1278 | } |
| 1279 | together.addTransition(sharedElementTransition); |
| 1280 | transition = together; |
| 1281 | } else { |
| 1282 | transition = staggered; |
| 1283 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1284 | } |
| 1285 | return transition; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * Configures custom transitions for a specific fragment container. |
| 1290 | * |
| 1291 | * @param containerId The container ID of the fragments to configure the transition for. |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1292 | * @param state The Transition State keeping track of the executing transitions. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1293 | * @param firstOutFragments The list of first fragments to be removed, keyed on the |
| 1294 | * container ID. |
| 1295 | * @param lastInFragments The list of last fragments to be added, keyed on the |
| 1296 | * container ID. |
| 1297 | * @param isBack true if this is popping the back stack or false if this is a |
| 1298 | * forward operation. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1299 | */ |
| 1300 | private void configureTransitions(int containerId, TransitionState state, boolean isBack, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1301 | SparseArray<Fragment> firstOutFragments, SparseArray<Fragment> lastInFragments) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 1302 | ViewGroup sceneRoot = (ViewGroup) mManager.mContainer.onFindViewById(containerId); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1303 | if (sceneRoot != null) { |
| 1304 | Fragment inFragment = lastInFragments.get(containerId); |
| 1305 | Fragment outFragment = firstOutFragments.get(containerId); |
| 1306 | |
| 1307 | Transition enterTransition = getEnterTransition(inFragment, isBack); |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1308 | TransitionSet sharedElementTransition = |
| 1309 | getSharedElementTransition(inFragment, outFragment, isBack); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1310 | Transition exitTransition = getExitTransition(outFragment, isBack); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1311 | |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1312 | if (enterTransition == null && sharedElementTransition == null && |
| 1313 | exitTransition == null) { |
| 1314 | return; // no transitions! |
| 1315 | } |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1316 | if (enterTransition != null) { |
| 1317 | enterTransition.addTarget(state.nonExistentView); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1318 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1319 | ArrayMap<String, View> namedViews = null; |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1320 | ArrayList<View> sharedElementTargets = new ArrayList<View>(); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1321 | if (sharedElementTransition != null) { |
| 1322 | namedViews = remapSharedElements(state, outFragment, isBack); |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1323 | setSharedElementTargets(sharedElementTransition, |
| 1324 | state.nonExistentView, namedViews, sharedElementTargets); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1325 | |
| 1326 | // Notify the start of the transition. |
| 1327 | SharedElementCallback callback = isBack ? |
| 1328 | outFragment.mEnterTransitionCallback : |
| 1329 | inFragment.mEnterTransitionCallback; |
| 1330 | ArrayList<String> names = new ArrayList<String>(namedViews.keySet()); |
| 1331 | ArrayList<View> views = new ArrayList<View>(namedViews.values()); |
| 1332 | callback.onSharedElementStart(names, views, null); |
| 1333 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1334 | |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1335 | ArrayList<View> exitingViews = captureExitingViews(exitTransition, outFragment, |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1336 | namedViews, state.nonExistentView); |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1337 | if (exitingViews == null || exitingViews.isEmpty()) { |
| 1338 | exitTransition = null; |
| 1339 | } |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1340 | excludeViews(enterTransition, exitTransition, exitingViews, true); |
| 1341 | excludeViews(enterTransition, sharedElementTransition, sharedElementTargets, true); |
| 1342 | excludeViews(exitTransition, sharedElementTransition, sharedElementTargets, true); |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1343 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1344 | // Set the epicenter of the exit transition |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1345 | if (mSharedElementTargetNames != null && namedViews != null) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1346 | View epicenterView = namedViews.get(mSharedElementTargetNames.get(0)); |
| 1347 | if (epicenterView != null) { |
George Mount | 27bc8aa | 2014-09-14 16:48:21 -0700 | [diff] [blame] | 1348 | if (exitTransition != null) { |
| 1349 | setEpicenter(exitTransition, epicenterView); |
| 1350 | } |
| 1351 | if (sharedElementTransition != null) { |
| 1352 | setEpicenter(sharedElementTransition, epicenterView); |
| 1353 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1357 | Transition transition = mergeTransitions(enterTransition, exitTransition, |
| 1358 | sharedElementTransition, inFragment, isBack); |
| 1359 | |
| 1360 | if (transition != null) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1361 | ArrayList<View> hiddenFragments = new ArrayList<View>(); |
| 1362 | ArrayList<View> enteringViews = addTransitionTargets(state, enterTransition, |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1363 | sharedElementTransition, exitTransition, transition, sceneRoot, inFragment, |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1364 | outFragment, hiddenFragments, isBack, sharedElementTargets); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1365 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1366 | transition.setNameOverrides(state.nameOverrides); |
| 1367 | // We want to exclude hidden views later, so we need a non-null list in the |
| 1368 | // transition now. |
| 1369 | transition.excludeTarget(state.nonExistentView, true); |
| 1370 | // Now exclude all currently hidden fragments. |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1371 | excludeHiddenFragments(hiddenFragments, containerId, transition); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1372 | TransitionManager.beginDelayedTransition(sceneRoot, transition); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1373 | // Remove the view targeting after the transition starts |
| 1374 | removeTargetedViewsFromTransitions(sceneRoot, state.nonExistentView, |
| 1375 | enterTransition, enteringViews, exitTransition, exitingViews, |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1376 | sharedElementTransition, sharedElementTargets, transition, |
| 1377 | hiddenFragments); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1382 | /** |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1383 | * Finds all children of the shared elements and sets the wrapping TransitionSet |
| 1384 | * targets to point to those. It also limits transitions that have no targets to the |
| 1385 | * specific shared elements. This allows developers to target child views of the |
| 1386 | * shared elements specifically, but this doesn't happen by default. |
| 1387 | */ |
| 1388 | private static void setSharedElementTargets(TransitionSet transition, |
| 1389 | View nonExistentView, ArrayMap<String, View> namedViews, |
| 1390 | ArrayList<View> sharedElementTargets) { |
| 1391 | sharedElementTargets.clear(); |
| 1392 | sharedElementTargets.addAll(namedViews.values()); |
| 1393 | |
| 1394 | final List<View> views = transition.getTargets(); |
| 1395 | views.clear(); |
| 1396 | final int count = sharedElementTargets.size(); |
| 1397 | for (int i = 0; i < count; i++) { |
| 1398 | final View view = sharedElementTargets.get(i); |
| 1399 | bfsAddViewChildren(views, view); |
| 1400 | } |
| 1401 | sharedElementTargets.add(nonExistentView); |
| 1402 | addTargets(transition, sharedElementTargets); |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * Uses a breadth-first scheme to add startView and all of its children to views. |
| 1407 | * It won't add a child if it is already in views. |
| 1408 | */ |
| 1409 | private static void bfsAddViewChildren(final List<View> views, final View startView) { |
| 1410 | final int startIndex = views.size(); |
| 1411 | if (containedBeforeIndex(views, startView, startIndex)) { |
| 1412 | return; // This child is already in the list, so all its children are also. |
| 1413 | } |
| 1414 | views.add(startView); |
| 1415 | for (int index = startIndex; index < views.size(); index++) { |
| 1416 | final View view = views.get(index); |
| 1417 | if (view instanceof ViewGroup) { |
| 1418 | ViewGroup viewGroup = (ViewGroup) view; |
| 1419 | final int childCount = viewGroup.getChildCount(); |
| 1420 | for (int childIndex = 0; childIndex < childCount; childIndex++) { |
| 1421 | final View child = viewGroup.getChildAt(childIndex); |
| 1422 | if (!containedBeforeIndex(views, child, startIndex)) { |
| 1423 | views.add(child); |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | /** |
| 1431 | * Does a linear search through views for view, limited to maxIndex. |
| 1432 | */ |
| 1433 | private static boolean containedBeforeIndex(final List<View> views, final View view, |
| 1434 | final int maxIndex) { |
| 1435 | for (int i = 0; i < maxIndex; i++) { |
| 1436 | if (views.get(i) == view) { |
| 1437 | return true; |
| 1438 | } |
| 1439 | } |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1443 | private static void excludeViews(Transition transition, Transition fromTransition, |
| 1444 | ArrayList<View> views, boolean exclude) { |
| 1445 | if (transition != null) { |
| 1446 | final int viewCount = fromTransition == null ? 0 : views.size(); |
| 1447 | for (int i = 0; i < viewCount; i++) { |
| 1448 | transition.excludeTarget(views.get(i), exclude); |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 1453 | /** |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1454 | * After the transition has started, remove all targets that we added to the transitions |
| 1455 | * so that the transitions are left in a clean state. |
| 1456 | */ |
| 1457 | private void removeTargetedViewsFromTransitions( |
| 1458 | final ViewGroup sceneRoot, final View nonExistingView, |
| 1459 | final Transition enterTransition, final ArrayList<View> enteringViews, |
| 1460 | final Transition exitTransition, final ArrayList<View> exitingViews, |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1461 | final Transition sharedElementTransition, final ArrayList<View> sharedElementTargets, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1462 | final Transition overallTransition, final ArrayList<View> hiddenViews) { |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1463 | if (overallTransition != null) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1464 | sceneRoot.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { |
| 1465 | @Override |
| 1466 | public boolean onPreDraw() { |
| 1467 | sceneRoot.getViewTreeObserver().removeOnPreDrawListener(this); |
| 1468 | if (enterTransition != null) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1469 | removeTargets(enterTransition, enteringViews); |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1470 | excludeViews(enterTransition, exitTransition, exitingViews, false); |
| 1471 | excludeViews(enterTransition, sharedElementTransition, sharedElementTargets, |
| 1472 | false); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1473 | } |
| 1474 | if (exitTransition != null) { |
| 1475 | removeTargets(exitTransition, exitingViews); |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 1476 | excludeViews(exitTransition, enterTransition, enteringViews, false); |
| 1477 | excludeViews(exitTransition, sharedElementTransition, sharedElementTargets, |
| 1478 | false); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1479 | } |
George Mount | 2deeaa3 | 2014-09-19 10:02:02 -0700 | [diff] [blame] | 1480 | if (sharedElementTransition != null) { |
| 1481 | removeTargets(sharedElementTransition, sharedElementTargets); |
| 1482 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1483 | int numViews = hiddenViews.size(); |
| 1484 | for (int i = 0; i < numViews; i++) { |
| 1485 | overallTransition.excludeTarget(hiddenViews.get(i), false); |
| 1486 | } |
| 1487 | overallTransition.excludeTarget(nonExistingView, false); |
| 1488 | return true; |
| 1489 | } |
| 1490 | }); |
| 1491 | } |
| 1492 | } |
| 1493 | |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1494 | /** |
| 1495 | * This method removes the views from transitions that target ONLY those views. |
| 1496 | * The views list should match those added in addTargets and should contain |
| 1497 | * one view that is not in the view hierarchy (state.nonExistentView). |
| 1498 | */ |
| 1499 | public static void removeTargets(Transition transition, ArrayList<View> views) { |
| 1500 | if (transition instanceof TransitionSet) { |
| 1501 | TransitionSet set = (TransitionSet) transition; |
| 1502 | int numTransitions = set.getTransitionCount(); |
| 1503 | for (int i = 0; i < numTransitions; i++) { |
| 1504 | Transition child = set.getTransitionAt(i); |
| 1505 | removeTargets(child, views); |
| 1506 | } |
| 1507 | } else if (!hasSimpleTarget(transition)) { |
| 1508 | List<View> targets = transition.getTargets(); |
| 1509 | if (targets != null && targets.size() == views.size() && |
| 1510 | targets.containsAll(views)) { |
| 1511 | // We have an exact match. We must have added these earlier in addTargets |
| 1512 | for (int i = views.size() - 1; i >= 0; i--) { |
| 1513 | transition.removeTarget(views.get(i)); |
| 1514 | } |
| 1515 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1519 | /** |
| 1520 | * This method adds views as targets to the transition, but only if the transition |
| 1521 | * doesn't already have a target. It is best for views to contain one View object |
| 1522 | * that does not exist in the view hierarchy (state.nonExistentView) so that |
| 1523 | * when they are removed later, a list match will suffice to remove the targets. |
| 1524 | * Otherwise, if you happened to have targeted the exact views for the transition, |
| 1525 | * the removeTargets call will remove them unexpectedly. |
| 1526 | */ |
| 1527 | public static void addTargets(Transition transition, ArrayList<View> views) { |
| 1528 | if (transition instanceof TransitionSet) { |
| 1529 | TransitionSet set = (TransitionSet) transition; |
| 1530 | int numTransitions = set.getTransitionCount(); |
| 1531 | for (int i = 0; i < numTransitions; i++) { |
| 1532 | Transition child = set.getTransitionAt(i); |
| 1533 | addTargets(child, views); |
| 1534 | } |
| 1535 | } else if (!hasSimpleTarget(transition)) { |
| 1536 | List<View> targets = transition.getTargets(); |
| 1537 | if (isNullOrEmpty(targets)) { |
| 1538 | // We can just add the target views |
| 1539 | int numViews = views.size(); |
| 1540 | for (int i = 0; i < numViews; i++) { |
| 1541 | transition.addTarget(views.get(i)); |
| 1542 | } |
| 1543 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
George Mount | 7ce5d75 | 2014-10-30 16:09:22 -0700 | [diff] [blame] | 1547 | private static boolean hasSimpleTarget(Transition transition) { |
| 1548 | return !isNullOrEmpty(transition.getTargetIds()) || |
| 1549 | !isNullOrEmpty(transition.getTargetNames()) || |
| 1550 | !isNullOrEmpty(transition.getTargetTypes()); |
| 1551 | } |
| 1552 | |
| 1553 | private static boolean isNullOrEmpty(List list) { |
| 1554 | return list == null || list.isEmpty(); |
| 1555 | } |
| 1556 | |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1557 | /** |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1558 | * Remaps a name-to-View map, substituting different names for keys. |
| 1559 | * |
| 1560 | * @param inMap A list of keys found in the map, in the order in toGoInMap |
| 1561 | * @param toGoInMap A list of keys to use for the new map, in the order of inMap |
| 1562 | * @param namedViews The current mapping |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1563 | * @return a new Map after it has been mapped with the new names as keys. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1564 | */ |
| 1565 | private static ArrayMap<String, View> remapNames(ArrayList<String> inMap, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1566 | ArrayList<String> toGoInMap, ArrayMap<String, View> namedViews) { |
| 1567 | ArrayMap<String, View> remappedViews = new ArrayMap<String, View>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1568 | if (!namedViews.isEmpty()) { |
| 1569 | int numKeys = inMap.size(); |
| 1570 | for (int i = 0; i < numKeys; i++) { |
| 1571 | View view = namedViews.get(inMap.get(i)); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1572 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1573 | if (view != null) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1574 | remappedViews.put(toGoInMap.get(i), view); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | } |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1578 | return remappedViews; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | /** |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1582 | * Maps shared elements to views in the entering fragment. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1583 | * |
| 1584 | * @param state The transition State as returned from {@link #beginTransition( |
| 1585 | * android.util.SparseArray, android.util.SparseArray, boolean)}. |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1586 | * @param inFragment The last fragment to be added. |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1587 | * @param isBack true if this is popping the back stack or false if this is a |
| 1588 | * forward operation. |
| 1589 | */ |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1590 | private ArrayMap<String, View> mapEnteringSharedElements(TransitionState state, |
| 1591 | Fragment inFragment, boolean isBack) { |
| 1592 | ArrayMap<String, View> namedViews = new ArrayMap<String, View>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1593 | View root = inFragment.getView(); |
| 1594 | if (root != null) { |
| 1595 | if (mSharedElementSourceNames != null) { |
| 1596 | root.findNamedViews(namedViews); |
| 1597 | if (isBack) { |
| 1598 | namedViews = remapNames(mSharedElementSourceNames, |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1599 | mSharedElementTargetNames, namedViews); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1600 | } else { |
| 1601 | namedViews.retainAll(mSharedElementTargetNames); |
| 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | return namedViews; |
| 1606 | } |
| 1607 | |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1608 | private void excludeHiddenFragments(final ArrayList<View> hiddenFragmentViews, int containerId, |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1609 | Transition transition) { |
| 1610 | if (mManager.mAdded != null) { |
| 1611 | for (int i = 0; i < mManager.mAdded.size(); i++) { |
| 1612 | Fragment fragment = mManager.mAdded.get(i); |
| 1613 | if (fragment.mView != null && fragment.mContainer != null && |
| 1614 | fragment.mContainerId == containerId) { |
| 1615 | if (fragment.mHidden) { |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1616 | if (!hiddenFragmentViews.contains(fragment.mView)) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1617 | transition.excludeTarget(fragment.mView, true); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1618 | hiddenFragmentViews.add(fragment.mView); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } else { |
| 1621 | transition.excludeTarget(fragment.mView, false); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1622 | hiddenFragmentViews.remove(fragment.mView); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1629 | private static void setEpicenter(Transition transition, View view) { |
| 1630 | final Rect epicenter = new Rect(); |
| 1631 | view.getBoundsOnScreen(epicenter); |
| 1632 | |
| 1633 | transition.setEpicenterCallback(new Transition.EpicenterCallback() { |
| 1634 | @Override |
| 1635 | public Rect onGetEpicenter(Transition transition) { |
| 1636 | return epicenter; |
| 1637 | } |
| 1638 | }); |
| 1639 | } |
| 1640 | |
| 1641 | private void setSharedElementEpicenter(Transition transition, final TransitionState state) { |
| 1642 | transition.setEpicenterCallback(new Transition.EpicenterCallback() { |
| 1643 | private Rect mEpicenter; |
| 1644 | |
| 1645 | @Override |
| 1646 | public Rect onGetEpicenter(Transition transition) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1647 | if (mEpicenter == null && state.enteringEpicenterView != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1648 | mEpicenter = new Rect(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1649 | state.enteringEpicenterView.getBoundsOnScreen(mEpicenter); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1650 | } |
| 1651 | return mEpicenter; |
| 1652 | } |
| 1653 | }); |
| 1654 | } |
| 1655 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1656 | public TransitionState popFromBackStack(boolean doStateMove, TransitionState state, |
| 1657 | SparseArray<Fragment> firstOutFragments, SparseArray<Fragment> lastInFragments) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1658 | if (FragmentManagerImpl.DEBUG) { |
| 1659 | Log.v(TAG, "popFromBackStack: " + this); |
| 1660 | LogWriter logw = new LogWriter(Log.VERBOSE, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 1661 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1662 | dump(" ", null, pw, null); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 1663 | pw.flush(); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1664 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1665 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1666 | if (state == null) { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1667 | if (firstOutFragments.size() != 0 || lastInFragments.size() != 0) { |
| 1668 | state = beginTransition(firstOutFragments, lastInFragments, true); |
| 1669 | } |
| 1670 | } else if (!doStateMove) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1671 | setNameOverrides(state, mSharedElementTargetNames, mSharedElementSourceNames); |
| 1672 | } |
| 1673 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1674 | bumpBackStackNesting(-1); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1675 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1676 | Op op = mTail; |
| 1677 | while (op != null) { |
| 1678 | switch (op.cmd) { |
| 1679 | case OP_ADD: { |
| 1680 | Fragment f = op.fragment; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 1681 | f.mNextAnim = op.popExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1682 | mManager.removeFragment(f, |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1683 | FragmentManagerImpl.reverseTransit(mTransition), |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1684 | mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1685 | } |
| 1686 | break; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1687 | case OP_REPLACE: { |
| 1688 | Fragment f = op.fragment; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1689 | if (f != null) { |
| 1690 | f.mNextAnim = op.popExitAnim; |
| 1691 | mManager.removeFragment(f, |
| 1692 | FragmentManagerImpl.reverseTransit(mTransition), |
| 1693 | mTransitionStyle); |
| 1694 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1695 | if (op.removed != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1696 | for (int i = 0; i < op.removed.size(); i++) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1697 | Fragment old = op.removed.get(i); |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 1698 | old.mNextAnim = op.popEnterAnim; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1699 | mManager.addFragment(old, false); |
| 1700 | } |
| 1701 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1702 | } |
| 1703 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1704 | case OP_REMOVE: { |
| 1705 | Fragment f = op.fragment; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 1706 | f.mNextAnim = op.popEnterAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1707 | mManager.addFragment(f, false); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1708 | } |
| 1709 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1710 | case OP_HIDE: { |
| 1711 | Fragment f = op.fragment; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 1712 | f.mNextAnim = op.popEnterAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1713 | mManager.showFragment(f, |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1714 | FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1715 | } |
| 1716 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1717 | case OP_SHOW: { |
| 1718 | Fragment f = op.fragment; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 1719 | f.mNextAnim = op.popExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1720 | mManager.hideFragment(f, |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1721 | FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1722 | } |
| 1723 | break; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1724 | case OP_DETACH: { |
| 1725 | Fragment f = op.fragment; |
Dianne Hackborn | d04ad54 | 2011-07-25 16:16:15 -0700 | [diff] [blame] | 1726 | f.mNextAnim = op.popEnterAnim; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1727 | mManager.attachFragment(f, |
| 1728 | FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1729 | } |
| 1730 | break; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1731 | case OP_ATTACH: { |
| 1732 | Fragment f = op.fragment; |
Dianne Hackborn | d04ad54 | 2011-07-25 16:16:15 -0700 | [diff] [blame] | 1733 | f.mNextAnim = op.popExitAnim; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1734 | mManager.detachFragment(f, |
| 1735 | FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1736 | } |
| 1737 | break; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1738 | default: { |
| 1739 | throw new IllegalArgumentException("Unknown cmd: " + op.cmd); |
| 1740 | } |
| 1741 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1742 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1743 | op = op.prev; |
| 1744 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1745 | |
Dianne Hackborn | 5f36c96 | 2010-08-26 15:54:17 -0700 | [diff] [blame] | 1746 | if (doStateMove) { |
| 1747 | mManager.moveToState(mManager.mCurState, |
| 1748 | FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle, true); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1749 | state = null; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1750 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1751 | |
| 1752 | if (mIndex >= 0) { |
| 1753 | mManager.freeBackStackIndex(mIndex); |
| 1754 | mIndex = -1; |
| 1755 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1756 | return state; |
| 1757 | } |
| 1758 | |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1759 | private static void setNameOverride(ArrayMap<String, String> overrides, |
| 1760 | String source, String target) { |
| 1761 | if (source != null && target != null && !source.equals(target)) { |
| 1762 | for (int index = 0; index < overrides.size(); index++) { |
| 1763 | if (source.equals(overrides.valueAt(index))) { |
| 1764 | overrides.setValueAt(index, target); |
| 1765 | return; |
| 1766 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1767 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1768 | overrides.put(source, target); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1769 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | private static void setNameOverrides(TransitionState state, ArrayList<String> sourceNames, |
| 1773 | ArrayList<String> targetNames) { |
George Mount | 13d7032 | 2015-07-21 14:30:24 -0700 | [diff] [blame] | 1774 | if (sourceNames != null && targetNames != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1775 | for (int i = 0; i < sourceNames.size(); i++) { |
| 1776 | String source = sourceNames.get(i); |
| 1777 | String target = targetNames.get(i); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1778 | setNameOverride(state.nameOverrides, source, target); |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | private void setBackNameOverrides(TransitionState state, ArrayMap<String, View> namedViews, |
| 1784 | boolean isEnd) { |
George Mount | 13d7032 | 2015-07-21 14:30:24 -0700 | [diff] [blame] | 1785 | int targetCount = mSharedElementTargetNames == null ? 0 : mSharedElementTargetNames.size(); |
| 1786 | int sourceCount = mSharedElementSourceNames == null ? 0 : mSharedElementSourceNames.size(); |
| 1787 | final int count = Math.min(targetCount, sourceCount); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1788 | for (int i = 0; i < count; i++) { |
| 1789 | String source = mSharedElementSourceNames.get(i); |
| 1790 | String originalTarget = mSharedElementTargetNames.get(i); |
George Mount | ecfd007 | 2014-09-07 10:06:25 -0700 | [diff] [blame] | 1791 | View view = namedViews.get(originalTarget); |
| 1792 | if (view != null) { |
| 1793 | String target = view.getTransitionName(); |
| 1794 | if (isEnd) { |
| 1795 | setNameOverride(state.nameOverrides, source, target); |
| 1796 | } else { |
| 1797 | setNameOverride(state.nameOverrides, target, source); |
| 1798 | } |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | private void setNameOverrides(TransitionState state, ArrayMap<String, View> namedViews, |
| 1804 | boolean isEnd) { |
George Mount | ec3364c | 2015-06-03 15:47:44 -0700 | [diff] [blame] | 1805 | int count = namedViews == null ? 0 : namedViews.size(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1806 | for (int i = 0; i < count; i++) { |
| 1807 | String source = namedViews.keyAt(i); |
| 1808 | String target = namedViews.valueAt(i).getTransitionName(); |
| 1809 | if (isEnd) { |
| 1810 | setNameOverride(state.nameOverrides, source, target); |
| 1811 | } else { |
| 1812 | setNameOverride(state.nameOverrides, target, source); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1813 | } |
| 1814 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1815 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1816 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1817 | public String getName() { |
| 1818 | return mName; |
| 1819 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1820 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1821 | public int getTransition() { |
| 1822 | return mTransition; |
| 1823 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1824 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1825 | public int getTransitionStyle() { |
| 1826 | return mTransitionStyle; |
| 1827 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 1828 | |
| 1829 | public boolean isEmpty() { |
| 1830 | return mNumOp == 0; |
| 1831 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1832 | |
| 1833 | public class TransitionState { |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1834 | public ArrayMap<String, String> nameOverrides = new ArrayMap<String, String>(); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1835 | public View enteringEpicenterView; |
| 1836 | public View nonExistentView; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1837 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1838 | } |