Merge "Fixing wrong origin when launching new task from overview" into ub-launcher3-qt-dev
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/FallbackActivityControllerHelper.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/FallbackActivityControllerHelper.java
index dc58a4e..2c42fd6 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/FallbackActivityControllerHelper.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/FallbackActivityControllerHelper.java
@@ -17,6 +17,7 @@
 
 import static com.android.launcher3.anim.Interpolators.LINEAR;
 import static com.android.quickstep.SysUINavigationMode.Mode.NO_BUTTON;
+import static com.android.quickstep.fallback.FallbackRecentsView.ZOOM_PROGRESS;
 import static com.android.quickstep.views.RecentsView.CONTENT_ALPHA;
 
 import android.animation.Animator;
@@ -33,6 +34,7 @@
 import com.android.launcher3.anim.AnimationSuccessListener;
 import com.android.launcher3.anim.AnimatorPlaybackController;
 import com.android.launcher3.userevent.nano.LauncherLogProto;
+import com.android.quickstep.fallback.FallbackRecentsView;
 import com.android.quickstep.util.LayoutUtils;
 import com.android.quickstep.util.RemoteAnimationTargetSet;
 import com.android.quickstep.views.RecentsView;
@@ -120,11 +122,14 @@
             return (transitionLength) -> { };
         }
 
-        RecentsView rv = activity.getOverviewPanel();
+        FallbackRecentsView rv = activity.getOverviewPanel();
         rv.setContentAlpha(0);
         rv.getClearAllButton().setVisibilityAlpha(0);
         rv.setDisallowScrollToClearAll(true);
 
