blob: 13c6f2730d089722bc3bba2cf8808e4f18e34c57 [file] [log] [blame]
Selim Cinek2627d722018-01-19 12:16:49 -08001/*
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.systemui.statusbar.notification;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
Selim Cinek7e222c3c2018-01-25 12:22:41 -080022import android.app.ActivityManager;
Selim Cinek2627d722018-01-19 12:16:49 -080023import android.graphics.Matrix;
24import android.graphics.Rect;
25import android.os.RemoteException;
26import android.util.MathUtils;
27import android.view.IRemoteAnimationFinishedCallback;
28import android.view.IRemoteAnimationRunner;
29import android.view.RemoteAnimationAdapter;
30import android.view.RemoteAnimationTarget;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010031import android.view.SyncRtSurfaceTransactionApplier;
32import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams;
Selim Cinekab4589a2019-04-29 18:42:59 -070033import android.view.View;
Selim Cinek2627d722018-01-19 12:16:49 -080034
Lucas Dupin086c6fc2018-10-16 18:06:43 -070035import com.android.internal.policy.ScreenDecorationsUtils;
Selim Cinek2627d722018-01-19 12:16:49 -080036import com.android.systemui.Interpolators;
Rohan Shah20790b82018-07-02 17:21:04 -070037import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
38import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
Selim Cinek2627d722018-01-19 12:16:49 -080039import com.android.systemui.statusbar.phone.CollapsedStatusBarFragment;
40import com.android.systemui.statusbar.phone.NotificationPanelView;
41import com.android.systemui.statusbar.phone.StatusBarWindowView;
Selim Cinek2627d722018-01-19 12:16:49 -080042
Selim Cinek2627d722018-01-19 12:16:49 -080043/**
44 * A class that allows activities to be launched in a seamless way where the notification
45 * transforms nicely into the starting window.
46 */
47public class ActivityLaunchAnimator {
48
49 private static final int ANIMATION_DURATION = 400;
50 public static final long ANIMATION_DURATION_FADE_CONTENT = 67;
51 public static final long ANIMATION_DURATION_FADE_APP = 200;
52 public static final long ANIMATION_DELAY_ICON_FADE_IN = ANIMATION_DURATION -
53 CollapsedStatusBarFragment.FADE_IN_DURATION - CollapsedStatusBarFragment.FADE_IN_DELAY
54 - 16;
Selim Cinekfc5c1992018-01-29 12:40:32 -080055 private static final long LAUNCH_TIMEOUT = 500;
Selim Cinek2627d722018-01-19 12:16:49 -080056 private final NotificationPanelView mNotificationPanel;
57 private final NotificationListContainer mNotificationContainer;
58 private final StatusBarWindowView mStatusBarWindow;
Lucas Dupin086c6fc2018-10-16 18:06:43 -070059 private final float mWindowCornerRadius;
Selim Cinek4a38f232018-06-22 13:48:33 -070060 private Callback mCallback;
Selim Cinekfc5c1992018-01-29 12:40:32 -080061 private final Runnable mTimeoutRunnable = () -> {
62 setAnimationPending(false);
Selim Cinek4a38f232018-06-22 13:48:33 -070063 mCallback.onExpandAnimationTimedOut();
Selim Cinekfc5c1992018-01-29 12:40:32 -080064 };
Selim Cinek7e222c3c2018-01-25 12:22:41 -080065 private boolean mAnimationPending;
Selim Cinek4a38f232018-06-22 13:48:33 -070066 private boolean mAnimationRunning;
67 private boolean mIsLaunchForActivity;
Selim Cinek2627d722018-01-19 12:16:49 -080068
69 public ActivityLaunchAnimator(StatusBarWindowView statusBarWindow,
Selim Cinek4a38f232018-06-22 13:48:33 -070070 Callback callback,
Selim Cinek2627d722018-01-19 12:16:49 -080071 NotificationPanelView notificationPanel,
72 NotificationListContainer container) {
73 mNotificationPanel = notificationPanel;
74 mNotificationContainer = container;
75 mStatusBarWindow = statusBarWindow;
Selim Cinek4a38f232018-06-22 13:48:33 -070076 mCallback = callback;
Lucas Dupin086c6fc2018-10-16 18:06:43 -070077 mWindowCornerRadius = ScreenDecorationsUtils
78 .getWindowCornerRadius(statusBarWindow.getResources());
Selim Cinek2627d722018-01-19 12:16:49 -080079 }
80
Jorim Jaggi04dc5962018-01-29 18:54:13 +010081 public RemoteAnimationAdapter getLaunchAnimation(
Selim Cinekab4589a2019-04-29 18:42:59 -070082 View sourceView, boolean occluded) {
83 if (!(sourceView instanceof ExpandableNotificationRow) || !mCallback.areLaunchAnimationsEnabled() || occluded) {
Jorim Jaggi77d0f36c2018-03-16 17:49:49 +010084 return null;
85 }
Selim Cinekab4589a2019-04-29 18:42:59 -070086 AnimationRunner animationRunner = new AnimationRunner(
87 (ExpandableNotificationRow) sourceView);
Jorim Jaggi04dc5962018-01-29 18:54:13 +010088 return new RemoteAnimationAdapter(animationRunner, ANIMATION_DURATION,
Jorim Jaggi64be98d2018-04-26 23:23:29 +020089 ANIMATION_DURATION - 150 /* statusBarTransitionDelay */);
Selim Cinek2627d722018-01-19 12:16:49 -080090 }
91
Selim Cinek7e222c3c2018-01-25 12:22:41 -080092 public boolean isAnimationPending() {
93 return mAnimationPending;
94 }
95
Selim Cinek4a38f232018-06-22 13:48:33 -070096 /**
97 * Set the launch result the intent requested
98 *
99 * @param launchResult the launch result
100 * @param wasIntentActivity was this launch for an activity
101 */
102 public void setLaunchResult(int launchResult, boolean wasIntentActivity) {
103 mIsLaunchForActivity = wasIntentActivity;
Selim Cinek7e222c3c2018-01-25 12:22:41 -0800104 setAnimationPending((launchResult == ActivityManager.START_TASK_TO_FRONT
105 || launchResult == ActivityManager.START_SUCCESS)
Selim Cinek4a38f232018-06-22 13:48:33 -0700106 && mCallback.areLaunchAnimationsEnabled());
107 }
108
109 public boolean isLaunchForActivity() {
110 return mIsLaunchForActivity;
Selim Cinek7e222c3c2018-01-25 12:22:41 -0800111 }
112
113 private void setAnimationPending(boolean pending) {
114 mAnimationPending = pending;
115 mStatusBarWindow.setExpandAnimationPending(pending);
Selim Cinekfc5c1992018-01-29 12:40:32 -0800116 if (pending) {
117 mStatusBarWindow.postDelayed(mTimeoutRunnable, LAUNCH_TIMEOUT);
118 } else {
119 mStatusBarWindow.removeCallbacks(mTimeoutRunnable);
120 }
Selim Cinek7e222c3c2018-01-25 12:22:41 -0800121 }
122
Selim Cinek4a38f232018-06-22 13:48:33 -0700123 public boolean isAnimationRunning() {
124 return mAnimationRunning;
125 }
126
Selim Cinek2627d722018-01-19 12:16:49 -0800127 class AnimationRunner extends IRemoteAnimationRunner.Stub {
128
129 private final ExpandableNotificationRow mSourceNotification;
130 private final ExpandAnimationParameters mParams;
131 private final Rect mWindowCrop = new Rect();
Lucas Dupin086c6fc2018-10-16 18:06:43 -0700132 private final float mNotificationCornerRadius;
133 private float mCornerRadius;
Selim Cinek4a38f232018-06-22 13:48:33 -0700134 private boolean mIsFullScreenLaunch = true;
Jorim Jaggi64be98d2018-04-26 23:23:29 +0200135 private final SyncRtSurfaceTransactionApplier mSyncRtTransactionApplier;
Selim Cinek2627d722018-01-19 12:16:49 -0800136
137 public AnimationRunner(ExpandableNotificationRow sourceNofitication) {
138 mSourceNotification = sourceNofitication;
139 mParams = new ExpandAnimationParameters();
Jorim Jaggi64be98d2018-04-26 23:23:29 +0200140 mSyncRtTransactionApplier = new SyncRtSurfaceTransactionApplier(mSourceNotification);
Lucas Dupin086c6fc2018-10-16 18:06:43 -0700141 mNotificationCornerRadius = Math.max(mSourceNotification.getCurrentTopRoundness(),
142 mSourceNotification.getCurrentBottomRoundness());
Selim Cinek2627d722018-01-19 12:16:49 -0800143 }
144
145 @Override
146 public void onAnimationStart(RemoteAnimationTarget[] remoteAnimationTargets,
Winson Chungd5852192019-09-06 17:20:28 -0700147 RemoteAnimationTarget[] remoteAnimationWallpaperTargets,
Selim Cinek2627d722018-01-19 12:16:49 -0800148 IRemoteAnimationFinishedCallback iRemoteAnimationFinishedCallback)
149 throws RemoteException {
150 mSourceNotification.post(() -> {
Jorim Jaggi95507722018-04-24 16:33:56 +0200151 RemoteAnimationTarget primary = getPrimaryRemoteAnimationTarget(
152 remoteAnimationTargets);
153 if (primary == null) {
154 setAnimationPending(false);
155 invokeCallback(iRemoteAnimationFinishedCallback);
Selim Cinek7c236e92019-07-26 21:47:19 -0700156 mNotificationPanel.collapse(false /* delayed */, 1.0f /* speedUpFactor */);
Jorim Jaggi95507722018-04-24 16:33:56 +0200157 return;
158 }
159
160 setExpandAnimationRunning(true);
Selim Cinek4a38f232018-06-22 13:48:33 -0700161 mIsFullScreenLaunch = primary.position.y == 0
Jorim Jaggi95507722018-04-24 16:33:56 +0200162 && primary.sourceContainerBounds.height()
163 >= mNotificationPanel.getHeight();
Selim Cinek4a38f232018-06-22 13:48:33 -0700164 if (!mIsFullScreenLaunch) {
Jorim Jaggi95507722018-04-24 16:33:56 +0200165 mNotificationPanel.collapseWithDuration(ANIMATION_DURATION);
166 }
167 ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
168 mParams.startPosition = mSourceNotification.getLocationOnScreen();
169 mParams.startTranslationZ = mSourceNotification.getTranslationZ();
170 mParams.startClipTopAmount = mSourceNotification.getClipTopAmount();
171 if (mSourceNotification.isChildInGroup()) {
172 int parentClip = mSourceNotification
173 .getNotificationParent().getClipTopAmount();
174 mParams.parentStartClipTopAmount = parentClip;
175 // We need to calculate how much the child is clipped by the parent
176 // because children always have 0 clipTopAmount
177 if (parentClip != 0) {
178 float childClip = parentClip
179 - mSourceNotification.getTranslationY();
180 if (childClip > 0.0f) {
181 mParams.startClipTopAmount = (int) Math.ceil(childClip);
Selim Cinek2627d722018-01-19 12:16:49 -0800182 }
Selim Cinek2627d722018-01-19 12:16:49 -0800183 }
184 }
Jorim Jaggi95507722018-04-24 16:33:56 +0200185 int targetWidth = primary.sourceContainerBounds.width();
186 int notificationHeight = mSourceNotification.getActualHeight()
187 - mSourceNotification.getClipBottomAmount();
188 int notificationWidth = mSourceNotification.getWidth();
189 anim.setDuration(ANIMATION_DURATION);
190 anim.setInterpolator(Interpolators.LINEAR);
191 anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
192 @Override
193 public void onAnimationUpdate(ValueAnimator animation) {
194 mParams.linearProgress = animation.getAnimatedFraction();
Lucas Dupin086c6fc2018-10-16 18:06:43 -0700195 float progress = Interpolators.FAST_OUT_SLOW_IN.getInterpolation(
Jorim Jaggi95507722018-04-24 16:33:56 +0200196 mParams.linearProgress);
197 int newWidth = (int) MathUtils.lerp(notificationWidth,
198 targetWidth, progress);
199 mParams.left = (int) ((targetWidth - newWidth) / 2.0f);
200 mParams.right = mParams.left + newWidth;
201 mParams.top = (int) MathUtils.lerp(mParams.startPosition[1],
202 primary.position.y, progress);
203 mParams.bottom = (int) MathUtils.lerp(mParams.startPosition[1]
204 + notificationHeight,
205 primary.position.y + primary.sourceContainerBounds.bottom,
206 progress);
Lucas Dupin086c6fc2018-10-16 18:06:43 -0700207 mCornerRadius = MathUtils.lerp(mNotificationCornerRadius,
208 mWindowCornerRadius, progress);
Jorim Jaggi95507722018-04-24 16:33:56 +0200209 applyParamsToWindow(primary);
210 applyParamsToNotification(mParams);
211 applyParamsToNotificationList(mParams);
212 }
213 });
214 anim.addListener(new AnimatorListenerAdapter() {
215 @Override
216 public void onAnimationEnd(Animator animation) {
217 setExpandAnimationRunning(false);
Jorim Jaggi95507722018-04-24 16:33:56 +0200218 invokeCallback(iRemoteAnimationFinishedCallback);
219 }
220 });
221 anim.start();
Selim Cinek7e222c3c2018-01-25 12:22:41 -0800222 setAnimationPending(false);
Selim Cinek2627d722018-01-19 12:16:49 -0800223 });
224 }
225
Jorim Jaggi95507722018-04-24 16:33:56 +0200226 private void invokeCallback(IRemoteAnimationFinishedCallback callback) {
227 try {
228 callback.onAnimationFinished();
229 } catch (RemoteException e) {
230 e.printStackTrace();
231 }
232 }
233
234 private RemoteAnimationTarget getPrimaryRemoteAnimationTarget(
235 RemoteAnimationTarget[] remoteAnimationTargets) {
236 RemoteAnimationTarget primary = null;
237 for (RemoteAnimationTarget app : remoteAnimationTargets) {
238 if (app.mode == RemoteAnimationTarget.MODE_OPENING) {
239 primary = app;
240 break;
241 }
242 }
243 return primary;
244 }
245
Selim Cinek2627d722018-01-19 12:16:49 -0800246 private void setExpandAnimationRunning(boolean running) {
247 mNotificationPanel.setLaunchingNotification(running);
248 mSourceNotification.setExpandAnimationRunning(running);
249 mStatusBarWindow.setExpandAnimationRunning(running);
250 mNotificationContainer.setExpandingNotification(running ? mSourceNotification : null);
Selim Cinek4a38f232018-06-22 13:48:33 -0700251 mAnimationRunning = running;
Selim Cinek2627d722018-01-19 12:16:49 -0800252 if (!running) {
Selim Cinek4a38f232018-06-22 13:48:33 -0700253 mCallback.onExpandAnimationFinished(mIsFullScreenLaunch);
Selim Cinek2627d722018-01-19 12:16:49 -0800254 applyParamsToNotification(null);
255 applyParamsToNotificationList(null);
256 }
257
258 }
259
260 private void applyParamsToNotificationList(ExpandAnimationParameters params) {
261 mNotificationContainer.applyExpandAnimationParams(params);
262 mNotificationPanel.applyExpandAnimationParams(params);
263 }
264
265 private void applyParamsToNotification(ExpandAnimationParameters params) {
266 mSourceNotification.applyExpandAnimationParams(params);
267 }
268
269 private void applyParamsToWindow(RemoteAnimationTarget app) {
Selim Cinek2627d722018-01-19 12:16:49 -0800270 Matrix m = new Matrix();
271 m.postTranslate(0, (float) (mParams.top - app.position.y));
Selim Cinek2627d722018-01-19 12:16:49 -0800272 mWindowCrop.set(mParams.left, 0, mParams.right, mParams.getHeight());
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100273 SurfaceParams params = new SurfaceParams(app.leash, 1f /* alpha */, m, mWindowCrop,
Jorim Jaggi67684882019-01-22 17:36:34 +0100274 app.prefixOrderIndex, mCornerRadius, true /* visible */);
Jorim Jaggi64be98d2018-04-26 23:23:29 +0200275 mSyncRtTransactionApplier.scheduleApply(params);
Selim Cinek2627d722018-01-19 12:16:49 -0800276 }
277
278 @Override
279 public void onAnimationCancelled() throws RemoteException {
Selim Cinek7e222c3c2018-01-25 12:22:41 -0800280 mSourceNotification.post(() -> {
281 setAnimationPending(false);
Selim Cinek4a38f232018-06-22 13:48:33 -0700282 mCallback.onLaunchAnimationCancelled();
Selim Cinek7e222c3c2018-01-25 12:22:41 -0800283 });
Selim Cinek2627d722018-01-19 12:16:49 -0800284 }
285 };
286
287 public static class ExpandAnimationParameters {
288 float linearProgress;
289 int[] startPosition;
290 float startTranslationZ;
291 int left;
292 int top;
293 int right;
294 int bottom;
Selim Cinekc25989e2018-02-16 16:42:14 -0800295 int startClipTopAmount;
296 int parentStartClipTopAmount;
Selim Cinek2627d722018-01-19 12:16:49 -0800297
298 public ExpandAnimationParameters() {
299 }
300
301 public int getTop() {
302 return top;
303 }
304
Selim Cinekc42db8d2018-12-04 13:29:25 -0800305 public int getBottom() {
306 return bottom;
307 }
308
Selim Cinek2627d722018-01-19 12:16:49 -0800309 public int getWidth() {
310 return right - left;
311 }
312
313 public int getHeight() {
314 return bottom - top;
315 }
316
317 public int getTopChange() {
Selim Cinekc25989e2018-02-16 16:42:14 -0800318 // We need this compensation to ensure that the QS moves in sync.
319 int clipTopAmountCompensation = 0;
320 if (startClipTopAmount != 0.0f) {
321 clipTopAmountCompensation = (int) MathUtils.lerp(0, startClipTopAmount,
322 Interpolators.FAST_OUT_SLOW_IN.getInterpolation(linearProgress));
323 }
324 return Math.min(top - startPosition[1] - clipTopAmountCompensation, 0);
Selim Cinek2627d722018-01-19 12:16:49 -0800325 }
326
Selim Cinekc25989e2018-02-16 16:42:14 -0800327 public float getProgress() {
328 return linearProgress;
329 }
Selim Cinek2627d722018-01-19 12:16:49 -0800330
331 public float getProgress(long delay, long duration) {
332 return MathUtils.constrain((linearProgress * ANIMATION_DURATION - delay)
333 / duration, 0.0f, 1.0f);
334 }
335
Selim Cinekc25989e2018-02-16 16:42:14 -0800336 public int getStartClipTopAmount() {
337 return startClipTopAmount;
338 }
339
340 public int getParentStartClipTopAmount() {
341 return parentStartClipTopAmount;
342 }
343
Selim Cinek2627d722018-01-19 12:16:49 -0800344 public float getStartTranslationZ() {
345 return startTranslationZ;
346 }
347 }
Selim Cinek4a38f232018-06-22 13:48:33 -0700348
349 public interface Callback {
350
351 /**
352 * Called when the launch animation was cancelled.
353 */
354 void onLaunchAnimationCancelled();
355
356 /**
357 * Called when the launch animation has timed out without starting an actual animation.
358 */
359 void onExpandAnimationTimedOut();
360
361 /**
362 * Called when the expand animation has finished.
363 *
364 * @param launchIsFullScreen True if this launch was fullscreen, such that now the window
365 * fills the whole screen
366 */
367 void onExpandAnimationFinished(boolean launchIsFullScreen);
368
369 /**
370 * Are animations currently enabled.
371 */
372 boolean areLaunchAnimationsEnabled();
373 }
Selim Cinek2627d722018-01-19 12:16:49 -0800374}