| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.transition; |
| 18 |
|
| 19 |
import static android.view.WindowManager.TRANSIT_CHANGE; |
| 20 |
import static android.view.WindowManager.TRANSIT_CLOSE; |
| 21 |
import static android.view.WindowManager.TRANSIT_FIRST_CUSTOM; |
| 22 |
import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY; |
| 23 |
import static android.view.WindowManager.TRANSIT_OPEN; |
| 24 |
import static android.view.WindowManager.TRANSIT_TO_BACK; |
| 25 |
import static android.view.WindowManager.TRANSIT_TO_FRONT; |
| 26 |
import static android.view.WindowManager.fixScale; |
| 27 |
import static android.window.TransitionInfo.FLAG_IS_OCCLUDED; |
| 28 |
import static android.window.TransitionInfo.FLAG_IS_WALLPAPER; |
| 29 |
import static android.window.TransitionInfo.FLAG_STARTING_WINDOW_TRANSFER_RECIPIENT; |
| 30 |
|
| 31 |
import static com.android.wm.shell.common.ExecutorUtils.executeRemoteCallWithTaskPermission; |
| 32 |
import static com.android.wm.shell.sysui.ShellSharedConstants.KEY_EXTRA_SHELL_SHELL_TRANSITIONS; |
| 33 |
|
| 34 |
import android.annotation.NonNull; |
| 35 |
import android.annotation.Nullable; |
| 36 |
import android.app.ActivityTaskManager; |
| 37 |
import android.app.IApplicationThread; |
| 38 |
import android.content.ContentResolver; |
| 39 |
import android.content.Context; |
| 40 |
import android.database.ContentObserver; |
| 41 |
import android.os.Handler; |
| 42 |
import android.os.IBinder; |
| 43 |
import android.os.RemoteException; |
| 44 |
import android.os.SystemProperties; |
| 45 |
import android.provider.Settings; |
| 46 |
import android.util.Log; |
| 47 |
import android.view.SurfaceControl; |
| 48 |
import android.view.WindowManager; |
| 49 |
import android.window.ITransitionPlayer; |
| 50 |
import android.window.RemoteTransition; |
| 51 |
import android.window.TransitionFilter; |
| 52 |
import android.window.TransitionInfo; |
| 53 |
import android.window.TransitionMetrics; |
| 54 |
import android.window.TransitionRequestInfo; |
| 55 |
import android.window.WindowContainerTransaction; |
| 56 |
import android.window.WindowContainerTransactionCallback; |
| 57 |
import android.window.WindowOrganizer; |
| 58 |
|
| 59 |
import androidx.annotation.BinderThread; |
| 60 |
|
| 61 |
import com.android.internal.R; |
| 62 |
import com.android.internal.annotations.VisibleForTesting; |
| 63 |
import com.android.internal.protolog.common.ProtoLog; |
| 64 |
import com.android.wm.shell.common.DisplayController; |
| 65 |
import com.android.wm.shell.common.ExternalInterfaceBinder; |
| 66 |
import com.android.wm.shell.common.RemoteCallable; |
| 67 |
import com.android.wm.shell.common.ShellExecutor; |
| 68 |
import com.android.wm.shell.common.TransactionPool; |
| 69 |
import com.android.wm.shell.common.annotations.ExternalThread; |
| 70 |
import com.android.wm.shell.protolog.ShellProtoLogGroup; |
| 71 |
import com.android.wm.shell.sysui.ShellController; |
| 72 |
import com.android.wm.shell.sysui.ShellInit; |
| 73 |
|
| 74 |
import java.util.ArrayList; |
| 75 |
import java.util.Arrays; |
| 76 |
|
| 77 |
|
| 78 |
public class Transitions implements RemoteCallable<Transitions> { |
| 79 |
static final String TAG = "ShellTransitions"; |
| 80 |
|
| 81 |
|
| 82 |
public static final boolean ENABLE_SHELL_TRANSITIONS = |
| 83 |
SystemProperties.getBoolean("persist.wm.debug.shell_transit", false); |
| 84 |
public static final boolean SHELL_TRANSITIONS_ROTATION = ENABLE_SHELL_TRANSITIONS |
| 85 |
&& SystemProperties.getBoolean("persist.wm.debug.shell_transit_rotate", false); |
| 86 |
|
| 87 |
|
| 88 |
public static final int TRANSIT_EXIT_PIP = TRANSIT_FIRST_CUSTOM + 1; |
| 89 |
|
| 90 |
public static final int TRANSIT_EXIT_PIP_TO_SPLIT = TRANSIT_FIRST_CUSTOM + 2; |
| 91 |
|
| 92 |
|
| 93 |
public static final int TRANSIT_REMOVE_PIP = TRANSIT_FIRST_CUSTOM + 3; |
| 94 |
|
| 95 |
|
| 96 |
public static final int TRANSIT_SPLIT_SCREEN_PAIR_OPEN = TRANSIT_FIRST_CUSTOM + 4; |
| 97 |
|
| 98 |
|
| 99 |
public static final int TRANSIT_SPLIT_SCREEN_OPEN_TO_SIDE = TRANSIT_FIRST_CUSTOM + 5; |
| 100 |
|
| 101 |
|
| 102 |
public static final int TRANSIT_SPLIT_DISMISS_SNAP = TRANSIT_FIRST_CUSTOM + 6; |
| 103 |
|
| 104 |
|
| 105 |
public static final int TRANSIT_SPLIT_DISMISS = TRANSIT_FIRST_CUSTOM + 7; |
| 106 |
|
| 107 |
|
| 108 |
public static final int TRANSIT_MAXIMIZE = WindowManager.TRANSIT_FIRST_CUSTOM + 8; |
| 109 |
|
| 110 |
|
| 111 |
public static final int TRANSIT_RESTORE_FROM_MAXIMIZE = WindowManager.TRANSIT_FIRST_CUSTOM + 9; |
| 112 |
|
| 113 |
private final WindowOrganizer mOrganizer; |
| 114 |
private final Context mContext; |
| 115 |
private final ShellExecutor mMainExecutor; |
| 116 |
private final ShellExecutor mAnimExecutor; |
| 117 |
private final TransitionPlayerImpl mPlayerImpl; |
| 118 |
private final DefaultTransitionHandler mDefaultTransitionHandler; |
| 119 |
private final RemoteTransitionHandler mRemoteTransitionHandler; |
| 120 |
private final DisplayController mDisplayController; |
| 121 |
private final ShellController mShellController; |
| 122 |
private final ShellTransitionImpl mImpl = new ShellTransitionImpl(); |
| 123 |
|
| 124 |
private boolean mIsRegistered = false; |
| 125 |
|
| 126 |
|
| 127 |
private final ArrayList<TransitionHandler> mHandlers = new ArrayList<>(); |
| 128 |
|
| 129 |
private final ArrayList<TransitionObserver> mObservers = new ArrayList<>(); |
| 130 |
|
| 131 |
|
| 132 |
private final ArrayList<Runnable> mRunWhenIdleQueue = new ArrayList<>(); |
| 133 |
|
| 134 |
private float mTransitionAnimationScaleSetting = 1.0f; |
| 135 |
|
| 136 |
private static final class ActiveTransition { |
| 137 |
IBinder mToken; |
| 138 |
TransitionHandler mHandler; |
| 139 |
boolean mMerged; |
| 140 |
boolean mAborted; |
| 141 |
TransitionInfo mInfo; |
| 142 |
SurfaceControl.Transaction mStartT; |
| 143 |
SurfaceControl.Transaction mFinishT; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
private final ArrayList<ActiveTransition> mActiveTransitions = new ArrayList<>(); |
| 148 |
|
| 149 |
public Transitions(@NonNull Context context, |
| 150 |
@NonNull ShellInit shellInit, |
| 151 |
@NonNull ShellController shellController, |
| 152 |
@NonNull WindowOrganizer organizer, |
| 153 |
@NonNull TransactionPool pool, |
| 154 |
@NonNull DisplayController displayController, |
| 155 |
@NonNull ShellExecutor mainExecutor, @NonNull Handler mainHandler, |
| 156 |
@NonNull ShellExecutor animExecutor) { |
| 157 |
mOrganizer = organizer; |
| 158 |
mContext = context; |
| 159 |
mMainExecutor = mainExecutor; |
| 160 |
mAnimExecutor = animExecutor; |
| 161 |
mDisplayController = displayController; |
| 162 |
mPlayerImpl = new TransitionPlayerImpl(); |
| 163 |
mDefaultTransitionHandler = new DefaultTransitionHandler(context, shellInit, |
| 164 |
displayController, pool, mainExecutor, mainHandler, animExecutor); |
| 165 |
mRemoteTransitionHandler = new RemoteTransitionHandler(mMainExecutor); |
| 166 |
mShellController = shellController; |
| 167 |
|
| 168 |
mHandlers.add(mDefaultTransitionHandler); |
| 169 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "addHandler: Default"); |
| 170 |
|
| 171 |
mHandlers.add(mRemoteTransitionHandler); |
| 172 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "addHandler: Remote"); |
| 173 |
shellInit.addInitCallback(this::onInit, this); |
| 174 |
} |
| 175 |
|
| 176 |
private void onInit() { |
| 177 |
mShellController.addExternalInterface(KEY_EXTRA_SHELL_SHELL_TRANSITIONS, |
| 178 |
this::createExternalInterface, this); |
| 179 |
|
| 180 |
ContentResolver resolver = mContext.getContentResolver(); |
| 181 |
mTransitionAnimationScaleSetting = getTransitionAnimationScaleSetting(); |
| 182 |
dispatchAnimScaleSetting(mTransitionAnimationScaleSetting); |
| 183 |
|
| 184 |
resolver.registerContentObserver( |
| 185 |
Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE), false, |
| 186 |
new SettingsObserver()); |
| 187 |
|
| 188 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 189 |
mIsRegistered = true; |
| 190 |
|
| 191 |
try { |
| 192 |
mOrganizer.registerTransitionPlayer(mPlayerImpl); |
| 193 |
} catch (RuntimeException e) { |
| 194 |
mIsRegistered = false; |
| 195 |
throw e; |
| 196 |
} |
| 197 |
|
| 198 |
TransitionMetrics.getInstance(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public boolean isRegistered() { |
| 203 |
return mIsRegistered; |
| 204 |
} |
| 205 |
|
| 206 |
private float getTransitionAnimationScaleSetting() { |
| 207 |
return fixScale(Settings.Global.getFloat(mContext.getContentResolver(), |
| 208 |
Settings.Global.TRANSITION_ANIMATION_SCALE, mContext.getResources().getFloat( |
| 209 |
R.dimen.config_appTransitionAnimationDurationScaleDefault))); |
| 210 |
} |
| 211 |
|
| 212 |
public ShellTransitions asRemoteTransitions() { |
| 213 |
return mImpl; |
| 214 |
} |
| 215 |
|
| 216 |
private ExternalInterfaceBinder createExternalInterface() { |
| 217 |
return new IShellTransitionsImpl(this); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public Context getContext() { |
| 222 |
return mContext; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public ShellExecutor getRemoteCallExecutor() { |
| 227 |
return mMainExecutor; |
| 228 |
} |
| 229 |
|
| 230 |
private void dispatchAnimScaleSetting(float scale) { |
| 231 |
for (int i = mHandlers.size() - 1; i >= 0; --i) { |
| 232 |
mHandlers.get(i).setAnimScaleSetting(scale); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public void addHandler(@NonNull TransitionHandler handler) { |
| 241 |
if (mHandlers.isEmpty()) { |
| 242 |
throw new RuntimeException("Unexpected handler added prior to initialization, please " |
| 243 |
+ "use ShellInit callbacks to ensure proper ordering"); |
| 244 |
} |
| 245 |
mHandlers.add(handler); |
| 246 |
|
| 247 |
handler.setAnimScaleSetting(mTransitionAnimationScaleSetting); |
| 248 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "addHandler: %s", |
| 249 |
handler.getClass().getSimpleName()); |
| 250 |
} |
| 251 |
|
| 252 |
public ShellExecutor getMainExecutor() { |
| 253 |
return mMainExecutor; |
| 254 |
} |
| 255 |
|
| 256 |
public ShellExecutor getAnimExecutor() { |
| 257 |
return mAnimExecutor; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
@VisibleForTesting |
| 262 |
void replaceDefaultHandlerForTest(TransitionHandler handler) { |
| 263 |
mHandlers.set(0, handler); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
public void registerRemote(@NonNull TransitionFilter filter, |
| 268 |
@NonNull RemoteTransition remoteTransition) { |
| 269 |
mRemoteTransitionHandler.addFiltered(filter, remoteTransition); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
public void unregisterRemote(@NonNull RemoteTransition remoteTransition) { |
| 274 |
mRemoteTransitionHandler.removeFiltered(remoteTransition); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
public void registerObserver(@NonNull TransitionObserver observer) { |
| 279 |
mObservers.add(observer); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
public void unregisterObserver(@NonNull TransitionObserver observer) { |
| 284 |
mObservers.remove(observer); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
public static void setRunningRemoteTransitionDelegate(IApplicationThread appThread) { |
| 289 |
if (appThread == null) return; |
| 290 |
try { |
| 291 |
ActivityTaskManager.getService().setRunningRemoteTransitionDelegate(appThread); |
| 292 |
} catch (SecurityException e) { |
| 293 |
Log.e(TAG, "Unable to boost animation process. This should only happen" |
| 294 |
+ " during unit tests"); |
| 295 |
} catch (RemoteException e) { |
| 296 |
e.rethrowFromSystemServer(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public void runOnIdle(Runnable runnable) { |
| 308 |
if (mActiveTransitions.isEmpty()) { |
| 309 |
runnable.run(); |
| 310 |
} else { |
| 311 |
mRunWhenIdleQueue.add(runnable); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
public static boolean isOpeningType(@WindowManager.TransitionType int type) { |
| 317 |
return type == TRANSIT_OPEN |
| 318 |
|| type == TRANSIT_TO_FRONT |
| 319 |
|| type == TRANSIT_KEYGUARD_GOING_AWAY; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
public static boolean isClosingType(@WindowManager.TransitionType int type) { |
| 324 |
return type == TRANSIT_CLOSE || type == TRANSIT_TO_BACK; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private static void setupStartState(@NonNull TransitionInfo info, |
| 331 |
@NonNull SurfaceControl.Transaction t, @NonNull SurfaceControl.Transaction finishT) { |
| 332 |
boolean isOpening = isOpeningType(info.getType()); |
| 333 |
for (int i = info.getChanges().size() - 1; i >= 0; --i) { |
| 334 |
final TransitionInfo.Change change = info.getChanges().get(i); |
| 335 |
if (change.hasFlags(TransitionInfo.FLAGS_IS_NON_APP_WINDOW)) { |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
continue; |
| 342 |
} |
| 343 |
final SurfaceControl leash = change.getLeash(); |
| 344 |
final int mode = info.getChanges().get(i).getMode(); |
| 345 |
|
| 346 |
if (mode == TRANSIT_TO_FRONT |
| 347 |
&& ((change.getStartAbsBounds().height() != change.getEndAbsBounds().height() |
| 348 |
|| change.getStartAbsBounds().width() != change.getEndAbsBounds().width()))) { |
| 349 |
|
| 350 |
|
| 351 |
t.setWindowCrop(leash, change.getEndAbsBounds().width(), |
| 352 |
change.getEndAbsBounds().height()); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
if (!TransitionInfo.isIndependent(change, info)) { |
| 357 |
if (mode == TRANSIT_OPEN || mode == TRANSIT_TO_FRONT || mode == TRANSIT_CHANGE) { |
| 358 |
t.show(leash); |
| 359 |
t.setMatrix(leash, 1, 0, 0, 1); |
| 360 |
t.setAlpha(leash, 1.f); |
| 361 |
t.setPosition(leash, change.getEndRelOffset().x, change.getEndRelOffset().y); |
| 362 |
} |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
if (mode == TRANSIT_OPEN || mode == TRANSIT_TO_FRONT) { |
| 367 |
t.show(leash); |
| 368 |
t.setMatrix(leash, 1, 0, 0, 1); |
| 369 |
if (isOpening |
| 370 |
|
| 371 |
&& (change.getFlags() & FLAG_STARTING_WINDOW_TRANSFER_RECIPIENT) == 0) { |
| 372 |
t.setAlpha(leash, 0.f); |
| 373 |
} |
| 374 |
} else if (mode == TRANSIT_CLOSE || mode == TRANSIT_TO_BACK) { |
| 375 |
finishT.hide(leash); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
private static void setupAnimHierarchy(@NonNull TransitionInfo info, |
| 385 |
@NonNull SurfaceControl.Transaction t, @NonNull SurfaceControl.Transaction finishT) { |
| 386 |
boolean isOpening = isOpeningType(info.getType()); |
| 387 |
if (info.getRootLeash().isValid()) { |
| 388 |
t.show(info.getRootLeash()); |
| 389 |
} |
| 390 |
final int numChanges = info.getChanges().size(); |
| 391 |
|
| 392 |
final int zSplitLine = numChanges + 1; |
| 393 |
|
| 394 |
for (int i = numChanges - 1; i >= 0; --i) { |
| 395 |
final TransitionInfo.Change change = info.getChanges().get(i); |
| 396 |
final SurfaceControl leash = change.getLeash(); |
| 397 |
final int mode = change.getMode(); |
| 398 |
|
| 399 |
|
| 400 |
if (!TransitionInfo.isIndependent(change, info)) { |
| 401 |
continue; |
| 402 |
} |
| 403 |
|
| 404 |
boolean hasParent = change.getParent() != null; |
| 405 |
|
| 406 |
if (!hasParent) { |
| 407 |
t.reparent(leash, info.getRootLeash()); |
| 408 |
t.setPosition(leash, change.getStartAbsBounds().left - info.getRootOffset().x, |
| 409 |
change.getStartAbsBounds().top - info.getRootOffset().y); |
| 410 |
} |
| 411 |
final int layer; |
| 412 |
|
| 413 |
if ((change.getFlags() & FLAG_IS_WALLPAPER) != 0) { |
| 414 |
|
| 415 |
layer = -zSplitLine; |
| 416 |
} else if (mode == TRANSIT_OPEN || mode == TRANSIT_TO_FRONT) { |
| 417 |
if (isOpening) { |
| 418 |
|
| 419 |
layer = zSplitLine + numChanges - i; |
| 420 |
} else { |
| 421 |
|
| 422 |
layer = zSplitLine - i; |
| 423 |
} |
| 424 |
} else if (mode == TRANSIT_CLOSE || mode == TRANSIT_TO_BACK) { |
| 425 |
if (isOpening) { |
| 426 |
|
| 427 |
layer = zSplitLine - i; |
| 428 |
} else { |
| 429 |
|
| 430 |
layer = zSplitLine + numChanges - i; |
| 431 |
} |
| 432 |
} else { |
| 433 |
layer = zSplitLine + numChanges - i; |
| 434 |
} |
| 435 |
t.setLayer(leash, layer); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
private int findActiveTransition(IBinder token) { |
| 440 |
for (int i = mActiveTransitions.size() - 1; i >= 0; --i) { |
| 441 |
if (mActiveTransitions.get(i).mToken == token) return i; |
| 442 |
} |
| 443 |
return -1; |
| 444 |
} |
| 445 |
|
| 446 |
@VisibleForTesting |
| 447 |
void onTransitionReady(@NonNull IBinder transitionToken, @NonNull TransitionInfo info, |
| 448 |
@NonNull SurfaceControl.Transaction t, @NonNull SurfaceControl.Transaction finishT) { |
| 449 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "onTransitionReady %s: %s", |
| 450 |
transitionToken, info); |
| 451 |
final int activeIdx = findActiveTransition(transitionToken); |
| 452 |
if (activeIdx < 0) { |
| 453 |
throw new IllegalStateException("Got transitionReady for non-active transition " |
| 454 |
+ transitionToken + ". expecting one of " |
| 455 |
+ Arrays.toString(mActiveTransitions.stream().map( |
| 456 |
activeTransition -> activeTransition.mToken).toArray())); |
| 457 |
} |
| 458 |
|
| 459 |
for (int i = 0; i < mObservers.size(); ++i) { |
| 460 |
mObservers.get(i).onTransitionReady(transitionToken, info, t, finishT); |
| 461 |
} |
| 462 |
|
| 463 |
if (!info.getRootLeash().isValid()) { |
| 464 |
|
| 465 |
|
| 466 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Invalid root leash (%s): %s", |
| 467 |
transitionToken, info); |
| 468 |
t.apply(); |
| 469 |
finishT.apply(); |
| 470 |
onAbort(transitionToken); |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
final int changeSize = info.getChanges().size(); |
| 475 |
boolean taskChange = false; |
| 476 |
boolean transferStartingWindow = false; |
| 477 |
boolean allOccluded = changeSize > 0; |
| 478 |
for (int i = changeSize - 1; i >= 0; --i) { |
| 479 |
final TransitionInfo.Change change = info.getChanges().get(i); |
| 480 |
taskChange |= change.getTaskInfo() != null; |
| 481 |
transferStartingWindow |= change.hasFlags(FLAG_STARTING_WINDOW_TRANSFER_RECIPIENT); |
| 482 |
if (!change.hasFlags(FLAG_IS_OCCLUDED)) { |
| 483 |
allOccluded = false; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
if (!taskChange && transferStartingWindow && changeSize == 2 |
| 492 |
|
| 493 |
|
| 494 |
|| ((info.getType() == TRANSIT_TO_BACK || info.getType() == TRANSIT_TO_FRONT) |
| 495 |
&& allOccluded)) { |
| 496 |
t.apply(); |
| 497 |
finishT.apply(); |
| 498 |
|
| 499 |
|
| 500 |
onAbort(transitionToken); |
| 501 |
releaseSurfaces(info); |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
final ActiveTransition active = mActiveTransitions.get(activeIdx); |
| 506 |
active.mInfo = info; |
| 507 |
active.mStartT = t; |
| 508 |
active.mFinishT = finishT; |
| 509 |
setupStartState(active.mInfo, active.mStartT, active.mFinishT); |
| 510 |
|
| 511 |
if (activeIdx > 0) { |
| 512 |
|
| 513 |
attemptMergeTransition(mActiveTransitions.get(0), active); |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
playTransition(active); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
void attemptMergeTransition(@NonNull ActiveTransition playing, |
| 525 |
@NonNull ActiveTransition merging) { |
| 526 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Transition %s ready while" |
| 527 |
+ " another transition %s is still animating. Notify the animating transition" |
| 528 |
+ " in case they can be merged", merging.mToken, playing.mToken); |
| 529 |
playing.mHandler.mergeAnimation(merging.mToken, merging.mInfo, merging.mStartT, |
| 530 |
playing.mToken, (wct, cb) -> onFinish(merging.mToken, wct, cb)); |
| 531 |
} |
| 532 |
|
| 533 |
private void playTransition(@NonNull ActiveTransition active) { |
| 534 |
for (int i = 0; i < mObservers.size(); ++i) { |
| 535 |
mObservers.get(i).onTransitionStarting(active.mToken); |
| 536 |
} |
| 537 |
|
| 538 |
setupAnimHierarchy(active.mInfo, active.mStartT, active.mFinishT); |
| 539 |
|
| 540 |
|
| 541 |
if (active.mHandler != null) { |
| 542 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, " try firstHandler %s", |
| 543 |
active.mHandler); |
| 544 |
boolean consumed = active.mHandler.startAnimation(active.mToken, active.mInfo, |
| 545 |
active.mStartT, active.mFinishT, (wct, cb) -> onFinish(active.mToken, wct, cb)); |
| 546 |
if (consumed) { |
| 547 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, " animated by firstHandler"); |
| 548 |
return; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
active.mHandler = dispatchTransition(active.mToken, active.mInfo, active.mStartT, |
| 553 |
active.mFinishT, (wct, cb) -> onFinish(active.mToken, wct, cb), active.mHandler); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
TransitionHandler dispatchTransition(@NonNull IBinder transition, @NonNull TransitionInfo info, |
| 561 |
@NonNull SurfaceControl.Transaction startT, @NonNull SurfaceControl.Transaction finishT, |
| 562 |
@NonNull TransitionFinishCallback finishCB, @Nullable TransitionHandler skip) { |
| 563 |
for (int i = mHandlers.size() - 1; i >= 0; --i) { |
| 564 |
if (mHandlers.get(i) == skip) continue; |
| 565 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, " try handler %s", |
| 566 |
mHandlers.get(i)); |
| 567 |
boolean consumed = mHandlers.get(i).startAnimation(transition, info, startT, finishT, |
| 568 |
finishCB); |
| 569 |
if (consumed) { |
| 570 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, " animated by %s", |
| 571 |
mHandlers.get(i)); |
| 572 |
return mHandlers.get(i); |
| 573 |
} |
| 574 |
} |
| 575 |
throw new IllegalStateException( |
| 576 |
"This shouldn't happen, maybe the default handler is broken."); |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public WindowContainerTransaction dispatchRequest(@NonNull IBinder transition, |
| 584 |
@NonNull TransitionRequestInfo request, @Nullable TransitionHandler skip) { |
| 585 |
for (int i = mHandlers.size() - 1; i >= 0; --i) { |
| 586 |
if (mHandlers.get(i) == skip) continue; |
| 587 |
WindowContainerTransaction wct = mHandlers.get(i).handleRequest(transition, request); |
| 588 |
if (wct != null) { |
| 589 |
return wct; |
| 590 |
} |
| 591 |
} |
| 592 |
return null; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
private void onAbort(IBinder transition) { |
| 597 |
onFinish(transition, null , null , true ); |
| 598 |
} |
| 599 |
|
| 600 |
private void onFinish(IBinder transition, |
| 601 |
@Nullable WindowContainerTransaction wct, |
| 602 |
@Nullable WindowContainerTransactionCallback wctCB) { |
| 603 |
onFinish(transition, wct, wctCB, false ); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
private void releaseSurfaces(@Nullable TransitionInfo info) { |
| 611 |
if (info == null) return; |
| 612 |
info.releaseAnimSurfaces(); |
| 613 |
} |
| 614 |
|
| 615 |
private void onFinish(IBinder transition, |
| 616 |
@Nullable WindowContainerTransaction wct, |
| 617 |
@Nullable WindowContainerTransactionCallback wctCB, |
| 618 |
boolean abort) { |
| 619 |
int activeIdx = findActiveTransition(transition); |
| 620 |
if (activeIdx < 0) { |
| 621 |
Log.e(TAG, "Trying to finish a non-running transition. Either remote crashed or " |
| 622 |
+ " a handler didn't properly deal with a merge.", new RuntimeException()); |
| 623 |
return; |
| 624 |
} else if (activeIdx > 0) { |
| 625 |
|
| 626 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Transition was merged (abort=%b:" |
| 627 |
+ " %s", abort, transition); |
| 628 |
final ActiveTransition active = mActiveTransitions.get(activeIdx); |
| 629 |
active.mMerged = true; |
| 630 |
active.mAborted = abort; |
| 631 |
if (active.mHandler != null) { |
| 632 |
active.mHandler.onTransitionConsumed( |
| 633 |
active.mToken, abort, abort ? null : active.mFinishT); |
| 634 |
} |
| 635 |
for (int i = 0; i < mObservers.size(); ++i) { |
| 636 |
mObservers.get(i).onTransitionMerged( |
| 637 |
active.mToken, mActiveTransitions.get(0).mToken); |
| 638 |
} |
| 639 |
return; |
| 640 |
} |
| 641 |
final ActiveTransition active = mActiveTransitions.get(activeIdx); |
| 642 |
active.mAborted = abort; |
| 643 |
if (active.mAborted && active.mHandler != null) { |
| 644 |
|
| 645 |
active.mHandler.onTransitionConsumed( |
| 646 |
transition, true , null ); |
| 647 |
} |
| 648 |
for (int i = 0; i < mObservers.size(); ++i) { |
| 649 |
mObservers.get(i).onTransitionFinished(active.mToken, active.mAborted); |
| 650 |
} |
| 651 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, |
| 652 |
"Transition animation finished (abort=%b), notifying core %s", abort, transition); |
| 653 |
if (active.mStartT != null) { |
| 654 |
|
| 655 |
|
| 656 |
active.mStartT.close(); |
| 657 |
} |
| 658 |
|
| 659 |
SurfaceControl.Transaction fullFinish = active.mFinishT; |
| 660 |
for (int iA = activeIdx + 1; iA < mActiveTransitions.size(); ++iA) { |
| 661 |
final ActiveTransition toMerge = mActiveTransitions.get(iA); |
| 662 |
if (!toMerge.mMerged) break; |
| 663 |
|
| 664 |
if (mActiveTransitions.get(iA).mStartT == null) break; |
| 665 |
if (fullFinish == null) { |
| 666 |
fullFinish = new SurfaceControl.Transaction(); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
fullFinish.merge(mActiveTransitions.get(iA).mStartT); |
| 671 |
fullFinish.merge(mActiveTransitions.get(iA).mFinishT); |
| 672 |
} |
| 673 |
if (fullFinish != null) { |
| 674 |
fullFinish.apply(); |
| 675 |
} |
| 676 |
|
| 677 |
releaseSurfaces(active.mInfo); |
| 678 |
mActiveTransitions.remove(activeIdx); |
| 679 |
mOrganizer.finishTransition(transition, wct, wctCB); |
| 680 |
while (activeIdx < mActiveTransitions.size()) { |
| 681 |
if (!mActiveTransitions.get(activeIdx).mMerged) break; |
| 682 |
ActiveTransition merged = mActiveTransitions.remove(activeIdx); |
| 683 |
mOrganizer.finishTransition(merged.mToken, null , null ); |
| 684 |
releaseSurfaces(merged.mInfo); |
| 685 |
} |
| 686 |
|
| 687 |
while (mActiveTransitions.size() > activeIdx |
| 688 |
&& mActiveTransitions.get(activeIdx).mAborted) { |
| 689 |
ActiveTransition aborted = mActiveTransitions.remove(activeIdx); |
| 690 |
|
| 691 |
if (aborted.mHandler != null) { |
| 692 |
aborted.mHandler.onTransitionConsumed( |
| 693 |
transition, true , null ); |
| 694 |
} |
| 695 |
mOrganizer.finishTransition(aborted.mToken, null , null ); |
| 696 |
for (int i = 0; i < mObservers.size(); ++i) { |
| 697 |
mObservers.get(i).onTransitionFinished(aborted.mToken, true); |
| 698 |
} |
| 699 |
releaseSurfaces(aborted.mInfo); |
| 700 |
} |
| 701 |
if (mActiveTransitions.size() <= activeIdx) { |
| 702 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "All active transition animations " |
| 703 |
+ "finished"); |
| 704 |
|
| 705 |
for (int i = 0; i < mRunWhenIdleQueue.size(); i++) { |
| 706 |
mRunWhenIdleQueue.get(i).run(); |
| 707 |
} |
| 708 |
mRunWhenIdleQueue.clear(); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
final ActiveTransition next = mActiveTransitions.get(activeIdx); |
| 713 |
if (next.mInfo == null) { |
| 714 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Pending transition after one" |
| 715 |
+ " finished, but it isn't ready yet."); |
| 716 |
return; |
| 717 |
} |
| 718 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Pending transitions after one" |
| 719 |
+ " finished, so start the next one."); |
| 720 |
playTransition(next); |
| 721 |
|
| 722 |
|
| 723 |
activeIdx = findActiveTransition(next.mToken); |
| 724 |
if (activeIdx < 0) { |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
return; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
int mergeIdx = activeIdx + 1; |
| 734 |
while (mergeIdx < mActiveTransitions.size()) { |
| 735 |
ActiveTransition mergeCandidate = mActiveTransitions.get(mergeIdx); |
| 736 |
if (mergeCandidate.mAborted) { |
| 737 |
|
| 738 |
|
| 739 |
++mergeIdx; |
| 740 |
continue; |
| 741 |
} |
| 742 |
if (mergeCandidate.mMerged) { |
| 743 |
throw new IllegalStateException("Can't merge a transition after not-merging" |
| 744 |
+ " a preceding one."); |
| 745 |
} |
| 746 |
attemptMergeTransition(next, mergeCandidate); |
| 747 |
mergeIdx = findActiveTransition(mergeCandidate.mToken); |
| 748 |
if (mergeIdx < 0) { |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
return; |
| 753 |
} |
| 754 |
++mergeIdx; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
void requestStartTransition(@NonNull IBinder transitionToken, |
| 759 |
@Nullable TransitionRequestInfo request) { |
| 760 |
ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "Transition requested: %s %s", |
| 761 |
transitionToken, request); |
| 762 |
if (findActiveTransition(transitionToken) >= 0) { |
| 763 |
throw new RuntimeException("Transition already started " + transitionToken); |
| 764 |
} |
| 765 |
final ActiveTransition active = new ActiveTransition(); |
| 766 |
WindowContainerTransaction wct = null; |
| 767 |
for (int i = mHandlers.size() - 1; i >= 0; --i) { |
| 768 |
wct = mHandlers.get(i).handleRequest(transitionToken, request); |
| 769 |
if (wct != null) { |
| 770 |
active.mHandler = mHandlers.get(i); |
| 771 |
break; |
| 772 |
} |
| 773 |
} |
| 774 |
if (request.getDisplayChange() != null) { |
| 775 |
TransitionRequestInfo.DisplayChange change = request.getDisplayChange(); |
| 776 |
if (change.getEndRotation() != change.getStartRotation()) { |
| 777 |
|
| 778 |
if (wct == null) { |
| 779 |
wct = new WindowContainerTransaction(); |
| 780 |
} |
| 781 |
mDisplayController.getChangeController().dispatchOnDisplayChange(wct, |
| 782 |
change.getDisplayId(), change.getStartRotation(), change.getEndRotation(), |
| 783 |
null ); |
| 784 |
} |
| 785 |
} |
| 786 |
mOrganizer.startTransition(transitionToken, wct != null && wct.isEmpty() ? null : wct); |
| 787 |
active.mToken = transitionToken; |
| 788 |
mActiveTransitions.add(active); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
public IBinder startTransition(@WindowManager.TransitionType int type, |
| 793 |
@NonNull WindowContainerTransaction wct, @Nullable TransitionHandler handler) { |
| 794 |
final ActiveTransition active = new ActiveTransition(); |
| 795 |
active.mHandler = handler; |
| 796 |
active.mToken = mOrganizer.startNewTransition(type, wct); |
| 797 |
mActiveTransitions.add(active); |
| 798 |
return active.mToken; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
public interface TransitionFinishCallback { |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
void onTransitionFinished(@Nullable WindowContainerTransaction wct, |
| 815 |
@Nullable WindowContainerTransactionCallback wctCB); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public interface TransitionHandler { |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
boolean startAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info, |
| 837 |
@NonNull SurfaceControl.Transaction startTransaction, |
| 838 |
@NonNull SurfaceControl.Transaction finishTransaction, |
| 839 |
@NonNull TransitionFinishCallback finishCallback); |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
default void mergeAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info, |
| 864 |
@NonNull SurfaceControl.Transaction t, @NonNull IBinder mergeTarget, |
| 865 |
@NonNull TransitionFinishCallback finishCallback) { } |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
@Nullable |
| 876 |
WindowContainerTransaction handleRequest(@NonNull IBinder transition, |
| 877 |
@NonNull TransitionRequestInfo request); |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
default void onTransitionConsumed(@NonNull IBinder transition, boolean aborted, |
| 889 |
@Nullable SurfaceControl.Transaction finishTransaction) { } |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
default void setAnimScaleSetting(float scale) {} |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
public interface TransitionObserver { |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
void onTransitionReady(@NonNull IBinder transition, @NonNull TransitionInfo info, |
| 917 |
@NonNull SurfaceControl.Transaction startTransaction, |
| 918 |
@NonNull SurfaceControl.Transaction finishTransaction); |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
void onTransitionStarting(@NonNull IBinder transition); |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
void onTransitionMerged(@NonNull IBinder merged, @NonNull IBinder playing); |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
void onTransitionFinished(@NonNull IBinder transition, boolean aborted); |
| 943 |
} |
| 944 |
|
| 945 |
@BinderThread |
| 946 |
private class TransitionPlayerImpl extends ITransitionPlayer.Stub { |
| 947 |
@Override |
| 948 |
public void onTransitionReady(IBinder iBinder, TransitionInfo transitionInfo, |
| 949 |
SurfaceControl.Transaction t, SurfaceControl.Transaction finishT) |
| 950 |
throws RemoteException { |
| 951 |
mMainExecutor.execute(() -> Transitions.this.onTransitionReady( |
| 952 |
iBinder, transitionInfo, t, finishT)); |
| 953 |
} |
| 954 |
|
| 955 |
@Override |
| 956 |
public void requestStartTransition(IBinder iBinder, |
| 957 |
TransitionRequestInfo request) throws RemoteException { |
| 958 |
mMainExecutor.execute(() -> Transitions.this.requestStartTransition(iBinder, request)); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
@ExternalThread |
| 966 |
private class ShellTransitionImpl implements ShellTransitions { |
| 967 |
@Override |
| 968 |
public void registerRemote(@NonNull TransitionFilter filter, |
| 969 |
@NonNull RemoteTransition remoteTransition) { |
| 970 |
mMainExecutor.execute(() -> { |
| 971 |
mRemoteTransitionHandler.addFiltered(filter, remoteTransition); |
| 972 |
}); |
| 973 |
} |
| 974 |
|
| 975 |
@Override |
| 976 |
public void unregisterRemote(@NonNull RemoteTransition remoteTransition) { |
| 977 |
mMainExecutor.execute(() -> { |
| 978 |
mRemoteTransitionHandler.removeFiltered(remoteTransition); |
| 979 |
}); |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
@BinderThread |
| 987 |
private static class IShellTransitionsImpl extends IShellTransitions.Stub |
| 988 |
implements ExternalInterfaceBinder { |
| 989 |
private Transitions mTransitions; |
| 990 |
|
| 991 |
IShellTransitionsImpl(Transitions transitions) { |
| 992 |
mTransitions = transitions; |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
@Override |
| 999 |
public void invalidate() { |
| 1000 |
mTransitions = null; |
| 1001 |
} |
| 1002 |
|
| 1003 |
@Override |
| 1004 |
public void registerRemote(@NonNull TransitionFilter filter, |
| 1005 |
@NonNull RemoteTransition remoteTransition) { |
| 1006 |
executeRemoteCallWithTaskPermission(mTransitions, "registerRemote", |
| 1007 |
(transitions) -> { |
| 1008 |
transitions.mRemoteTransitionHandler.addFiltered(filter, remoteTransition); |
| 1009 |
}); |
| 1010 |
} |
| 1011 |
|
| 1012 |
@Override |
| 1013 |
public void unregisterRemote(@NonNull RemoteTransition remoteTransition) { |
| 1014 |
executeRemoteCallWithTaskPermission(mTransitions, "unregisterRemote", |
| 1015 |
(transitions) -> { |
| 1016 |
transitions.mRemoteTransitionHandler.removeFiltered(remoteTransition); |
| 1017 |
}); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
private class SettingsObserver extends ContentObserver { |
| 1022 |
|
| 1023 |
SettingsObserver() { |
| 1024 |
super(null); |
| 1025 |
} |
| 1026 |
|
| 1027 |
@Override |
| 1028 |
public void onChange(boolean selfChange) { |
| 1029 |
super.onChange(selfChange); |
| 1030 |
mTransitionAnimationScaleSetting = getTransitionAnimationScaleSetting(); |
| 1031 |
|
| 1032 |
mMainExecutor.execute(() -> dispatchAnimScaleSetting(mTransitionAnimationScaleSetting)); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
} |