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 | |
Clara Bayarri | cd916f2 | 2016-06-13 13:14:51 +0100 | [diff] [blame] | 19 | import android.os.Build; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 24 | import android.util.LogWriter; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 25 | import android.view.View; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 26 | |
George Mount | a7245b4 | 2016-02-10 17:03:32 -0800 | [diff] [blame] | 27 | import com.android.internal.util.FastPrintWriter; |
| 28 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 29 | import java.io.FileDescriptor; |
| 30 | import java.io.PrintWriter; |
Clara Bayarri | cd916f2 | 2016-06-13 13:14:51 +0100 | [diff] [blame] | 31 | import java.lang.reflect.Modifier; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | |
| 34 | final class BackStackState implements Parcelable { |
| 35 | final int[] mOps; |
| 36 | final int mTransition; |
| 37 | final int mTransitionStyle; |
| 38 | final String mName; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 39 | final int mIndex; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 40 | final int mBreadCrumbTitleRes; |
| 41 | final CharSequence mBreadCrumbTitleText; |
| 42 | final int mBreadCrumbShortTitleRes; |
| 43 | final CharSequence mBreadCrumbShortTitleText; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 44 | final ArrayList<String> mSharedElementSourceNames; |
| 45 | final ArrayList<String> mSharedElementTargetNames; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 46 | final boolean mAllowOptimization; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 47 | |
| 48 | public BackStackState(FragmentManagerImpl fm, BackStackRecord bse) { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 49 | final int numOps = bse.mOps.size(); |
| 50 | mOps = new int[numOps * 6]; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 51 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 52 | if (!bse.mAddToBackStack) { |
| 53 | throw new IllegalStateException("Not on back stack"); |
| 54 | } |
| 55 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 56 | int pos = 0; |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 57 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 58 | final BackStackRecord.Op op = bse.mOps.get(opNum); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 59 | mOps[pos++] = op.cmd; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 60 | mOps[pos++] = op.fragment != null ? op.fragment.mIndex : -1; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 61 | mOps[pos++] = op.enterAnim; |
| 62 | mOps[pos++] = op.exitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 63 | mOps[pos++] = op.popEnterAnim; |
| 64 | mOps[pos++] = op.popExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 65 | } |
| 66 | mTransition = bse.mTransition; |
| 67 | mTransitionStyle = bse.mTransitionStyle; |
| 68 | mName = bse.mName; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 69 | mIndex = bse.mIndex; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 70 | mBreadCrumbTitleRes = bse.mBreadCrumbTitleRes; |
| 71 | mBreadCrumbTitleText = bse.mBreadCrumbTitleText; |
| 72 | mBreadCrumbShortTitleRes = bse.mBreadCrumbShortTitleRes; |
| 73 | mBreadCrumbShortTitleText = bse.mBreadCrumbShortTitleText; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 74 | mSharedElementSourceNames = bse.mSharedElementSourceNames; |
| 75 | mSharedElementTargetNames = bse.mSharedElementTargetNames; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 76 | mAllowOptimization = bse.mAllowOptimization; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 77 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 78 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 79 | public BackStackState(Parcel in) { |
| 80 | mOps = in.createIntArray(); |
| 81 | mTransition = in.readInt(); |
| 82 | mTransitionStyle = in.readInt(); |
| 83 | mName = in.readString(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 84 | mIndex = in.readInt(); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 85 | mBreadCrumbTitleRes = in.readInt(); |
| 86 | mBreadCrumbTitleText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 87 | mBreadCrumbShortTitleRes = in.readInt(); |
| 88 | mBreadCrumbShortTitleText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 89 | mSharedElementSourceNames = in.createStringArrayList(); |
| 90 | mSharedElementTargetNames = in.createStringArrayList(); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 91 | mAllowOptimization = in.readInt() != 0; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 92 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 93 | |
| 94 | public BackStackRecord instantiate(FragmentManagerImpl fm) { |
| 95 | BackStackRecord bse = new BackStackRecord(fm); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 96 | int pos = 0; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 97 | int num = 0; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 98 | while (pos < mOps.length) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 99 | BackStackRecord.Op op = new BackStackRecord.Op(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 100 | op.cmd = mOps[pos++]; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 101 | if (FragmentManagerImpl.DEBUG) { |
| 102 | Log.v(FragmentManagerImpl.TAG, |
| 103 | "Instantiate " + bse + " op #" + num + " base fragment #" + mOps[pos]); |
| 104 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 105 | int findex = mOps[pos++]; |
| 106 | if (findex >= 0) { |
| 107 | Fragment f = fm.mActive.get(findex); |
| 108 | op.fragment = f; |
| 109 | } else { |
| 110 | op.fragment = null; |
| 111 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 112 | op.enterAnim = mOps[pos++]; |
| 113 | op.exitAnim = mOps[pos++]; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 114 | op.popEnterAnim = mOps[pos++]; |
| 115 | op.popExitAnim = mOps[pos++]; |
George Mount | 41fb994 | 2016-03-30 13:59:23 -0700 | [diff] [blame] | 116 | bse.mEnterAnim = op.enterAnim; |
| 117 | bse.mExitAnim = op.exitAnim; |
| 118 | bse.mPopEnterAnim = op.popEnterAnim; |
| 119 | bse.mPopExitAnim = op.popExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 120 | bse.addOp(op); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 121 | num++; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 122 | } |
| 123 | bse.mTransition = mTransition; |
| 124 | bse.mTransitionStyle = mTransitionStyle; |
| 125 | bse.mName = mName; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 126 | bse.mIndex = mIndex; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 127 | bse.mAddToBackStack = true; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 128 | bse.mBreadCrumbTitleRes = mBreadCrumbTitleRes; |
| 129 | bse.mBreadCrumbTitleText = mBreadCrumbTitleText; |
| 130 | bse.mBreadCrumbShortTitleRes = mBreadCrumbShortTitleRes; |
| 131 | bse.mBreadCrumbShortTitleText = mBreadCrumbShortTitleText; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 132 | bse.mSharedElementSourceNames = mSharedElementSourceNames; |
| 133 | bse.mSharedElementTargetNames = mSharedElementTargetNames; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 134 | bse.mAllowOptimization = mAllowOptimization; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 135 | bse.bumpBackStackNesting(1); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 136 | return bse; |
| 137 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 138 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 139 | public int describeContents() { |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | public void writeToParcel(Parcel dest, int flags) { |
| 144 | dest.writeIntArray(mOps); |
| 145 | dest.writeInt(mTransition); |
| 146 | dest.writeInt(mTransitionStyle); |
| 147 | dest.writeString(mName); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 148 | dest.writeInt(mIndex); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 149 | dest.writeInt(mBreadCrumbTitleRes); |
| 150 | TextUtils.writeToParcel(mBreadCrumbTitleText, dest, 0); |
| 151 | dest.writeInt(mBreadCrumbShortTitleRes); |
| 152 | TextUtils.writeToParcel(mBreadCrumbShortTitleText, dest, 0); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 153 | dest.writeStringList(mSharedElementSourceNames); |
| 154 | dest.writeStringList(mSharedElementTargetNames); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 155 | dest.writeInt(mAllowOptimization ? 1 : 0); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 156 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 157 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 158 | public static final Parcelable.Creator<BackStackState> CREATOR |
| 159 | = new Parcelable.Creator<BackStackState>() { |
| 160 | public BackStackState createFromParcel(Parcel in) { |
| 161 | return new BackStackState(in); |
| 162 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 163 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 164 | public BackStackState[] newArray(int size) { |
| 165 | return new BackStackState[size]; |
| 166 | } |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @hide Entry of an operation on the fragment back stack. |
| 172 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 173 | final class BackStackRecord extends FragmentTransaction implements |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 174 | FragmentManager.BackStackEntry, FragmentManagerImpl.OpGenerator { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 175 | static final String TAG = FragmentManagerImpl.TAG; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 176 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 177 | final FragmentManagerImpl mManager; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 178 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 179 | static final int OP_NULL = 0; |
| 180 | static final int OP_ADD = 1; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 181 | static final int OP_REPLACE = 2; |
| 182 | static final int OP_REMOVE = 3; |
| 183 | static final int OP_HIDE = 4; |
| 184 | static final int OP_SHOW = 5; |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 185 | static final int OP_DETACH = 6; |
| 186 | static final int OP_ATTACH = 7; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 187 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 188 | static final class Op { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 189 | int cmd; |
| 190 | Fragment fragment; |
| 191 | int enterAnim; |
| 192 | int exitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 193 | int popEnterAnim; |
| 194 | int popExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 195 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 196 | |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 197 | ArrayList<Op> mOps = new ArrayList<>(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 198 | int mEnterAnim; |
| 199 | int mExitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 200 | int mPopEnterAnim; |
| 201 | int mPopExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 202 | int mTransition; |
| 203 | int mTransitionStyle; |
| 204 | boolean mAddToBackStack; |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 205 | boolean mAllowAddToBackStack = true; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 206 | String mName; |
| 207 | boolean mCommitted; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 208 | int mIndex = -1; |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 209 | boolean mAllowOptimization; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 210 | |
| 211 | int mBreadCrumbTitleRes; |
| 212 | CharSequence mBreadCrumbTitleText; |
| 213 | int mBreadCrumbShortTitleRes; |
| 214 | CharSequence mBreadCrumbShortTitleText; |
| 215 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 216 | ArrayList<String> mSharedElementSourceNames; |
| 217 | ArrayList<String> mSharedElementTargetNames; |
| 218 | |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public String toString() { |
| 221 | StringBuilder sb = new StringBuilder(128); |
| 222 | sb.append("BackStackEntry{"); |
| 223 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 224 | if (mIndex >= 0) { |
| 225 | sb.append(" #"); |
| 226 | sb.append(mIndex); |
| 227 | } |
| 228 | if (mName != null) { |
| 229 | sb.append(" "); |
| 230 | sb.append(mName); |
| 231 | } |
| 232 | sb.append("}"); |
| 233 | return sb.toString(); |
| 234 | } |
| 235 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 236 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 237 | dump(prefix, writer, true); |
| 238 | } |
| 239 | |
| 240 | void dump(String prefix, PrintWriter writer, boolean full) { |
| 241 | if (full) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 242 | writer.print(prefix); |
| 243 | writer.print("mName="); |
| 244 | writer.print(mName); |
| 245 | writer.print(" mIndex="); |
| 246 | writer.print(mIndex); |
| 247 | writer.print(" mCommitted="); |
| 248 | writer.println(mCommitted); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 249 | if (mTransition != FragmentTransaction.TRANSIT_NONE) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 250 | writer.print(prefix); |
| 251 | writer.print("mTransition=#"); |
| 252 | writer.print(Integer.toHexString(mTransition)); |
| 253 | writer.print(" mTransitionStyle=#"); |
| 254 | writer.println(Integer.toHexString(mTransitionStyle)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 255 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 256 | if (mEnterAnim != 0 || mExitAnim != 0) { |
| 257 | writer.print(prefix); |
| 258 | writer.print("mEnterAnim=#"); |
| 259 | writer.print(Integer.toHexString(mEnterAnim)); |
| 260 | writer.print(" mExitAnim=#"); |
| 261 | writer.println(Integer.toHexString(mExitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 262 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 263 | if (mPopEnterAnim != 0 || mPopExitAnim != 0) { |
| 264 | writer.print(prefix); |
| 265 | writer.print("mPopEnterAnim=#"); |
| 266 | writer.print(Integer.toHexString(mPopEnterAnim)); |
| 267 | writer.print(" mPopExitAnim=#"); |
| 268 | writer.println(Integer.toHexString(mPopExitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 269 | } |
| 270 | if (mBreadCrumbTitleRes != 0 || mBreadCrumbTitleText != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 271 | writer.print(prefix); |
| 272 | writer.print("mBreadCrumbTitleRes=#"); |
| 273 | writer.print(Integer.toHexString(mBreadCrumbTitleRes)); |
| 274 | writer.print(" mBreadCrumbTitleText="); |
| 275 | writer.println(mBreadCrumbTitleText); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 276 | } |
| 277 | if (mBreadCrumbShortTitleRes != 0 || mBreadCrumbShortTitleText != null) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 278 | writer.print(prefix); |
| 279 | writer.print("mBreadCrumbShortTitleRes=#"); |
| 280 | writer.print(Integer.toHexString(mBreadCrumbShortTitleRes)); |
| 281 | writer.print(" mBreadCrumbShortTitleText="); |
| 282 | writer.println(mBreadCrumbShortTitleText); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 283 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 284 | } |
| 285 | |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 286 | if (!mOps.isEmpty()) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 287 | writer.print(prefix); |
| 288 | writer.println("Operations:"); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 289 | String innerPrefix = prefix + " "; |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 290 | final int numOps = mOps.size(); |
| 291 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 292 | final Op op = mOps.get(opNum); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 293 | String cmdStr; |
| 294 | switch (op.cmd) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 295 | case OP_NULL: |
| 296 | cmdStr = "NULL"; |
| 297 | break; |
| 298 | case OP_ADD: |
| 299 | cmdStr = "ADD"; |
| 300 | break; |
| 301 | case OP_REPLACE: |
| 302 | cmdStr = "REPLACE"; |
| 303 | break; |
| 304 | case OP_REMOVE: |
| 305 | cmdStr = "REMOVE"; |
| 306 | break; |
| 307 | case OP_HIDE: |
| 308 | cmdStr = "HIDE"; |
| 309 | break; |
| 310 | case OP_SHOW: |
| 311 | cmdStr = "SHOW"; |
| 312 | break; |
| 313 | case OP_DETACH: |
| 314 | cmdStr = "DETACH"; |
| 315 | break; |
| 316 | case OP_ATTACH: |
| 317 | cmdStr = "ATTACH"; |
| 318 | break; |
| 319 | default: |
| 320 | cmdStr = "cmd=" + op.cmd; |
| 321 | break; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 322 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 323 | writer.print(prefix); |
| 324 | writer.print(" Op #"); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 325 | writer.print(opNum); |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 326 | writer.print(": "); |
| 327 | writer.print(cmdStr); |
| 328 | writer.print(" "); |
| 329 | writer.println(op.fragment); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 330 | if (full) { |
| 331 | if (op.enterAnim != 0 || op.exitAnim != 0) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 332 | writer.print(innerPrefix); |
| 333 | writer.print("enterAnim=#"); |
| 334 | writer.print(Integer.toHexString(op.enterAnim)); |
| 335 | writer.print(" exitAnim=#"); |
| 336 | writer.println(Integer.toHexString(op.exitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 337 | } |
| 338 | if (op.popEnterAnim != 0 || op.popExitAnim != 0) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 339 | writer.print(innerPrefix); |
| 340 | writer.print("popEnterAnim=#"); |
| 341 | writer.print(Integer.toHexString(op.popEnterAnim)); |
| 342 | writer.print(" popExitAnim=#"); |
| 343 | writer.println(Integer.toHexString(op.popExitAnim)); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 344 | } |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 345 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 350 | public BackStackRecord(FragmentManagerImpl manager) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 351 | mManager = manager; |
Clara Bayarri | 7eda644 | 2016-11-10 20:45:38 +0000 | [diff] [blame^] | 352 | int targetSdkVersion = manager.mHost.getContext().getApplicationInfo().targetSdkVersion; |
| 353 | // TODO: make the check N_MR1 or O |
| 354 | mAllowOptimization = targetSdkVersion > Build.VERSION_CODES.N; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 355 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 356 | |
| 357 | public int getId() { |
| 358 | return mIndex; |
| 359 | } |
| 360 | |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 361 | public int getBreadCrumbTitleRes() { |
| 362 | return mBreadCrumbTitleRes; |
| 363 | } |
| 364 | |
| 365 | public int getBreadCrumbShortTitleRes() { |
| 366 | return mBreadCrumbShortTitleRes; |
| 367 | } |
| 368 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 369 | public CharSequence getBreadCrumbTitle() { |
| 370 | if (mBreadCrumbTitleRes != 0) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 371 | return mManager.mHost.getContext().getText(mBreadCrumbTitleRes); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 372 | } |
| 373 | return mBreadCrumbTitleText; |
| 374 | } |
| 375 | |
| 376 | public CharSequence getBreadCrumbShortTitle() { |
| 377 | if (mBreadCrumbShortTitleRes != 0) { |
Todd Kennedy | a5fc6f0 | 2015-04-14 18:22:54 -0700 | [diff] [blame] | 378 | return mManager.mHost.getContext().getText(mBreadCrumbShortTitleRes); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 379 | } |
| 380 | return mBreadCrumbShortTitleText; |
| 381 | } |
| 382 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 383 | void addOp(Op op) { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 384 | mOps.add(op); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 385 | op.enterAnim = mEnterAnim; |
| 386 | op.exitAnim = mExitAnim; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 387 | op.popEnterAnim = mPopEnterAnim; |
| 388 | op.popExitAnim = mPopExitAnim; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 389 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 390 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 391 | public FragmentTransaction add(Fragment fragment, String tag) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 392 | doAddOp(0, fragment, tag, OP_ADD); |
| 393 | return this; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | public FragmentTransaction add(int containerViewId, Fragment fragment) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 397 | doAddOp(containerViewId, fragment, null, OP_ADD); |
| 398 | return this; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | public FragmentTransaction add(int containerViewId, Fragment fragment, String tag) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 402 | doAddOp(containerViewId, fragment, tag, OP_ADD); |
| 403 | return this; |
| 404 | } |
| 405 | |
| 406 | private void doAddOp(int containerViewId, Fragment fragment, String tag, int opcmd) { |
Clara Bayarri | cd916f2 | 2016-06-13 13:14:51 +0100 | [diff] [blame] | 407 | if (mManager.mHost.getContext() != null) { |
| 408 | final int targetSdkVersion = |
| 409 | mManager.mHost.getContext().getApplicationInfo().targetSdkVersion; |
| 410 | final Class fragmentClass = fragment.getClass(); |
| 411 | final int modifiers = fragmentClass.getModifiers(); |
| 412 | // TODO: make the check N_MR1 or O |
| 413 | if (targetSdkVersion > Build.VERSION_CODES.N && (fragmentClass.isAnonymousClass() |
| 414 | || !Modifier.isPublic(modifiers) |
| 415 | || (fragmentClass.isMemberClass() && !Modifier.isStatic(modifiers)))) { |
Clara Bayarri | eb7b7ec | 2016-06-23 11:08:55 +0100 | [diff] [blame] | 416 | throw new IllegalStateException("Fragment " + fragmentClass.getCanonicalName() |
| 417 | + " must be a public static class to be properly recreated from" |
| 418 | + " instance state."); |
Clara Bayarri | cd916f2 | 2016-06-13 13:14:51 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 421 | fragment.mFragmentManager = mManager; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 422 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 423 | if (tag != null) { |
| 424 | if (fragment.mTag != null && !tag.equals(fragment.mTag)) { |
| 425 | throw new IllegalStateException("Can't change tag of fragment " |
| 426 | + fragment + ": was " + fragment.mTag |
| 427 | + " now " + tag); |
| 428 | } |
| 429 | fragment.mTag = tag; |
| 430 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 431 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 432 | if (containerViewId != 0) { |
Adam Powell | e01f595 | 2016-02-23 15:25:42 -0800 | [diff] [blame] | 433 | if (containerViewId == View.NO_ID) { |
| 434 | throw new IllegalArgumentException("Can't add fragment " |
| 435 | + fragment + " with tag " + tag + " to container view with no id"); |
| 436 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 437 | if (fragment.mFragmentId != 0 && fragment.mFragmentId != containerViewId) { |
| 438 | throw new IllegalStateException("Can't change container ID of fragment " |
| 439 | + fragment + ": was " + fragment.mFragmentId |
| 440 | + " now " + containerViewId); |
| 441 | } |
| 442 | fragment.mContainerId = fragment.mFragmentId = containerViewId; |
| 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 | Op op = new Op(); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 446 | op.cmd = opcmd; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 447 | op.fragment = fragment; |
| 448 | addOp(op); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | public FragmentTransaction replace(int containerViewId, Fragment fragment) { |
| 452 | return replace(containerViewId, fragment, null); |
| 453 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 454 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 455 | public FragmentTransaction replace(int containerViewId, Fragment fragment, String tag) { |
| 456 | if (containerViewId == 0) { |
| 457 | throw new IllegalArgumentException("Must use non-zero containerViewId"); |
| 458 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 459 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 460 | doAddOp(containerViewId, fragment, tag, OP_REPLACE); |
| 461 | return this; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 462 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 463 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 464 | public FragmentTransaction remove(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 465 | Op op = new Op(); |
| 466 | op.cmd = OP_REMOVE; |
| 467 | op.fragment = fragment; |
| 468 | addOp(op); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 469 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 470 | return this; |
| 471 | } |
| 472 | |
| 473 | public FragmentTransaction hide(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 474 | Op op = new Op(); |
| 475 | op.cmd = OP_HIDE; |
| 476 | op.fragment = fragment; |
| 477 | addOp(op); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 478 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 479 | return this; |
| 480 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 481 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 482 | public FragmentTransaction show(Fragment fragment) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 483 | Op op = new Op(); |
| 484 | op.cmd = OP_SHOW; |
| 485 | op.fragment = fragment; |
| 486 | addOp(op); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 487 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 488 | return this; |
| 489 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 490 | |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 491 | public FragmentTransaction detach(Fragment fragment) { |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 492 | Op op = new Op(); |
| 493 | op.cmd = OP_DETACH; |
| 494 | op.fragment = fragment; |
| 495 | addOp(op); |
| 496 | |
| 497 | return this; |
| 498 | } |
| 499 | |
| 500 | public FragmentTransaction attach(Fragment fragment) { |
Dianne Hackborn | 47c4156 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 501 | Op op = new Op(); |
| 502 | op.cmd = OP_ATTACH; |
| 503 | op.fragment = fragment; |
| 504 | addOp(op); |
| 505 | |
| 506 | return this; |
| 507 | } |
| 508 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 509 | public FragmentTransaction setCustomAnimations(int enter, int exit) { |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 510 | return setCustomAnimations(enter, exit, 0, 0); |
| 511 | } |
| 512 | |
| 513 | public FragmentTransaction setCustomAnimations(int enter, int exit, |
| 514 | int popEnter, int popExit) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 515 | mEnterAnim = enter; |
| 516 | mExitAnim = exit; |
Chet Haase | bc37784 | 2011-03-22 11:35:22 -0700 | [diff] [blame] | 517 | mPopEnterAnim = popEnter; |
| 518 | mPopExitAnim = popExit; |
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 setTransition(int transition) { |
| 523 | mTransition = transition; |
| 524 | return this; |
| 525 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 526 | |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 527 | @Override |
George Mount | 448bace | 2014-08-18 16:27:43 -0700 | [diff] [blame] | 528 | public FragmentTransaction addSharedElement(View sharedElement, String name) { |
| 529 | String transitionName = sharedElement.getTransitionName(); |
| 530 | if (transitionName == null) { |
| 531 | throw new IllegalArgumentException("Unique transitionNames are required for all" + |
| 532 | " sharedElements"); |
| 533 | } |
| 534 | if (mSharedElementSourceNames == null) { |
| 535 | mSharedElementSourceNames = new ArrayList<String>(); |
| 536 | mSharedElementTargetNames = new ArrayList<String>(); |
| 537 | } |
| 538 | mSharedElementSourceNames.add(transitionName); |
| 539 | mSharedElementTargetNames.add(name); |
| 540 | return this; |
| 541 | } |
| 542 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 543 | public FragmentTransaction setTransitionStyle(int styleRes) { |
| 544 | mTransitionStyle = styleRes; |
| 545 | return this; |
| 546 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 547 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 548 | public FragmentTransaction addToBackStack(String name) { |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 549 | if (!mAllowAddToBackStack) { |
| 550 | throw new IllegalStateException( |
| 551 | "This FragmentTransaction is not allowed to be added to the back stack."); |
| 552 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 553 | mAddToBackStack = true; |
| 554 | mName = name; |
| 555 | return this; |
| 556 | } |
| 557 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 558 | public boolean isAddToBackStackAllowed() { |
| 559 | return mAllowAddToBackStack; |
| 560 | } |
| 561 | |
| 562 | public FragmentTransaction disallowAddToBackStack() { |
| 563 | if (mAddToBackStack) { |
| 564 | throw new IllegalStateException( |
| 565 | "This transaction is already being added to the back stack"); |
| 566 | } |
| 567 | mAllowAddToBackStack = false; |
| 568 | return this; |
| 569 | } |
| 570 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 571 | public FragmentTransaction setBreadCrumbTitle(int res) { |
| 572 | mBreadCrumbTitleRes = res; |
| 573 | mBreadCrumbTitleText = null; |
| 574 | return this; |
| 575 | } |
| 576 | |
| 577 | public FragmentTransaction setBreadCrumbTitle(CharSequence text) { |
| 578 | mBreadCrumbTitleRes = 0; |
| 579 | mBreadCrumbTitleText = text; |
| 580 | return this; |
| 581 | } |
| 582 | |
| 583 | public FragmentTransaction setBreadCrumbShortTitle(int res) { |
| 584 | mBreadCrumbShortTitleRes = res; |
| 585 | mBreadCrumbShortTitleText = null; |
| 586 | return this; |
| 587 | } |
| 588 | |
| 589 | public FragmentTransaction setBreadCrumbShortTitle(CharSequence text) { |
| 590 | mBreadCrumbShortTitleRes = 0; |
| 591 | mBreadCrumbShortTitleText = text; |
| 592 | return this; |
| 593 | } |
| 594 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 595 | void bumpBackStackNesting(int amt) { |
| 596 | if (!mAddToBackStack) { |
| 597 | return; |
| 598 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 599 | if (FragmentManagerImpl.DEBUG) { |
| 600 | Log.v(TAG, "Bump nesting in " + this |
| 601 | + " by " + amt); |
| 602 | } |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 603 | final int numOps = mOps.size(); |
| 604 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 605 | final Op op = mOps.get(opNum); |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 606 | if (op.fragment != null) { |
| 607 | op.fragment.mBackStackNesting += amt; |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 608 | if (FragmentManagerImpl.DEBUG) { |
| 609 | Log.v(TAG, "Bump nesting of " |
| 610 | + op.fragment + " to " + op.fragment.mBackStackNesting); |
| 611 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 612 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 616 | public int commit() { |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 617 | return commitInternal(false); |
| 618 | } |
| 619 | |
| 620 | public int commitAllowingStateLoss() { |
| 621 | return commitInternal(true); |
| 622 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 623 | |
Adam Powell | 8585ed6 | 2016-02-04 15:38:20 -0800 | [diff] [blame] | 624 | @Override |
| 625 | public void commitNow() { |
| 626 | disallowAddToBackStack(); |
| 627 | mManager.execSingleAction(this, false); |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void commitNowAllowingStateLoss() { |
| 632 | disallowAddToBackStack(); |
| 633 | mManager.execSingleAction(this, true); |
| 634 | } |
| 635 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 636 | @Override |
| 637 | public FragmentTransaction setAllowOptimization(boolean allowOptimization) { |
| 638 | mAllowOptimization = allowOptimization; |
| 639 | return this; |
| 640 | } |
| 641 | |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 642 | int commitInternal(boolean allowStateLoss) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 643 | if (mCommitted) { |
| 644 | throw new IllegalStateException("commit already called"); |
| 645 | } |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 646 | if (FragmentManagerImpl.DEBUG) { |
| 647 | Log.v(TAG, "Commit: " + this); |
| 648 | LogWriter logw = new LogWriter(Log.VERBOSE, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 649 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 650 | dump(" ", null, pw, null); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 651 | pw.flush(); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 652 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 653 | mCommitted = true; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 654 | if (mAddToBackStack) { |
| 655 | mIndex = mManager.allocBackStackIndex(this); |
| 656 | } else { |
| 657 | mIndex = -1; |
| 658 | } |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 659 | mManager.enqueueAction(this, allowStateLoss); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 660 | return mIndex; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 661 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 662 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 663 | /** |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 664 | * Implementation of {@link android.app.FragmentManagerImpl.OpGenerator}. |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 665 | * This operation is added to the list of pending actions during {@link #commit()}, and |
| 666 | * will be executed on the UI thread to run this FragmentTransaction. |
| 667 | * |
| 668 | * @param records Modified to add this BackStackRecord |
| 669 | * @param isRecordPop Modified to add a false (this isn't a pop) |
| 670 | * @return true always because the records and isRecordPop will always be changed |
| 671 | */ |
| 672 | @Override |
| 673 | public boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop) { |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 674 | if (FragmentManagerImpl.DEBUG) { |
| 675 | Log.v(TAG, "Run: " + this); |
| 676 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 677 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 678 | records.add(this); |
| 679 | isRecordPop.add(false); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 680 | if (mAddToBackStack) { |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 681 | mManager.addBackStackState(this); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 682 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 683 | return true; |
| 684 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 685 | |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 686 | boolean interactsWith(int containerId) { |
| 687 | final int numOps = mOps.size(); |
| 688 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 689 | final Op op = mOps.get(opNum); |
| 690 | if (op.fragment.mContainerId == containerId) { |
| 691 | return true; |
| 692 | } |
| 693 | } |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | boolean interactsWith(ArrayList<BackStackRecord> records, int startIndex, int endIndex) { |
| 698 | if (endIndex == startIndex) { |
| 699 | return false; |
| 700 | } |
| 701 | final int numOps = mOps.size(); |
| 702 | int lastContainer = -1; |
| 703 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 704 | final Op op = mOps.get(opNum); |
| 705 | final int container = op.fragment.mContainerId; |
| 706 | if (container != 0 && container != lastContainer) { |
| 707 | lastContainer = container; |
| 708 | for (int i = startIndex; i < endIndex; i++) { |
| 709 | BackStackRecord record = records.get(i); |
| 710 | final int numThoseOps = record.mOps.size(); |
| 711 | for (int thoseOpIndex = 0; thoseOpIndex < numThoseOps; thoseOpIndex++) { |
| 712 | final Op thatOp = record.mOps.get(thoseOpIndex); |
| 713 | if (thatOp.fragment.mContainerId == container) { |
| 714 | return true; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | return false; |
| 721 | } |
| 722 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 723 | /** |
| 724 | * Executes the operations contained within this transaction. The Fragment states will only |
| 725 | * be modified if optimizations are not allowed. |
| 726 | */ |
| 727 | void executeOps() { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 728 | final int numOps = mOps.size(); |
| 729 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 730 | final Op op = mOps.get(opNum); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 731 | final Fragment f = op.fragment; |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 732 | f.setNextTransition(mTransition, mTransitionStyle); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 733 | switch (op.cmd) { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 734 | case OP_ADD: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 735 | f.setNextAnim(op.enterAnim); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 736 | mManager.addFragment(f, false); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 737 | break; |
| 738 | case OP_REMOVE: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 739 | f.setNextAnim(op.exitAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 740 | mManager.removeFragment(f); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 741 | break; |
| 742 | case OP_HIDE: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 743 | f.setNextAnim(op.exitAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 744 | mManager.hideFragment(f); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 745 | break; |
| 746 | case OP_SHOW: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 747 | f.setNextAnim(op.enterAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 748 | mManager.showFragment(f); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 749 | break; |
| 750 | case OP_DETACH: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 751 | f.setNextAnim(op.exitAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 752 | mManager.detachFragment(f); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 753 | break; |
| 754 | case OP_ATTACH: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 755 | f.setNextAnim(op.enterAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 756 | mManager.attachFragment(f); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 757 | break; |
| 758 | default: |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 759 | throw new IllegalArgumentException("Unknown cmd: " + op.cmd); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 760 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 761 | if (!mAllowOptimization && op.cmd != OP_ADD) { |
| 762 | mManager.moveFragmentToExpectedState(f); |
| 763 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 764 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 765 | if (!mAllowOptimization) { |
| 766 | // Added fragments are added at the end to comply with prior behavior. |
| 767 | mManager.moveToState(mManager.mCurState); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 770 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 771 | /** |
| 772 | * Reverses the execution of the operations within this transaction. The Fragment states will |
| 773 | * only be modified if optimizations are not allowed. |
| 774 | */ |
| 775 | void executePopOps() { |
| 776 | for (int opNum = mOps.size() - 1; opNum >= 0; opNum--) { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 777 | final Op op = mOps.get(opNum); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 778 | Fragment f = op.fragment; |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 779 | f.setNextTransition(FragmentManagerImpl.reverseTransit(mTransition), mTransitionStyle); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 780 | switch (op.cmd) { |
| 781 | case OP_ADD: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 782 | f.setNextAnim(op.popExitAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 783 | mManager.removeFragment(f); |
| 784 | break; |
| 785 | case OP_REMOVE: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 786 | f.setNextAnim(op.popEnterAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 787 | mManager.addFragment(f, false); |
| 788 | break; |
| 789 | case OP_HIDE: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 790 | f.setNextAnim(op.popEnterAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 791 | mManager.showFragment(f); |
| 792 | break; |
| 793 | case OP_SHOW: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 794 | f.setNextAnim(op.popExitAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 795 | mManager.hideFragment(f); |
| 796 | break; |
| 797 | case OP_DETACH: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 798 | f.setNextAnim(op.popEnterAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 799 | mManager.attachFragment(f); |
| 800 | break; |
| 801 | case OP_ATTACH: |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 802 | f.setNextAnim(op.popExitAnim); |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 803 | mManager.detachFragment(f); |
| 804 | break; |
| 805 | default: |
| 806 | throw new IllegalArgumentException("Unknown cmd: " + op.cmd); |
| 807 | } |
| 808 | if (!mAllowOptimization && op.cmd != OP_ADD) { |
| 809 | mManager.moveFragmentToExpectedState(f); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 812 | if (!mAllowOptimization) { |
| 813 | mManager.moveToState(mManager.mCurState); |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 814 | } |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 815 | } |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 816 | |
George Mount | eca8e22 | 2016-07-07 13:13:05 -0700 | [diff] [blame] | 817 | /** |
| 818 | * Removes all OP_REPLACE ops and replaces them with the proper add and remove |
| 819 | * operations that are equivalent to the replace. This must be called prior to |
| 820 | * {@link #executeOps()} or any other call that operations on mOps. |
| 821 | * |
| 822 | * @param added Initialized to the fragments that are in the mManager.mAdded, this |
| 823 | * will be modified to contain the fragments that will be in mAdded |
| 824 | * after the execution ({@link #executeOps()}. |
| 825 | */ |
| 826 | void expandReplaceOps(ArrayList<Fragment> added) { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 827 | for (int opNum = 0; opNum < mOps.size(); opNum++) { |
| 828 | final Op op = mOps.get(opNum); |
| 829 | switch (op.cmd) { |
| 830 | case OP_ADD: |
| 831 | case OP_ATTACH: |
| 832 | added.add(op.fragment); |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 833 | break; |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 834 | case OP_REMOVE: |
| 835 | case OP_DETACH: |
| 836 | added.remove(op.fragment); |
| 837 | break; |
| 838 | case OP_REPLACE: { |
| 839 | Fragment f = op.fragment; |
| 840 | int containerId = f.mContainerId; |
| 841 | boolean alreadyAdded = false; |
| 842 | for (int i = added.size() - 1; i >= 0; i--) { |
| 843 | Fragment old = added.get(i); |
| 844 | if (old.mContainerId == containerId) { |
| 845 | if (old == f) { |
| 846 | alreadyAdded = true; |
| 847 | } else { |
| 848 | Op removeOp = new Op(); |
| 849 | removeOp.cmd = OP_REMOVE; |
| 850 | removeOp.fragment = old; |
| 851 | removeOp.enterAnim = op.enterAnim; |
| 852 | removeOp.popEnterAnim = op.popEnterAnim; |
| 853 | removeOp.exitAnim = op.exitAnim; |
| 854 | removeOp.popExitAnim = op.popExitAnim; |
| 855 | mOps.add(opNum, removeOp); |
| 856 | added.remove(old); |
| 857 | opNum++; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | if (alreadyAdded) { |
| 862 | mOps.remove(opNum); |
| 863 | opNum--; |
| 864 | } else { |
| 865 | op.cmd = OP_ADD; |
| 866 | added.add(f); |
| 867 | } |
| 868 | } |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 874 | boolean isPostponed() { |
| 875 | for (int opNum = 0; opNum < mOps.size(); opNum++) { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 876 | final Op op = mOps.get(opNum); |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 877 | if (isFragmentPostponed(op)) { |
George Mount | c855f7d | 2015-06-10 15:00:17 -0700 | [diff] [blame] | 878 | return true; |
| 879 | } |
| 880 | } |
| 881 | return false; |
| 882 | } |
| 883 | |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 884 | void setOnStartPostponedListener(Fragment.OnStartEnterTransitionListener listener) { |
| 885 | for (int opNum = 0; opNum < mOps.size(); opNum++) { |
| 886 | final Op op = mOps.get(opNum); |
| 887 | if (isFragmentPostponed(op)) { |
| 888 | op.fragment.setOnStartEnterTransitionListener(listener); |
George Mount | 61f8331 | 2015-09-09 16:10:29 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
George Mount | 86bfc66 | 2016-07-12 16:06:06 -0700 | [diff] [blame] | 893 | private static boolean isFragmentPostponed(Op op) { |
| 894 | final Fragment fragment = op.fragment; |
| 895 | return (fragment.mAdded && fragment.mView != null && !fragment.mDetached && |
| 896 | !fragment.mHidden && fragment.isPostponed()); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 897 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 898 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 899 | public String getName() { |
| 900 | return mName; |
| 901 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 902 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 903 | public int getTransition() { |
| 904 | return mTransition; |
| 905 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 906 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 907 | public int getTransitionStyle() { |
| 908 | return mTransitionStyle; |
| 909 | } |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 910 | |
| 911 | public boolean isEmpty() { |
George Mount | f2045f8 | 2016-06-27 13:15:01 -0700 | [diff] [blame] | 912 | return mOps.isEmpty(); |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 913 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 914 | } |