| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.back; |
| 18 |
|
| 19 |
import static android.view.RemoteAnimationTarget.MODE_CLOSING; |
| 20 |
import static android.view.RemoteAnimationTarget.MODE_OPENING; |
| 21 |
|
| 22 |
import static com.android.wm.shell.common.ExecutorUtils.executeRemoteCallWithTaskPermission; |
| 23 |
import static com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_BACK_PREVIEW; |
| 24 |
import static com.android.wm.shell.sysui.ShellSharedConstants.KEY_EXTRA_SHELL_BACK_ANIMATION; |
| 25 |
|
| 26 |
import android.annotation.NonNull; |
| 27 |
import android.annotation.Nullable; |
| 28 |
import android.app.ActivityTaskManager; |
| 29 |
import android.app.IActivityTaskManager; |
| 30 |
import android.app.WindowConfiguration; |
| 31 |
import android.content.ContentResolver; |
| 32 |
import android.content.Context; |
| 33 |
import android.database.ContentObserver; |
| 34 |
import android.hardware.HardwareBuffer; |
| 35 |
import android.hardware.input.InputManager; |
| 36 |
import android.net.Uri; |
| 37 |
import android.os.Handler; |
| 38 |
import android.os.IBinder; |
| 39 |
import android.os.RemoteException; |
| 40 |
import android.os.SystemClock; |
| 41 |
import android.os.SystemProperties; |
| 42 |
import android.os.UserHandle; |
| 43 |
import android.provider.Settings.Global; |
| 44 |
import android.util.Log; |
| 45 |
import android.view.IWindowFocusObserver; |
| 46 |
import android.view.InputDevice; |
| 47 |
import android.view.KeyCharacterMap; |
| 48 |
import android.view.KeyEvent; |
| 49 |
import android.view.MotionEvent; |
| 50 |
import android.view.RemoteAnimationTarget; |
| 51 |
import android.view.SurfaceControl; |
| 52 |
import android.window.BackAnimationAdaptor; |
| 53 |
import android.window.BackEvent; |
| 54 |
import android.window.BackNavigationInfo; |
| 55 |
import android.window.IBackAnimationRunner; |
| 56 |
import android.window.IBackNaviAnimationController; |
| 57 |
import android.window.IOnBackInvokedCallback; |
| 58 |
|
| 59 |
import com.android.internal.annotations.VisibleForTesting; |
| 60 |
import com.android.internal.protolog.common.ProtoLog; |
| 61 |
import com.android.wm.shell.common.ExternalInterfaceBinder; |
| 62 |
import com.android.wm.shell.common.RemoteCallable; |
| 63 |
import com.android.wm.shell.common.ShellExecutor; |
| 64 |
import com.android.wm.shell.common.annotations.ShellBackgroundThread; |
| 65 |
import com.android.wm.shell.common.annotations.ShellMainThread; |
| 66 |
import com.android.wm.shell.sysui.ShellController; |
| 67 |
import com.android.wm.shell.sysui.ShellInit; |
| 68 |
|
| 69 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public class BackAnimationController implements RemoteCallable<BackAnimationController> { |
| 75 |
private static final String TAG = "BackAnimationController"; |
| 76 |
private static final int SETTING_VALUE_OFF = 0; |
| 77 |
private static final int SETTING_VALUE_ON = 1; |
| 78 |
public static final boolean IS_ENABLED = |
| 79 |
SystemProperties.getInt("persist.wm.debug.predictive_back", |
| 80 |
SETTING_VALUE_ON) == SETTING_VALUE_ON; |
| 81 |
|
| 82 |
public static boolean IS_U_ANIMATION_ENABLED = |
| 83 |
SystemProperties.getInt("persist.wm.debug.predictive_back_anim", |
| 84 |
SETTING_VALUE_OFF) == SETTING_VALUE_ON; |
| 85 |
|
| 86 |
private final AtomicBoolean mEnableAnimations = new AtomicBoolean(false); |
| 87 |
|
| 88 |
private static final boolean USE_TRANSITION = |
| 89 |
SystemProperties.getInt("persist.wm.debug.predictive_back_ani_trans", 1) != 0; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private static final long MAX_TRANSITION_DURATION = 2000; |
| 95 |
|
| 96 |
|
| 97 |
private boolean mBackGestureStarted = false; |
| 98 |
|
| 99 |
|
| 100 |
private boolean mTransitionInProgress = false; |
| 101 |
|
| 102 |
private boolean mShouldStartOnNextMoveEvent = false; |
| 103 |
|
| 104 |
private boolean mTriggerBack; |
| 105 |
|
| 106 |
@Nullable |
| 107 |
private BackNavigationInfo mBackNavigationInfo; |
| 108 |
private final SurfaceControl.Transaction mTransaction; |
| 109 |
private final IActivityTaskManager mActivityTaskManager; |
| 110 |
private final Context mContext; |
| 111 |
private final ContentResolver mContentResolver; |
| 112 |
private final ShellController mShellController; |
| 113 |
private final ShellExecutor mShellExecutor; |
| 114 |
private final Handler mBgHandler; |
| 115 |
@Nullable |
| 116 |
private IOnBackInvokedCallback mBackToLauncherCallback; |
| 117 |
private float mTriggerThreshold; |
| 118 |
private final Runnable mResetTransitionRunnable = () -> { |
| 119 |
finishAnimation(); |
| 120 |
mTransitionInProgress = false; |
| 121 |
}; |
| 122 |
|
| 123 |
private RemoteAnimationTarget mAnimationTarget; |
| 124 |
IBackAnimationRunner mIBackAnimationRunner; |
| 125 |
private IBackNaviAnimationController mBackAnimationController; |
| 126 |
private BackAnimationAdaptor mBackAnimationAdaptor; |
| 127 |
|
| 128 |
private final TouchTracker mTouchTracker = new TouchTracker(); |
| 129 |
private final CachingBackDispatcher mCachingBackDispatcher = new CachingBackDispatcher(); |
| 130 |
|
| 131 |
@VisibleForTesting |
| 132 |
final IWindowFocusObserver mFocusObserver = new IWindowFocusObserver.Stub() { |
| 133 |
@Override |
| 134 |
public void focusGained(IBinder inputToken) { } |
| 135 |
@Override |
| 136 |
public void focusLost(IBinder inputToken) { |
| 137 |
mShellExecutor.execute(() -> { |
| 138 |
if (!mBackGestureStarted || mTransitionInProgress) { |
| 139 |
|
| 140 |
|
| 141 |
return; |
| 142 |
} |
| 143 |
setTriggerBack(false); |
| 144 |
onGestureFinished(false); |
| 145 |
}); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private class CachingBackDispatcher { |
| 154 |
private IOnBackInvokedCallback mOnBackCallback; |
| 155 |
private boolean mTriggerBack; |
| 156 |
|
| 157 |
private boolean mWaitingAnimation; |
| 158 |
|
| 159 |
void startWaitingAnimation() { |
| 160 |
mWaitingAnimation = true; |
| 161 |
} |
| 162 |
|
| 163 |
boolean set(IOnBackInvokedCallback callback, boolean triggerBack) { |
| 164 |
if (mWaitingAnimation) { |
| 165 |
mOnBackCallback = callback; |
| 166 |
mTriggerBack = triggerBack; |
| 167 |
return true; |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
boolean consume() { |
| 173 |
boolean consumed = false; |
| 174 |
if (mWaitingAnimation && mOnBackCallback != null) { |
| 175 |
if (mTriggerBack) { |
| 176 |
final BackEvent backFinish = mTouchTracker.createProgressEvent(1); |
| 177 |
dispatchOnBackProgressed(mBackToLauncherCallback, backFinish); |
| 178 |
dispatchOnBackInvoked(mOnBackCallback); |
| 179 |
} else { |
| 180 |
final BackEvent backFinish = mTouchTracker.createProgressEvent(0); |
| 181 |
dispatchOnBackProgressed(mBackToLauncherCallback, backFinish); |
| 182 |
dispatchOnBackCancelled(mOnBackCallback); |
| 183 |
} |
| 184 |
startTransition(); |
| 185 |
consumed = true; |
| 186 |
} |
| 187 |
mOnBackCallback = null; |
| 188 |
mWaitingAnimation = false; |
| 189 |
return consumed; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
public BackAnimationController( |
| 194 |
@NonNull ShellInit shellInit, |
| 195 |
@NonNull ShellController shellController, |
| 196 |
@NonNull @ShellMainThread ShellExecutor shellExecutor, |
| 197 |
@NonNull @ShellBackgroundThread Handler backgroundHandler, |
| 198 |
Context context) { |
| 199 |
this(shellInit, shellController, shellExecutor, backgroundHandler, |
| 200 |
new SurfaceControl.Transaction(), ActivityTaskManager.getService(), |
| 201 |
context, context.getContentResolver()); |
| 202 |
} |
| 203 |
|
| 204 |
@VisibleForTesting |
| 205 |
BackAnimationController( |
| 206 |
@NonNull ShellInit shellInit, |
| 207 |
@NonNull ShellController shellController, |
| 208 |
@NonNull @ShellMainThread ShellExecutor shellExecutor, |
| 209 |
@NonNull @ShellBackgroundThread Handler bgHandler, |
| 210 |
@NonNull SurfaceControl.Transaction transaction, |
| 211 |
@NonNull IActivityTaskManager activityTaskManager, |
| 212 |
Context context, ContentResolver contentResolver) { |
| 213 |
mShellController = shellController; |
| 214 |
mShellExecutor = shellExecutor; |
| 215 |
mTransaction = transaction; |
| 216 |
mActivityTaskManager = activityTaskManager; |
| 217 |
mContext = context; |
| 218 |
mContentResolver = contentResolver; |
| 219 |
mBgHandler = bgHandler; |
| 220 |
shellInit.addInitCallback(this::onInit, this); |
| 221 |
} |
| 222 |
|
| 223 |
@VisibleForTesting |
| 224 |
void setEnableUAnimation(boolean enable) { |
| 225 |
IS_U_ANIMATION_ENABLED = enable; |
| 226 |
} |
| 227 |
|
| 228 |
private void onInit() { |
| 229 |
setupAnimationDeveloperSettingsObserver(mContentResolver, mBgHandler); |
| 230 |
mShellController.addExternalInterface(KEY_EXTRA_SHELL_BACK_ANIMATION, |
| 231 |
this::createExternalInterface, this); |
| 232 |
} |
| 233 |
|
| 234 |
private void setupAnimationDeveloperSettingsObserver( |
| 235 |
@NonNull ContentResolver contentResolver, |
| 236 |
@NonNull @ShellBackgroundThread final Handler backgroundHandler) { |
| 237 |
ContentObserver settingsObserver = new ContentObserver(backgroundHandler) { |
| 238 |
@Override |
| 239 |
public void onChange(boolean selfChange, Uri uri) { |
| 240 |
updateEnableAnimationFromSetting(); |
| 241 |
} |
| 242 |
}; |
| 243 |
contentResolver.registerContentObserver( |
| 244 |
Global.getUriFor(Global.ENABLE_BACK_ANIMATION), |
| 245 |
false, settingsObserver, UserHandle.USER_SYSTEM |
| 246 |
); |
| 247 |
updateEnableAnimationFromSetting(); |
| 248 |
} |
| 249 |
|
| 250 |
@ShellBackgroundThread |
| 251 |
private void updateEnableAnimationFromSetting() { |
| 252 |
int settingValue = Global.getInt(mContext.getContentResolver(), |
| 253 |
Global.ENABLE_BACK_ANIMATION, SETTING_VALUE_OFF); |
| 254 |
boolean isEnabled = settingValue == SETTING_VALUE_ON; |
| 255 |
mEnableAnimations.set(isEnabled); |
| 256 |
ProtoLog.d(WM_SHELL_BACK_PREVIEW, "Back animation enabled=%s", |
| 257 |
isEnabled); |
| 258 |
} |
| 259 |
|
| 260 |
public BackAnimation getBackAnimationImpl() { |
| 261 |
return mBackAnimation; |
| 262 |
} |
| 263 |
|
| 264 |
private ExternalInterfaceBinder createExternalInterface() { |
| 265 |
return new IBackAnimationImpl(this); |
| 266 |
} |
| 267 |
|
| 268 |
private final BackAnimationImpl mBackAnimation = new BackAnimationImpl(); |
| 269 |
|
| 270 |
@Override |
| 271 |
public Context getContext() { |
| 272 |
return mContext; |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public ShellExecutor getRemoteCallExecutor() { |
| 277 |
return mShellExecutor; |
| 278 |
} |
| 279 |
|
| 280 |
private class BackAnimationImpl implements BackAnimation { |
| 281 |
@Override |
| 282 |
public void onBackMotion( |
| 283 |
float touchX, float touchY, int keyAction, @BackEvent.SwipeEdge int swipeEdge) { |
| 284 |
mShellExecutor.execute(() -> onMotionEvent(touchX, touchY, keyAction, swipeEdge)); |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
public void setTriggerBack(boolean triggerBack) { |
| 289 |
mShellExecutor.execute(() -> BackAnimationController.this.setTriggerBack(triggerBack)); |
| 290 |
} |
| 291 |
|
| 292 |
@Override |
| 293 |
public void setSwipeThresholds(float triggerThreshold, float progressThreshold) { |
| 294 |
mShellExecutor.execute(() -> BackAnimationController.this.setSwipeThresholds( |
| 295 |
triggerThreshold, progressThreshold)); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
private static class IBackAnimationImpl extends IBackAnimation.Stub |
| 300 |
implements ExternalInterfaceBinder { |
| 301 |
private BackAnimationController mController; |
| 302 |
|
| 303 |
IBackAnimationImpl(BackAnimationController controller) { |
| 304 |
mController = controller; |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public void setBackToLauncherCallback(IOnBackInvokedCallback callback) { |
| 309 |
executeRemoteCallWithTaskPermission(mController, "setBackToLauncherCallback", |
| 310 |
(controller) -> controller.setBackToLauncherCallback(callback)); |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public void clearBackToLauncherCallback() { |
| 315 |
executeRemoteCallWithTaskPermission(mController, "clearBackToLauncherCallback", |
| 316 |
(controller) -> controller.clearBackToLauncherCallback()); |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public void onBackToLauncherAnimationFinished() { |
| 321 |
executeRemoteCallWithTaskPermission(mController, "onBackToLauncherAnimationFinished", |
| 322 |
(controller) -> controller.onBackToLauncherAnimationFinished()); |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public void invalidate() { |
| 327 |
mController = null; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
@VisibleForTesting |
| 332 |
void setBackToLauncherCallback(IOnBackInvokedCallback callback) { |
| 333 |
mBackToLauncherCallback = callback; |
| 334 |
if (USE_TRANSITION) { |
| 335 |
createAdaptor(); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
private void clearBackToLauncherCallback() { |
| 340 |
mBackToLauncherCallback = null; |
| 341 |
} |
| 342 |
|
| 343 |
@VisibleForTesting |
| 344 |
void onBackToLauncherAnimationFinished() { |
| 345 |
final boolean triggerBack = mTriggerBack; |
| 346 |
IOnBackInvokedCallback callback = mBackNavigationInfo != null |
| 347 |
? mBackNavigationInfo.getOnBackInvokedCallback() : null; |
| 348 |
|
| 349 |
finishAnimation(); |
| 350 |
if (callback != null) { |
| 351 |
if (triggerBack) { |
| 352 |
dispatchOnBackInvoked(callback); |
| 353 |
} else { |
| 354 |
dispatchOnBackCancelled(callback); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public void onMotionEvent(float touchX, float touchY, int keyAction, |
| 364 |
@BackEvent.SwipeEdge int swipeEdge) { |
| 365 |
if (mTransitionInProgress) { |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
mTouchTracker.update(touchX, touchY); |
| 370 |
if (keyAction == MotionEvent.ACTION_DOWN) { |
| 371 |
if (!mBackGestureStarted) { |
| 372 |
mShouldStartOnNextMoveEvent = true; |
| 373 |
} |
| 374 |
} else if (keyAction == MotionEvent.ACTION_MOVE) { |
| 375 |
if (!mBackGestureStarted && mShouldStartOnNextMoveEvent) { |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
onGestureStarted(touchX, touchY, swipeEdge); |
| 380 |
mShouldStartOnNextMoveEvent = false; |
| 381 |
} |
| 382 |
onMove(touchX, touchY, swipeEdge); |
| 383 |
} else if (keyAction == MotionEvent.ACTION_UP || keyAction == MotionEvent.ACTION_CANCEL) { |
| 384 |
ProtoLog.d(WM_SHELL_BACK_PREVIEW, |
| 385 |
"Finishing gesture with event action: %d", keyAction); |
| 386 |
if (keyAction == MotionEvent.ACTION_CANCEL) { |
| 387 |
mTriggerBack = false; |
| 388 |
} |
| 389 |
onGestureFinished(true); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
private void onGestureStarted(float touchX, float touchY, @BackEvent.SwipeEdge int swipeEdge) { |
| 394 |
ProtoLog.d(WM_SHELL_BACK_PREVIEW, "initAnimation mMotionStarted=%b", mBackGestureStarted); |
| 395 |
if (mBackGestureStarted || mBackNavigationInfo != null) { |
| 396 |
Log.e(TAG, "Animation is being initialized but is already started."); |
| 397 |
finishAnimation(); |
| 398 |
} |
| 399 |
|
| 400 |
mTouchTracker.setGestureStartLocation(touchX, touchY, swipeEdge); |
| 401 |
mBackGestureStarted = true; |
| 402 |
|
| 403 |
try { |
| 404 |
boolean requestAnimation = mEnableAnimations.get(); |
| 405 |
mBackNavigationInfo = mActivityTaskManager.startBackNavigation(requestAnimation, |
| 406 |
mFocusObserver, mBackAnimationAdaptor); |
| 407 |
onBackNavigationInfoReceived(mBackNavigationInfo); |
| 408 |
} catch (RemoteException remoteException) { |
| 409 |
Log.e(TAG, "Failed to initAnimation", remoteException); |
| 410 |
finishAnimation(); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
private void onBackNavigationInfoReceived(@Nullable BackNavigationInfo backNavigationInfo) { |
| 415 |
ProtoLog.d(WM_SHELL_BACK_PREVIEW, "Received backNavigationInfo:%s", backNavigationInfo); |
| 416 |
if (backNavigationInfo == null) { |
| 417 |
Log.e(TAG, "Received BackNavigationInfo is null."); |
| 418 |
return; |
| 419 |
} |
| 420 |
int backType = backNavigationInfo.getType(); |
| 421 |
IOnBackInvokedCallback targetCallback = null; |
| 422 |
final boolean dispatchToLauncher = shouldDispatchToLauncher(backType); |
| 423 |
if (backType == BackNavigationInfo.TYPE_CROSS_ACTIVITY) { |
| 424 |
HardwareBuffer hardwareBuffer = backNavigationInfo.getScreenshotHardwareBuffer(); |
| 425 |
if (hardwareBuffer != null) { |
| 426 |
displayTargetScreenshot(hardwareBuffer, |
| 427 |
backNavigationInfo.getTaskWindowConfiguration()); |
| 428 |
} |
| 429 |
targetCallback = mBackNavigationInfo.getOnBackInvokedCallback(); |
| 430 |
mTransaction.apply(); |
| 431 |
} else if (dispatchToLauncher) { |
| 432 |
targetCallback = mBackToLauncherCallback; |
| 433 |
if (USE_TRANSITION) { |
| 434 |
mCachingBackDispatcher.startWaitingAnimation(); |
| 435 |
} |
| 436 |
} else if (backType == BackNavigationInfo.TYPE_CALLBACK) { |
| 437 |
targetCallback = mBackNavigationInfo.getOnBackInvokedCallback(); |
| 438 |
} |
| 439 |
if (!USE_TRANSITION || !dispatchToLauncher) { |
| 440 |
dispatchOnBackStarted( |
| 441 |
targetCallback, |
| 442 |
mTouchTracker.createStartEvent( |
| 443 |
mBackNavigationInfo.getDepartingAnimationTarget())); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
private void displayTargetScreenshot(@NonNull HardwareBuffer hardwareBuffer, |
| 453 |
WindowConfiguration taskWindowConfiguration) { |
| 454 |
SurfaceControl screenshotSurface = |
| 455 |
mBackNavigationInfo == null ? null : mBackNavigationInfo.getScreenshotSurface(); |
| 456 |
if (screenshotSurface == null) { |
| 457 |
Log.e(TAG, "BackNavigationInfo doesn't contain a surface for the screenshot. "); |
| 458 |
return; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
float sx = 1; |
| 463 |
float sy = 1; |
| 464 |
float w = taskWindowConfiguration.getBounds().width(); |
| 465 |
float h = taskWindowConfiguration.getBounds().height(); |
| 466 |
|
| 467 |
if (w != hardwareBuffer.getWidth()) { |
| 468 |
sx = w / hardwareBuffer.getWidth(); |
| 469 |
} |
| 470 |
|
| 471 |
if (h != hardwareBuffer.getHeight()) { |
| 472 |
sy = h / hardwareBuffer.getHeight(); |
| 473 |
} |
| 474 |
mTransaction.setScale(screenshotSurface, sx, sy); |
| 475 |
mTransaction.setBuffer(screenshotSurface, hardwareBuffer); |
| 476 |
mTransaction.setVisibility(screenshotSurface, true); |
| 477 |
} |
| 478 |
|
| 479 |
private void onMove(float touchX, float touchY, @BackEvent.SwipeEdge int swipeEdge) { |
| 480 |
if (!mBackGestureStarted || mBackNavigationInfo == null) { |
| 481 |
return; |
| 482 |
} |
| 483 |
final BackEvent backEvent = mTouchTracker.createProgressEvent(); |
| 484 |
if (USE_TRANSITION && mBackAnimationController != null && mAnimationTarget != null) { |
| 485 |
dispatchOnBackProgressed(mBackToLauncherCallback, backEvent); |
| 486 |
} else if (mEnableAnimations.get()) { |
| 487 |
int backType = mBackNavigationInfo.getType(); |
| 488 |
IOnBackInvokedCallback targetCallback; |
| 489 |
if (shouldDispatchToLauncher(backType)) { |
| 490 |
targetCallback = mBackToLauncherCallback; |
| 491 |
} else { |
| 492 |
targetCallback = mBackNavigationInfo.getOnBackInvokedCallback(); |
| 493 |
} |
| 494 |
dispatchOnBackProgressed(targetCallback, backEvent); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
private void injectBackKey() { |
| 499 |
sendBackEvent(KeyEvent.ACTION_DOWN); |
| 500 |
sendBackEvent(KeyEvent.ACTION_UP); |
| 501 |
} |
| 502 |
|
| 503 |
private void sendBackEvent(int action) { |
| 504 |
final long when = SystemClock.uptimeMillis(); |
| 505 |
final KeyEvent ev = new KeyEvent(when, when, action, KeyEvent.KEYCODE_BACK, 0 , |
| 506 |
0 , KeyCharacterMap.VIRTUAL_KEYBOARD, 0 , |
| 507 |
KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, |
| 508 |
InputDevice.SOURCE_KEYBOARD); |
| 509 |
|
| 510 |
ev.setDisplayId(mContext.getDisplay().getDisplayId()); |
| 511 |
if (!InputManager.getInstance() |
| 512 |
.injectInputEvent(ev, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC)) { |
| 513 |
Log.e(TAG, "Inject input event fail"); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
private void onGestureFinished(boolean fromTouch) { |
| 518 |
ProtoLog.d(WM_SHELL_BACK_PREVIEW, "onGestureFinished() mTriggerBack == %s", mTriggerBack); |
| 519 |
if (!mBackGestureStarted) { |
| 520 |
finishAnimation(); |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
if (fromTouch) { |
| 525 |
|
| 526 |
|
| 527 |
mBackGestureStarted = false; |
| 528 |
} |
| 529 |
|
| 530 |
if (mTransitionInProgress) { |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
if (mBackNavigationInfo == null) { |
| 535 |
|
| 536 |
|
| 537 |
if (mTriggerBack) { |
| 538 |
injectBackKey(); |
| 539 |
} |
| 540 |
finishAnimation(); |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
int backType = mBackNavigationInfo.getType(); |
| 545 |
boolean shouldDispatchToLauncher = shouldDispatchToLauncher(backType); |
| 546 |
IOnBackInvokedCallback targetCallback = shouldDispatchToLauncher |
| 547 |
? mBackToLauncherCallback |
| 548 |
: mBackNavigationInfo.getOnBackInvokedCallback(); |
| 549 |
if (mCachingBackDispatcher.set(targetCallback, mTriggerBack)) { |
| 550 |
return; |
| 551 |
} |
| 552 |
if (shouldDispatchToLauncher) { |
| 553 |
startTransition(); |
| 554 |
} |
| 555 |
if (mTriggerBack) { |
| 556 |
dispatchOnBackInvoked(targetCallback); |
| 557 |
} else { |
| 558 |
dispatchOnBackCancelled(targetCallback); |
| 559 |
} |
| 560 |
if (backType != BackNavigationInfo.TYPE_RETURN_TO_HOME || !shouldDispatchToLauncher) { |
| 561 |
|
| 562 |
finishAnimation(); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
private boolean shouldDispatchToLauncher(int backType) { |
| 567 |
return backType == BackNavigationInfo.TYPE_RETURN_TO_HOME |
| 568 |
&& mBackToLauncherCallback != null |
| 569 |
&& mEnableAnimations.get() |
| 570 |
&& mBackNavigationInfo != null |
| 571 |
&& ((USE_TRANSITION && mBackNavigationInfo.isPrepareRemoteAnimation()) |
| 572 |
|| mBackNavigationInfo.getDepartingAnimationTarget() != null); |
| 573 |
} |
| 574 |
|
| 575 |
private void dispatchOnBackStarted(IOnBackInvokedCallback callback, |
| 576 |
BackEvent backEvent) { |
| 577 |
if (callback == null) { |
| 578 |
return; |
| 579 |
} |
| 580 |
try { |
| 581 |
if (shouldDispatchAnimation(callback)) { |
| 582 |
callback.onBackStarted(backEvent); |
| 583 |
} |
| 584 |
} catch (RemoteException e) { |
| 585 |
Log.e(TAG, "dispatchOnBackStarted error: ", e); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
private void dispatchOnBackInvoked(IOnBackInvokedCallback callback) { |
| 590 |
if (callback == null) { |
| 591 |
return; |
| 592 |
} |
| 593 |
try { |
| 594 |
callback.onBackInvoked(); |
| 595 |
} catch (RemoteException e) { |
| 596 |
Log.e(TAG, "dispatchOnBackInvoked error: ", e); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
private void dispatchOnBackCancelled(IOnBackInvokedCallback callback) { |
| 601 |
if (callback == null) { |
| 602 |
return; |
| 603 |
} |
| 604 |
try { |
| 605 |
if (shouldDispatchAnimation(callback)) { |
| 606 |
callback.onBackCancelled(); |
| 607 |
} |
| 608 |
} catch (RemoteException e) { |
| 609 |
Log.e(TAG, "dispatchOnBackCancelled error: ", e); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
private void dispatchOnBackProgressed(IOnBackInvokedCallback callback, |
| 614 |
BackEvent backEvent) { |
| 615 |
if (callback == null) { |
| 616 |
return; |
| 617 |
} |
| 618 |
try { |
| 619 |
if (shouldDispatchAnimation(callback)) { |
| 620 |
callback.onBackProgressed(backEvent); |
| 621 |
} |
| 622 |
} catch (RemoteException e) { |
| 623 |
Log.e(TAG, "dispatchOnBackProgressed error: ", e); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
private boolean shouldDispatchAnimation(IOnBackInvokedCallback callback) { |
| 628 |
return (IS_U_ANIMATION_ENABLED || callback == mBackToLauncherCallback) |
| 629 |
&& mEnableAnimations.get(); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public void setTriggerBack(boolean triggerBack) { |
| 636 |
if (mTransitionInProgress) { |
| 637 |
return; |
| 638 |
} |
| 639 |
mTriggerBack = triggerBack; |
| 640 |
mTouchTracker.setTriggerBack(triggerBack); |
| 641 |
} |
| 642 |
|
| 643 |
private void setSwipeThresholds(float triggerThreshold, float progressThreshold) { |
| 644 |
mTouchTracker.setProgressThreshold(progressThreshold); |
| 645 |
mTriggerThreshold = triggerThreshold; |
| 646 |
} |
| 647 |
|
| 648 |
private void finishAnimation() { |
| 649 |
ProtoLog.d(WM_SHELL_BACK_PREVIEW, "BackAnimationController: finishAnimation()"); |
| 650 |
mTouchTracker.reset(); |
| 651 |
BackNavigationInfo backNavigationInfo = mBackNavigationInfo; |
| 652 |
boolean triggerBack = mTriggerBack; |
| 653 |
mBackNavigationInfo = null; |
| 654 |
mAnimationTarget = null; |
| 655 |
mTriggerBack = false; |
| 656 |
mShouldStartOnNextMoveEvent = false; |
| 657 |
if (backNavigationInfo == null) { |
| 658 |
return; |
| 659 |
} |
| 660 |
|
| 661 |
if (!USE_TRANSITION) { |
| 662 |
RemoteAnimationTarget animationTarget = backNavigationInfo |
| 663 |
.getDepartingAnimationTarget(); |
| 664 |
if (animationTarget != null) { |
| 665 |
if (animationTarget.leash != null && animationTarget.leash.isValid()) { |
| 666 |
mTransaction.remove(animationTarget.leash); |
| 667 |
} |
| 668 |
} |
| 669 |
SurfaceControl screenshotSurface = backNavigationInfo.getScreenshotSurface(); |
| 670 |
if (screenshotSurface != null && screenshotSurface.isValid()) { |
| 671 |
mTransaction.remove(screenshotSurface); |
| 672 |
} |
| 673 |
mTransaction.apply(); |
| 674 |
} |
| 675 |
stopTransition(); |
| 676 |
backNavigationInfo.onBackNavigationFinished(triggerBack); |
| 677 |
if (USE_TRANSITION) { |
| 678 |
final IBackNaviAnimationController controller = mBackAnimationController; |
| 679 |
if (controller != null) { |
| 680 |
try { |
| 681 |
controller.finish(triggerBack); |
| 682 |
} catch (RemoteException r) { |
| 683 |
|
| 684 |
} |
| 685 |
} |
| 686 |
mBackAnimationController = null; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
private void startTransition() { |
| 691 |
if (mTransitionInProgress) { |
| 692 |
return; |
| 693 |
} |
| 694 |
mTransitionInProgress = true; |
| 695 |
mShellExecutor.executeDelayed(mResetTransitionRunnable, MAX_TRANSITION_DURATION); |
| 696 |
} |
| 697 |
|
| 698 |
private void stopTransition() { |
| 699 |
if (!mTransitionInProgress) { |
| 700 |
return; |
| 701 |
} |
| 702 |
mShellExecutor.removeCallbacks(mResetTransitionRunnable); |
| 703 |
mTransitionInProgress = false; |
| 704 |
} |
| 705 |
|
| 706 |
private void createAdaptor() { |
| 707 |
mIBackAnimationRunner = new IBackAnimationRunner.Stub() { |
| 708 |
@Override |
| 709 |
public void onAnimationCancelled() { |
| 710 |
|
| 711 |
} |
| 712 |
@Override |
| 713 |
public void onAnimationStart(IBackNaviAnimationController controller, int type, |
| 714 |
RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, |
| 715 |
RemoteAnimationTarget[] nonApps) { |
| 716 |
mShellExecutor.execute(() -> { |
| 717 |
mBackAnimationController = controller; |
| 718 |
for (int i = 0; i < apps.length; i++) { |
| 719 |
final RemoteAnimationTarget target = apps[i]; |
| 720 |
if (MODE_CLOSING == target.mode) { |
| 721 |
mAnimationTarget = target; |
| 722 |
} else if (MODE_OPENING == target.mode) { |
| 723 |
|
| 724 |
|
| 725 |
SurfaceControl.Transaction tx = |
| 726 |
new SurfaceControl.Transaction(); |
| 727 |
tx.setAlpha(target.leash, 1); |
| 728 |
tx.apply(); |
| 729 |
} |
| 730 |
} |
| 731 |
dispatchOnBackStarted(mBackToLauncherCallback, |
| 732 |
mTouchTracker.createStartEvent(mAnimationTarget)); |
| 733 |
final BackEvent backInit = mTouchTracker.createProgressEvent(); |
| 734 |
if (!mCachingBackDispatcher.consume()) { |
| 735 |
dispatchOnBackProgressed(mBackToLauncherCallback, backInit); |
| 736 |
} |
| 737 |
}); |
| 738 |
} |
| 739 |
}; |
| 740 |
mBackAnimationAdaptor = new BackAnimationAdaptor(mIBackAnimationRunner, |
| 741 |
BackNavigationInfo.TYPE_RETURN_TO_HOME); |
| 742 |
} |
| 743 |
} |