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