+        boolean fromState = !animateActivity;
+        rv.setInOverviewState(fromState);
+
         return new AnimationFactory() {
 
             boolean isAnimatingToRecents = false;
@@ -141,15 +146,28 @@
 
             @Override
             public void createActivityController(long transitionLength) {
-                if (!isAnimatingToRecents) {
-                    return;
+                AnimatorSet animatorSet = new AnimatorSet();
+                if (isAnimatingToRecents) {
+                    ObjectAnimator anim = ObjectAnimator.ofFloat(rv, CONTENT_ALPHA, 0, 1);
+                    anim.setDuration(transitionLength).setInterpolator(LINEAR);
+                    animatorSet.play(anim);
                 }
 
-                ObjectAnimator anim = ObjectAnimator.ofFloat(rv, CONTENT_ALPHA, 0, 1);
+                ObjectAnimator anim = ObjectAnimator.ofFloat(rv, ZOOM_PROGRESS, 1, 0);
                 anim.setDuration(transitionLength).setInterpolator(LINEAR);
-                AnimatorSet animatorSet = new AnimatorSet();
                 animatorSet.play(anim);
-                callback.accept(AnimatorPlaybackController.wrap(animatorSet, transitionLength));
+
+                AnimatorPlaybackController controller =
+                        AnimatorPlaybackController.wrap(animatorSet, transitionLength);
+
+                // Since we are changing the start position of the UI, reapply the state, at the end
+                controller.setEndAction(() -> {
+                    boolean endState = true;
+                    rv.setInOverviewState(controller.getInterpolatedProgress() > 0.5 ?
+                                    endState : fromState);
+                });
+
+                callback.accept(controller);
             }
         };
     }
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/RecentsActivity.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/RecentsActivity.java
index 5decc3e..32a9261 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/RecentsActivity.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/RecentsActivity.java
@@ -125,7 +125,14 @@
             @Override
             public void onCreateAnimation(RemoteAnimationTargetCompat[] targetCompats,
                     AnimationResult result) {
-                result.setAnimation(composeRecentsLaunchAnimator(taskView, targetCompats));
+                AnimatorSet anim = composeRecentsLaunchAnimator(taskView, targetCompats);
+                anim.addListener(new AnimatorListenerAdapter() {
+                    @Override
+                    public void onAnimationEnd(Animator animation) {
+                        mFallbackRecentsView.resetViewUI();
+                    }
+                });
+                result.setAnimation(anim);
             }
         };
         return ActivityOptionsCompat.makeRemoteAnimation(new RemoteAnimationAdapterCompat(
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/fallback/FallbackRecentsView.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/fallback/FallbackRecentsView.java
index a5aa1bf..c2876180 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/fallback/FallbackRecentsView.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/fallback/FallbackRecentsView.java
@@ -15,19 +15,45 @@
  */
 package com.android.quickstep.fallback;
 
+import static com.android.launcher3.LauncherAnimUtils.SCALE_PROPERTY;
+
 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Rect;
 import android.util.AttributeSet;
+import android.util.FloatProperty;
 import android.view.View;
 
 import com.android.launcher3.DeviceProfile;
+import com.android.launcher3.LauncherState.ScaleAndTranslation;
+import com.android.launcher3.Utilities;
 import com.android.quickstep.RecentsActivity;
 import com.android.quickstep.util.LayoutUtils;
 import com.android.quickstep.views.RecentsView;
+import com.android.quickstep.views.TaskView;
 
 public class FallbackRecentsView extends RecentsView<RecentsActivity> {
 
+    public static final FloatProperty<FallbackRecentsView> ZOOM_PROGRESS =
+            new FloatProperty<FallbackRecentsView> ("zoomInProgress") {
+
+                @Override
+                public void setValue(FallbackRecentsView view, float value) {
+                    view.setZoomProgress(value);
+                }
+
+                @Override
+                public Float get(FallbackRecentsView view) {
+                    return view.mZoomInProgress;
+                }
+            };
+
+    private float mZoomInProgress = 0;
+    private boolean mInOverviewState = true;
+
+    private float mZoomScale = 1f;
+    private float mZoomTranslationY = 0f;
+
     public FallbackRecentsView(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
     }
@@ -71,4 +97,46 @@
         // Just use the activity task size for multi-window as well.
         return false;
     }
+
+    public void resetViewUI() {
+        setZoomProgress(0);
+        resetTaskVisuals();
+    }
+
+    public void setInOverviewState(boolean inOverviewState) {
+        if (mInOverviewState != inOverviewState) {
+            mInOverviewState = inOverviewState;
+            if (mInOverviewState) {
+                resetTaskVisuals();
+            } else {
+                setZoomProgress(1);
+            }
+        }
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        super.onLayout(changed, left, top, right, bottom);
+
+        if (getTaskViewCount() == 0) {
+            mZoomScale = 1f;
+            mZoomTranslationY = 0f;
+        } else {
+            TaskView dummyTask = getTaskViewAt(0);
+            ScaleAndTranslation sat = getTempClipAnimationHelper()
+                    .updateForFullscreenOverview(dummyTask)
+                    .getScaleAndTranslation();
+            mZoomScale = sat.scale;
+            mZoomTranslationY = sat.translationY;
+        }
+
+        setZoomProgress(mZoomInProgress);
+    }
+
+    public void setZoomProgress(float progress) {
+        mZoomInProgress = progress;
+        SCALE_PROPERTY.set(this, Utilities.mapRange(mZoomInProgress, 1, mZoomScale));
+        TRANSLATION_Y.set(this, Utilities.mapRange(mZoomInProgress, 0, mZoomTranslationY));
+        FULLSCREEN_PROGRESS.set(this, mZoomInProgress);
+    }
 }
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java
index 837c2dc..9eda2f9 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java
@@ -28,13 +28,20 @@
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAnimUtils.ViewProgressProperty;
+import com.android.launcher3.LauncherState;
+import com.android.launcher3.LauncherStateManager;
+import com.android.launcher3.LauncherStateManager.AnimationConfig;
 import com.android.launcher3.R;
 import com.android.launcher3.ShortcutAndWidgetContainer;
+import com.android.launcher3.anim.AnimatorSetBuilder;
+import com.android.launcher3.anim.PropertySetter;
 import com.android.launcher3.anim.SpringObjectAnimator;
 
 import java.util.ArrayList;
 import java.util.List;
 
+import static com.android.launcher3.LauncherState.BACKGROUND_APP;
+import static com.android.launcher3.LauncherState.NORMAL;
 import static com.android.launcher3.anim.Interpolators.LINEAR;
 
 /**
@@ -56,7 +63,7 @@
     private final float mSpringTransY;
     private final View mViewToIgnore;
 
-    private final List<ValueAnimator> mAnimators = new ArrayList<>();
+    private final List<Animator> mAnimators = new ArrayList<>();
 
     /**
      * @param floatingViewOriginalView The FloatingIconView's original view.
@@ -104,6 +111,9 @@
             View qsb = launcher.findViewById(R.id.search_container_all_apps);
             addStaggeredAnimationForView(qsb, grid.inv.numRows + 2, totalRows);
         }
+
+        addWorkspaceScrimAnimationForState(launcher, BACKGROUND_APP, 0);
+        addWorkspaceScrimAnimationForState(launcher, NORMAL, ALPHA_DURATION_MS);
     }
 
     /**
@@ -150,4 +160,14 @@
         alpha.setStartDelay(startDelay);
         mAnimators.add(alpha);
     }
+
+    private void addWorkspaceScrimAnimationForState(Launcher launcher, LauncherState state,
+            long duration) {
+        AnimatorSetBuilder scrimAnimBuilder = new AnimatorSetBuilder();
+        AnimationConfig scrimAnimConfig = new AnimationConfig();
+        scrimAnimConfig.duration = duration;
+        PropertySetter scrimPropertySetter = scrimAnimConfig.getPropertySetter(scrimAnimBuilder);
+        launcher.getWorkspace().getStateTransitionAnimation().setScrim(scrimPropertySetter, state);
+        mAnimators.add(scrimAnimBuilder.build());
+    }
 }
diff --git a/quickstep/tests/src/com/android/quickstep/AbstractQuickStepTest.java b/quickstep/tests/src/com/android/quickstep/AbstractQuickStepTest.java
index c77726e..cc79c9d 100644
--- a/quickstep/tests/src/com/android/quickstep/AbstractQuickStepTest.java
+++ b/quickstep/tests/src/com/android/quickstep/AbstractQuickStepTest.java
@@ -18,14 +18,14 @@
 
 import com.android.launcher3.ui.AbstractLauncherUiTest;
 
-import org.junit.Rule;
-import org.junit.rules.TestRule;
+import org.junit.rules.RuleChain;
 
 /**
  * Base class for all instrumentation tests that deal with Quickstep.
  */
 public abstract class AbstractQuickStepTest extends AbstractLauncherUiTest {
-    @Rule
-    public TestRule mQuickstepOnOffExecutor =
-            new NavigationModeSwitchRule(mLauncher);
+    protected AbstractQuickStepTest() {
+        mOrderSensitiveRules = RuleChain.outerRule(new NavigationModeSwitchRule(mLauncher)).
+                around(mOrderSensitiveRules);
+    }
 }
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index f784226..9f846bb 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -1394,6 +1394,10 @@
         builder.play(stepAnimator);
     }
 
