[coastguard skipped] Merge sparse cherrypicks from sparse-10317220-L65500000961600299 into tm-qpr3-release.

COASTGUARD_SKIP: I1bb3331bf5553f9a1984dd7eb2c29d94651e8a14
COASTGUARD_SKIP: I7b3dcd06e7483fde745a1d56dfee7c4efc262ed7
COASTGUARD_SKIP: Ifcdac241bfe33f44f5f03f1a6db682c57f0cd388

Change-Id: Iafe5015e9e926c4837236974232c95aa4cf24398
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
index f1a8c95..4d40db0 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
@@ -260,20 +260,22 @@
                 );
             }
 
-            public void mergeAnimation(IBinder candidateTransition, TransitionInfo candidateInfo,
-                    SurfaceControl.Transaction candidateT, IBinder currentTransition,
-                    IRemoteTransitionFinishedCallback candidateFinishCallback) {
+            public void mergeAnimation(IBinder transition, TransitionInfo info,
+                    SurfaceControl.Transaction t, IBinder mergeTarget,
+                    IRemoteTransitionFinishedCallback finishCallback) {
                 try {
-                    final IRemoteTransitionFinishedCallback currentFinishCB;
+                    final IRemoteTransitionFinishedCallback origFinishCB;
                     synchronized (mFinishCallbacks) {
-                        currentFinishCB = mFinishCallbacks.remove(currentTransition);
+                        origFinishCB = mFinishCallbacks.remove(transition);
                     }
-                    if (currentFinishCB == null) {
-                        Slog.e(TAG, "Called mergeAnimation, but finish callback is missing");
+                    info.releaseAllSurfaces();
+                    t.close();
+                    if (origFinishCB == null) {
+                        // already finished (or not started yet), so do nothing.
                         return;
                     }
                     runner.onAnimationCancelled(false /* isKeyguardOccluded */);
-                    currentFinishCB.onTransitionFinished(null /* wct */, null /* t */);
+                    origFinishCB.onTransitionFinished(null /* wct */, null /* t */);
                 } catch (RemoteException e) {
                     // nothing, we'll just let it finish on its own I guess.
                 }
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
index 58ed3aa..8218976 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
@@ -958,12 +958,14 @@
 
                 @Override
                 public void onAnimationCancelled(boolean isKeyguardOccluded) {
-                    mContext.getMainExecutor().execute(() -> {
-                        if (mOccludeByDreamAnimator != null) {
-                            mOccludeByDreamAnimator.cancel();
-                        }
-                    });
-                    Log.d(TAG, "Occlude by Dream animation cancelled.");
+                    if (mOccludeByDreamAnimator != null) {
+                        mOccludeByDreamAnimator.cancel();
+                    }
+                    setOccluded(isKeyguardOccluded /* isOccluded */, false /* animate */);
+                    if (DEBUG) {
+                        Log.d(TAG, "Occlude by Dream animation cancelled. Occluded state is now: "
+                                + mOccluded);
+                    }
                 }
 
                 @Override
@@ -1069,7 +1071,10 @@
                         }
                     });
 
-                    Log.d(TAG, "Unocclude animation cancelled.");
+                    setOccluded(isKeyguardOccluded /* isOccluded */, false /* animate */);
+                    Log.d(TAG, "Unocclude animation cancelled. Occluded state is now: "
+                            + mOccluded);
+
                     mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_OCCLUSION);
                 }
 
@@ -3436,7 +3441,10 @@
         public void onAnimationCancelled(boolean isKeyguardOccluded) throws RemoteException {
             super.onAnimationCancelled(isKeyguardOccluded);
 
-            Log.d(TAG, "Occlude animation cancelled by WM.");
+            Log.d(TAG, "Occlude animation cancelled by WM. "
+                    + "Setting occluded state to: " + isKeyguardOccluded);
+            setOccluded(isKeyguardOccluded /* occluded */, false /* animate */);
+
             mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_OCCLUSION);
         }
     }
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index 6e54217..f064f83 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -2134,14 +2134,10 @@
         }
 
         mWindowManagerInternal.registerAppTransitionListener(new AppTransitionListener() {
-            private boolean mOccludeChangingInTransition = false;
-
             @Override
             public int onAppTransitionStartingLocked(boolean keyguardGoingAway,
                     boolean keyguardOccluding, long duration, long statusBarAnimationStartTime,
                     long statusBarAnimationDuration) {
-                mOccludeChangingInTransition = keyguardGoingAway || keyguardOccluding;
-
                 // When remote animation is enabled for KEYGUARD_GOING_AWAY transition, SysUI
                 // receives IRemoteAnimationRunner#onAnimationStart to start animation, so we don't
                 // need to call IKeyguardService#keyguardGoingAway here.
@@ -2157,11 +2153,6 @@
                         0 /* duration */);
 
                 synchronized (mLock) {
-                    if (mOccludeChangingInTransition) {
-                        mKeyguardOccludedChanged = true;
-                        mOccludeChangingInTransition = false;
-                    }
-                    applyKeyguardOcclusionChange(false);
                     mLockAfterAppTransitionFinished = false;
                 }
             }
@@ -2169,16 +2160,12 @@
             @Override
             public void onAppTransitionFinishedLocked(IBinder token) {
                 synchronized (mLock) {
-                    if (mOccludeChangingInTransition) {
-                        mKeyguardOccludedChanged = true;
-                        mOccludeChangingInTransition = false;
-                    }
-                    applyKeyguardOcclusionChange(false /* transitionStarted */);
                     if (!mLockAfterAppTransitionFinished) {
                         return;
                     }
                     mLockAfterAppTransitionFinished = false;
                 }
+
                 lockNow(null);
             }
         });
@@ -3368,7 +3355,7 @@
         if (mKeyguardOccludedChanged) {
             if (DEBUG_KEYGUARD) Slog.d(TAG, "transition/occluded changed occluded="
                     + mPendingKeyguardOccluded);
-            if (setKeyguardOccludedLw(mPendingKeyguardOccluded, true /* force */,
+            if (setKeyguardOccludedLw(mPendingKeyguardOccluded, false /* force */,
                     transitionStarted)) {
                 return FINISH_LAYOUT_REDO_LAYOUT | FINISH_LAYOUT_REDO_WALLPAPER;
             }
@@ -3629,7 +3616,6 @@
     private boolean setKeyguardOccludedLw(boolean isOccluded, boolean force,
             boolean transitionStarted) {
         if (DEBUG_KEYGUARD) Slog.d(TAG, "setKeyguardOccluded occluded=" + isOccluded);
-        mPendingKeyguardOccluded = isOccluded;
         mKeyguardOccludedChanged = false;
         if (isKeyguardOccluded() == isOccluded && !force) {
             return false;