George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 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 | */ |
| 16 | package android.app; |
| 17 | |
| 18 | import android.animation.Animator; |
| 19 | import android.animation.AnimatorListenerAdapter; |
| 20 | import android.animation.ObjectAnimator; |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 21 | import android.app.SharedElementCallback.OnSharedElementsReadyListener; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.ResultReceiver; |
Dake Gu | b04b36e | 2014-08-08 11:22:30 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 26 | import android.transition.Transition; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 27 | import android.transition.TransitionManager; |
| 28 | import android.util.ArrayMap; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 29 | import android.view.View; |
George Mount | d265bd7 | 2014-06-02 07:22:59 -0700 | [diff] [blame] | 30 | import android.view.ViewGroup; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 31 | import android.view.ViewGroupOverlay; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 32 | import android.view.ViewTreeObserver; |
George Mount | d7d0fb3 | 2016-01-04 14:56:08 -0800 | [diff] [blame] | 33 | import android.view.ViewTreeObserver.OnPreDrawListener; |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 34 | import android.view.Window; |
George Mount | 10d312b | 2015-12-16 14:00:35 -0800 | [diff] [blame] | 35 | import android.view.accessibility.AccessibilityEvent; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
| 38 | |
| 39 | /** |
| 40 | * This ActivityTransitionCoordinator is created by the Activity to manage |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 41 | * the enter scene and shared element transfer into the Scene, either during |
| 42 | * launch of an Activity or returning from a launched Activity. |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 43 | */ |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 44 | class EnterTransitionCoordinator extends ActivityTransitionCoordinator { |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 45 | private static final String TAG = "EnterTransitionCoordinator"; |
| 46 | |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 47 | private static final int MIN_ANIMATION_FRAMES = 2; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 48 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 49 | private boolean mSharedElementTransitionStarted; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 50 | private Activity mActivity; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 51 | private boolean mHasStopped; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 52 | private boolean mIsCanceled; |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 53 | private ObjectAnimator mBackgroundAnimator; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 54 | private boolean mIsExitTransitionComplete; |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 55 | private boolean mIsReadyForTransition; |
| 56 | private Bundle mSharedElementsBundle; |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 57 | private boolean mWasOpaque; |
George Mount | 82ffc14 | 2014-06-30 16:47:34 -0700 | [diff] [blame] | 58 | private boolean mAreViewsReady; |
George Mount | 8d3cd2c | 2014-07-08 11:07:33 -0700 | [diff] [blame] | 59 | private boolean mIsViewsTransitionStarted; |
George Mount | a2bbbb3 | 2014-08-12 10:16:20 -0700 | [diff] [blame] | 60 | private Transition mEnterViewsTransition; |
George Mount | d7d0fb3 | 2016-01-04 14:56:08 -0800 | [diff] [blame] | 61 | private OnPreDrawListener mViewsReadyListener; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 62 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 63 | public EnterTransitionCoordinator(Activity activity, ResultReceiver resultReceiver, |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 64 | ArrayList<String> sharedElementNames, boolean isReturning) { |
| 65 | super(activity.getWindow(), sharedElementNames, |
| 66 | getListener(activity, isReturning), isReturning); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 67 | mActivity = activity; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 68 | setResultReceiver(resultReceiver); |
| 69 | prepareEnter(); |
| 70 | Bundle resultReceiverBundle = new Bundle(); |
| 71 | resultReceiverBundle.putParcelable(KEY_REMOTE_RECEIVER, this); |
| 72 | mResultReceiver.send(MSG_SET_REMOTE_RECEIVER, resultReceiverBundle); |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 73 | final View decorView = getDecor(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 74 | if (decorView != null) { |
| 75 | decorView.getViewTreeObserver().addOnPreDrawListener( |
| 76 | new ViewTreeObserver.OnPreDrawListener() { |
| 77 | @Override |
| 78 | public boolean onPreDraw() { |
| 79 | if (mIsReadyForTransition) { |
| 80 | decorView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 81 | } |
| 82 | return mIsReadyForTransition; |
George Mount | ed1e01d | 2014-06-05 13:49:12 -0700 | [diff] [blame] | 83 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 84 | }); |
| 85 | } |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Dake Gu | b04b36e | 2014-08-08 11:22:30 -0700 | [diff] [blame] | 88 | public void viewInstancesReady(ArrayList<String> accepted, ArrayList<String> localNames, |
| 89 | ArrayList<View> localViews) { |
| 90 | boolean remap = false; |
| 91 | for (int i = 0; i < localViews.size(); i++) { |
| 92 | View view = localViews.get(i); |
| 93 | if (!TextUtils.equals(view.getTransitionName(), localNames.get(i)) |
| 94 | || !view.isAttachedToWindow()) { |
| 95 | remap = true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | if (remap) { |
| 100 | triggerViewsReady(mapNamedElements(accepted, localNames)); |
| 101 | } else { |
| 102 | triggerViewsReady(mapSharedElements(accepted, localViews)); |
| 103 | } |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 104 | } |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 105 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 106 | public void namedViewsReady(ArrayList<String> accepted, ArrayList<String> localNames) { |
George Mount | 82ffc14 | 2014-06-30 16:47:34 -0700 | [diff] [blame] | 107 | triggerViewsReady(mapNamedElements(accepted, localNames)); |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 108 | } |
| 109 | |
George Mount | a2bbbb3 | 2014-08-12 10:16:20 -0700 | [diff] [blame] | 110 | public Transition getEnterViewsTransition() { |
| 111 | return mEnterViewsTransition; |
| 112 | } |
| 113 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 114 | @Override |
| 115 | protected void viewsReady(ArrayMap<String, View> sharedElements) { |
| 116 | super.viewsReady(sharedElements); |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 117 | mIsReadyForTransition = true; |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 118 | hideViews(mSharedElements); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 119 | if (getViewsTransition() != null && mTransitioningViews != null) { |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 120 | hideViews(mTransitioningViews); |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 121 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 122 | if (mIsReturning) { |
| 123 | sendSharedElementDestination(); |
| 124 | } else { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 125 | moveSharedElementsToOverlay(); |
| 126 | } |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 127 | if (mSharedElementsBundle != null) { |
| 128 | onTakeSharedElements(); |
| 129 | } |
George Mount | d265bd7 | 2014-06-02 07:22:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
George Mount | 82ffc14 | 2014-06-30 16:47:34 -0700 | [diff] [blame] | 132 | private void triggerViewsReady(final ArrayMap<String, View> sharedElements) { |
| 133 | if (mAreViewsReady) { |
| 134 | return; |
| 135 | } |
| 136 | mAreViewsReady = true; |
George Mount | 1fb941d | 2014-10-30 09:15:32 -0700 | [diff] [blame] | 137 | final ViewGroup decor = getDecor(); |
George Mount | 82ffc14 | 2014-06-30 16:47:34 -0700 | [diff] [blame] | 138 | // Ensure the views have been laid out before capturing the views -- we need the epicenter. |
George Mount | 1fb941d | 2014-10-30 09:15:32 -0700 | [diff] [blame] | 139 | if (decor == null || (decor.isAttachedToWindow() && |
| 140 | (sharedElements.isEmpty() || !sharedElements.valueAt(0).isLayoutRequested()))) { |
George Mount | 82ffc14 | 2014-06-30 16:47:34 -0700 | [diff] [blame] | 141 | viewsReady(sharedElements); |
| 142 | } else { |
George Mount | d7d0fb3 | 2016-01-04 14:56:08 -0800 | [diff] [blame] | 143 | mViewsReadyListener = new ViewTreeObserver.OnPreDrawListener() { |
| 144 | @Override |
| 145 | public boolean onPreDraw() { |
| 146 | mViewsReadyListener = null; |
| 147 | decor.getViewTreeObserver().removeOnPreDrawListener(this); |
| 148 | viewsReady(sharedElements); |
| 149 | return true; |
| 150 | } |
| 151 | }; |
| 152 | decor.getViewTreeObserver().addOnPreDrawListener(mViewsReadyListener); |
George Mount | 82ffc14 | 2014-06-30 16:47:34 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 156 | private ArrayMap<String, View> mapNamedElements(ArrayList<String> accepted, |
| 157 | ArrayList<String> localNames) { |
| 158 | ArrayMap<String, View> sharedElements = new ArrayMap<String, View>(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 159 | ViewGroup decorView = getDecor(); |
| 160 | if (decorView != null) { |
| 161 | decorView.findNamedViews(sharedElements); |
| 162 | } |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 163 | if (accepted != null) { |
| 164 | for (int i = 0; i < localNames.size(); i++) { |
| 165 | String localName = localNames.get(i); |
| 166 | String acceptedName = accepted.get(i); |
| 167 | if (localName != null && !localName.equals(acceptedName)) { |
| 168 | View view = sharedElements.remove(localName); |
| 169 | if (view != null) { |
| 170 | sharedElements.put(acceptedName, view); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return sharedElements; |
| 176 | } |
| 177 | |
George Mount | d265bd7 | 2014-06-02 07:22:59 -0700 | [diff] [blame] | 178 | private void sendSharedElementDestination() { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 179 | boolean allReady; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 180 | final View decorView = getDecor(); |
Dake Gu | 872efe4 | 2014-08-29 15:24:58 -0700 | [diff] [blame] | 181 | if (allowOverlappingTransitions() && getEnterViewsTransition() != null) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 182 | allReady = false; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 183 | } else if (decorView == null) { |
| 184 | allReady = true; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 185 | } else { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 186 | allReady = !decorView.isLayoutRequested(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 187 | if (allReady) { |
| 188 | for (int i = 0; i < mSharedElements.size(); i++) { |
| 189 | if (mSharedElements.get(i).isLayoutRequested()) { |
| 190 | allReady = false; |
| 191 | break; |
| 192 | } |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | if (allReady) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 197 | Bundle state = captureSharedElementState(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 198 | moveSharedElementsToOverlay(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 199 | mResultReceiver.send(MSG_SHARED_ELEMENT_DESTINATION, state); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 200 | } else if (decorView != null) { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 201 | decorView.getViewTreeObserver() |
George Mount | d265bd7 | 2014-06-02 07:22:59 -0700 | [diff] [blame] | 202 | .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { |
| 203 | @Override |
| 204 | public boolean onPreDraw() { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 205 | decorView.getViewTreeObserver().removeOnPreDrawListener(this); |
George Mount | f31d83c | 2014-08-29 13:32:20 -0700 | [diff] [blame] | 206 | if (mResultReceiver != null) { |
| 207 | Bundle state = captureSharedElementState(); |
George Mount | f31d83c | 2014-08-29 13:32:20 -0700 | [diff] [blame] | 208 | moveSharedElementsToOverlay(); |
| 209 | mResultReceiver.send(MSG_SHARED_ELEMENT_DESTINATION, state); |
| 210 | } |
George Mount | d265bd7 | 2014-06-02 07:22:59 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | }); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 214 | } |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 215 | if (allowOverlappingTransitions()) { |
| 216 | startEnterTransitionOnly(); |
| 217 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 220 | private static SharedElementCallback getListener(Activity activity, boolean isReturning) { |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 221 | return isReturning ? activity.mExitTransitionListener : activity.mEnterTransitionListener; |
| 222 | } |
| 223 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 224 | @Override |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 225 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 226 | switch (resultCode) { |
| 227 | case MSG_TAKE_SHARED_ELEMENTS: |
| 228 | if (!mIsCanceled) { |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 229 | mSharedElementsBundle = resultData; |
| 230 | onTakeSharedElements(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 231 | } |
| 232 | break; |
| 233 | case MSG_EXIT_TRANSITION_COMPLETE: |
| 234 | if (!mIsCanceled) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 235 | mIsExitTransitionComplete = true; |
| 236 | if (mSharedElementTransitionStarted) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 237 | onRemoteExitTransitionComplete(); |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | case MSG_CANCEL: |
| 242 | cancel(); |
| 243 | break; |
| 244 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
George Mount | d7d0fb3 | 2016-01-04 14:56:08 -0800 | [diff] [blame] | 247 | /** |
| 248 | * This is called onResume. If an Activity is resuming and the transitions |
| 249 | * haven't started yet, force the views to appear. This is likely to be |
| 250 | * caused by the top Activity finishing before the transitions started. |
| 251 | * In that case, we can finish any transition that was started, but we |
| 252 | * should cancel any pending transition and just bring those Views visible. |
| 253 | */ |
| 254 | public void forceViewsToAppear() { |
| 255 | if (!mIsReturning) { |
| 256 | return; |
| 257 | } |
| 258 | if (!mIsReadyForTransition) { |
| 259 | mIsReadyForTransition = true; |
| 260 | final ViewGroup decor = getDecor(); |
| 261 | if (decor != null && mViewsReadyListener != null) { |
| 262 | decor.getViewTreeObserver().removeOnPreDrawListener(mViewsReadyListener); |
| 263 | mViewsReadyListener = null; |
| 264 | } |
| 265 | showViews(mTransitioningViews, true); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 266 | setTransitioningViewsVisiblity(View.VISIBLE, true); |
George Mount | d7d0fb3 | 2016-01-04 14:56:08 -0800 | [diff] [blame] | 267 | mSharedElements.clear(); |
| 268 | mAllSharedElementNames.clear(); |
| 269 | mTransitioningViews.clear(); |
| 270 | mIsReadyForTransition = true; |
| 271 | viewsTransitionComplete(); |
| 272 | sharedElementTransitionComplete(); |
| 273 | } else { |
| 274 | if (!mSharedElementTransitionStarted) { |
| 275 | moveSharedElementsFromOverlay(); |
| 276 | mSharedElementTransitionStarted = true; |
| 277 | showViews(mSharedElements, true); |
| 278 | mSharedElements.clear(); |
| 279 | sharedElementTransitionComplete(); |
| 280 | } |
| 281 | if (!mIsViewsTransitionStarted) { |
| 282 | mIsViewsTransitionStarted = true; |
| 283 | showViews(mTransitioningViews, true); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 284 | setTransitioningViewsVisiblity(View.VISIBLE, true); |
George Mount | d7d0fb3 | 2016-01-04 14:56:08 -0800 | [diff] [blame] | 285 | mTransitioningViews.clear(); |
| 286 | viewsTransitionComplete(); |
| 287 | } |
| 288 | cancelPendingTransitions(); |
| 289 | } |
| 290 | mAreViewsReady = true; |
| 291 | } |
| 292 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 293 | private void cancel() { |
| 294 | if (!mIsCanceled) { |
| 295 | mIsCanceled = true; |
George Mount | 8d3cd2c | 2014-07-08 11:07:33 -0700 | [diff] [blame] | 296 | if (getViewsTransition() == null || mIsViewsTransitionStarted) { |
George Mount | ce2ee3d | 2014-09-09 15:04:31 -0700 | [diff] [blame] | 297 | showViews(mSharedElements, true); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 298 | } else if (mTransitioningViews != null) { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 299 | mTransitioningViews.addAll(mSharedElements); |
| 300 | } |
George Mount | a228d7e | 2015-11-17 12:49:22 -0800 | [diff] [blame] | 301 | moveSharedElementsFromOverlay(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 302 | mSharedElementNames.clear(); |
| 303 | mSharedElements.clear(); |
| 304 | mAllSharedElementNames.clear(); |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 305 | startSharedElementTransition(null); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 306 | onRemoteExitTransitionComplete(); |
| 307 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 310 | public boolean isReturning() { |
| 311 | return mIsReturning; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 314 | protected void prepareEnter() { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 315 | ViewGroup decorView = getDecor(); |
| 316 | if (mActivity == null || decorView == null) { |
| 317 | return; |
| 318 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 319 | mActivity.overridePendingTransition(0, 0); |
| 320 | if (!mIsReturning) { |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 321 | mWasOpaque = mActivity.convertToTranslucent(null, null); |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 322 | Drawable background = decorView.getBackground(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 323 | if (background != null) { |
| 324 | getWindow().setBackgroundDrawable(null); |
| 325 | background = background.mutate(); |
| 326 | background.setAlpha(0); |
| 327 | getWindow().setBackgroundDrawable(background); |
| 328 | } |
| 329 | } else { |
| 330 | mActivity = null; // all done with it now. |
| 331 | } |
| 332 | } |
| 333 | |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 334 | @Override |
| 335 | protected Transition getViewsTransition() { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 336 | Window window = getWindow(); |
| 337 | if (window == null) { |
| 338 | return null; |
| 339 | } |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 340 | if (mIsReturning) { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 341 | return window.getReenterTransition(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 342 | } else { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 343 | return window.getEnterTransition(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | protected Transition getSharedElementTransition() { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 348 | Window window = getWindow(); |
| 349 | if (window == null) { |
| 350 | return null; |
| 351 | } |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 352 | if (mIsReturning) { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 353 | return window.getSharedElementReenterTransition(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 354 | } else { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 355 | return window.getSharedElementEnterTransition(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 359 | private void startSharedElementTransition(Bundle sharedElementState) { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 360 | ViewGroup decorView = getDecor(); |
| 361 | if (decorView == null) { |
| 362 | return; |
| 363 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 364 | // Remove rejected shared elements |
| 365 | ArrayList<String> rejectedNames = new ArrayList<String>(mAllSharedElementNames); |
| 366 | rejectedNames.removeAll(mSharedElementNames); |
| 367 | ArrayList<View> rejectedSnapshots = createSnapshots(sharedElementState, rejectedNames); |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 368 | if (mListener != null) { |
| 369 | mListener.onRejectSharedElements(rejectedSnapshots); |
| 370 | } |
George Mount | 83c692e | 2014-10-23 11:11:17 -0700 | [diff] [blame] | 371 | removeNullViews(rejectedSnapshots); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 372 | startRejectedAnimations(rejectedSnapshots); |
| 373 | |
| 374 | // Now start shared element transition |
| 375 | ArrayList<View> sharedElementSnapshots = createSnapshots(sharedElementState, |
| 376 | mSharedElementNames); |
George Mount | ce2ee3d | 2014-09-09 15:04:31 -0700 | [diff] [blame] | 377 | showViews(mSharedElements, true); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 378 | scheduleSetSharedElementEnd(sharedElementSnapshots); |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 379 | ArrayList<SharedElementOriginalState> originalImageViewState = |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 380 | setSharedElementState(sharedElementState, sharedElementSnapshots); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 381 | requestLayoutForSharedElements(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 382 | |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 383 | boolean startEnterTransition = allowOverlappingTransitions() && !mIsReturning; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 384 | boolean startSharedElementTransition = true; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 385 | setGhostVisibility(View.INVISIBLE); |
| 386 | scheduleGhostVisibilityChange(View.INVISIBLE); |
George Mount | 80141d1 | 2015-07-14 10:03:06 -0700 | [diff] [blame] | 387 | pauseInput(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 388 | Transition transition = beginTransition(decorView, startEnterTransition, |
| 389 | startSharedElementTransition); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 390 | scheduleGhostVisibilityChange(View.VISIBLE); |
| 391 | setGhostVisibility(View.VISIBLE); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 392 | |
| 393 | if (startEnterTransition) { |
| 394 | startEnterTransition(transition); |
| 395 | } |
| 396 | |
Dake Gu | c18f4cc | 2014-07-11 17:48:37 -0700 | [diff] [blame] | 397 | setOriginalSharedElementState(mSharedElements, originalImageViewState); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 398 | |
| 399 | if (mResultReceiver != null) { |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 400 | // We can't trust that the view will disappear on the same frame that the shared |
| 401 | // element appears here. Assure that we get at least 2 frames for double-buffering. |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 402 | decorView.postOnAnimation(new Runnable() { |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 403 | int mAnimations; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 404 | |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 405 | @Override |
| 406 | public void run() { |
| 407 | if (mAnimations++ < MIN_ANIMATION_FRAMES) { |
George Mount | 4188d0d | 2014-09-11 17:47:11 -0700 | [diff] [blame] | 408 | View decorView = getDecor(); |
| 409 | if (decorView != null) { |
| 410 | decorView.postOnAnimation(this); |
| 411 | } |
George Mount | 407d1fa | 2014-08-28 16:11:49 -0700 | [diff] [blame] | 412 | } else if (mResultReceiver != null) { |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 413 | mResultReceiver.send(MSG_HIDE_SHARED_ELEMENTS, null); |
| 414 | mResultReceiver = null; // all done sending messages. |
| 415 | } |
| 416 | } |
| 417 | }); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 418 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 419 | } |
| 420 | |
George Mount | 83c692e | 2014-10-23 11:11:17 -0700 | [diff] [blame] | 421 | private static void removeNullViews(ArrayList<View> views) { |
| 422 | if (views != null) { |
| 423 | for (int i = views.size() - 1; i >= 0; i--) { |
| 424 | if (views.get(i) == null) { |
| 425 | views.remove(i); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 431 | private void onTakeSharedElements() { |
| 432 | if (!mIsReadyForTransition || mSharedElementsBundle == null) { |
| 433 | return; |
| 434 | } |
| 435 | final Bundle sharedElementState = mSharedElementsBundle; |
| 436 | mSharedElementsBundle = null; |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 437 | OnSharedElementsReadyListener listener = new OnSharedElementsReadyListener() { |
| 438 | @Override |
| 439 | public void onSharedElementsReady() { |
| 440 | final View decorView = getDecor(); |
| 441 | if (decorView != null) { |
| 442 | decorView.getViewTreeObserver() |
| 443 | .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 444 | @Override |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 445 | public boolean onPreDraw() { |
| 446 | decorView.getViewTreeObserver().removeOnPreDrawListener(this); |
| 447 | startTransition(new Runnable() { |
| 448 | @Override |
| 449 | public void run() { |
| 450 | startSharedElementTransition(sharedElementState); |
| 451 | } |
| 452 | }); |
| 453 | return false; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 454 | } |
| 455 | }); |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 456 | decorView.invalidate(); |
| 457 | } |
| 458 | } |
| 459 | }; |
| 460 | if (mListener == null) { |
| 461 | listener.onSharedElementsReady(); |
| 462 | } else { |
| 463 | mListener.onSharedElementsArrived(mSharedElementNames, mSharedElements, listener); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 464 | } |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 465 | } |
| 466 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 467 | private void requestLayoutForSharedElements() { |
| 468 | int numSharedElements = mSharedElements.size(); |
| 469 | for (int i = 0; i < numSharedElements; i++) { |
| 470 | mSharedElements.get(i).requestLayout(); |
| 471 | } |
| 472 | } |
| 473 | |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 474 | private Transition beginTransition(ViewGroup decorView, boolean startEnterTransition, |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 475 | boolean startSharedElementTransition) { |
| 476 | Transition sharedElementTransition = null; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 477 | if (startSharedElementTransition) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 478 | if (!mSharedElementNames.isEmpty()) { |
| 479 | sharedElementTransition = configureTransition(getSharedElementTransition(), false); |
| 480 | } |
| 481 | if (sharedElementTransition == null) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 482 | sharedElementTransitionStarted(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 483 | sharedElementTransitionComplete(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 484 | } else { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 485 | sharedElementTransition.addListener(new Transition.TransitionListenerAdapter() { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 486 | @Override |
| 487 | public void onTransitionStart(Transition transition) { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 488 | sharedElementTransitionStarted(); |
| 489 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 490 | |
| 491 | @Override |
| 492 | public void onTransitionEnd(Transition transition) { |
| 493 | transition.removeListener(this); |
| 494 | sharedElementTransitionComplete(); |
| 495 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 496 | }); |
| 497 | } |
| 498 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 499 | Transition viewsTransition = null; |
| 500 | if (startEnterTransition) { |
| 501 | mIsViewsTransitionStarted = true; |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 502 | if (mTransitioningViews != null && !mTransitioningViews.isEmpty()) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 503 | viewsTransition = configureTransition(getViewsTransition(), true); |
| 504 | if (viewsTransition != null && !mIsReturning) { |
| 505 | stripOffscreenViews(); |
| 506 | } |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 507 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 508 | if (viewsTransition == null) { |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 509 | viewsTransitionComplete(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 510 | } else { |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 511 | final ArrayList<View> transitioningViews = mTransitioningViews; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 512 | viewsTransition.addListener(new ContinueTransitionListener() { |
| 513 | @Override |
George Mount | a2bbbb3 | 2014-08-12 10:16:20 -0700 | [diff] [blame] | 514 | public void onTransitionStart(Transition transition) { |
| 515 | mEnterViewsTransition = transition; |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 516 | if (transitioningViews != null) { |
| 517 | showViews(transitioningViews, false); |
| 518 | } |
George Mount | a2bbbb3 | 2014-08-12 10:16:20 -0700 | [diff] [blame] | 519 | super.onTransitionStart(transition); |
| 520 | } |
| 521 | |
| 522 | @Override |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 523 | public void onTransitionEnd(Transition transition) { |
George Mount | a2bbbb3 | 2014-08-12 10:16:20 -0700 | [diff] [blame] | 524 | mEnterViewsTransition = null; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 525 | transition.removeListener(this); |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 526 | viewsTransitionComplete(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 527 | super.onTransitionEnd(transition); |
| 528 | } |
| 529 | }); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | Transition transition = mergeTransitions(sharedElementTransition, viewsTransition); |
| 534 | if (transition != null) { |
| 535 | transition.addListener(new ContinueTransitionListener()); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 536 | if (startEnterTransition) { |
| 537 | setTransitioningViewsVisiblity(View.INVISIBLE, false); |
| 538 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 539 | TransitionManager.beginDelayedTransition(decorView, transition); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 540 | if (startSharedElementTransition && !mSharedElementNames.isEmpty()) { |
| 541 | mSharedElements.get(0).invalidate(); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 542 | } |
| 543 | if (startEnterTransition) { |
| 544 | setTransitioningViewsVisiblity(View.VISIBLE, true); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 545 | } |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 546 | } else { |
| 547 | transitionStarted(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 548 | } |
| 549 | return transition; |
| 550 | } |
| 551 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 552 | @Override |
| 553 | protected void onTransitionsComplete() { |
| 554 | moveSharedElementsFromOverlay(); |
George Mount | 10d312b | 2015-12-16 14:00:35 -0800 | [diff] [blame] | 555 | final ViewGroup decorView = getDecor(); |
| 556 | if (decorView != null) { |
| 557 | decorView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
| 558 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 559 | } |
| 560 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 561 | private void sharedElementTransitionStarted() { |
| 562 | mSharedElementTransitionStarted = true; |
| 563 | if (mIsExitTransitionComplete) { |
| 564 | send(MSG_EXIT_TRANSITION_COMPLETE, null); |
| 565 | } |
| 566 | } |
| 567 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 568 | private void startEnterTransition(Transition transition) { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 569 | ViewGroup decorView = getDecor(); |
| 570 | if (!mIsReturning && decorView != null) { |
| 571 | Drawable background = decorView.getBackground(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 572 | if (background != null) { |
| 573 | background = background.mutate(); |
George Mount | 99c82fd | 2014-09-03 07:27:47 -0700 | [diff] [blame] | 574 | getWindow().setBackgroundDrawable(background); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 575 | mBackgroundAnimator = ObjectAnimator.ofInt(background, "alpha", 255); |
George Mount | ed1e01d | 2014-06-05 13:49:12 -0700 | [diff] [blame] | 576 | mBackgroundAnimator.setDuration(getFadeDuration()); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 577 | mBackgroundAnimator.addListener(new AnimatorListenerAdapter() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 578 | @Override |
| 579 | public void onAnimationEnd(Animator animation) { |
| 580 | makeOpaque(); |
| 581 | } |
| 582 | }); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 583 | mBackgroundAnimator.start(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 584 | } else if (transition != null) { |
| 585 | transition.addListener(new Transition.TransitionListenerAdapter() { |
| 586 | @Override |
| 587 | public void onTransitionEnd(Transition transition) { |
| 588 | transition.removeListener(this); |
| 589 | makeOpaque(); |
| 590 | } |
| 591 | }); |
| 592 | } else { |
| 593 | makeOpaque(); |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | public void stop() { |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 599 | // Restore the background to its previous state since the |
| 600 | // Activity is stopping. |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 601 | if (mBackgroundAnimator != null) { |
| 602 | mBackgroundAnimator.end(); |
| 603 | mBackgroundAnimator = null; |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 604 | } else if (mWasOpaque) { |
| 605 | ViewGroup decorView = getDecor(); |
| 606 | if (decorView != null) { |
| 607 | Drawable drawable = decorView.getBackground(); |
| 608 | if (drawable != null) { |
| 609 | drawable.setAlpha(1); |
| 610 | } |
| 611 | } |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 612 | } |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 613 | makeOpaque(); |
| 614 | mIsCanceled = true; |
| 615 | mResultReceiver = null; |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 616 | mActivity = null; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 617 | moveSharedElementsFromOverlay(); |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 618 | if (mTransitioningViews != null) { |
| 619 | showViews(mTransitioningViews, true); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 620 | setTransitioningViewsVisiblity(View.VISIBLE, true); |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 621 | } |
| 622 | showViews(mSharedElements, true); |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 623 | clearState(); |
| 624 | } |
| 625 | |
George Mount | fbd4596 | 2015-01-26 14:38:19 -0800 | [diff] [blame] | 626 | /** |
| 627 | * Cancels the enter transition. |
| 628 | * @return True if the enter transition is still pending capturing the target state. If so, |
| 629 | * any transition started on the decor will do nothing. |
| 630 | */ |
| 631 | public boolean cancelEnter() { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 632 | setGhostVisibility(View.INVISIBLE); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 633 | mHasStopped = true; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 634 | mIsCanceled = true; |
George Mount | 3bbc2ed | 2015-11-18 11:10:55 -0800 | [diff] [blame] | 635 | clearState(); |
| 636 | return super.cancelPendingTransitions(); |
| 637 | } |
| 638 | |
| 639 | @Override |
| 640 | protected void clearState() { |
| 641 | mSharedElementsBundle = null; |
| 642 | mEnterViewsTransition = null; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 643 | mResultReceiver = null; |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 644 | if (mBackgroundAnimator != null) { |
| 645 | mBackgroundAnimator.cancel(); |
| 646 | mBackgroundAnimator = null; |
| 647 | } |
George Mount | 3bbc2ed | 2015-11-18 11:10:55 -0800 | [diff] [blame] | 648 | super.clearState(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | private void makeOpaque() { |
George Mount | 8c2614c | 2014-06-10 11:12:01 -0700 | [diff] [blame] | 652 | if (!mHasStopped && mActivity != null) { |
George Mount | 3cc716c | 2014-06-12 16:35:35 -0700 | [diff] [blame] | 653 | if (mWasOpaque) { |
| 654 | mActivity.convertFromTranslucent(); |
| 655 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 656 | mActivity = null; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | private boolean allowOverlappingTransitions() { |
George Mount | 5aec62e | 2015-05-08 14:21:20 -0700 | [diff] [blame] | 661 | return mIsReturning ? getWindow().getAllowReturnTransitionOverlap() |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 662 | : getWindow().getAllowEnterTransitionOverlap(); |
| 663 | } |
| 664 | |
| 665 | private void startRejectedAnimations(final ArrayList<View> rejectedSnapshots) { |
| 666 | if (rejectedSnapshots == null || rejectedSnapshots.isEmpty()) { |
| 667 | return; |
| 668 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 669 | final ViewGroup decorView = getDecor(); |
| 670 | if (decorView != null) { |
| 671 | ViewGroupOverlay overlay = decorView.getOverlay(); |
| 672 | ObjectAnimator animator = null; |
| 673 | int numRejected = rejectedSnapshots.size(); |
| 674 | for (int i = 0; i < numRejected; i++) { |
| 675 | View snapshot = rejectedSnapshots.get(i); |
| 676 | overlay.add(snapshot); |
| 677 | animator = ObjectAnimator.ofFloat(snapshot, View.ALPHA, 1, 0); |
| 678 | animator.start(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 679 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 680 | animator.addListener(new AnimatorListenerAdapter() { |
| 681 | @Override |
| 682 | public void onAnimationEnd(Animator animation) { |
| 683 | ViewGroupOverlay overlay = decorView.getOverlay(); |
| 684 | int numRejected = rejectedSnapshots.size(); |
| 685 | for (int i = 0; i < numRejected; i++) { |
| 686 | overlay.remove(rejectedSnapshots.get(i)); |
| 687 | } |
| 688 | } |
| 689 | }); |
| 690 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | protected void onRemoteExitTransitionComplete() { |
| 694 | if (!allowOverlappingTransitions()) { |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 695 | startEnterTransitionOnly(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 698 | |
| 699 | private void startEnterTransitionOnly() { |
| 700 | startTransition(new Runnable() { |
| 701 | @Override |
| 702 | public void run() { |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 703 | boolean startEnterTransition = true; |
| 704 | boolean startSharedElementTransition = false; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 705 | ViewGroup decorView = getDecor(); |
| 706 | if (decorView != null) { |
| 707 | Transition transition = beginTransition(decorView, startEnterTransition, |
| 708 | startSharedElementTransition); |
| 709 | startEnterTransition(transition); |
| 710 | } |
George Mount | 13ccb79 | 2014-06-06 17:02:20 -0700 | [diff] [blame] | 711 | } |
| 712 | }); |
| 713 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 714 | } |