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 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 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 | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Craig Mautner | 85b764e | 2014-08-27 13:45:10 -0700 | [diff] [blame] | 23 | import android.graphics.Color; |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 24 | import android.graphics.Matrix; |
| 25 | import android.graphics.RectF; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.ColorDrawable; |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
George Mount | 2c256a0 | 2015-07-15 16:30:20 -0700 | [diff] [blame] | 28 | import android.os.Build.VERSION_CODES; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 32 | import android.os.ResultReceiver; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 33 | import android.transition.Transition; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 34 | import android.transition.TransitionManager; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 35 | import android.view.View; |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 36 | import android.view.ViewGroup; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 37 | import android.view.ViewTreeObserver; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | |
| 41 | /** |
| 42 | * This ActivityTransitionCoordinator is created in ActivityOptions#makeSceneTransitionAnimation |
| 43 | * to govern the exit of the Scene and the shared elements when calling an Activity as well as |
| 44 | * the reentry of the Scene when coming back from the called Activity. |
| 45 | */ |
| 46 | class ExitTransitionCoordinator extends ActivityTransitionCoordinator { |
| 47 | private static final String TAG = "ExitTransitionCoordinator"; |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 48 | private static final long MAX_WAIT_MS = 1000; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 49 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 50 | private Bundle mSharedElementBundle; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 51 | private boolean mExitNotified; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 52 | private boolean mSharedElementNotified; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 53 | private Activity mActivity; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 54 | private boolean mIsBackgroundReady; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 55 | private boolean mIsCanceled; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 56 | private Handler mHandler; |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 57 | private ObjectAnimator mBackgroundAnimator; |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 58 | private boolean mIsHidden; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 59 | private Bundle mExitSharedElementBundle; |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 60 | private boolean mIsExitStarted; |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 61 | private boolean mSharedElementsHidden; |
| 62 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 63 | public ExitTransitionCoordinator(Activity activity, ArrayList<String> names, |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 64 | ArrayList<String> accepted, ArrayList<View> mapped, boolean isReturning) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 65 | super(activity.getWindow(), names, getListener(activity, isReturning), isReturning); |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 66 | viewsReady(mapSharedElements(accepted, mapped)); |
George Mount | 60625b0 | 2014-06-24 07:46:23 -0700 | [diff] [blame] | 67 | stripOffscreenViews(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 68 | mIsBackgroundReady = !isReturning; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 69 | mActivity = activity; |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
George Mount | 6558056 | 2014-08-29 08:15:48 -0700 | [diff] [blame] | 72 | private static SharedElementCallback getListener(Activity activity, boolean isReturning) { |
George Mount | d80154f | 2014-05-21 08:40:03 -0700 | [diff] [blame] | 73 | return isReturning ? activity.mEnterTransitionListener : activity.mExitTransitionListener; |
George Mount | 800d72b | 2014-05-19 07:09:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 76 | @Override |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 77 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 78 | switch (resultCode) { |
| 79 | case MSG_SET_REMOTE_RECEIVER: |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 80 | stopCancel(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 81 | mResultReceiver = resultData.getParcelable(KEY_REMOTE_RECEIVER); |
| 82 | if (mIsCanceled) { |
| 83 | mResultReceiver.send(MSG_CANCEL, null); |
| 84 | mResultReceiver = null; |
| 85 | } else { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 86 | notifyComplete(); |
| 87 | } |
| 88 | break; |
| 89 | case MSG_HIDE_SHARED_ELEMENTS: |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 90 | stopCancel(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 91 | if (!mIsCanceled) { |
| 92 | hideSharedElements(); |
| 93 | } |
| 94 | break; |
| 95 | case MSG_START_EXIT_TRANSITION: |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 96 | mHandler.removeMessages(MSG_CANCEL); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 97 | startExit(); |
| 98 | break; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 99 | case MSG_SHARED_ELEMENT_DESTINATION: |
| 100 | mExitSharedElementBundle = resultData; |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 101 | sharedElementExitBack(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 106 | private void stopCancel() { |
| 107 | if (mHandler != null) { |
| 108 | mHandler.removeMessages(MSG_CANCEL); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private void delayCancel() { |
| 113 | if (mHandler != null) { |
| 114 | mHandler.sendEmptyMessageDelayed(MSG_CANCEL, MAX_WAIT_MS); |
| 115 | } |
| 116 | } |
| 117 | |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 118 | public void resetViews() { |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 119 | if (mTransitioningViews != null) { |
| 120 | showViews(mTransitioningViews, true); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 121 | setTransitioningViewsVisiblity(View.VISIBLE, true); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 122 | } |
George Mount | ce2ee3d | 2014-09-09 15:04:31 -0700 | [diff] [blame] | 123 | showViews(mSharedElements, true); |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 124 | mIsHidden = true; |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 125 | ViewGroup decorView = getDecor(); |
| 126 | if (!mIsReturning && decorView != null) { |
| 127 | decorView.suppressLayout(false); |
Dake Gu | f7ff220 | 2014-07-25 14:00:17 -0700 | [diff] [blame] | 128 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 129 | moveSharedElementsFromOverlay(); |
George Mount | 8d3cd2c | 2014-07-08 11:07:33 -0700 | [diff] [blame] | 130 | clearState(); |
George Mount | 1fecfb2 | 2014-06-18 14:55:55 -0700 | [diff] [blame] | 131 | } |
| 132 | |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 133 | private void sharedElementExitBack() { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 134 | final ViewGroup decorView = getDecor(); |
| 135 | if (decorView != null) { |
| 136 | decorView.suppressLayout(true); |
Dake Gu | f7ff220 | 2014-07-25 14:00:17 -0700 | [diff] [blame] | 137 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 138 | if (decorView != null && mExitSharedElementBundle != null && |
| 139 | !mExitSharedElementBundle.isEmpty() && |
Dake Gu | 872efe4 | 2014-08-29 15:24:58 -0700 | [diff] [blame] | 140 | !mSharedElements.isEmpty() && getSharedElementTransition() != null) { |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 141 | startTransition(new Runnable() { |
| 142 | public void run() { |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 143 | startSharedElementExit(decorView); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 144 | } |
| 145 | }); |
George Mount | 00dde0b | 2014-07-01 15:27:07 -0700 | [diff] [blame] | 146 | } else { |
| 147 | sharedElementTransitionComplete(); |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 151 | private void startSharedElementExit(final ViewGroup decorView) { |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 152 | Transition transition = getSharedElementExitTransition(); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 153 | transition.addListener(new Transition.TransitionListenerAdapter() { |
| 154 | @Override |
| 155 | public void onTransitionEnd(Transition transition) { |
| 156 | transition.removeListener(this); |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 157 | if (isViewsTransitionComplete()) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 158 | delayCancel(); |
| 159 | } |
| 160 | } |
| 161 | }); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 162 | final ArrayList<View> sharedElementSnapshots = createSnapshots(mExitSharedElementBundle, |
| 163 | mSharedElementNames); |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 164 | decorView.getViewTreeObserver() |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 165 | .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { |
| 166 | @Override |
| 167 | public boolean onPreDraw() { |
George Mount | 6e7fb60 | 2014-09-04 16:20:20 -0700 | [diff] [blame] | 168 | decorView.getViewTreeObserver().removeOnPreDrawListener(this); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 169 | setSharedElementState(mExitSharedElementBundle, sharedElementSnapshots); |
| 170 | return true; |
| 171 | } |
| 172 | }); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 173 | setGhostVisibility(View.INVISIBLE); |
| 174 | scheduleGhostVisibilityChange(View.INVISIBLE); |
George Mount | 1732f52 | 2014-09-17 16:59:36 -0700 | [diff] [blame] | 175 | if (mListener != null) { |
| 176 | mListener.onSharedElementEnd(mSharedElementNames, mSharedElements, |
| 177 | sharedElementSnapshots); |
| 178 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 179 | TransitionManager.beginDelayedTransition(decorView, transition); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 180 | scheduleGhostVisibilityChange(View.VISIBLE); |
| 181 | setGhostVisibility(View.VISIBLE); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 182 | decorView.invalidate(); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 183 | } |
| 184 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 185 | private void hideSharedElements() { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 186 | moveSharedElementsFromOverlay(); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 187 | if (!mIsHidden) { |
George Mount | 0f0c473 | 2014-09-05 13:47:47 -0700 | [diff] [blame] | 188 | hideViews(mSharedElements); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 189 | } |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 190 | mSharedElementsHidden = true; |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 191 | finishIfNecessary(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 192 | } |
| 193 | |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 194 | public void startExit() { |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 195 | if (!mIsExitStarted) { |
| 196 | mIsExitStarted = true; |
George Mount | 80141d1 | 2015-07-14 10:03:06 -0700 | [diff] [blame] | 197 | pauseInput(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 198 | ViewGroup decorView = getDecor(); |
| 199 | if (decorView != null) { |
| 200 | decorView.suppressLayout(true); |
Dake Gu | f7ff220 | 2014-07-25 14:00:17 -0700 | [diff] [blame] | 201 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 202 | moveSharedElementsToOverlay(); |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 203 | startTransition(new Runnable() { |
| 204 | @Override |
| 205 | public void run() { |
| 206 | beginTransitions(); |
| 207 | } |
| 208 | }); |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 209 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | public void startExit(int resultCode, Intent data) { |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 213 | if (!mIsExitStarted) { |
| 214 | mIsExitStarted = true; |
George Mount | 80141d1 | 2015-07-14 10:03:06 -0700 | [diff] [blame] | 215 | pauseInput(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 216 | ViewGroup decorView = getDecor(); |
| 217 | if (decorView != null) { |
| 218 | decorView.suppressLayout(true); |
Dake Gu | f7ff220 | 2014-07-25 14:00:17 -0700 | [diff] [blame] | 219 | } |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 220 | mHandler = new Handler() { |
| 221 | @Override |
| 222 | public void handleMessage(Message msg) { |
| 223 | mIsCanceled = true; |
| 224 | finish(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 225 | } |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 226 | }; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 227 | delayCancel(); |
| 228 | moveSharedElementsToOverlay(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 229 | if (decorView != null && decorView.getBackground() == null) { |
Craig Mautner | 85b764e | 2014-08-27 13:45:10 -0700 | [diff] [blame] | 230 | getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK)); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 231 | } |
George Mount | 2c256a0 | 2015-07-15 16:30:20 -0700 | [diff] [blame] | 232 | final boolean targetsM = decorView == null || decorView.getContext() |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 233 | .getApplicationInfo().targetSdkVersion >= VERSION_CODES.M; |
George Mount | 2c256a0 | 2015-07-15 16:30:20 -0700 | [diff] [blame] | 234 | ArrayList<String> sharedElementNames = targetsM ? mSharedElementNames : |
| 235 | mAllSharedElementNames; |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 236 | ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(mActivity, this, |
George Mount | 2c256a0 | 2015-07-15 16:30:20 -0700 | [diff] [blame] | 237 | sharedElementNames, resultCode, data); |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 238 | mActivity.convertToTranslucent(new Activity.TranslucentConversionListener() { |
| 239 | @Override |
| 240 | public void onTranslucentConversionComplete(boolean drawComplete) { |
| 241 | if (!mIsCanceled) { |
| 242 | fadeOutBackground(); |
| 243 | } |
| 244 | } |
| 245 | }, options); |
| 246 | startTransition(new Runnable() { |
| 247 | @Override |
| 248 | public void run() { |
| 249 | startExitTransition(); |
| 250 | } |
| 251 | }); |
| 252 | } |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 253 | } |
| 254 | |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 255 | public void stop() { |
| 256 | if (mIsReturning && mActivity != null) { |
| 257 | // Override the previous ActivityOptions. We don't want the |
| 258 | // activity to have options since we're essentially canceling the |
| 259 | // transition and finishing right now. |
| 260 | mActivity.convertToTranslucent(null, null); |
| 261 | finish(); |
| 262 | } |
| 263 | } |
| 264 | |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 265 | private void startExitTransition() { |
George Mount | f451d8a | 2014-06-23 11:09:32 -0700 | [diff] [blame] | 266 | Transition transition = getExitTransition(); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 267 | ViewGroup decorView = getDecor(); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 268 | if (transition != null && decorView != null && mTransitioningViews != null) { |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 269 | setTransitioningViewsVisiblity(View.VISIBLE, false); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 270 | TransitionManager.beginDelayedTransition(decorView, transition); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 271 | setTransitioningViewsVisiblity(View.INVISIBLE, true); |
George Mount | b4c5d7b | 2014-07-24 13:19:51 -0700 | [diff] [blame] | 272 | } else { |
| 273 | transitionStarted(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 274 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | private void fadeOutBackground() { |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 278 | if (mBackgroundAnimator == null) { |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 279 | ViewGroup decor = getDecor(); |
| 280 | Drawable background; |
| 281 | if (decor != null && (background = decor.getBackground()) != null) { |
George Mount | 99c82fd | 2014-09-03 07:27:47 -0700 | [diff] [blame] | 282 | background = background.mutate(); |
| 283 | getWindow().setBackgroundDrawable(background); |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 284 | mBackgroundAnimator = ObjectAnimator.ofInt(background, "alpha", 0); |
| 285 | mBackgroundAnimator.addListener(new AnimatorListenerAdapter() { |
| 286 | @Override |
| 287 | public void onAnimationEnd(Animator animation) { |
| 288 | mBackgroundAnimator = null; |
| 289 | if (!mIsCanceled) { |
| 290 | mIsBackgroundReady = true; |
| 291 | notifyComplete(); |
| 292 | } |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 293 | } |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 294 | }); |
| 295 | mBackgroundAnimator.setDuration(getFadeDuration()); |
| 296 | mBackgroundAnimator.start(); |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 297 | } else { |
| 298 | mIsBackgroundReady = true; |
George Mount | 700db2a | 2014-07-07 17:17:49 -0700 | [diff] [blame] | 299 | } |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 300 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 301 | } |
| 302 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 303 | private Transition getExitTransition() { |
| 304 | Transition viewsTransition = null; |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 305 | if (mTransitioningViews != null && !mTransitioningViews.isEmpty()) { |
George Mount | 8881502 | 2014-06-25 14:33:54 -0700 | [diff] [blame] | 306 | viewsTransition = configureTransition(getViewsTransition(), true); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 307 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 308 | if (viewsTransition == null) { |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 309 | viewsTransitionComplete(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 310 | } else { |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 311 | final ArrayList<View> transitioningViews = mTransitioningViews; |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 312 | viewsTransition.addListener(new ContinueTransitionListener() { |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 313 | @Override |
| 314 | public void onTransitionEnd(Transition transition) { |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 315 | transition.removeListener(this); |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 316 | viewsTransitionComplete(); |
George Mount | b694e08 | 2014-09-12 07:34:52 -0700 | [diff] [blame] | 317 | if (mIsHidden && transitioningViews != null) { |
| 318 | showViews(transitioningViews, true); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 319 | setTransitioningViewsVisiblity(View.VISIBLE, true); |
George Mount | 8cab50a | 2014-05-15 09:57:17 -0700 | [diff] [blame] | 320 | } |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 321 | if (mSharedElementBundle != null) { |
| 322 | delayCancel(); |
| 323 | } |
| 324 | super.onTransitionEnd(transition); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 325 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 326 | }); |
| 327 | } |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 328 | return viewsTransition; |
| 329 | } |
| 330 | |
| 331 | private Transition getSharedElementExitTransition() { |
| 332 | Transition sharedElementTransition = null; |
| 333 | if (!mSharedElements.isEmpty()) { |
George Mount | 8881502 | 2014-06-25 14:33:54 -0700 | [diff] [blame] | 334 | sharedElementTransition = configureTransition(getSharedElementTransition(), false); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 335 | } |
| 336 | if (sharedElementTransition == null) { |
| 337 | sharedElementTransitionComplete(); |
| 338 | } else { |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 339 | sharedElementTransition.addListener(new ContinueTransitionListener() { |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 340 | @Override |
| 341 | public void onTransitionEnd(Transition transition) { |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 342 | transition.removeListener(this); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 343 | sharedElementTransitionComplete(); |
| 344 | if (mIsHidden) { |
George Mount | ce2ee3d | 2014-09-09 15:04:31 -0700 | [diff] [blame] | 345 | showViews(mSharedElements, true); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | }); |
| 349 | mSharedElements.get(0).invalidate(); |
| 350 | } |
| 351 | return sharedElementTransition; |
| 352 | } |
| 353 | |
| 354 | private void beginTransitions() { |
| 355 | Transition sharedElementTransition = getSharedElementExitTransition(); |
| 356 | Transition viewsTransition = getExitTransition(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 357 | |
| 358 | Transition transition = mergeTransitions(sharedElementTransition, viewsTransition); |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 359 | ViewGroup decorView = getDecor(); |
| 360 | if (transition != null && decorView != null) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 361 | setGhostVisibility(View.INVISIBLE); |
| 362 | scheduleGhostVisibilityChange(View.INVISIBLE); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 363 | if (viewsTransition != null) { |
| 364 | setTransitioningViewsVisiblity(View.VISIBLE, false); |
| 365 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 366 | TransitionManager.beginDelayedTransition(decorView, transition); |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 367 | scheduleGhostVisibilityChange(View.VISIBLE); |
| 368 | setGhostVisibility(View.VISIBLE); |
George Mount | 6297672 | 2016-02-04 16:45:53 -0800 | [diff] [blame] | 369 | if (viewsTransition != null) { |
| 370 | setTransitioningViewsVisiblity(View.INVISIBLE, true); |
| 371 | } |
George Mount | 48bd13c | 2014-09-12 10:54:54 -0700 | [diff] [blame] | 372 | decorView.invalidate(); |
George Mount | b4c5d7b | 2014-07-24 13:19:51 -0700 | [diff] [blame] | 373 | } else { |
| 374 | transitionStarted(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 375 | } |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 376 | } |
| 377 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 378 | protected boolean isReadyToNotify() { |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 379 | return mSharedElementBundle != null && mResultReceiver != null && mIsBackgroundReady; |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 380 | } |
| 381 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 382 | @Override |
| 383 | protected void sharedElementTransitionComplete() { |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 384 | mSharedElementBundle = mExitSharedElementBundle == null |
| 385 | ? captureSharedElementState() : captureExitSharedElementsState(); |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 386 | super.sharedElementTransitionComplete(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 387 | } |
| 388 | |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 389 | private Bundle captureExitSharedElementsState() { |
| 390 | Bundle bundle = new Bundle(); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 391 | RectF bounds = new RectF(); |
| 392 | Matrix matrix = new Matrix(); |
George Mount | 01e9a97 | 2014-07-09 15:39:23 -0700 | [diff] [blame] | 393 | for (int i = 0; i < mSharedElements.size(); i++) { |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 394 | String name = mSharedElementNames.get(i); |
| 395 | Bundle sharedElementState = mExitSharedElementBundle.getBundle(name); |
| 396 | if (sharedElementState != null) { |
| 397 | bundle.putBundle(name, sharedElementState); |
| 398 | } else { |
| 399 | View view = mSharedElements.get(i); |
Dake Gu | 7bf379c | 2014-07-15 16:29:38 -0700 | [diff] [blame] | 400 | captureSharedElementState(view, name, bundle, matrix, bounds); |
George Mount | 67d9243 | 2014-06-06 13:34:20 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | return bundle; |
| 404 | } |
| 405 | |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 406 | @Override |
| 407 | protected void onTransitionsComplete() { |
| 408 | notifyComplete(); |
| 409 | } |
| 410 | |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 411 | protected void notifyComplete() { |
| 412 | if (isReadyToNotify()) { |
| 413 | if (!mSharedElementNotified) { |
| 414 | mSharedElementNotified = true; |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 415 | delayCancel(); |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 416 | if (mListener == null) { |
| 417 | mResultReceiver.send(MSG_TAKE_SHARED_ELEMENTS, mSharedElementBundle); |
| 418 | notifyExitComplete(); |
| 419 | } else { |
| 420 | final ResultReceiver resultReceiver = mResultReceiver; |
| 421 | final Bundle sharedElementBundle = mSharedElementBundle; |
| 422 | mListener.onSharedElementsArrived(mSharedElementNames, mSharedElements, |
| 423 | new OnSharedElementsReadyListener() { |
| 424 | @Override |
| 425 | public void onSharedElementsReady() { |
| 426 | resultReceiver.send(MSG_TAKE_SHARED_ELEMENTS, |
| 427 | sharedElementBundle); |
| 428 | notifyExitComplete(); |
| 429 | } |
| 430 | }); |
Dake Gu | f7ff220 | 2014-07-25 14:00:17 -0700 | [diff] [blame] | 431 | } |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 432 | } else { |
| 433 | notifyExitComplete(); |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 438 | private void notifyExitComplete() { |
George Mount | 41725de | 2015-04-09 08:23:05 -0700 | [diff] [blame] | 439 | if (!mExitNotified && isViewsTransitionComplete()) { |
George Mount | 4dc668c | 2015-04-09 20:55:59 +0000 | [diff] [blame] | 440 | mExitNotified = true; |
| 441 | mResultReceiver.send(MSG_EXIT_TRANSITION_COMPLETE, null); |
| 442 | mResultReceiver = null; // done talking |
| 443 | ViewGroup decorView = getDecor(); |
| 444 | if (!mIsReturning && decorView != null) { |
| 445 | decorView.suppressLayout(false); |
| 446 | } |
| 447 | finishIfNecessary(); |
| 448 | } |
| 449 | } |
| 450 | |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 451 | private void finishIfNecessary() { |
George Mount | e678ab6 | 2014-06-30 15:28:28 -0700 | [diff] [blame] | 452 | if (mIsReturning && mExitNotified && mActivity != null && (mSharedElements.isEmpty() || |
George Mount | b5ef7f8 | 2014-07-09 14:55:03 -0700 | [diff] [blame] | 453 | mSharedElementsHidden)) { |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 454 | finish(); |
George Mount | c93ca16 | 2014-05-23 19:21:36 -0700 | [diff] [blame] | 455 | } |
| 456 | if (!mIsReturning && mExitNotified) { |
| 457 | mActivity = null; // don't need it anymore |
| 458 | } |
| 459 | } |
| 460 | |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 461 | private void finish() { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 462 | stopCancel(); |
George Mount | f1abef6 | 2014-09-22 13:58:21 -0700 | [diff] [blame] | 463 | if (mActivity != null) { |
| 464 | mActivity.mActivityTransitionState.clear(); |
| 465 | mActivity.finish(); |
| 466 | mActivity.overridePendingTransition(0, 0); |
| 467 | mActivity = null; |
| 468 | } |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 469 | // Clear the state so that we can't hold any references accidentally and leak memory. |
George Mount | 3bbc2ed | 2015-11-18 11:10:55 -0800 | [diff] [blame] | 470 | clearState(); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | protected void clearState() { |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 475 | mHandler = null; |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 476 | mSharedElementBundle = null; |
| 477 | if (mBackgroundAnimator != null) { |
| 478 | mBackgroundAnimator.cancel(); |
| 479 | mBackgroundAnimator = null; |
| 480 | } |
| 481 | mExitSharedElementBundle = null; |
George Mount | 3bbc2ed | 2015-11-18 11:10:55 -0800 | [diff] [blame] | 482 | super.clearState(); |
George Mount | a0a0260 | 2014-06-20 18:22:26 -0700 | [diff] [blame] | 483 | } |
| 484 | |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 485 | @Override |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 486 | protected boolean moveSharedElementWithParent() { |
| 487 | return !mIsReturning; |
| 488 | } |
| 489 | |
| 490 | @Override |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 491 | protected Transition getViewsTransition() { |
| 492 | if (mIsReturning) { |
George Mount | 68f96d8 | 2014-07-31 13:13:10 -0700 | [diff] [blame] | 493 | return getWindow().getReturnTransition(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 494 | } else { |
| 495 | return getWindow().getExitTransition(); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | protected Transition getSharedElementTransition() { |
| 500 | if (mIsReturning) { |
George Mount | 68f96d8 | 2014-07-31 13:13:10 -0700 | [diff] [blame] | 501 | return getWindow().getSharedElementReturnTransition(); |
George Mount | a712e8c | 2014-05-20 15:10:20 -0700 | [diff] [blame] | 502 | } else { |
| 503 | return getWindow().getSharedElementExitTransition(); |
| 504 | } |
| 505 | } |
George Mount | 31a2172 | 2014-03-24 17:44:36 -0700 | [diff] [blame] | 506 | } |