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,
1888c2614ce4328640642d8e8be437859e0508a39b4George Mount                resultReceiver, sharedElementNames, mEnterActivityOptions.isReturning());
1898c2614ce4328640642d8e8be437859e0508a39b4George Mount
1908c2614ce4328640642d8e8be437859e0508a39b4George Mount        if (!mIsEnterPostponed) {
1918c2614ce4328640642d8e8be437859e0508a39b4George Mount            startEnter();
1928c2614ce4328640642d8e8be437859e0508a39b4George Mount        }
1938c2614ce4328640642d8e8be437859e0508a39b4George Mount    }
1948c2614ce4328640642d8e8be437859e0508a39b4George Mount
1958c2614ce4328640642d8e8be437859e0508a39b4George Mount    public void postponeEnterTransition() {
1968c2614ce4328640642d8e8be437859e0508a39b4George Mount        mIsEnterPostponed = true;
1978c2614ce4328640642d8e8be437859e0508a39b4George Mount    }
1988c2614ce4328640642d8e8be437859e0508a39b4George Mount
1998c2614ce4328640642d8e8be437859e0508a39b4George Mount    public void startPostponedEnterTransition() {
2008c2614ce4328640642d8e8be437859e0508a39b4George Mount        if (mIsEnterPostponed) {
2018c2614ce4328640642d8e8be437859e0508a39b4George Mount            mIsEnterPostponed = false;
2028c2614ce4328640642d8e8be437859e0508a39b4George Mount            if (mEnterTransitionCoordinator != null) {
2038c2614ce4328640642d8e8be437859e0508a39b4George Mount                startEnter();
2048c2614ce4328640642d8e8be437859e0508a39b4George Mount            }
2058c2614ce4328640642d8e8be437859e0508a39b4George Mount        }
2068c2614ce4328640642d8e8be437859e0508a39b4George Mount    }
2078c2614ce4328640642d8e8be437859e0508a39b4George Mount
2088c2614ce4328640642d8e8be437859e0508a39b4George Mount    private void startEnter() {
209d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount        if (mEnterTransitionCoordinator.isReturning()) {
2101fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            if (mExitingToView != null) {
211b04b36eaadb2f3e56206d8ae157df06bb89f9866Dake Gu                mEnterTransitionCoordinator.viewInstancesReady(mExitingFrom, mExitingTo,
212b04b36eaadb2f3e56206d8ae157df06bb89f9866Dake Gu                        mExitingToView);
2131fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            } else {
2141fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mEnterTransitionCoordinator.namedViewsReady(mExitingFrom, mExitingTo);
2151fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            }
21662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        } else {
2171fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mEnterTransitionCoordinator.namedViewsReady(null, null);
2188c2614ce4328640642d8e8be437859e0508a39b4George Mount            mEnteringNames = mEnterTransitionCoordinator.getAllSharedElementNames();
21962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
2208c2614ce4328640642d8e8be437859e0508a39b4George Mount
22162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mExitingFrom = null;
22262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mExitingTo = null;
2231fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        mExitingToView = null;
22462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        mEnterActivityOptions = null;
22562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
22662ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
22762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void onStop() {
2288cab50afda0d8485436f72a93d668697f549d3b3George Mount        restoreExitedViews();
22962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        if (mEnterTransitionCoordinator != null) {
23062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            mEnterTransitionCoordinator.stop();
23162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            mEnterTransitionCoordinator = null;
23262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
233f1abef6fc2194e72af521202ee4b17c10e03c935George Mount        if (mReturnExitCoordinator != null) {
234f1abef6fc2194e72af521202ee4b17c10e03c935George Mount            mReturnExitCoordinator.stop();
235f1abef6fc2194e72af521202ee4b17c10e03c935George Mount            mReturnExitCoordinator = null;
236f1abef6fc2194e72af521202ee4b17c10e03c935George Mount        }
23762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
23862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
239bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount    public void onResume(Activity activity, boolean isTopOfTask) {
240bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        // After orientation change, the onResume can come in before the top Activity has
241bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        // left, so if the Activity is not top, wait a second for the top Activity to exit.
242bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        if (isTopOfTask || mEnterTransitionCoordinator == null) {
243bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            restoreExitedViews();
244bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            restoreReenteringViews();
245bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        } else {
246bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            activity.mHandler.postDelayed(new Runnable() {
247bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                @Override
248bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                public void run() {
249bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                    if (mEnterTransitionCoordinator == null ||
250bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                            mEnterTransitionCoordinator.isWaitingForRemoteExit()) {
251bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                        restoreExitedViews();
252bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                        restoreReenteringViews();
253bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                    }
254bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount                }
255bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount            }, 1000);
256bdc4d8dc9635064fd67f379c341b69bdc03f8968George Mount        }
2578cab50afda0d8485436f72a93d668697f549d3b3George Mount    }
2588cab50afda0d8485436f72a93d668697f549d3b3George Mount
259a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount    public void clear() {
260a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mEnteringNames = null;
261a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitingFrom = null;
262a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitingTo = null;
263a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitingToView = null;
264a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mCalledExitCoordinator = null;
265a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mEnterTransitionCoordinator = null;
266a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mEnterActivityOptions = null;
267a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount        mExitTransitionCoordinators = null;
268a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount    }
269a0a0260e48e1ee4e9b5d98b49571e8d2a6fd6c3aGeorge Mount
2708cab50afda0d8485436f72a93d668697f549d3b3George Mount    private void restoreExitedViews() {
2711fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        if (mCalledExitCoordinator != null) {
2721fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mCalledExitCoordinator.resetViews();
2731fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            mCalledExitCoordinator = null;
2748cab50afda0d8485436f72a93d668697f549d3b3George Mount        }
2758cab50afda0d8485436f72a93d668697f549d3b3George Mount    }
2768cab50afda0d8485436f72a93d668697f549d3b3George Mount
277d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount    private void restoreReenteringViews() {
278d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount        if (mEnterTransitionCoordinator != null && mEnterTransitionCoordinator.isReturning()) {
279d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mEnterTransitionCoordinator.forceViewsToAppear();
280d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mExitingFrom = null;
281d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mExitingTo = null;
282d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount            mExitingToView = null;
283d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount        }
284d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount    }
285d7d0fb3e65f993ae02d71388dc616b467639cd8dGeorge Mount
286fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount    public boolean startExitBackTransition(final Activity activity) {
2875ae0b7abf0d026973d556a8d6685dc2b986bf7b5George Mount        if (mEnteringNames == null || mCalledExitCoordinator != null) {
28862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return false;
28962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        } else {
29062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            if (!mHasExited) {
29162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                mHasExited = true;
292a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                Transition enterViewsTransition = null;
293a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                ViewGroup decor = null;
294fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                boolean delayExitBack = false;
29562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                if (mEnterTransitionCoordinator != null) {
296a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    enterViewsTransition = mEnterTransitionCoordinator.getEnterViewsTransition();
297a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    decor = mEnterTransitionCoordinator.getDecor();
298fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    delayExitBack = mEnterTransitionCoordinator.cancelEnter();
29962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                    mEnterTransitionCoordinator = null;
300a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    if (enterViewsTransition != null && decor != null) {
301a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                        enterViewsTransition.pause(decor);
302a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    }
30362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount                }
30462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
305f1abef6fc2194e72af521202ee4b17c10e03c935George Mount                mReturnExitCoordinator =
3061fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                        new ExitTransitionCoordinator(activity, mEnteringNames, null, null, true);
307a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                if (enterViewsTransition != null && decor != null) {
308a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                    enterViewsTransition.resume(decor);
309a2bbbb3bac464c3a52e33008401fed4a7542744cGeorge Mount                }
310fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                if (delayExitBack && decor != null) {
311fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    final ViewGroup finalDecor = decor;
312fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    decor.getViewTreeObserver().addOnPreDrawListener(
313fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                            new ViewTreeObserver.OnPreDrawListener() {
314fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                @Override
315fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                public boolean onPreDraw() {
316fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    finalDecor.getViewTreeObserver().removeOnPreDrawListener(this);
317fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    if (mReturnExitCoordinator != null) {
318fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                        mReturnExitCoordinator.startExit(activity.mResultCode,
319fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                                activity.mResultData);
320fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    }
321fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                    return true;
322fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                                }
323fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                            });
324fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                } else {
325fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                    mReturnExitCoordinator.startExit(activity.mResultCode, activity.mResultData);
326fbd459642fe732115f135e456eafdec2dc8e9bb6George Mount                }
32762ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            }
32862ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return true;
32962ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
33062ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
33162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount
33262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    public void startExitOutTransition(Activity activity, Bundle options) {
3339826f636ad4fe3714d60972acd918e09eb44d971George Mount        if (!activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)) {
33462ab9b78b77b7dd851c47115f4d8d7611d657585George Mount            return;
33562ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
3361fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        ActivityOptions activityOptions = new ActivityOptions(options);
33701e9a97fe68dbba42a0edd0ad965ccfe2b8efe7dGeorge Mount        mEnterTransitionCoordinator = null;
3381fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount        if (activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
3391fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            int key = activityOptions.getExitCoordinatorKey();
3401fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            int index = mExitTransitionCoordinators.indexOfKey(key);
3411fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            if (index >= 0) {
3421fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mCalledExitCoordinator = mExitTransitionCoordinators.valueAt(index).get();
3431fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                mExitTransitionCoordinators.removeAt(index);
3441fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                if (mCalledExitCoordinator != null) {
3451fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    mExitingFrom = mCalledExitCoordinator.getAcceptedNames();
3461fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    mExitingTo = mCalledExitCoordinator.getMappedNames();
347700db2a325bced35cebc403f272f988fad522892George Mount                    mExitingToView = mCalledExitCoordinator.copyMappedViews();
3481fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                    mCalledExitCoordinator.startExit();
3491fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount                }
3501fecfb2ddcdf4335ff543bdd549b8e4d36139da8George Mount            }
35162ab9b78b77b7dd851c47115f4d8d7611d657585George Mount        }
35262ab9b78b77b7dd851c47115f4d8d7611d657585George Mount    }
35362ab9b78b77b7dd851c47115f4d8d7611d657585George Mount}
354