15ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn/*
25ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * Copyright (C) 2010 The Android Open Source Project
35ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn *
45ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
55ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * you may not use this file except in compliance with the License.
65ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * You may obtain a copy of the License at
75ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn *
85ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
95ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn *
105ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
115ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
125ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * See the License for the specific language governing permissions and
145ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * limitations under the License.
155ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn */
165ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
175ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornpackage android.app;
185ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
19cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarriimport android.os.Build;
205ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornimport android.os.Parcel;
215ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornimport android.os.Parcelable;
22c6669ca63299219d815464129dac051ab2404286Dianne Hackbornimport android.text.TextUtils;
23445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackbornimport android.util.Log;
24f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackbornimport android.util.LogWriter;
25d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mountimport android.view.View;
265ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
27a7245b42536bb9e71229651ef7135b039c7df70cGeorge Mountimport com.android.internal.util.FastPrintWriter;
28a7245b42536bb9e71229651ef7135b039c7df70cGeorge Mount
2930d7189067524000c738c188c4ff91f84f474d25Dianne Hackbornimport java.io.FileDescriptor;
3030d7189067524000c738c188c4ff91f84f474d25Dianne Hackbornimport java.io.PrintWriter;
31cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarriimport java.lang.reflect.Modifier;
325ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornimport java.util.ArrayList;
335ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
345ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornfinal class BackStackState implements Parcelable {
355ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    final int[] mOps;
365ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    final int mTransition;
375ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    final int mTransitionStyle;
385ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    final String mName;
39dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    final int mIndex;
40c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    final int mBreadCrumbTitleRes;
41c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    final CharSequence mBreadCrumbTitleText;
42c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    final int mBreadCrumbShortTitleRes;
43c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    final CharSequence mBreadCrumbShortTitleText;
44d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount    final ArrayList<String> mSharedElementSourceNames;
45d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount    final ArrayList<String> mSharedElementTargetNames;
46a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount    final boolean mReorderingAllowed;
47c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
48c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public BackStackState(FragmentManagerImpl fm, BackStackRecord bse) {
49f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        final int numOps = bse.mOps.size();
50f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        mOps = new int[numOps * 6];
51c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
52b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        if (!bse.mAddToBackStack) {
53b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn            throw new IllegalStateException("Not on back stack");
54b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
55b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
565ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        int pos = 0;
57f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        for (int opNum = 0; opNum < numOps; opNum++) {
58f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final BackStackRecord.Op op = bse.mOps.get(opNum);
595ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            mOps[pos++] = op.cmd;
60ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            mOps[pos++] = op.fragment != null ? op.fragment.mIndex : -1;
615ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            mOps[pos++] = op.enterAnim;
625ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            mOps[pos++] = op.exitAnim;
63bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase            mOps[pos++] = op.popEnterAnim;
64bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase            mOps[pos++] = op.popExitAnim;
655ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
665ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mTransition = bse.mTransition;
675ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mTransitionStyle = bse.mTransitionStyle;
685ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mName = bse.mName;
69dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        mIndex = bse.mIndex;
70c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleRes = bse.mBreadCrumbTitleRes;
71c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleText = bse.mBreadCrumbTitleText;
72c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleRes = bse.mBreadCrumbShortTitleRes;
73c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleText = bse.mBreadCrumbShortTitleText;
74d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        mSharedElementSourceNames = bse.mSharedElementSourceNames;
75d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        mSharedElementTargetNames = bse.mSharedElementTargetNames;
76a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        mReorderingAllowed = bse.mReorderingAllowed;
775ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
78c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
795ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public BackStackState(Parcel in) {
805ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mOps = in.createIntArray();
815ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mTransition = in.readInt();
825ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mTransitionStyle = in.readInt();
835ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mName = in.readString();
84dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        mIndex = in.readInt();
85c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleRes = in.readInt();
86c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
87c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleRes = in.readInt();
88c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
89d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        mSharedElementSourceNames = in.createStringArrayList();
90d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        mSharedElementTargetNames = in.createStringArrayList();
91a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        mReorderingAllowed = in.readInt() != 0;
925ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
93c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
94c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public BackStackRecord instantiate(FragmentManagerImpl fm) {
95c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        BackStackRecord bse = new BackStackRecord(fm);
965ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        int pos = 0;
97f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        int num = 0;
985ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        while (pos < mOps.length) {
99c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            BackStackRecord.Op op = new BackStackRecord.Op();
1005ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            op.cmd = mOps[pos++];
101d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            if (FragmentManagerImpl.DEBUG) {
102d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                Log.v(FragmentManagerImpl.TAG,
103d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        "Instantiate " + bse + " op #" + num + " base fragment #" + mOps[pos]);
104d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            }
105ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            int findex = mOps[pos++];
106ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            if (findex >= 0) {
107ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                Fragment f = fm.mActive.get(findex);
108ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                op.fragment = f;
109ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            } else {
110ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                op.fragment = null;
111ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            }
1125ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            op.enterAnim = mOps[pos++];
1135ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            op.exitAnim = mOps[pos++];
114bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase            op.popEnterAnim = mOps[pos++];
115bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase            op.popExitAnim = mOps[pos++];
11641fb99480c84683f44b17d2d40bcbfdb8bc12b42George Mount            bse.mEnterAnim = op.enterAnim;
11741fb99480c84683f44b17d2d40bcbfdb8bc12b42George Mount            bse.mExitAnim = op.exitAnim;
11841fb99480c84683f44b17d2d40bcbfdb8bc12b42George Mount            bse.mPopEnterAnim = op.popEnterAnim;
11941fb99480c84683f44b17d2d40bcbfdb8bc12b42George Mount            bse.mPopExitAnim = op.popExitAnim;
1205ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            bse.addOp(op);
121f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            num++;
1225ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
1235ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        bse.mTransition = mTransition;
1245ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        bse.mTransitionStyle = mTransitionStyle;
1255ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        bse.mName = mName;
126dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        bse.mIndex = mIndex;
127b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        bse.mAddToBackStack = true;
128c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        bse.mBreadCrumbTitleRes = mBreadCrumbTitleRes;
129c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        bse.mBreadCrumbTitleText = mBreadCrumbTitleText;
130c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        bse.mBreadCrumbShortTitleRes = mBreadCrumbShortTitleRes;
131c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        bse.mBreadCrumbShortTitleText = mBreadCrumbShortTitleText;
132d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        bse.mSharedElementSourceNames = mSharedElementSourceNames;
133d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        bse.mSharedElementTargetNames = mSharedElementTargetNames;
134a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        bse.mReorderingAllowed = mReorderingAllowed;
135b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        bse.bumpBackStackNesting(1);
1365ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return bse;
1375ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
138c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
1395ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public int describeContents() {
1405ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return 0;
1415ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
1425ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
1435ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public void writeToParcel(Parcel dest, int flags) {
1445ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        dest.writeIntArray(mOps);
1455ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        dest.writeInt(mTransition);
1465ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        dest.writeInt(mTransitionStyle);
1475ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        dest.writeString(mName);
148dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        dest.writeInt(mIndex);
149c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        dest.writeInt(mBreadCrumbTitleRes);
150c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        TextUtils.writeToParcel(mBreadCrumbTitleText, dest, 0);
151c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        dest.writeInt(mBreadCrumbShortTitleRes);
152c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        TextUtils.writeToParcel(mBreadCrumbShortTitleText, dest, 0);
153d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        dest.writeStringList(mSharedElementSourceNames);
154d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        dest.writeStringList(mSharedElementTargetNames);
155a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        dest.writeInt(mReorderingAllowed ? 1 : 0);
1565ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
157c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
1585ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public static final Parcelable.Creator<BackStackState> CREATOR
1595ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            = new Parcelable.Creator<BackStackState>() {
1605ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        public BackStackState createFromParcel(Parcel in) {
1615ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            return new BackStackState(in);
1625ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
163c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
1645ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        public BackStackState[] newArray(int size) {
1655ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            return new BackStackState[size];
1665ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
1675ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    };
1685ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn}
1695ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
1705ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn/**
1715ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn * @hide Entry of an operation on the fragment back stack.
1725ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn */
173ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackbornfinal class BackStackRecord extends FragmentTransaction implements
174eca8e221db923927cdeca927c0b75807a7612145George Mount        FragmentManager.BackStackEntry, FragmentManagerImpl.OpGenerator {
175f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    static final String TAG = FragmentManagerImpl.TAG;
176c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
177b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    final FragmentManagerImpl mManager;
178c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
1795ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    static final int OP_NULL = 0;
1805ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    static final int OP_ADD = 1;
181b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    static final int OP_REPLACE = 2;
182b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    static final int OP_REMOVE = 3;
183b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    static final int OP_HIDE = 4;
184b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    static final int OP_SHOW = 5;
18547c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn    static final int OP_DETACH = 6;
18647c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn    static final int OP_ATTACH = 7;
1875f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell    static final int OP_SET_PRIMARY_NAV = 8;
1885f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell    static final int OP_UNSET_PRIMARY_NAV = 9;
189c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
1905ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    static final class Op {
1915ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        int cmd;
1925ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        Fragment fragment;
1935ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        int enterAnim;
1945ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        int exitAnim;
195bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        int popEnterAnim;
196bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        int popExitAnim;
1975f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell
1985f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        Op() {
1995f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        }
2005f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell
2015f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        Op(int cmd, Fragment fragment) {
2025f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            this.cmd = cmd;
2035f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            this.fragment = fragment;
2045f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        }
2055ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
206c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
207f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount    ArrayList<Op> mOps = new ArrayList<>();
2085ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    int mEnterAnim;
2095ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    int mExitAnim;
210bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase    int mPopEnterAnim;
211bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase    int mPopExitAnim;
2125ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    int mTransition;
2135ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    int mTransitionStyle;
2145ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    boolean mAddToBackStack;
2150c24a5514c1ff143a223720a090b19a86a75945fAdam Powell    boolean mAllowAddToBackStack = true;
2165ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    String mName;
2175ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    boolean mCommitted;
218f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    int mIndex = -1;
219a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount    boolean mReorderingAllowed;
220c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
221adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell    ArrayList<Runnable> mCommitRunnables;
222adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell
223c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    int mBreadCrumbTitleRes;
224c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    CharSequence mBreadCrumbTitleText;
225c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    int mBreadCrumbShortTitleRes;
226c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    CharSequence mBreadCrumbShortTitleText;
227c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
228d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount    ArrayList<String> mSharedElementSourceNames;
229d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount    ArrayList<String> mSharedElementTargetNames;
230d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount
231f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    @Override
232f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    public String toString() {
233f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        StringBuilder sb = new StringBuilder(128);
234f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        sb.append("BackStackEntry{");
235f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        sb.append(Integer.toHexString(System.identityHashCode(this)));
236f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        if (mIndex >= 0) {
237f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            sb.append(" #");
238f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            sb.append(mIndex);
239d04ad541def2bca8fc2d810514ee15ab97893d35Dianne Hackborn        }
240f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        if (mName != null) {
241f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            sb.append(" ");
242f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            sb.append(mName);
24330d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn        }
244f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        sb.append("}");
245f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        return sb.toString();
246f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    }
247f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn
248f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
249f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        dump(prefix, writer, true);
250f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    }
251f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn
252f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn    void dump(String prefix, PrintWriter writer, boolean full) {
253f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        if (full) {
254d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print(prefix);
255d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print("mName=");
256d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print(mName);
257d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print(" mIndex=");
258d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print(mIndex);
259d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print(" mCommitted=");
260d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.println(mCommitted);
261f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            if (mTransition != FragmentTransaction.TRANSIT_NONE) {
262d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(prefix);
263d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print("mTransition=#");
264d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(Integer.toHexString(mTransition));
265d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(" mTransitionStyle=#");
266d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.println(Integer.toHexString(mTransitionStyle));
267f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
268d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            if (mEnterAnim != 0 || mExitAnim != 0) {
269d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(prefix);
270d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print("mEnterAnim=#");
271d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(Integer.toHexString(mEnterAnim));
272d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(" mExitAnim=#");
273d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.println(Integer.toHexString(mExitAnim));
274f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
275d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            if (mPopEnterAnim != 0 || mPopExitAnim != 0) {
276d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(prefix);
277d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print("mPopEnterAnim=#");
278d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(Integer.toHexString(mPopEnterAnim));
279d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(" mPopExitAnim=#");
280d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.println(Integer.toHexString(mPopExitAnim));
281f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
282f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            if (mBreadCrumbTitleRes != 0 || mBreadCrumbTitleText != null) {
283d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(prefix);
284d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print("mBreadCrumbTitleRes=#");
285d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(Integer.toHexString(mBreadCrumbTitleRes));
286d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(" mBreadCrumbTitleText=");
287d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.println(mBreadCrumbTitleText);
288f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
289f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            if (mBreadCrumbShortTitleRes != 0 || mBreadCrumbShortTitleText != null) {
290d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(prefix);
291d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print("mBreadCrumbShortTitleRes=#");
292d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(Integer.toHexString(mBreadCrumbShortTitleRes));
293d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(" mBreadCrumbShortTitleText=");
294d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.println(mBreadCrumbShortTitleText);
295f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
29630d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn        }
29730d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn
298f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        if (!mOps.isEmpty()) {
299d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.print(prefix);
300d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            writer.println("Operations:");
30130d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn            String innerPrefix = prefix + "    ";
302f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final int numOps = mOps.size();
303f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            for (int opNum = 0; opNum < numOps; opNum++) {
304f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                final Op op = mOps.get(opNum);
305f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                String cmdStr;
306f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                switch (op.cmd) {
307d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_NULL:
308d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "NULL";
309d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
310d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_ADD:
311d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "ADD";
312d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
313d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_REPLACE:
314d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "REPLACE";
315d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
316d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_REMOVE:
317d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "REMOVE";
318d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
319d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_HIDE:
320d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "HIDE";
321d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
322d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_SHOW:
323d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "SHOW";
324d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
325d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_DETACH:
326d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "DETACH";
327d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
328d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    case OP_ATTACH:
329d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "ATTACH";
330d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
3315f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    case OP_SET_PRIMARY_NAV:
3325f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        cmdStr="SET_PRIMARY_NAV";
3335f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        break;
3345f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    case OP_UNSET_PRIMARY_NAV:
3355f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        cmdStr="UNSET_PRIMARY_NAV";
3365f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        break;
3375f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell
338d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    default:
339d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        cmdStr = "cmd=" + op.cmd;
340d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        break;
34130d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn                }
342d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(prefix);
343d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print("  Op #");
344f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                writer.print(opNum);
345d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(": ");
346d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(cmdStr);
347d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.print(" ");
348d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                writer.println(op.fragment);
349f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (full) {
350f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    if (op.enterAnim != 0 || op.exitAnim != 0) {
351d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print(innerPrefix);
352d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print("enterAnim=#");
353d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print(Integer.toHexString(op.enterAnim));
354d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print(" exitAnim=#");
355d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.println(Integer.toHexString(op.exitAnim));
356f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    }
357f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    if (op.popEnterAnim != 0 || op.popExitAnim != 0) {
358d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print(innerPrefix);
359d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print("popEnterAnim=#");
360d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print(Integer.toHexString(op.popEnterAnim));
361d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.print(" popExitAnim=#");
362d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                        writer.println(Integer.toHexString(op.popExitAnim));
363f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    }
364bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase                }
36530d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn            }
36630d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn        }
36730d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn    }
36830d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn
369c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public BackStackRecord(FragmentManagerImpl manager) {
3705ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mManager = manager;
371a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        mReorderingAllowed = mManager.getTargetSdk() > Build.VERSION_CODES.N_MR1;
3725ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
373c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
374c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public int getId() {
375c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return mIndex;
376c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
377c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
378327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public int getBreadCrumbTitleRes() {
379327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return mBreadCrumbTitleRes;
380327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
381327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
382327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public int getBreadCrumbShortTitleRes() {
383327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return mBreadCrumbShortTitleRes;
384327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
385327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
386c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public CharSequence getBreadCrumbTitle() {
387d5f70891e114ed47ca7e0e806d2352827292b6d1George Mount        if (mBreadCrumbTitleRes != 0 && mManager.mHost != null) {
388a5fc6f006f67867417b7a427de6e7394c4312decTodd Kennedy            return mManager.mHost.getContext().getText(mBreadCrumbTitleRes);
389c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        }
390c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return mBreadCrumbTitleText;
391c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
392c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
393c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public CharSequence getBreadCrumbShortTitle() {
394d5f70891e114ed47ca7e0e806d2352827292b6d1George Mount        if (mBreadCrumbShortTitleRes != 0 && mManager.mHost != null) {
395a5fc6f006f67867417b7a427de6e7394c4312decTodd Kennedy            return mManager.mHost.getContext().getText(mBreadCrumbShortTitleRes);
396c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        }
397c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return mBreadCrumbShortTitleText;
398c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
399c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4005ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    void addOp(Op op) {
401f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        mOps.add(op);
4025ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        op.enterAnim = mEnterAnim;
4035ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        op.exitAnim = mExitAnim;
404bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        op.popEnterAnim = mPopEnterAnim;
405bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        op.popExitAnim = mPopExitAnim;
4065ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
407c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4085ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction add(Fragment fragment, String tag) {
409b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        doAddOp(0, fragment, tag, OP_ADD);
410b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return this;
4115ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
4125ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
4135ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction add(int containerViewId, Fragment fragment) {
414b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        doAddOp(containerViewId, fragment, null, OP_ADD);
415b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return this;
4165ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
4175ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
4185ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction add(int containerViewId, Fragment fragment, String tag) {
419b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        doAddOp(containerViewId, fragment, tag, OP_ADD);
420b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return this;
421b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    }
422b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn
423b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    private void doAddOp(int containerViewId, Fragment fragment, String tag, int opcmd) {
424f1d88e6e48c510f8c73019890fa4cebb6bb12d69George Mount        if (mManager.getTargetSdk() > Build.VERSION_CODES.N_MR1) {
425cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarri            final Class fragmentClass = fragment.getClass();
426cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarri            final int modifiers = fragmentClass.getModifiers();
427d5f70891e114ed47ca7e0e806d2352827292b6d1George Mount            if ((fragmentClass.isAnonymousClass() || !Modifier.isPublic(modifiers)
428cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarri                    || (fragmentClass.isMemberClass() && !Modifier.isStatic(modifiers)))) {
429eb7b7ecd40cdfabc1cb3e474738d11ebf4f386c2Clara Bayarri                throw new IllegalStateException("Fragment " + fragmentClass.getCanonicalName()
430eb7b7ecd40cdfabc1cb3e474738d11ebf4f386c2Clara Bayarri                        + " must be a public static class to be  properly recreated from"
431eb7b7ecd40cdfabc1cb3e474738d11ebf4f386c2Clara Bayarri                        + " instance state.");
432cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarri            }
433cd916f24a30ea4717ae2c0e66dfeac3355f56869Clara Bayarri        }
4343e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        fragment.mFragmentManager = mManager;
435c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4365ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (tag != null) {
4375ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            if (fragment.mTag != null && !tag.equals(fragment.mTag)) {
4385ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                throw new IllegalStateException("Can't change tag of fragment "
4395ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        + fragment + ": was " + fragment.mTag
4405ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        + " now " + tag);
4415ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
4425ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            fragment.mTag = tag;
4435ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
444c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4455ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (containerViewId != 0) {
446e01f5957fbbea04138073af423c1c9ae0b510038Adam Powell            if (containerViewId == View.NO_ID) {
447e01f5957fbbea04138073af423c1c9ae0b510038Adam Powell                throw new IllegalArgumentException("Can't add fragment "
448e01f5957fbbea04138073af423c1c9ae0b510038Adam Powell                        + fragment + " with tag " + tag + " to container view with no id");
449e01f5957fbbea04138073af423c1c9ae0b510038Adam Powell            }
4505ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            if (fragment.mFragmentId != 0 && fragment.mFragmentId != containerViewId) {
4515ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                throw new IllegalStateException("Can't change container ID of fragment "
4525ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        + fragment + ": was " + fragment.mFragmentId
4535ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        + " now " + containerViewId);
4545ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
4555ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            fragment.mContainerId = fragment.mFragmentId = containerViewId;
4565ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
457c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4585f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(opcmd, fragment));
4595ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
4605ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
4615ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction replace(int containerViewId, Fragment fragment) {
4625ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return replace(containerViewId, fragment, null);
4635ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
464c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4655ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction replace(int containerViewId, Fragment fragment, String tag) {
4665ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (containerViewId == 0) {
4675ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            throw new IllegalArgumentException("Must use non-zero containerViewId");
4685ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
469c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
470b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        doAddOp(containerViewId, fragment, tag, OP_REPLACE);
471b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return this;
4725ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
473c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4745ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction remove(Fragment fragment) {
4755f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(OP_REMOVE, fragment));
476c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4775ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
4785ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
4795ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
4805ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction hide(Fragment fragment) {
4815f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(OP_HIDE, fragment));
482c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4835ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
4845ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
485c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4865ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction show(Fragment fragment) {
4875f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(OP_SHOW, fragment));
488c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4895ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
4905ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
491c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
49247c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn    public FragmentTransaction detach(Fragment fragment) {
4935f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(OP_DETACH, fragment));
49447c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn
49547c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn        return this;
49647c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn    }
49747c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn
49847c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn    public FragmentTransaction attach(Fragment fragment) {
4995f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(OP_ATTACH, fragment));
5005f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell
5015f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        return this;
5025f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell    }
5035f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell
5045f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell    public FragmentTransaction setPrimaryNavigationFragment(Fragment fragment) {
5055f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        addOp(new Op(OP_SET_PRIMARY_NAV, fragment));
50647c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn
50747c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn        return this;
50847c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn    }
50947c4156567ce5abf8a6fca7fefe6ae9a20fb5117Dianne Hackborn
5105ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction setCustomAnimations(int enter, int exit) {
511bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        return setCustomAnimations(enter, exit, 0, 0);
512bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase    }
513bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase
514bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase    public FragmentTransaction setCustomAnimations(int enter, int exit,
515bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase            int popEnter, int popExit) {
5165ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mEnterAnim = enter;
5175ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mExitAnim = exit;
518bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        mPopEnterAnim = popEnter;
519bc377841db05bd5197ffadb58ba52c54b2a85f16Chet Haase        mPopExitAnim = popExit;
5205ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
5215ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
522c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
5235ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction setTransition(int transition) {
5245ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mTransition = transition;
5255ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
5265ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
527c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
528d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount    @Override
529448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount    public FragmentTransaction addSharedElement(View sharedElement, String name) {
530448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        String transitionName = sharedElement.getTransitionName();
531448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        if (transitionName == null) {
532448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount            throw new IllegalArgumentException("Unique transitionNames are required for all" +
533448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount                    " sharedElements");
534448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        }
535448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        if (mSharedElementSourceNames == null) {
536448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount            mSharedElementSourceNames = new ArrayList<String>();
537448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount            mSharedElementTargetNames = new ArrayList<String>();
5384efeb4f4d47a2ea5c37da9b0ad52cb288e94483eGeorge Mount        } else if (mSharedElementTargetNames.contains(name)) {
5394efeb4f4d47a2ea5c37da9b0ad52cb288e94483eGeorge Mount            throw new IllegalArgumentException("A shared element with the target name '"
5404efeb4f4d47a2ea5c37da9b0ad52cb288e94483eGeorge Mount                    + name + "' has already been added to the transaction.");
5414efeb4f4d47a2ea5c37da9b0ad52cb288e94483eGeorge Mount        } else if (mSharedElementSourceNames.contains(transitionName)) {
5424efeb4f4d47a2ea5c37da9b0ad52cb288e94483eGeorge Mount            throw new IllegalArgumentException("A shared element with the source name '"
5434efeb4f4d47a2ea5c37da9b0ad52cb288e94483eGeorge Mount                    + transitionName + " has already been added to the transaction.");
544448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        }
545448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        mSharedElementSourceNames.add(transitionName);
546448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        mSharedElementTargetNames.add(name);
547448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount        return this;
548448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount    }
549448bace8612b0f9a41844339ca5db1b8eac1ac5cGeorge Mount
5505ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction setTransitionStyle(int styleRes) {
5515ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mTransitionStyle = styleRes;
5525ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
5535ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
554c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
5555ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public FragmentTransaction addToBackStack(String name) {
5560c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        if (!mAllowAddToBackStack) {
5570c24a5514c1ff143a223720a090b19a86a75945fAdam Powell            throw new IllegalStateException(
5580c24a5514c1ff143a223720a090b19a86a75945fAdam Powell                    "This FragmentTransaction is not allowed to be added to the back stack.");
5590c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        }
5605ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mAddToBackStack = true;
5615ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mName = name;
5625ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return this;
5635ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
5645ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
5650c24a5514c1ff143a223720a090b19a86a75945fAdam Powell    public boolean isAddToBackStackAllowed() {
5660c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        return mAllowAddToBackStack;
5670c24a5514c1ff143a223720a090b19a86a75945fAdam Powell    }
5680c24a5514c1ff143a223720a090b19a86a75945fAdam Powell
5690c24a5514c1ff143a223720a090b19a86a75945fAdam Powell    public FragmentTransaction disallowAddToBackStack() {
5700c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        if (mAddToBackStack) {
5710c24a5514c1ff143a223720a090b19a86a75945fAdam Powell            throw new IllegalStateException(
5720c24a5514c1ff143a223720a090b19a86a75945fAdam Powell                    "This transaction is already being added to the back stack");
5730c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        }
5740c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        mAllowAddToBackStack = false;
5750c24a5514c1ff143a223720a090b19a86a75945fAdam Powell        return this;
5760c24a5514c1ff143a223720a090b19a86a75945fAdam Powell    }
5770c24a5514c1ff143a223720a090b19a86a75945fAdam Powell
578c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public FragmentTransaction setBreadCrumbTitle(int res) {
579c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleRes = res;
580c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleText = null;
581c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return this;
582c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
583c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
584c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public FragmentTransaction setBreadCrumbTitle(CharSequence text) {
585c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleRes = 0;
586c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbTitleText = text;
587c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return this;
588c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
589c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
590c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public FragmentTransaction setBreadCrumbShortTitle(int res) {
591c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleRes = res;
592c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleText = null;
593c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return this;
594c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
595c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
596c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public FragmentTransaction setBreadCrumbShortTitle(CharSequence text) {
597c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleRes = 0;
598c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBreadCrumbShortTitleText = text;
599c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return this;
600c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
601c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
602b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    void bumpBackStackNesting(int amt) {
603b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        if (!mAddToBackStack) {
604b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn            return;
605b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
606d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        if (FragmentManagerImpl.DEBUG) {
607d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            Log.v(TAG, "Bump nesting in " + this
608d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    + " by " + amt);
609d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        }
610f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        final int numOps = mOps.size();
611f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        for (int opNum = 0; opNum < numOps; opNum++) {
612f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final Op op = mOps.get(opNum);
613ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            if (op.fragment != null) {
614ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                op.fragment.mBackStackNesting += amt;
615d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                if (FragmentManagerImpl.DEBUG) {
616d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                    Log.v(TAG, "Bump nesting of "
617d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                            + op.fragment + " to " + op.fragment.mBackStackNesting);
618d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount                }
619ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            }
620b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
621b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
622b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
623adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell    @Override
62410d69ac2503c0515a07b2bb95041af7b5b6af3f9Adam Powell    public FragmentTransaction runOnCommit(Runnable runnable) {
625adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        if (runnable == null) {
626adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell            throw new IllegalArgumentException("runnable cannot be null");
627adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        }
628adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        disallowAddToBackStack();
629adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        if (mCommitRunnables == null) {
630adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell            mCommitRunnables = new ArrayList<>();
631adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        }
632adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        mCommitRunnables.add(runnable);
633adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        return this;
634adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell    }
635adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell
636adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell    public void runOnCommitRunnables() {
637adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        if (mCommitRunnables != null) {
638adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell            for (int i = 0, N = mCommitRunnables.size(); i < N; i++) {
639adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell                mCommitRunnables.get(i).run();
640adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell            }
641adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell            mCommitRunnables = null;
642adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell        }
643adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell    }
644adfd62c484f6d888cc9339e79619ccf72c3b133fAdam Powell
645dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    public int commit() {
646ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn        return commitInternal(false);
647ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    }
648ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn
649ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public int commitAllowingStateLoss() {
650ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn        return commitInternal(true);
651ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    }
652d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount
6538585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell    @Override
6548585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell    public void commitNow() {
6558585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell        disallowAddToBackStack();
6568585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell        mManager.execSingleAction(this, false);
6578585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell    }
6588585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell
6598585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell    @Override
6608585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell    public void commitNowAllowingStateLoss() {
6618585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell        disallowAddToBackStack();
6628585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell        mManager.execSingleAction(this, true);
6638585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell    }
6648585ed66b947ef30d6a43f0bb91885ed0c5ebfb4Adam Powell
665eca8e221db923927cdeca927c0b75807a7612145George Mount    @Override
666a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount    public FragmentTransaction setReorderingAllowed(boolean reorderingAllowed) {
667a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        mReorderingAllowed = reorderingAllowed;
668eca8e221db923927cdeca927c0b75807a7612145George Mount        return this;
669eca8e221db923927cdeca927c0b75807a7612145George Mount    }
670eca8e221db923927cdeca927c0b75807a7612145George Mount
671ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    int commitInternal(boolean allowStateLoss) {
672d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        if (mCommitted) {
673d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            throw new IllegalStateException("commit already called");
674d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        }
675f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        if (FragmentManagerImpl.DEBUG) {
676f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            Log.v(TAG, "Commit: " + this);
677f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
6788c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn            PrintWriter pw = new FastPrintWriter(logw, false, 1024);
679f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            dump("  ", null, pw, null);
6808c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn            pw.flush();
681f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        }
6825ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        mCommitted = true;
683dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        if (mAddToBackStack) {
684dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            mIndex = mManager.allocBackStackIndex(this);
685dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        } else {
686dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            mIndex = -1;
687dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        }
688ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn        mManager.enqueueAction(this, allowStateLoss);
689dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        return mIndex;
6905ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
691d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount
692eca8e221db923927cdeca927c0b75807a7612145George Mount    /**
69386bfc666d33592f7db5584036aab8c44164d8dc2George Mount     * Implementation of {@link android.app.FragmentManagerImpl.OpGenerator}.
694eca8e221db923927cdeca927c0b75807a7612145George Mount     * This operation is added to the list of pending actions during {@link #commit()}, and
695eca8e221db923927cdeca927c0b75807a7612145George Mount     * will be executed on the UI thread to run this FragmentTransaction.
696eca8e221db923927cdeca927c0b75807a7612145George Mount     *
697eca8e221db923927cdeca927c0b75807a7612145George Mount     * @param records Modified to add this BackStackRecord
698eca8e221db923927cdeca927c0b75807a7612145George Mount     * @param isRecordPop Modified to add a false (this isn't a pop)
699eca8e221db923927cdeca927c0b75807a7612145George Mount     * @return true always because the records and isRecordPop will always be changed
700eca8e221db923927cdeca927c0b75807a7612145George Mount     */
701eca8e221db923927cdeca927c0b75807a7612145George Mount    @Override
702eca8e221db923927cdeca927c0b75807a7612145George Mount    public boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop) {
703d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        if (FragmentManagerImpl.DEBUG) {
704d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            Log.v(TAG, "Run: " + this);
705d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        }
706c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
707eca8e221db923927cdeca927c0b75807a7612145George Mount        records.add(this);
708eca8e221db923927cdeca927c0b75807a7612145George Mount        isRecordPop.add(false);
709dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        if (mAddToBackStack) {
710eca8e221db923927cdeca927c0b75807a7612145George Mount            mManager.addBackStackState(this);
711a7245b42536bb9e71229651ef7135b039c7df70cGeorge Mount        }
712eca8e221db923927cdeca927c0b75807a7612145George Mount        return true;
713eca8e221db923927cdeca927c0b75807a7612145George Mount    }
714d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount
71586bfc666d33592f7db5584036aab8c44164d8dc2George Mount    boolean interactsWith(int containerId) {
71686bfc666d33592f7db5584036aab8c44164d8dc2George Mount        final int numOps = mOps.size();
71786bfc666d33592f7db5584036aab8c44164d8dc2George Mount        for (int opNum = 0; opNum < numOps; opNum++) {
71886bfc666d33592f7db5584036aab8c44164d8dc2George Mount            final Op op = mOps.get(opNum);
7195f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            final int fragContainer = op.fragment != null ? op.fragment.mContainerId : 0;
7205f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            if (fragContainer != 0 && fragContainer == containerId) {
72186bfc666d33592f7db5584036aab8c44164d8dc2George Mount                return true;
72286bfc666d33592f7db5584036aab8c44164d8dc2George Mount            }
72386bfc666d33592f7db5584036aab8c44164d8dc2George Mount        }
72486bfc666d33592f7db5584036aab8c44164d8dc2George Mount        return false;
72586bfc666d33592f7db5584036aab8c44164d8dc2George Mount    }
72686bfc666d33592f7db5584036aab8c44164d8dc2George Mount
72786bfc666d33592f7db5584036aab8c44164d8dc2George Mount    boolean interactsWith(ArrayList<BackStackRecord> records, int startIndex, int endIndex) {
72886bfc666d33592f7db5584036aab8c44164d8dc2George Mount        if (endIndex == startIndex) {
72986bfc666d33592f7db5584036aab8c44164d8dc2George Mount            return false;
73086bfc666d33592f7db5584036aab8c44164d8dc2George Mount        }
73186bfc666d33592f7db5584036aab8c44164d8dc2George Mount        final int numOps = mOps.size();
73286bfc666d33592f7db5584036aab8c44164d8dc2George Mount        int lastContainer = -1;
73386bfc666d33592f7db5584036aab8c44164d8dc2George Mount        for (int opNum = 0; opNum < numOps; opNum++) {
73486bfc666d33592f7db5584036aab8c44164d8dc2George Mount            final Op op = mOps.get(opNum);
7355f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            final int container = op.fragment != null ? op.fragment.mContainerId : 0;
73686bfc666d33592f7db5584036aab8c44164d8dc2George Mount            if (container != 0 && container != lastContainer) {
73786bfc666d33592f7db5584036aab8c44164d8dc2George Mount                lastContainer = container;
73886bfc666d33592f7db5584036aab8c44164d8dc2George Mount                for (int i = startIndex; i < endIndex; i++) {
73986bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    BackStackRecord record = records.get(i);
74086bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    final int numThoseOps = record.mOps.size();
74186bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    for (int thoseOpIndex = 0; thoseOpIndex < numThoseOps; thoseOpIndex++) {
74286bfc666d33592f7db5584036aab8c44164d8dc2George Mount                        final Op thatOp = record.mOps.get(thoseOpIndex);
7435f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        final int thatContainer = thatOp.fragment != null
7445f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                ? thatOp.fragment.mContainerId : 0;
7455f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        if (thatContainer == container) {
74686bfc666d33592f7db5584036aab8c44164d8dc2George Mount                            return true;
74786bfc666d33592f7db5584036aab8c44164d8dc2George Mount                        }
74886bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    }
74986bfc666d33592f7db5584036aab8c44164d8dc2George Mount                }
75086bfc666d33592f7db5584036aab8c44164d8dc2George Mount            }
75186bfc666d33592f7db5584036aab8c44164d8dc2George Mount        }
75286bfc666d33592f7db5584036aab8c44164d8dc2George Mount        return false;
75386bfc666d33592f7db5584036aab8c44164d8dc2George Mount    }
75486bfc666d33592f7db5584036aab8c44164d8dc2George Mount
755eca8e221db923927cdeca927c0b75807a7612145George Mount    /**
756eca8e221db923927cdeca927c0b75807a7612145George Mount     * Executes the operations contained within this transaction. The Fragment states will only
757eca8e221db923927cdeca927c0b75807a7612145George Mount     * be modified if optimizations are not allowed.
758eca8e221db923927cdeca927c0b75807a7612145George Mount     */
759eca8e221db923927cdeca927c0b75807a7612145George Mount    void executeOps() {
760f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        final int numOps = mOps.size();
761f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        for (int opNum = 0; opNum < numOps; opNum++) {
762f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final Op op = mOps.get(opNum);
763eca8e221db923927cdeca927c0b75807a7612145George Mount            final Fragment f = op.fragment;
7645f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            if (f != null) {
7655f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                f.setNextTransition(mTransition, mTransitionStyle);
7665f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            }
7675ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            switch (op.cmd) {
768f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_ADD:
76986bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.enterAnim);
7705ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                    mManager.addFragment(f, false);
771f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    break;
772f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_REMOVE:
77386bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.exitAnim);
774eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.removeFragment(f);
775f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    break;
776f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_HIDE:
77786bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.exitAnim);
778eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.hideFragment(f);
779f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    break;
780f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_SHOW:
78186bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.enterAnim);
782eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.showFragment(f);
783f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    break;
784f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_DETACH:
78586bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.exitAnim);
786eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.detachFragment(f);
787f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    break;
788f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_ATTACH:
78986bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.enterAnim);
790eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.attachFragment(f);
791f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    break;
7925f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                case OP_SET_PRIMARY_NAV:
7935f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    mManager.setPrimaryNavigationFragment(f);
7945f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    break;
7955f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                case OP_UNSET_PRIMARY_NAV:
7965f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    mManager.setPrimaryNavigationFragment(null);
7975f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    break;
798f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                default:
7995ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                    throw new IllegalArgumentException("Unknown cmd: " + op.cmd);
8005ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
801a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount            if (!mReorderingAllowed && op.cmd != OP_ADD && f != null) {
802eca8e221db923927cdeca927c0b75807a7612145George Mount                mManager.moveFragmentToExpectedState(f);
803eca8e221db923927cdeca927c0b75807a7612145George Mount            }
8045ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
805a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        if (!mReorderingAllowed) {
806eca8e221db923927cdeca927c0b75807a7612145George Mount            // Added fragments are added at the end to comply with prior behavior.
807687e5500421b4d99f864bebfed6c573dc4f38191George Mount            mManager.moveToState(mManager.mCurState, true);
8085ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
8095ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
810c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
811eca8e221db923927cdeca927c0b75807a7612145George Mount    /**
812eca8e221db923927cdeca927c0b75807a7612145George Mount     * Reverses the execution of the operations within this transaction. The Fragment states will
813eca8e221db923927cdeca927c0b75807a7612145George Mount     * only be modified if optimizations are not allowed.
814a42f8e41eebbac6f5a2f0dfa75f95306fcc34588George Mount     *
815a42f8e41eebbac6f5a2f0dfa75f95306fcc34588George Mount     * @param moveToState {@code true} if added fragments should be moved to their final state
816a42f8e41eebbac6f5a2f0dfa75f95306fcc34588George Mount     *                    in unoptimized transactions
817eca8e221db923927cdeca927c0b75807a7612145George Mount     */
818a42f8e41eebbac6f5a2f0dfa75f95306fcc34588George Mount    void executePopOps(boolean moveToState) {
819eca8e221db923927cdeca927c0b75807a7612145George Mount        for (int opNum = mOps.size() - 1; opNum >= 0; opNum--) {
820f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final Op op = mOps.get(opNum);
821eca8e221db923927cdeca927c0b75807a7612145George Mount            Fragment f = op.fragment;
8225f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            if (f != null) {
8235f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                f.setNextTransition(FragmentManagerImpl.reverseTransit(mTransition),
8245f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        mTransitionStyle);
8255f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell            }
826eca8e221db923927cdeca927c0b75807a7612145George Mount            switch (op.cmd) {
827eca8e221db923927cdeca927c0b75807a7612145George Mount                case OP_ADD:
82886bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.popExitAnim);
829eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.removeFragment(f);
830eca8e221db923927cdeca927c0b75807a7612145George Mount                    break;
831eca8e221db923927cdeca927c0b75807a7612145George Mount                case OP_REMOVE:
83286bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.popEnterAnim);
833eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.addFragment(f, false);
834eca8e221db923927cdeca927c0b75807a7612145George Mount                    break;
835eca8e221db923927cdeca927c0b75807a7612145George Mount                case OP_HIDE:
83686bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.popEnterAnim);
837eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.showFragment(f);
838eca8e221db923927cdeca927c0b75807a7612145George Mount                    break;
839eca8e221db923927cdeca927c0b75807a7612145George Mount                case OP_SHOW:
84086bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.popExitAnim);
841eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.hideFragment(f);
842eca8e221db923927cdeca927c0b75807a7612145George Mount                    break;
843eca8e221db923927cdeca927c0b75807a7612145George Mount                case OP_DETACH:
84486bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.popEnterAnim);
845eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.attachFragment(f);
846eca8e221db923927cdeca927c0b75807a7612145George Mount                    break;
847eca8e221db923927cdeca927c0b75807a7612145George Mount                case OP_ATTACH:
84886bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    f.setNextAnim(op.popExitAnim);
849eca8e221db923927cdeca927c0b75807a7612145George Mount                    mManager.detachFragment(f);
850eca8e221db923927cdeca927c0b75807a7612145George Mount                    break;
8515f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                case OP_SET_PRIMARY_NAV:
8525f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    mManager.setPrimaryNavigationFragment(null);
8535f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    break;
8545f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                case OP_UNSET_PRIMARY_NAV:
8555f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    mManager.setPrimaryNavigationFragment(f);
8565f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    break;
857eca8e221db923927cdeca927c0b75807a7612145George Mount                default:
858eca8e221db923927cdeca927c0b75807a7612145George Mount                    throw new IllegalArgumentException("Unknown cmd: " + op.cmd);
859eca8e221db923927cdeca927c0b75807a7612145George Mount            }
860a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount            if (!mReorderingAllowed && op.cmd != OP_REMOVE && f != null) {
861eca8e221db923927cdeca927c0b75807a7612145George Mount                mManager.moveFragmentToExpectedState(f);
862f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            }
863f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        }
864a0ffafffdd3a051edd88ef9d5eb32a960291dffcGeorge Mount        if (!mReorderingAllowed && moveToState) {
865687e5500421b4d99f864bebfed6c573dc4f38191George Mount            mManager.moveToState(mManager.mCurState, true);
866f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        }
867eca8e221db923927cdeca927c0b75807a7612145George Mount    }
868f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount
869eca8e221db923927cdeca927c0b75807a7612145George Mount    /**
8705f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * Expands all meta-ops into their more primitive equivalents. This must be called prior to
8715f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * {@link #executeOps()} or any other call that operations on mOps for forward navigation.
8725f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * It should not be called for pop/reverse navigation operations.
8735f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     *
8745f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * <p>Removes all OP_REPLACE ops and replaces them with the proper add and remove
8755f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * operations that are equivalent to the replace.</p>
8765f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     *
8775f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * <p>Adds OP_UNSET_PRIMARY_NAV ops to match OP_SET_PRIMARY_NAV, OP_REMOVE and OP_DETACH
8785f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * ops so that we can restore the old primary nav fragment later. Since callers call this
8795f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * method in a loop before running ops from several transactions at once, the caller should
8805f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * pass the return value from this method as the oldPrimaryNav parameter for the next call.
8815f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * The first call in such a loop should pass the value of
8825f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * {@link FragmentManager#getPrimaryNavigationFragment()}.</p>
883eca8e221db923927cdeca927c0b75807a7612145George Mount     *
884eca8e221db923927cdeca927c0b75807a7612145George Mount     * @param added Initialized to the fragments that are in the mManager.mAdded, this
885eca8e221db923927cdeca927c0b75807a7612145George Mount     *              will be modified to contain the fragments that will be in mAdded
886eca8e221db923927cdeca927c0b75807a7612145George Mount     *              after the execution ({@link #executeOps()}.
8875f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * @param oldPrimaryNav The tracked primary navigation fragment as of the beginning of
8885f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     *                      this set of ops
8895f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell     * @return the new oldPrimaryNav fragment after this record's ops would be run
890eca8e221db923927cdeca927c0b75807a7612145George Mount     */
891221f15d44ba24942a38db8f8dd37b491c2ae2a62Aurimas Liutikas    @SuppressWarnings("ReferenceEquality")
8925f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell    Fragment expandOps(ArrayList<Fragment> added, Fragment oldPrimaryNav) {
893f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        for (int opNum = 0; opNum < mOps.size(); opNum++) {
894f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final Op op = mOps.get(opNum);
895f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            switch (op.cmd) {
896f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_ADD:
897f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_ATTACH:
898f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    added.add(op.fragment);
89986bfc666d33592f7db5584036aab8c44164d8dc2George Mount                    break;
900f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_REMOVE:
9015f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                case OP_DETACH: {
902f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    added.remove(op.fragment);
9035f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    if (op.fragment == oldPrimaryNav) {
9045f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        mOps.add(opNum, new Op(OP_UNSET_PRIMARY_NAV, op.fragment));
9055f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        opNum++;
9065f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        oldPrimaryNav = null;
9075f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    }
9085f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                }
9095f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                break;
910f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                case OP_REPLACE: {
9115f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    final Fragment f = op.fragment;
9125f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    final int containerId = f.mContainerId;
913f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    boolean alreadyAdded = false;
914f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    for (int i = added.size() - 1; i >= 0; i--) {
9155f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                        final Fragment old = added.get(i);
916f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                        if (old.mContainerId == containerId) {
917f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                            if (old == f) {
918f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                alreadyAdded = true;
919f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                            } else {
9205f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                // This is duplicated from above since we only make
9215f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                // a single pass for expanding ops. Unset any outgoing primary nav.
9225f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                if (old == oldPrimaryNav) {
9235f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                    mOps.add(opNum, new Op(OP_UNSET_PRIMARY_NAV, old));
9245f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                    opNum++;
9255f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                    oldPrimaryNav = null;
9265f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                }
9275f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                                final Op removeOp = new Op(OP_REMOVE, old);
928f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                removeOp.enterAnim = op.enterAnim;
929f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                removeOp.popEnterAnim = op.popEnterAnim;
930f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                removeOp.exitAnim = op.exitAnim;
931f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                removeOp.popExitAnim = op.popExitAnim;
932f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                mOps.add(opNum, removeOp);
933f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                added.remove(old);
934f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                                opNum++;
935f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                            }
936f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                        }
937f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    }
938f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    if (alreadyAdded) {
939f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                        mOps.remove(opNum);
940f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                        opNum--;
941f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    } else {
942f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                        op.cmd = OP_ADD;
943f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                        added.add(f);
944f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                    }
945f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                }
946f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount                break;
9475f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                case OP_SET_PRIMARY_NAV: {
9485f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    // It's ok if this is null, that means we will restore to no active
9495f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    // primary navigation fragment on a pop.
9505f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    mOps.add(opNum, new Op(OP_UNSET_PRIMARY_NAV, oldPrimaryNav));
9515f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    opNum++;
9525f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    // Will be set by the OP_SET_PRIMARY_NAV we inserted before when run
9535f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                    oldPrimaryNav = op.fragment;
9545f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                }
9555f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                break;
956f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            }
957f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        }
9585f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        return oldPrimaryNav;
959f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount    }
960f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount
96199c532d0187808591d59366ac92b6d5560a1fbb1George Mount    /**
96299c532d0187808591d59366ac92b6d5560a1fbb1George Mount     * Removes fragments that are added or removed during a pop operation.
96399c532d0187808591d59366ac92b6d5560a1fbb1George Mount     *
96499c532d0187808591d59366ac92b6d5560a1fbb1George Mount     * @param added Initialized to the fragments that are in the mManager.mAdded, this
96599c532d0187808591d59366ac92b6d5560a1fbb1George Mount     *              will be modified to contain the fragments that will be in mAdded
96699c532d0187808591d59366ac92b6d5560a1fbb1George Mount     *              after the execution ({@link #executeOps()}.
96799c532d0187808591d59366ac92b6d5560a1fbb1George Mount     */
96899c532d0187808591d59366ac92b6d5560a1fbb1George Mount    void trackAddedFragmentsInPop(ArrayList<Fragment> added) {
96999c532d0187808591d59366ac92b6d5560a1fbb1George Mount        for (int opNum = 0; opNum < mOps.size(); opNum++) {
97099c532d0187808591d59366ac92b6d5560a1fbb1George Mount            final Op op = mOps.get(opNum);
97199c532d0187808591d59366ac92b6d5560a1fbb1George Mount            switch (op.cmd) {
97299c532d0187808591d59366ac92b6d5560a1fbb1George Mount                case OP_ADD:
97399c532d0187808591d59366ac92b6d5560a1fbb1George Mount                case OP_ATTACH:
97499c532d0187808591d59366ac92b6d5560a1fbb1George Mount                    added.remove(op.fragment);
97599c532d0187808591d59366ac92b6d5560a1fbb1George Mount                    break;
97699c532d0187808591d59366ac92b6d5560a1fbb1George Mount                case OP_REMOVE:
97799c532d0187808591d59366ac92b6d5560a1fbb1George Mount                case OP_DETACH:
97899c532d0187808591d59366ac92b6d5560a1fbb1George Mount                    added.add(op.fragment);
97999c532d0187808591d59366ac92b6d5560a1fbb1George Mount                    break;
98099c532d0187808591d59366ac92b6d5560a1fbb1George Mount            }
98199c532d0187808591d59366ac92b6d5560a1fbb1George Mount        }
98299c532d0187808591d59366ac92b6d5560a1fbb1George Mount    }
98399c532d0187808591d59366ac92b6d5560a1fbb1George Mount
98486bfc666d33592f7db5584036aab8c44164d8dc2George Mount    boolean isPostponed() {
98586bfc666d33592f7db5584036aab8c44164d8dc2George Mount        for (int opNum = 0; opNum < mOps.size(); opNum++) {
986f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount            final Op op = mOps.get(opNum);
98786bfc666d33592f7db5584036aab8c44164d8dc2George Mount            if (isFragmentPostponed(op)) {
988c855f7d08f1eede615e10507d5480fceeafd3c3cGeorge Mount                return true;
989d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            }
990d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount        }
991c855f7d08f1eede615e10507d5480fceeafd3c3cGeorge Mount        return false;
992d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount    }
993d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount
99486bfc666d33592f7db5584036aab8c44164d8dc2George Mount    void setOnStartPostponedListener(Fragment.OnStartEnterTransitionListener listener) {
99586bfc666d33592f7db5584036aab8c44164d8dc2George Mount        for (int opNum = 0; opNum < mOps.size(); opNum++) {
99686bfc666d33592f7db5584036aab8c44164d8dc2George Mount            final Op op = mOps.get(opNum);
99786bfc666d33592f7db5584036aab8c44164d8dc2George Mount            if (isFragmentPostponed(op)) {
99886bfc666d33592f7db5584036aab8c44164d8dc2George Mount                op.fragment.setOnStartEnterTransitionListener(listener);
999c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            }
1000c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount        }
1001c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount    }
1002c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount
100386bfc666d33592f7db5584036aab8c44164d8dc2George Mount    private static boolean isFragmentPostponed(Op op) {
100486bfc666d33592f7db5584036aab8c44164d8dc2George Mount        final Fragment fragment = op.fragment;
10055f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell        return fragment != null && fragment.mAdded && fragment.mView != null && !fragment.mDetached
10065f3a05c15fa636e911a646e35765ba8bbed7d5e1Adam Powell                && !fragment.mHidden && fragment.isPostponed();
10075ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
1008c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
10095ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public String getName() {
10105ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return mName;
10115ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
1012c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
10135ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public int getTransition() {
10145ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return mTransition;
10155ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
1016c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
10175ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public int getTransitionStyle() {
10185ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        return mTransitionStyle;
10195ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
10202b6230e0de4bac2829ac27b19e95ba75c3da82b4Adam Powell
10212b6230e0de4bac2829ac27b19e95ba75c3da82b4Adam Powell    public boolean isEmpty() {
1022f2045f8f0c5c6aba96b6a4ed570cfcbe3e7e1d64George Mount        return mOps.isEmpty();
10232b6230e0de4bac2829ac27b19e95ba75c3da82b4Adam Powell    }
10245ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn}
1025