+    public WorkspaceStateTransitionAnimation getStateTransitionAnimation() {
+        return mStateTransitionAnimation;
+    }
+
     public void updateAccessibilityFlags() {
         // TODO: Update the accessibility flags appropriately when dragging.
         int accessibilityFlag = mLauncher.getStateManager().getState().workspaceAccessibilityFlag;
diff --git a/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java b/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
index 8d0259d..065d065 100644
--- a/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
+++ b/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
@@ -130,7 +130,10 @@
         propertySetter.setFloat(mWorkspace.getPageIndicator(), View.TRANSLATION_Y,
                 hotseatScaleAndTranslation.translationY, translationInterpolator);
 
-        // Set scrim
+        setScrim(propertySetter, state);
+    }
+
+    public void setScrim(PropertySetter propertySetter, LauncherState state) {
         WorkspaceAndHotseatScrim scrim = mLauncher.getDragLayer().getScrim();
         propertySetter.setFloat(scrim, SCRIM_PROGRESS, state.getWorkspaceScrimAlpha(mLauncher),
                 LINEAR);
diff --git a/src/com/android/launcher3/util/TraceHelper.java b/src/com/android/launcher3/util/TraceHelper.java
index 4fea2e9..c24bb67 100644
--- a/src/com/android/launcher3/util/TraceHelper.java
+++ b/src/com/android/launcher3/util/TraceHelper.java
@@ -27,16 +27,17 @@
 import com.android.launcher3.config.FeatureFlags;
 
 /**
- * A wrapper around {@link Trace} to allow easier proguarding for production builds.
+ * A wrapper around {@link Trace} with some utility information.
  *
  * To enable any tracing log, execute the following command:
+ * $ adb shell setprop log.tag.LAUNCHER_TRACE VERBOSE
  * $ adb shell setprop log.tag.TAGNAME VERBOSE
  */
 public class TraceHelper {
 
-    private static final boolean ENABLED = FeatureFlags.IS_DOGFOOD_BUILD;
+    private static final boolean ENABLED = isLoggable("LAUNCHER_TRACE", VERBOSE);
 
-    private static final boolean SYSTEM_TRACE = false;
+    private static final boolean SYSTEM_TRACE = ENABLED;
     private static final ArrayMap<String, MutableLong> sUpTimes = ENABLED ? new ArrayMap<>() : null;
 
     public static void beginSection(String sectionName) {
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
index c7c36b0..17a5335 100644
--- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
+++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
@@ -58,19 +58,17 @@
 import com.android.launcher3.tapl.LauncherInstrumentation;
 import com.android.launcher3.tapl.TestHelpers;
 import com.android.launcher3.util.Wait;
+import com.android.launcher3.util.rule.FailureWatcher;
 import com.android.launcher3.util.rule.LauncherActivityRule;
 import com.android.launcher3.util.rule.ShellCommandRule;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
+import org.junit.rules.RuleChain;
 import org.junit.rules.TestRule;
-import org.junit.rules.TestWatcher;
-import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 
-import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.IOException;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
@@ -93,7 +91,6 @@
     public static final long SHORT_UI_TIMEOUT = 300;
     public static final long DEFAULT_UI_TIMEOUT = 10000;
     private static final String TAG = "AbstractLauncherUiTest";
-    private static int sScreenshotCount = 0;
 
     protected MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
     protected final UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
@@ -167,42 +164,11 @@
             } : base;
 
     @Rule
-    public TestWatcher mFailureWatcher = new TestWatcher() {
-        private void dumpViewHierarchy() {
-            final ByteArrayOutputStream stream = new ByteArrayOutputStream();
-            try {
-                mDevice.dumpWindowHierarchy(stream);
-                stream.flush();
-                stream.close();
-                for (String line : stream.toString().split("\\r?\\n")) {
-                    Log.e(TAG, line.trim());
-                }
-            } catch (IOException e) {
-                Log.e(TAG, "error dumping XML to logcat", e);
-            }
-        }
+    public RuleChain mOrderSensitiveRules = RuleChain.outerRule(new FailureWatcher(this));
 
-        @Override
-        protected void failed(Throwable e, Description description) {
-            if (mDevice == null) return;
-            final String pathname = getInstrumentation().getTargetContext().
-                    getFilesDir().getPath() + "/TaplTestScreenshot" + sScreenshotCount++ + ".png";
-            Log.e(TAG, "Failed test " + description.getMethodName() +
-                    ", screenshot will be saved to " + pathname +
-                    ", track trace is below, UI object dump is further below:\n" +
-                    Log.getStackTraceString(e));
-            dumpViewHierarchy();
-
-            try {
-                final String dumpsysResult = mDevice.executeShellCommand(
-                                "dumpsys activity service TouchInteractionService");
-                Log.d(TAG, "TouchInteractionService: " + dumpsysResult);
-            } catch (IOException ex) {
-            }
-
-            mDevice.takeScreenshot(new File(pathname));
-        }
-    };
+    public UiDevice getDevice() {
+        return mDevice;
+    }
 
     @Before
     public void setUp() throws Exception {
diff --git a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java
new file mode 100644
index 0000000..09cc98d
--- /dev/null
+++ b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java
@@ -0,0 +1,59 @@
+package com.android.launcher3.util.rule;
+
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import android.util.Log;
+
+import com.android.launcher3.ui.AbstractLauncherUiTest;
+
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+
+public class FailureWatcher extends TestWatcher {
+    private static final String TAG = "FailureWatcher";
+    private static int sScreenshotCount = 0;
+    private AbstractLauncherUiTest mAbstractLauncherUiTest;
+
+    public FailureWatcher(AbstractLauncherUiTest abstractLauncherUiTest) {
+        mAbstractLauncherUiTest = abstractLauncherUiTest;
+    }
+
+    private void dumpViewHierarchy() {
+        final ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        try {
+            mAbstractLauncherUiTest.getDevice().dumpWindowHierarchy(stream);
+            stream.flush();
+            stream.close();
+            for (String line : stream.toString().split("\\r?\\n")) {
+                Log.e(TAG, line.trim());
+            }
+        } catch (IOException e) {
+            Log.e(TAG, "error dumping XML to logcat", e);
+        }
+    }
+
+    @Override
+    protected void failed(Throwable e, Description description) {
+        if (mAbstractLauncherUiTest.getDevice() == null) return;
+        final String pathname = getInstrumentation().getTargetContext().
+                getFilesDir().getPath() + "/TaplTestScreenshot" + sScreenshotCount++ + ".png";
+        Log.e(TAG, "Failed test " + description.getMethodName() +
+                ", screenshot will be saved to " + pathname +
+                ", track trace is below, UI object dump is further below:\n" +
+                Log.getStackTraceString(e));
+        dumpViewHierarchy();
+
+        try {
+            final String dumpsysResult = mAbstractLauncherUiTest.getDevice().executeShellCommand(
+                    "dumpsys activity service TouchInteractionService");
+            Log.d(TAG, "TouchInteractionService: " + dumpsysResult);
+        } catch (IOException ex) {
+        }
+
+        mAbstractLauncherUiTest.getDevice().takeScreenshot(new File(pathname));
+    }
+}