Fixing task window is not getting alpha set properly when long-swiping

We were setting the alpha of the target window to 0 on the UI thread, and setting it
back to 1, through ClipAnimationHelper on background thread

Bug:109701914
Change-Id: I4abb73298b5e957a66f91c5654b184cdd398724b
diff --git a/quickstep/src/com/android/quickstep/LongSwipeHelper.java b/quickstep/src/com/android/quickstep/LongSwipeHelper.java
index fbcde8b..336be2b 100644
--- a/quickstep/src/com/android/quickstep/LongSwipeHelper.java
+++ b/quickstep/src/com/android/quickstep/LongSwipeHelper.java
@@ -20,11 +20,8 @@
 import static com.android.launcher3.LauncherState.OVERVIEW;
 import static com.android.launcher3.anim.Interpolators.DEACCEL;
 import static com.android.quickstep.WindowTransformSwipeHandler.MAX_SWIPE_DURATION;
-import static com.android.systemui.shared.recents.utilities.Utilities.getNextFrameNumber;
-import static com.android.systemui.shared.recents.utilities.Utilities.getSurface;
 
 import android.animation.ValueAnimator;
-import android.view.Surface;
 
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAnimUtils;
@@ -39,7 +36,6 @@
 import com.android.quickstep.util.RemoteAnimationTargetSet;
 import com.android.quickstep.views.RecentsView;
 import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
-import com.android.systemui.shared.system.TransactionCompat;
 
 /**
  * Utility class to handle long swipe from an app.
@@ -65,7 +61,6 @@
     }
 
     private void init() {
-        setTargetAlpha(0, true);
         mFlingBlockCheck.blockFling();
 
         // Init animations
@@ -83,8 +78,7 @@
     }
 
     public void destroy() {
-        // TODO: We can probably also hide the task view
-        setTargetAlpha(1, false);
+        // TODO: We can probably also show the task view
 
         mLauncher.getStateManager().goToState(OVERVIEW, false);
     }
@@ -136,31 +130,6 @@
         animator.start();
     }
 
-    private void setTargetAlpha(float alpha, boolean defer) {
-        final Surface surface = getSurface(mLauncher.getDragLayer());
-        final long frameNumber = defer && surface != null ? getNextFrameNumber(surface) : -1;
-        if (defer) {
-            if (frameNumber == -1) {
-                defer = false;
-            } else {
-                mLauncher.getDragLayer().invalidate();
-            }
-        }
-
-        TransactionCompat transaction = new TransactionCompat();
-        for (RemoteAnimationTargetCompat app : mTargetSet.apps) {
-            if (!(app.isNotInRecents
-                    || app.activityType == RemoteAnimationTargetCompat.ACTIVITY_TYPE_HOME)) {
-                transaction.setAlpha(app.leash, alpha);
-                if (defer) {
-                    transaction.deferTransactionUntil(app.leash, surface, frameNumber);
-                }
-            }
-        }
-        transaction.setEarlyWakeup();
-        transaction.apply();
-    }
-
     private void onSwipeAnimationComplete(boolean toAllApps, boolean isFling, Runnable callback) {
         mLauncher.getStateManager().goToState(toAllApps ? ALL_APPS : OVERVIEW, false);
         if (!toAllApps) {
@@ -176,4 +145,12 @@
 
         callback.run();
     }
+
+    public float getTargetAlpha(RemoteAnimationTargetCompat app, Float expectedAlpha) {
+        if (!(app.isNotInRecents
+                || app.activityType == RemoteAnimationTargetCompat.ACTIVITY_TYPE_HOME)) {
+            return 0;
+        }
+        return expectedAlpha;
+    }
 }
diff --git a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java
index 584c7f4..765b5ff 100644
--- a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java
+++ b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java
@@ -85,6 +85,7 @@
 import com.android.systemui.shared.system.WindowManagerWrapper;
 
 import java.util.StringJoiner;
+import java.util.function.BiFunction;
 
 @TargetApi(Build.VERSION_CODES.O)
 public class WindowTransformSwipeHandler<T extends BaseDraggingActivity> {
@@ -927,6 +928,7 @@
 
         if (mLongSwipeController != null) {
             mLongSwipeController.destroy();
+            setTargetAlphaProvider((t, a1) -> a1);
 
             // Rebuild animations
             buildAnimationController();
@@ -968,6 +970,7 @@
         mLongSwipeController = mActivityControlHelper.getLongSwipeController(
                 mActivity, mRecentsAnimationWrapper.targetSet);
         onLongSwipeDisplacementUpdated();
+        setTargetAlphaProvider(mLongSwipeController::getTargetAlpha);
     }
 
     private void onLongSwipeGestureFinishUi(float velocity, boolean isFling) {
@@ -982,4 +985,12 @@
                 () -> setStateOnUiThread(STATE_HANDLER_INVALIDATED));
 
     }
+
+    private void setTargetAlphaProvider(
+            BiFunction<RemoteAnimationTargetCompat, Float, Float> provider) {
+        mClipAnimationHelper.setTaskAlphaCallback(provider);
+        // TODO: For some reason, when calling updateFinalShift multiple times on the same frame,
+        // only the first callback is executed.
+        Utilities.postAsyncCallback(mMainThreadHandler, this::updateFinalShift);
+    }
 }