ActivityTransitionState.java revision f1abef6fc2194e72af521202ee4b17c10e03c935
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.app;
17
18import android.os.Bundle;
19import android.os.ResultReceiver;
20import android.transition.Transition;
21import android.util.ArrayMap;
22import android.util.SparseArray;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.Window;
26
27import java.lang.ref.WeakReference;
28import java.util.ArrayList;
29
30/**
31 * This class contains all persistence-related functionality for Activity Transitions.
32 * Activities start exit and enter Activity Transitions through this class.
33 */
34class ActivityTransitionState {
35
36    private static final String ENTERING_SHARED_ELEMENTS = "android:enteringSharedElements";
37
38    private static final String EXITING_MAPPED_FROM = "android:exitingMappedFrom";
39
40    private static final String EXITING_MAPPED_TO = "android:exitingMappedTo";
41
42    /**
43     * The shared elements that the calling Activity has said that they transferred to this
44     * Activity.
45     */
46    private ArrayList<String> mEnteringNames;
47
48    /**
49     * The names of shared elements that were shared to the called Activity.
50     */
51    private ArrayList<String> mExitingFrom;
52
53    /**
54     * The names of local Views that were shared out, mapped to those elements in mExitingFrom.
55     */
56    private ArrayList<String> mExitingTo;
57
58    /**
59     * The local Views that were shared out, mapped to those elements in mExitingFrom.
60     */
61    private ArrayList<View> mExitingToView;
62
63    /**
64     * The ExitTransitionCoordinator used to start an Activity. Used to make the elements restore
65     * Visibility of exited Views.
66     */
67    private ExitTransitionCoordinator mCalledExitCoordinator;
68
69    /**
70     * The ExitTransitionCoordinator used to return to a previous Activity when called with
71     * {@link android.app.Activity#finishAfterTransition()}.
72     */
73    private ExitTransitionCoordinator mReturnExitCoordinator;
74
75    /**
76     * We must be able to cancel entering transitions to stop changing the Window to
77     * opaque when we exit before making the Window opaque.
78     */
79    private EnterTransitionCoordinator mEnterTransitionCoordinator;
80
81    /**
82     * ActivityOptions used on entering this Activity.
83     */
84    private ActivityOptions mEnterActivityOptions;
85
86    /**
87     * Has an exit transition been started? If so, we don't want to double-exit.
88     */
89    private boolean mHasExited;
90
91    /**
92     * Postpone painting and starting the enter transition until this is false.
93     */
94    private boolean mIsEnterPostponed;
95
96    /**
97     * Potential exit transition coordinators.
98     */
99    private SparseArray<WeakReference<ExitTransitionCoordinator>> mExitTransitionCoordinators;
100
101    /**
102     * Next key for mExitTransitionCoordinator.
103     */
104    private int mExitTransitionCoordinatorsKey = 1;
105
106    private boolean mIsEnterTriggered;
107
108    public ActivityTransitionState() {
109    }
110
111    public int addExitTransitionCoordinator(ExitTransitionCoordinator exitTransitionCoordinator) {
112        if (mExitTransitionCoordinators == null) {
113            mExitTransitionCoordinators =
114                    new SparseArray<WeakReference<ExitTransitionCoordinator>>();
115        }
116        WeakReference<ExitTransitionCoordinator> ref = new WeakReference(exitTransitionCoordinator);
117        // clean up old references:
118        for (int i = mExitTransitionCoordinators.size() - 1; i >= 0; i--) {
119            WeakReference<ExitTransitionCoordinator> oldRef
120                    = mExitTransitionCoordinators.valueAt(i);
121            if (oldRef.get() == null) {
122                mExitTransitionCoordinators.removeAt(i);
123            }
124        }
125        int newKey = mExitTransitionCoordinatorsKey++;
126        mExitTransitionCoordinators.append(newKey, ref);
127        return newKey;
128    }
129
130    public void readState(Bundle bundle) {
131        if (bundle != null) {
132            if (mEnterTransitionCoordinator == null || mEnterTransitionCoordinator.isReturning()) {
133                mEnteringNames = bundle.getStringArrayList(ENTERING_SHARED_ELEMENTS);
134            }
135            if (mEnterTransitionCoordinator == null) {
136                mExitingFrom = bundle.getStringArrayList(EXITING_MAPPED_FROM);
137                mExitingTo = bundle.getStringArrayList(EXITING_MAPPED_TO);
138            }
139        }
140    }
141
142    public void saveState(Bundle bundle) {
143        if (mEnteringNames != null) {
144            bundle.putStringArrayList(ENTERING_SHARED_ELEMENTS, mEnteringNames);
145        }
146        if (mExitingFrom != null) {
147            bundle.putStringArrayList(EXITING_MAPPED_FROM, mExitingFrom);
148            bundle.putStringArrayList(EXITING_MAPPED_TO, mExitingTo);
149        }
150    }
151
152    public void setEnterActivityOptions(Activity activity, ActivityOptions options) {
153        if (activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
154                && options != null && mEnterActivityOptions == null
155                && mEnterTransitionCoordinator == null
156                && options.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
157            mEnterActivityOptions = options;
158            mIsEnterTriggered = false;
159            if (mEnterActivityOptions.isReturning()) {
160                restoreExitedViews();
161                int result = mEnterActivityOptions.getResultCode();
162                if (result != 0) {
163                    activity.onActivityReenter(result, mEnterActivityOptions.getResultData());
164                }
165            }
166        }
167    }
168
169    public void enterReady(Activity activity) {
170        if (mEnterActivityOptions == null || mIsEnterTriggered) {
171            return;
172        }
173        mIsEnterTriggered = true;
174        mHasExited = false;
175        ArrayList<String> sharedElementNames = mEnterActivityOptions.getSharedElementNames();
176        ResultReceiver resultReceiver = mEnterActivityOptions.getResultReceiver();
177        if (mEnterActivityOptions.isReturning()) {
178            restoreExitedViews();
179            activity.getWindow().getDecorView().setVisibility(View.VISIBLE);
180        }
181        mEnterTransitionCoordinator = new EnterTransitionCoordinator(activity,
182                resultReceiver, sharedElementNames, mEnterActivityOptions.isReturning());
183
184        if (!mIsEnterPostponed) {
185            startEnter();
186        }
187    }
188
189    public void postponeEnterTransition() {
190        mIsEnterPostponed = true;
191    }
192
193    public void startPostponedEnterTransition() {
194        if (mIsEnterPostponed) {
195            mIsEnterPostponed = false;
196            if (mEnterTransitionCoordinator != null) {
197                startEnter();
198            }
199        }
200    }
201
202    private void startEnter() {
203        if (mEnterActivityOptions.isReturning()) {
204            if (mExitingToView != null) {
205                mEnterTransitionCoordinator.viewInstancesReady(mExitingFrom, mExitingTo,
206                        mExitingToView);
207            } else {
208                mEnterTransitionCoordinator.namedViewsReady(mExitingFrom, mExitingTo);
209            }
210        } else {
211            mEnterTransitionCoordinator.namedViewsReady(null, null);
212            mEnteringNames = mEnterTransitionCoordinator.getAllSharedElementNames();
213        }
214
215        mExitingFrom = null;
216        mExitingTo = null;
217        mExitingToView = null;
218        mEnterActivityOptions = null;
219    }
220
221    public void onStop() {
222        restoreExitedViews();
223        if (mEnterTransitionCoordinator != null) {
224            mEnterTransitionCoordinator.stop();
225            mEnterTransitionCoordinator = null;
226        }
227        if (mReturnExitCoordinator != null) {
228            mReturnExitCoordinator.stop();
229            mReturnExitCoordinator = null;
230        }
231    }
232
233    public void onResume() {
234        restoreExitedViews();
235    }
236
237    public void clear() {
238        mEnteringNames = null;
239        mExitingFrom = null;
240        mExitingTo = null;
241        mExitingToView = null;
242        mCalledExitCoordinator = null;
243        mEnterTransitionCoordinator = null;
244        mEnterActivityOptions = null;
245        mExitTransitionCoordinators = null;
246    }
247
248    private void restoreExitedViews() {
249        if (mCalledExitCoordinator != null) {
250            mCalledExitCoordinator.resetViews();
251            mCalledExitCoordinator = null;
252        }
253    }
254
255    public boolean startExitBackTransition(Activity activity) {
256        if (mEnteringNames == null) {
257            return false;
258        } else {
259            if (!mHasExited) {
260                mHasExited = true;
261                Transition enterViewsTransition = null;
262                ViewGroup decor = null;
263                if (mEnterTransitionCoordinator != null) {
264                    enterViewsTransition = mEnterTransitionCoordinator.getEnterViewsTransition();
265                    decor = mEnterTransitionCoordinator.getDecor();
266                    mEnterTransitionCoordinator.cancelEnter();
267                    mEnterTransitionCoordinator = null;
268                    if (enterViewsTransition != null && decor != null) {
269                        enterViewsTransition.pause(decor);
270                    }
271                }
272
273                mReturnExitCoordinator =
274                        new ExitTransitionCoordinator(activity, mEnteringNames, null, null, true);
275                if (enterViewsTransition != null && decor != null) {
276                    enterViewsTransition.resume(decor);
277                }
278                mReturnExitCoordinator.startExit(activity.mResultCode, activity.mResultData);
279            }
280            return true;
281        }
282    }
283
284    public void startExitOutTransition(Activity activity, Bundle options) {
285        if (!activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)) {
286            return;
287        }
288        ActivityOptions activityOptions = new ActivityOptions(options);
289        mEnterTransitionCoordinator = null;
290        if (activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
291            int key = activityOptions.getExitCoordinatorKey();
292            int index = mExitTransitionCoordinators.indexOfKey(key);
293            if (index >= 0) {
294                mCalledExitCoordinator = mExitTransitionCoordinators.valueAt(index).get();
295                mExitTransitionCoordinators.removeAt(index);
296                if (mCalledExitCoordinator != null) {
297                    mExitingFrom = mCalledExitCoordinator.getAcceptedNames();
298                    mExitingTo = mCalledExitCoordinator.getMappedNames();
299                    mExitingToView = mCalledExitCoordinator.copyMappedViews();
300                    mCalledExitCoordinator.startExit();
301                }
302            }
303        }
304    }
305}
306