Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 21 | |
| 22 | import android.graphics.Point; |
| 23 | import android.graphics.Rect; |
| 24 | import android.os.Handler; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.SystemClock; |
| 27 | import android.util.Slog; |
| 28 | import android.view.IRemoteAnimationFinishedCallback; |
| 29 | import android.view.IRemoteAnimationFinishedCallback.Stub; |
| 30 | import android.view.RemoteAnimationAdapter; |
| 31 | import android.view.RemoteAnimationTarget; |
| 32 | import android.view.SurfaceControl; |
| 33 | import android.view.SurfaceControl.Transaction; |
| 34 | |
| 35 | import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback; |
| 36 | |
| 37 | import java.util.ArrayList; |
| 38 | |
| 39 | /** |
| 40 | * Helper class to run app animations in a remote process. |
| 41 | */ |
| 42 | class RemoteAnimationController { |
| 43 | private static final String TAG = TAG_WITH_CLASS_NAME ? "RemoteAnimationController" : TAG_WM; |
| 44 | private static final long TIMEOUT_MS = 2000; |
| 45 | |
| 46 | private final WindowManagerService mService; |
| 47 | private final RemoteAnimationAdapter mRemoteAnimationAdapter; |
| 48 | private final ArrayList<RemoteAnimationAdapterWrapper> mPendingAnimations = new ArrayList<>(); |
| 49 | private final Rect mTmpRect = new Rect(); |
| 50 | private final Handler mHandler; |
| 51 | |
| 52 | private final IRemoteAnimationFinishedCallback mFinishedCallback = new Stub() { |
| 53 | @Override |
| 54 | public void onAnimationFinished() throws RemoteException { |
| 55 | RemoteAnimationController.this.onAnimationFinished(); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | private final Runnable mTimeoutRunnable = () -> { |
| 60 | onAnimationFinished(); |
| 61 | invokeAnimationCancelled(); |
| 62 | }; |
| 63 | |
| 64 | RemoteAnimationController(WindowManagerService service, |
| 65 | RemoteAnimationAdapter remoteAnimationAdapter, Handler handler) { |
| 66 | mService = service; |
| 67 | mRemoteAnimationAdapter = remoteAnimationAdapter; |
| 68 | mHandler = handler; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates an animation for each individual {@link AppWindowToken}. |
| 73 | * |
| 74 | * @param appWindowToken The app to animate. |
| 75 | * @param position The position app bounds, in screen coordinates. |
| 76 | * @param stackBounds The stack bounds of the app. |
| 77 | * @return The adapter to be run on the app. |
| 78 | */ |
| 79 | AnimationAdapter createAnimationAdapter(AppWindowToken appWindowToken, Point position, |
| 80 | Rect stackBounds) { |
| 81 | final RemoteAnimationAdapterWrapper adapter = new RemoteAnimationAdapterWrapper( |
| 82 | appWindowToken, position, stackBounds); |
| 83 | mPendingAnimations.add(adapter); |
| 84 | return adapter; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Called when the transition is ready to be started, and all leashes have been set up. |
| 89 | */ |
| 90 | void goodToGo() { |
| 91 | mHandler.postDelayed(mTimeoutRunnable, TIMEOUT_MS); |
| 92 | try { |
| 93 | mRemoteAnimationAdapter.getRunner().onAnimationStart(createAnimations(), |
| 94 | mFinishedCallback); |
| 95 | } catch (RemoteException e) { |
| 96 | Slog.e(TAG, "Failed to start remote animation", e); |
| 97 | onAnimationFinished(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private RemoteAnimationTarget[] createAnimations() { |
| 102 | final RemoteAnimationTarget[] result = new RemoteAnimationTarget[mPendingAnimations.size()]; |
| 103 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
| 104 | result[i] = mPendingAnimations.get(i).createRemoteAppAnimation(); |
| 105 | } |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | private void onAnimationFinished() { |
| 110 | mHandler.removeCallbacks(mTimeoutRunnable); |
| 111 | synchronized (mService.mWindowMap) { |
| 112 | mService.openSurfaceTransaction(); |
| 113 | try { |
| 114 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
| 115 | final RemoteAnimationAdapterWrapper adapter = mPendingAnimations.get(i); |
| 116 | adapter.mCapturedFinishCallback.onAnimationFinished(adapter); |
| 117 | } |
| 118 | } finally { |
| 119 | mService.closeSurfaceTransaction("RemoteAnimationController#finished"); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private void invokeAnimationCancelled() { |
| 125 | try { |
| 126 | mRemoteAnimationAdapter.getRunner().onAnimationCancelled(); |
| 127 | } catch (RemoteException e) { |
| 128 | Slog.e(TAG, "Failed to notify cancel", e); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private class RemoteAnimationAdapterWrapper implements AnimationAdapter { |
| 133 | |
| 134 | private final AppWindowToken mAppWindowToken; |
| 135 | private SurfaceControl mCapturedLeash; |
| 136 | private OnAnimationFinishedCallback mCapturedFinishCallback; |
| 137 | private final Point mPosition = new Point(); |
| 138 | private final Rect mStackBounds = new Rect(); |
| 139 | |
| 140 | RemoteAnimationAdapterWrapper(AppWindowToken appWindowToken, Point position, |
| 141 | Rect stackBounds) { |
| 142 | mAppWindowToken = appWindowToken; |
| 143 | mPosition.set(position.x, position.y); |
| 144 | mStackBounds.set(stackBounds); |
| 145 | } |
| 146 | |
| 147 | RemoteAnimationTarget createRemoteAppAnimation() { |
| 148 | return new RemoteAnimationTarget(mAppWindowToken.getTask().mTaskId, getMode(), |
| 149 | mCapturedLeash, !mAppWindowToken.fillsParent(), |
| 150 | mAppWindowToken.findMainWindow().mWinAnimator.mLastClipRect, |
| 151 | mAppWindowToken.getPrefixOrderIndex(), mPosition, mStackBounds); |
| 152 | } |
| 153 | |
| 154 | private int getMode() { |
| 155 | if (mService.mOpeningApps.contains(mAppWindowToken)) { |
| 156 | return RemoteAnimationTarget.MODE_OPENING; |
| 157 | } else { |
| 158 | return RemoteAnimationTarget.MODE_CLOSING; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public boolean getDetachWallpaper() { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int getBackgroundColor() { |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void startAnimation(SurfaceControl animationLeash, Transaction t, |
| 174 | OnAnimationFinishedCallback finishCallback) { |
| 175 | |
| 176 | // Restore z-layering, position and stack crop until client has a chance to modify it. |
| 177 | t.setLayer(animationLeash, mAppWindowToken.getPrefixOrderIndex()); |
| 178 | t.setPosition(animationLeash, mPosition.x, mPosition.y); |
| 179 | mTmpRect.set(mStackBounds); |
| 180 | mTmpRect.offsetTo(0, 0); |
| 181 | t.setWindowCrop(animationLeash, mTmpRect); |
| 182 | mCapturedLeash = animationLeash; |
| 183 | mCapturedFinishCallback = finishCallback; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onAnimationCancelled(SurfaceControl animationLeash) { |
| 188 | mPendingAnimations.remove(this); |
| 189 | if (mPendingAnimations.isEmpty()) { |
| 190 | mHandler.removeCallbacks(mTimeoutRunnable); |
| 191 | invokeAnimationCancelled(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public long getDurationHint() { |
| 197 | return mRemoteAnimationAdapter.getDuration(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public long getStatusBarTransitionsStartTime() { |
| 202 | return SystemClock.uptimeMillis() |
| 203 | + mRemoteAnimationAdapter.getStatusBarTransitionDelay(); |
| 204 | } |
| 205 | } |
| 206 | } |