162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount/*
262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Copyright (C) 2014 The Android Open Source Project
362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *
462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Licensed under the Apache License, Version 2.0 (the "License");
562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * you may not use this file except in compliance with the License.
662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * You may obtain a copy of the License at
762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *
862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *      http://www.apache.org/licenses/LICENSE-2.0
962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount *
1062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Unless required by applicable law or agreed to in writing, software
1162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * distributed under the License is distributed on an "AS IS" BASIS,
1262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * See the License for the specific language governing permissions and
1462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * limitations under the License.
1562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount */
1662ab9b78b77b7dd851c47115f4d8d7611d657585George Mountpackage android.app;
1762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
1862ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport android.os.Bundle;
1962ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport android.os.ResultReceiver;
20a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mountimport android.transition.Transition;
211fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mountimport android.util.SparseArray;
2262ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport android.view.View;
23a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mountimport android.view.ViewGroup;
24fbd459642fe732115f135e456eafdec2dc8e9bb6George Mountimport android.view.ViewTreeObserver;
2562ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport android.view.Window;
2662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
271fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mountimport java.lang.ref.WeakReference;
2862ab9b78b77b7dd851c47115f4d8d7611d657585George Mountimport java.util.ArrayList;
2962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
3062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount/**
3162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * This class contains all persistence-related functionality for Activity Transitions.
3262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount * Activities start exit and enter Activity Transitions through this class.
3362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount */
3462ab9b78b77b7dd851c47115f4d8d7611d657585George Mountclass ActivityTransitionState {
3562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
3662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private static final String ENTERING_SHARED_ELEMENTS = "android:enteringSharedElements";
3762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
3862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private static final String EXITING_MAPPED_FROM = "android:exitingMappedFrom";
3962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
4062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private static final String EXITING_MAPPED_TO = "android:exitingMappedTo";
4162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
4262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
4362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * The shared elements that the calling Activity has said that they transferred to this
4462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * Activity.
4562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
4662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private ArrayList<String> mEnteringNames;
4762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
4862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
4962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * The names of shared elements that were shared to the called Activity.
5062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
5162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private ArrayList<String> mExitingFrom;
5262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
5362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
5462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * The names of local Views that were shared out, mapped to those elements in mExitingFrom.
5562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
5662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private ArrayList<String> mExitingTo;
5762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
5862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
591fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     * The local Views that were shared out, mapped to those elements in mExitingFrom.
601fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     */
611fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    private ArrayList<View> mExitingToView;
621fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount
631fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    /**
641fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     * The ExitTransitionCoordinator used to start an Activity. Used to make the elements restore
6562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * Visibility of exited Views.
6662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
671fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    private ExitTransitionCoordinator mCalledExitCoordinator;
6862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
6962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
70f1abef6fc2194e72af521202ee4b17c10e03c935George Mount     * The ExitTransitionCoordinator used to return to a previous Activity when called with
71f1abef6fc2194e72af521202ee4b17c10e03c935George Mount     * {@link android.app.Activity#finishAfterTransition()}.
72f1abef6fc2194e72af521202ee4b17c10e03c935George Mount     */
73f1abef6fc2194e72af521202ee4b17c10e03c935George Mount    private ExitTransitionCoordinator mReturnExitCoordinator;
74f1abef6fc2194e72af521202ee4b17c10e03c935George Mount
75f1abef6fc2194e72af521202ee4b17c10e03c935George Mount    /**
7662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * We must be able to cancel entering transitions to stop changing the Window to
7762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * opaque when we exit before making the Window opaque.
7862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
7962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private EnterTransitionCoordinator mEnterTransitionCoordinator;
8062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
8162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
8262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * ActivityOptions used on entering this Activity.
8362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
8462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private ActivityOptions mEnterActivityOptions;
8562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
8662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    /**
8762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     * Has an exit transition been started? If so, we don't want to double-exit.
8862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount     */
8962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    private boolean mHasExited;
9062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
918c2614ce4328640642d8e8be437859e0508a39b4George Mount    /**
928c2614ce4328640642d8e8be437859e0508a39b4George Mount     * Postpone painting and starting the enter transition until this is false.
938c2614ce4328640642d8e8be437859e0508a39b4George Mount     */
948c2614ce4328640642d8e8be437859e0508a39b4George Mount    private boolean mIsEnterPostponed;
958c2614ce4328640642d8e8be437859e0508a39b4George Mount
961fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    /**
971fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     * Potential exit transition coordinators.
981fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     */
991fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    private SparseArray<WeakReference<ExitTransitionCoordinator>> mExitTransitionCoordinators;
1001fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount
1011fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    /**
1021fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     * Next key for mExitTransitionCoordinator.
1031fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount     */
1041fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    private int mExitTransitionCoordinatorsKey = 1;
1051fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount
10601e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount    private boolean mIsEnterTriggered;
10701e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount
10862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public ActivityTransitionState() {
10962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
11062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
1111fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    public int addExitTransitionCoordinator(ExitTransitionCoordinator exitTransitionCoordinator) {
1121fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        if (mExitTransitionCoordinators == null) {
1131fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mExitTransitionCoordinators =
1141fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    new SparseArray<WeakReference<ExitTransitionCoordinator>>();
1151fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        }
1161fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        WeakReference<ExitTransitionCoordinator> ref = new WeakReference(exitTransitionCoordinator);
1171fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        // clean up old references:
1181fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        for (int i = mExitTransitionCoordinators.size() - 1; i >= 0; i--) {
1191fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            WeakReference<ExitTransitionCoordinator> oldRef
1201fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    = mExitTransitionCoordinators.valueAt(i);
1211fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            if (oldRef.get() == null) {
1221fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mExitTransitionCoordinators.removeAt(i);
1231fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            }
1241fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        }
1251fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        int newKey = mExitTransitionCoordinatorsKey++;
1261fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        mExitTransitionCoordinators.append(newKey, ref);
1271fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        return newKey;
1281fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount    }
1291fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount
13062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void readState(Bundle bundle) {
13162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        if (bundle != null) {
13262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            if (mEnterTransitionCoordinator == null || mEnterTransitionCoordinator.isReturning()) {
13362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                mEnteringNames = bundle.getStringArrayList(ENTERING_SHARED_ELEMENTS);
13462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            }
13562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            if (mEnterTransitionCoordinator == null) {
13662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                mExitingFrom = bundle.getStringArrayList(EXITING_MAPPED_FROM);
13762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                mExitingTo = bundle.getStringArrayList(EXITING_MAPPED_TO);
13862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            }
13962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
14062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
14162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
14262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void saveState(Bundle bundle) {
14362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        if (mEnteringNames != null) {
14462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            bundle.putStringArrayList(ENTERING_SHARED_ELEMENTS, mEnteringNames);
14562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
14662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        if (mExitingFrom != null) {
14762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            bundle.putStringArrayList(EXITING_MAPPED_FROM, mExitingFrom);
14862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            bundle.putStringArrayList(EXITING_MAPPED_TO, mExitingTo);
14962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
15062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
15162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
15262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void setEnterActivityOptions(Activity activity, ActivityOptions options) {
15352d77f2429685978923eb63ac9c0ec938f928243George Mount        final Window window = activity.getWindow();
15452d77f2429685978923eb63ac9c0ec938f928243George Mount        if (window == null) {
15552d77f2429685978923eb63ac9c0ec938f928243George Mount            return;
15652d77f2429685978923eb63ac9c0ec938f928243George Mount        }
15752d77f2429685978923eb63ac9c0ec938f928243George Mount        // ensure Decor View has been created so that the window features are activated
15852d77f2429685978923eb63ac9c0ec938f928243George Mount        window.getDecorView();
15952d77f2429685978923eb63ac9c0ec938f928243George Mount        if (window.hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
16062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                && options != null && mEnterActivityOptions == null
16101e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount                && mEnterTransitionCoordinator == null
16262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                && options.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
16362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            mEnterActivityOptions = options;
16401e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount            mIsEnterTriggered = false;
16562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            if (mEnterActivityOptions.isReturning()) {
16699c82fd1de23deeb8cf640bb574c76af76429df6George Mount                restoreExitedViews();
16762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                int result = mEnterActivityOptions.getResultCode();
16862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                if (result != 0) {
16962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                    activity.onActivityReenter(result, mEnterActivityOptions.getResultData());
17062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                }
17162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            }
17262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
17362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
17462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
17562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void enterReady(Activity activity) {
17601e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount        if (mEnterActivityOptions == null || mIsEnterTriggered) {
17762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return;
17862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
17901e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount        mIsEnterTriggered = true;
18062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mHasExited = false;
18162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        ArrayList<String> sharedElementNames = mEnterActivityOptions.getSharedElementNames();
18262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        ResultReceiver resultReceiver = mEnterActivityOptions.getResultReceiver();
18362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        if (mEnterActivityOptions.isReturning()) {
1848cab50afda0d8485436f72a93d668697f549d3b3George Mount            restoreExitedViews();
18562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            activity.getWindow().getDecorView().setVisibility(View.VISIBLE);
1868c2614ce4328640642d8e8be437859e0508a39b4George Mount        }
1878c2614ce4328640642d8e8be437859e0508a39b4George Mount        mEnterTransitionCoordinator = new EnterTransitionCoordinator(activity,
188413739e8c62a7cd83ec519203a2628504b1b0d16George Mount                resultReceiver, sharedElementNames, mEnterActivityOptions.isReturning(),
189413739e8c62a7cd83ec519203a2628504b1b0d16George Mount                mEnterActivityOptions.isCrossTask());
190413739e8c62a7cd83ec519203a2628504b1b0d16George Mount        if (mEnterActivityOptions.isCrossTask()) {
191413739e8c62a7cd83ec519203a2628504b1b0d16George Mount            mExitingFrom = new ArrayList<>(mEnterActivityOptions.getSharedElementNames());
192413739e8c62a7cd83ec519203a2628504b1b0d16George Mount            mExitingTo = new ArrayList<>(mEnterActivityOptions.getSharedElementNames());
193413739e8c62a7cd83ec519203a2628504b1b0d16George Mount        }
1948c2614ce4328640642d8e8be437859e0508a39b4George Mount
1958c2614ce4328640642d8e8be437859e0508a39b4George Mount        if (!mIsEnterPostponed) {
1968c2614ce4328640642d8e8be437859e0508a39b4George Mount            startEnter();
1978c2614ce4328640642d8e8be437859e0508a39b4George Mount        }
1988c2614ce4328640642d8e8be437859e0508a39b4George Mount    }
1998c2614ce4328640642d8e8be437859e0508a39b4George Mount
2008c2614ce4328640642d8e8be437859e0508a39b4George Mount    public void postponeEnterTransition() {
2018c2614ce4328640642d8e8be437859e0508a39b4George Mount        mIsEnterPostponed = true;
2028c2614ce4328640642d8e8be437859e0508a39b4George Mount    }
2038c2614ce4328640642d8e8be437859e0508a39b4George Mount
2048c2614ce4328640642d8e8be437859e0508a39b4George Mount    public void startPostponedEnterTransition() {
2058c2614ce4328640642d8e8be437859e0508a39b4George Mount        if (mIsEnterPostponed) {
2068c2614ce4328640642d8e8be437859e0508a39b4George Mount            mIsEnterPostponed = false;
2078c2614ce4328640642d8e8be437859e0508a39b4George Mount            if (mEnterTransitionCoordinator != null) {
2088c2614ce4328640642d8e8be437859e0508a39b4George Mount                startEnter();
2098c2614ce4328640642d8e8be437859e0508a39b4George Mount            }
2108c2614ce4328640642d8e8be437859e0508a39b4George Mount        }
2118c2614ce4328640642d8e8be437859e0508a39b4George Mount    }
2128c2614ce4328640642d8e8be437859e0508a39b4George Mount
2138c2614ce4328640642d8e8be437859e0508a39b4George Mount    private void startEnter() {
214d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount        if (mEnterTransitionCoordinator.isReturning()) {
2151fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            if (mExitingToView != null) {
216b04b36eaadb2f3e56206d8ae157df06bb89f9866Dake Gu                mEnterTransitionCoordinator.viewInstancesReady(mExitingFrom, mExitingTo,
217b04b36eaadb2f3e56206d8ae157df06bb89f9866Dake Gu                        mExitingToView);
2181fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            } else {
2191fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mEnterTransitionCoordinator.namedViewsReady(mExitingFrom, mExitingTo);
2201fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            }
22162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        } else {
2221fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mEnterTransitionCoordinator.namedViewsReady(null, null);
2238c2614ce4328640642d8e8be437859e0508a39b4George Mount            mEnteringNames = mEnterTransitionCoordinator.getAllSharedElementNames();
22462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
2258c2614ce4328640642d8e8be437859e0508a39b4George Mount
22662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mExitingFrom = null;
22762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mExitingTo = null;
2281fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        mExitingToView = null;
22962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mEnterActivityOptions = null;
23062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
23162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
23262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void onStop() {
2338cab50afda0d8485436f72a93d668697f549d3b3George Mount        restoreExitedViews();
23462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        if (mEnterTransitionCoordinator != null) {
23562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            mEnterTransitionCoordinator.stop();
23662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            mEnterTransitionCoordinator = null;
23762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
238f1abef6fc2194e72af521202ee4b17c10e03c935George Mount        if (mReturnExitCoordinator != null) {
239f1abef6fc2194e72af521202ee4b17c10e03c935George Mount            mReturnExitCoordinator.stop();
240f1abef6fc2194e72af521202ee4b17c10e03c935George Mount            mReturnExitCoordinator = null;
241f1abef6fc2194e72af521202ee4b17c10e03c935George Mount        }
24262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
24362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
244bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount    public void onResume(Activity activity, boolean isTopOfTask) {
245bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        // After orientation change, the onResume can come in before the top Activity has
246bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        // left, so if the Activity is not top, wait a second for the top Activity to exit.
247bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        if (isTopOfTask || mEnterTransitionCoordinator == null) {
248bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            restoreExitedViews();
249bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            restoreReenteringViews();
250bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        } else {
251bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            activity.mHandler.postDelayed(new Runnable() {
252bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                @Override
253bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                public void run() {
254bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                    if (mEnterTransitionCoordinator == null ||
255bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                            mEnterTransitionCoordinator.isWaitingForRemoteExit()) {
256bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                        restoreExitedViews();
257bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                        restoreReenteringViews();
258bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                    }
259bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                }
260bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            }, 1000);
261bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        }
2628cab50afda0d8485436f72a93d668697f549d3b3George Mount    }
2638cab50afda0d8485436f72a93d668697f549d3b3George Mount
264a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount    public void clear() {
265a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mEnteringNames = null;
266a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitingFrom = null;
267a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitingTo = null;
268a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitingToView = null;
269a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mCalledExitCoordinator = null;
270a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mEnterTransitionCoordinator = null;
271a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mEnterActivityOptions = null;
272a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitTransitionCoordinators = null;
273a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount    }
274a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount
2758cab50afda0d8485436f72a93d668697f549d3b3George Mount    private void restoreExitedViews() {
2761fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        if (mCalledExitCoordinator != null) {
2771fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mCalledExitCoordinator.resetViews();
2781fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mCalledExitCoordinator = null;
2798cab50afda0d8485436f72a93d668697f549d3b3George Mount        }
2808cab50afda0d8485436f72a93d668697f549d3b3George Mount    }
2818cab50afda0d8485436f72a93d668697f549d3b3George Mount
282d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount    private void restoreReenteringViews() {
283413739e8c62a7cd83ec519203a2628504b1b0d16George Mount        if (mEnterTransitionCoordinator != null && mEnterTransitionCoordinator.isReturning() &&
284413739e8c62a7cd83ec519203a2628504b1b0d16George Mount                !mEnterTransitionCoordinator.isCrossTask()) {
285d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mEnterTransitionCoordinator.forceViewsToAppear();
286d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mExitingFrom = null;
287d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mExitingTo = null;
288d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mExitingToView = null;
289d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount        }
290d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount    }
291d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount
292fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount    public boolean startExitBackTransition(final Activity activity) {
2935ae0b7abf0d026973d556a8d6685dc2b986bf7b5George Mount        if (mEnteringNames == null || mCalledExitCoordinator != null) {
29462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return false;
29562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        } else {
29662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            if (!mHasExited) {
29762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                mHasExited = true;
298a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                Transition enterViewsTransition = null;
299a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                ViewGroup decor = null;
300fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                boolean delayExitBack = false;
30162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                if (mEnterTransitionCoordinator != null) {
302a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    enterViewsTransition = mEnterTransitionCoordinator.getEnterViewsTransition();
303a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    decor = mEnterTransitionCoordinator.getDecor();
304fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    delayExitBack = mEnterTransitionCoordinator.cancelEnter();
30562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                    mEnterTransitionCoordinator = null;
306a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    if (enterViewsTransition != null && decor != null) {
307a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                        enterViewsTransition.pause(decor);
308a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    }
30962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                }
31062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
311413739e8c62a7cd83ec519203a2628504b1b0d16George Mount                mReturnExitCoordinator = new ExitTransitionCoordinator(activity,
312413739e8c62a7cd83ec519203a2628504b1b0d16George Mount                        activity.getWindow(), activity.mEnterTransitionListener, mEnteringNames,
313413739e8c62a7cd83ec519203a2628504b1b0d16George Mount                        null, null, true);
314a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                if (enterViewsTransition != null && decor != null) {
315a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    enterViewsTransition.resume(decor);
316a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                }
317fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                if (delayExitBack && decor != null) {
318fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    final ViewGroup finalDecor = decor;
319fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    decor.getViewTreeObserver().addOnPreDrawListener(
320fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                            new ViewTreeObserver.OnPreDrawListener() {
321fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                @Override
322fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                public boolean onPreDraw() {
323fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    finalDecor.getViewTreeObserver().removeOnPreDrawListener(this);
324fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    if (mReturnExitCoordinator != null) {
325fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                        mReturnExitCoordinator.startExit(activity.mResultCode,
326fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                                activity.mResultData);
327fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    }
328fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    return true;
329fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                }
330fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                            });
331fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                } else {
332fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    mReturnExitCoordinator.startExit(activity.mResultCode, activity.mResultData);
333fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                }
33462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            }
33562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return true;
33662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
33762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
33862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
33962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void startExitOutTransition(Activity activity, Bundle options) {
340a3edbf6a5d4698570f2b5610390b5c70251f52d7George Mount        mEnterTransitionCoordinator = null;
341a3edbf6a5d4698570f2b5610390b5c70251f52d7George Mount        if (!activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) ||
342a3edbf6a5d4698570f2b5610390b5c70251f52d7George Mount                mExitTransitionCoordinators == null) {
34362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return;
34462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
3451fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        ActivityOptions activityOptions = new ActivityOptions(options);
3461fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        if (activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
3471fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            int key = activityOptions.getExitCoordinatorKey();
3481fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            int index = mExitTransitionCoordinators.indexOfKey(key);
3491fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            if (index >= 0) {
3501fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mCalledExitCoordinator = mExitTransitionCoordinators.valueAt(index).get();
3511fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mExitTransitionCoordinators.removeAt(index);
3521fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                if (mCalledExitCoordinator != null) {
3531fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    mExitingFrom = mCalledExitCoordinator.getAcceptedNames();
3541fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    mExitingTo = mCalledExitCoordinator.getMappedNames();
355700db2a325bced35cebc403f272f988fad522892George Mount                    mExitingToView = mCalledExitCoordinator.copyMappedViews();
3561fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    mCalledExitCoordinator.startExit();
3571fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                }
3581fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            }
35962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
36062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
36162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount}
362