blob: 00489837cca85c7cc3abe903903ca27e96038d25 [file] [log] [blame]
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001/*
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
17package com.android.server.wm;
18
19import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
22import android.graphics.Point;
23import android.graphics.Rect;
24import android.os.Handler;
25import android.os.RemoteException;
26import android.os.SystemClock;
27import android.util.Slog;
28import android.view.IRemoteAnimationFinishedCallback;
29import android.view.IRemoteAnimationFinishedCallback.Stub;
30import android.view.RemoteAnimationAdapter;
31import android.view.RemoteAnimationTarget;
32import android.view.SurfaceControl;
33import android.view.SurfaceControl.Transaction;
34
35import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
36
37import java.util.ArrayList;
38
39/**
40 * Helper class to run app animations in a remote process.
41 */
42class 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() {
Jorim Jaggi93f9fe32018-01-25 15:06:13 +010091 if (mPendingAnimations.isEmpty()) {
92 onAnimationFinished();
93 return;
94 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +010095 mHandler.postDelayed(mTimeoutRunnable, TIMEOUT_MS);
96 try {
97 mRemoteAnimationAdapter.getRunner().onAnimationStart(createAnimations(),
98 mFinishedCallback);
99 } catch (RemoteException e) {
100 Slog.e(TAG, "Failed to start remote animation", e);
101 onAnimationFinished();
102 }
103 }
104
105 private RemoteAnimationTarget[] createAnimations() {
Jorim Jaggi17770212018-01-22 20:01:21 +0100106 final ArrayList<RemoteAnimationTarget> targets = new ArrayList<>();
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100107 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
Jorim Jaggi17770212018-01-22 20:01:21 +0100108 final RemoteAnimationTarget target =
109 mPendingAnimations.get(i).createRemoteAppAnimation();
110 if (target != null) {
111 targets.add(target);
112 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100113 }
Jorim Jaggi17770212018-01-22 20:01:21 +0100114 return targets.toArray(new RemoteAnimationTarget[targets.size()]);
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100115 }
116
117 private void onAnimationFinished() {
118 mHandler.removeCallbacks(mTimeoutRunnable);
119 synchronized (mService.mWindowMap) {
120 mService.openSurfaceTransaction();
121 try {
122 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
123 final RemoteAnimationAdapterWrapper adapter = mPendingAnimations.get(i);
124 adapter.mCapturedFinishCallback.onAnimationFinished(adapter);
125 }
126 } finally {
127 mService.closeSurfaceTransaction("RemoteAnimationController#finished");
128 }
129 }
130 }
131
132 private void invokeAnimationCancelled() {
133 try {
134 mRemoteAnimationAdapter.getRunner().onAnimationCancelled();
135 } catch (RemoteException e) {
136 Slog.e(TAG, "Failed to notify cancel", e);
137 }
138 }
139
140 private class RemoteAnimationAdapterWrapper implements AnimationAdapter {
141
142 private final AppWindowToken mAppWindowToken;
143 private SurfaceControl mCapturedLeash;
144 private OnAnimationFinishedCallback mCapturedFinishCallback;
145 private final Point mPosition = new Point();
146 private final Rect mStackBounds = new Rect();
147
148 RemoteAnimationAdapterWrapper(AppWindowToken appWindowToken, Point position,
149 Rect stackBounds) {
150 mAppWindowToken = appWindowToken;
151 mPosition.set(position.x, position.y);
152 mStackBounds.set(stackBounds);
153 }
154
155 RemoteAnimationTarget createRemoteAppAnimation() {
Jorim Jaggi17770212018-01-22 20:01:21 +0100156 final Task task = mAppWindowToken.getTask();
157 final WindowState mainWindow = mAppWindowToken.findMainWindow();
158 if (task == null) {
159 return null;
160 }
161 if (mainWindow == null) {
162 return null;
163 }
164 return new RemoteAnimationTarget(task.mTaskId, getMode(),
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100165 mCapturedLeash, !mAppWindowToken.fillsParent(),
Jorim Jaggi17770212018-01-22 20:01:21 +0100166 mainWindow.mWinAnimator.mLastClipRect,
Jorim Jaggi33a701a2017-12-01 14:58:18 +0100167 mAppWindowToken.getPrefixOrderIndex(), mPosition, mStackBounds);
168 }
169
170 private int getMode() {
171 if (mService.mOpeningApps.contains(mAppWindowToken)) {
172 return RemoteAnimationTarget.MODE_OPENING;
173 } else {
174 return RemoteAnimationTarget.MODE_CLOSING;
175 }
176 }
177
178 @Override
179 public boolean getDetachWallpaper() {
180 return false;
181 }
182
183 @Override
184 public int getBackgroundColor() {
185 return 0;
186 }
187
188 @Override
189 public void startAnimation(SurfaceControl animationLeash, Transaction t,
190 OnAnimationFinishedCallback finishCallback) {
191
192 // Restore z-layering, position and stack crop until client has a chance to modify it.
193 t.setLayer(animationLeash, mAppWindowToken.getPrefixOrderIndex());
194 t.setPosition(animationLeash, mPosition.x, mPosition.y);
195 mTmpRect.set(mStackBounds);
196 mTmpRect.offsetTo(0, 0);
197 t.setWindowCrop(animationLeash, mTmpRect);
198 mCapturedLeash = animationLeash;
199 mCapturedFinishCallback = finishCallback;
200 }
201
202 @Override
203 public void onAnimationCancelled(SurfaceControl animationLeash) {
204 mPendingAnimations.remove(this);
205 if (mPendingAnimations.isEmpty()) {
206 mHandler.removeCallbacks(mTimeoutRunnable);
207 invokeAnimationCancelled();
208 }
209 }
210
211 @Override
212 public long getDurationHint() {
213 return mRemoteAnimationAdapter.getDuration();
214 }
215
216 @Override
217 public long getStatusBarTransitionsStartTime() {
218 return SystemClock.uptimeMillis()
219 + mRemoteAnimationAdapter.getStatusBarTransitionDelay();
220 }
221 }
222}