Fix some more divider/ime interractions

Fixes the following issues:
- Opening an app, then going to recents and putting it into split
  wasn't going to minimized.
- Compose thing in secondary split would leave split in weird state

Also added several debug logs behind a variable for easier
debugging in future.

Bug: 151181674
Bug: 151060730
Test: Open an already-open app from recents into split-screen.
      Cancel opening IME partway by defocusing editfield.
Change-Id: I4de4c3e543504087eae2458ca62d08c155899397
diff --git a/packages/SystemUI/src/com/android/systemui/stackdivider/Divider.java b/packages/SystemUI/src/com/android/systemui/stackdivider/Divider.java
index 63bd076..d7eab3a 100644
--- a/packages/SystemUI/src/com/android/systemui/stackdivider/Divider.java
+++ b/packages/SystemUI/src/com/android/systemui/stackdivider/Divider.java
@@ -31,7 +31,6 @@
 import android.os.Handler;
 import android.os.RemoteException;
 import android.provider.Settings;
-import android.util.Log;
 import android.util.Slog;
 import android.view.IWindowContainer;
 import android.view.LayoutInflater;
@@ -73,7 +72,7 @@
         DisplayController.OnDisplaysChangedListener {
     private static final String TAG = "Divider";
 
-    static final boolean DEBUG = true;
+    static final boolean DEBUG = false;
 
     static final int DEFAULT_APP_TRANSITION_DURATION = 336;
     static final float ADJUSTED_NONFOCUS_DIM = 0.3f;
@@ -211,13 +210,24 @@
             mTargetShown = imeShouldShow;
             if (mLastAdjustTop < 0) {
                 mLastAdjustTop = imeShouldShow ? hiddenTop : shownTop;
+            } else {
+                // Check for an "interruption" of an existing animation. In this case, we need to
+                // fake-flip the last-known state direction so that the animation completes in the
+                // other direction.
+                if (mTargetAdjusted != targetAdjusted && targetAdjusted == mAdjusted) {
+                    if (mLastAdjustTop != (imeShouldShow ? mShownTop : mHiddenTop)) {
+                        mAdjusted = mTargetAdjusted;
+                    }
+                }
             }
             if (mPaused) {
                 mPausedTargetAdjusted = targetAdjusted;
+                if (DEBUG) Slog.d(TAG, " ime starting but paused " + dumpState());
                 return;
             }
             mTargetAdjusted = targetAdjusted;
             updateDimTargets();
+            if (DEBUG) Slog.d(TAG, " ime starting. vis:" + splitIsVisible + "  " + dumpState());
             if (mAnimation != null || (mImeWasShown && imeShouldShow
                     && mTargetAdjusted != mAdjusted)) {
                 // We need to animate adjustment independently of the IME position, so
@@ -356,9 +366,24 @@
             mAnimation.start();
         }
 
+        private String dumpState() {
+            return "top:" + mHiddenTop + "->" + mShownTop
+                    + " adj:" + mAdjusted + "->" + mTargetAdjusted + "(" + mLastAdjustTop + ")"
+                    + " shw:" + mImeWasShown + "->" + mTargetShown
+                    + " dims:" + mLastPrimaryDim + "," + mLastSecondaryDim
+                    + "->" + mTargetPrimaryDim + "," + mTargetSecondaryDim
+                    + " shf:" + mSecondaryHasFocus + " desync:" + (mAnimation != null)
+                    + " paus:" + mPaused + "[" + mPausedTargetAdjusted + "]";
+        }
+
         /** Completely aborts/resets adjustment state */
         public void pause(int displayId) {
+            if (DEBUG) Slog.d(TAG, "ime pause posting " + dumpState());
             mHandler.post(() -> {
+                if (DEBUG) Slog.d(TAG, "ime pause run posted " + dumpState());
+                if (mPaused) {
+                    return;
+                }
                 mPaused = true;
                 mPausedTargetAdjusted = mTargetAdjusted;
                 mTargetAdjusted = false;
@@ -369,11 +394,16 @@
         }
 
         public void resume(int displayId) {
+            if (DEBUG) Slog.d(TAG, "ime resume posting " + dumpState());
             mHandler.post(() -> {
+                if (DEBUG) Slog.d(TAG, "ime resume run posted " + dumpState());
+                if (!mPaused) {
+                    return;
+                }
                 mPaused = false;
                 mTargetAdjusted = mPausedTargetAdjusted;
                 updateDimTargets();
-                if (mTargetAdjusted && mView != null) {
+                if ((mTargetAdjusted != mAdjusted) && !mMinimized && mView != null) {
                     // End unminimize animations since they conflict with adjustment animations.
                     mView.finishAnimations();
                 }
@@ -532,6 +562,7 @@
     }
 
     void updateVisibility(final boolean visible) {
+        if (DEBUG) Slog.d(TAG, "Updating visibility " + mVisible + "->" + visible);
         if (mVisible != visible) {
             mVisible = visible;
             mView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
@@ -558,12 +589,21 @@
 
     /** Switch to minimized state if appropriate */
     public void setMinimized(final boolean minimized) {
+        if (DEBUG) Slog.d(TAG, "posting ext setMinimized " + minimized + " vis:" + mVisible);
         mHandler.post(() -> {
+            if (DEBUG) Slog.d(TAG, "run posted ext setMinimized " + minimized + " vis:" + mVisible);
+            if (!mVisible) {
+                return;
+            }
             setHomeMinimized(minimized, mHomeStackResizable);
         });
     }
 
     private void setHomeMinimized(final boolean minimized, boolean homeStackResizable) {
+        if (DEBUG) {
+            Slog.d(TAG, "setHomeMinimized  min:" + mMinimized + "->" + minimized + " hrsz:"
+                    + mHomeStackResizable + "->" + homeStackResizable + " split:" + inSplitMode());
+        }
         WindowContainerTransaction wct = new WindowContainerTransaction();
         // Update minimized state
         if (mMinimized != minimized) {
@@ -698,7 +738,7 @@
         if (!inSplitMode()) {
             // Wasn't in split-mode yet, so enter now.
             if (DEBUG) {
-                Log.d(TAG, " entering split mode with minimized=true");
+                Slog.d(TAG, " entering split mode with minimized=true");
             }
             updateVisibility(true /* visible */);
         }
@@ -709,7 +749,7 @@
         if (!inSplitMode()) {
             // Wasn't in split-mode, so enter now.
             if (DEBUG) {
-                Log.d(TAG, " enter split mode unminimized ");
+                Slog.d(TAG, " enter split mode unminimized ");
             }
             updateVisibility(true /* visible */);
         }
diff --git a/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java b/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
index 0d6403d..131f4e1 100644
--- a/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
+++ b/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
@@ -34,6 +34,7 @@
 import android.os.Handler;
 import android.os.Message;
 import android.util.AttributeSet;
+import android.util.Slog;
 import android.view.Choreographer;
 import android.view.Display;
 import android.view.InsetsState;
@@ -75,6 +76,7 @@
 public class DividerView extends FrameLayout implements OnTouchListener,
         OnComputeInternalInsetsListener {
     private static final String TAG = "DividerView";
+    private static final boolean DEBUG = Divider.DEBUG;
 
     public interface DividerCallbacks {
         void onDraggingStart();
@@ -629,6 +631,7 @@
             cancelFlingAnimation();
             updateDockSide();
         }
+        if (DEBUG) Slog.d(TAG, "Getting fling " + position + "->" + snapTarget.position);
         final boolean taskPositionSameAtEnd = snapTarget.flag == SnapTarget.FLAG_NONE;
         ValueAnimator anim = ValueAnimator.ofInt(position, snapTarget.position);
         anim.addUpdateListener(animation -> resizeStackDelayed((int) animation.getAnimatedValue(),
@@ -637,6 +640,7 @@
                         : snapTarget.taskPosition,
                 snapTarget));
         Consumer<Boolean> endAction = cancelled -> {
+            if (DEBUG) Slog.d(TAG, "End Fling " + cancelled + " min:" + mIsInMinimizeInteraction);
             final boolean wasMinimizeInteraction = mIsInMinimizeInteraction;
             // Reset minimized divider position after unminimized state animation finishes.
             if (!cancelled && !mDockedStackMinimized && mIsInMinimizeInteraction) {
@@ -697,6 +701,7 @@
                     endAction.accept(mCancelled);
                 } else {
                     final Boolean cancelled = mCancelled;
+                    if (DEBUG) Slog.d(TAG, "Posting endFling " + cancelled + " d:" + delay + "ms");
                     mHandler.postDelayed(() -> endAction.accept(cancelled), delay);
                 }
             }
@@ -894,6 +899,7 @@
 
     public void setMinimizedDockStack(boolean minimized, long animDuration,
             boolean isHomeStackResizable) {
+        if (DEBUG) Slog.d(TAG, "setMinDock: " + mDockedStackMinimized + "->" + minimized);
         mHomeStackResizable = isHomeStackResizable;
         updateDockSide();
         if (!isHomeStackResizable) {
@@ -1065,6 +1071,11 @@
         mDividerPositionX = dockedRect.right;
         mDividerPositionY = dockedRect.bottom;
 
+        if (DEBUG) {
+            Slog.d(TAG, "Resizing split surfaces: " + dockedRect + " " + dockedTaskRect
+                    + " " + otherRect + " " + otherTaskRect);
+        }
+
         t.setPosition(mTiles.mPrimarySurface, dockedTaskRect.left, dockedTaskRect.top);
         Rect crop = new Rect(dockedRect);
         crop.offsetTo(-Math.min(dockedTaskRect.left - dockedRect.left, 0),
diff --git a/packages/SystemUI/src/com/android/systemui/wm/DisplayImeController.java b/packages/SystemUI/src/com/android/systemui/wm/DisplayImeController.java
index 2f1b160..5411839 100644
--- a/packages/SystemUI/src/com/android/systemui/wm/DisplayImeController.java
+++ b/packages/SystemUI/src/com/android/systemui/wm/DisplayImeController.java
@@ -49,6 +49,8 @@
 public class DisplayImeController implements DisplayController.OnDisplaysChangedListener {
     private static final String TAG = "DisplayImeController";
 
+    private static final boolean DEBUG = false;
+
     public static final int ANIMATION_DURATION_SHOW_MS = 275;
     public static final int ANIMATION_DURATION_HIDE_MS = 340;
     public static final Interpolator INTERPOLATOR = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
@@ -209,6 +211,7 @@
             if ((types & WindowInsets.Type.ime()) == 0) {
                 return;
             }
+            if (DEBUG) Slog.d(TAG, "Got showInsets for ime");
             startAnimation(true /* show */);
         }
 
@@ -217,6 +220,7 @@
             if ((types & WindowInsets.Type.ime()) == 0) {
                 return;
             }
+            if (DEBUG) Slog.d(TAG, "Got hideInsets for ime");
             startAnimation(false /* show */);
         }
 
@@ -241,6 +245,11 @@
                 return;
             }
             mHandler.post(() -> {
+                if (DEBUG) {
+                    Slog.d(TAG, "Run startAnim  show:" + show + "  was:"
+                            + (mAnimationDirection == DIRECTION_SHOW ? "SHOW"
+                            : (mAnimationDirection == DIRECTION_HIDE ? "HIDE" : "NONE")));
+                }
                 if ((mAnimationDirection == DIRECTION_SHOW && show)
                         || (mAnimationDirection == DIRECTION_HIDE && !show)) {
                     return;
@@ -289,6 +298,11 @@
                     public void onAnimationStart(Animator animation) {
                         SurfaceControl.Transaction t = mTransactionPool.acquire();
                         t.setPosition(mImeSourceControl.getLeash(), x, startY);
+                        if (DEBUG) {
+                            Slog.d(TAG, "onAnimationStart d:" + mDisplayId + " top:"
+                                    + imeTop(imeSource, hiddenY) + "->" + imeTop(imeSource, shownY)
+                                    + " showing:" + (mAnimationDirection == DIRECTION_SHOW));
+                        }
                         dispatchStartPositioning(mDisplayId, imeTop(imeSource, hiddenY),
                                 imeTop(imeSource, shownY), mAnimationDirection == DIRECTION_SHOW,
                                 t);
@@ -304,6 +318,7 @@
                     }
                     @Override
                     public void onAnimationEnd(Animator animation) {
+                        if (DEBUG) Slog.d(TAG, "onAnimationEnd " + mCancelled);
                         SurfaceControl.Transaction t = mTransactionPool.acquire();
                         if (!mCancelled) {
                             t.setPosition(mImeSourceControl.getLeash(), x, endY);