Merge "Fixing insets mapping in 3-button and 2-button mode" into ub-launcher3-qt-dev
diff --git a/quickstep/recents_ui_overrides/res/values/dimens.xml b/quickstep/recents_ui_overrides/res/values/dimens.xml
index 61c576e..b316edd 100644
--- a/quickstep/recents_ui_overrides/res/values/dimens.xml
+++ b/quickstep/recents_ui_overrides/res/values/dimens.xml
@@ -23,4 +23,8 @@
 
     <!-- Minimum distance to swipe to trigger accessibility gesture -->
     <dimen name="accessibility_gesture_min_swipe_distance">80dp</dimen>
+
+    <!-- Swipe up to home related -->
+    <dimen name="swipe_up_fling_min_visible_change">18dp</dimen>
+    <dimen name="swipe_up_y_overshoot">10dp</dimen>
 </resources>
\ No newline at end of file
diff --git a/quickstep/recents_ui_overrides/src/com/android/launcher3/appprediction/PredictionUiStateManager.java b/quickstep/recents_ui_overrides/src/com/android/launcher3/appprediction/PredictionUiStateManager.java
index 48a163d..6dad9af 100644
--- a/quickstep/recents_ui_overrides/src/com/android/launcher3/appprediction/PredictionUiStateManager.java
+++ b/quickstep/recents_ui_overrides/src/com/android/launcher3/appprediction/PredictionUiStateManager.java
@@ -67,8 +67,8 @@
 
     // TODO (b/129421797): Update the client constants
     public enum Client {
-        HOME("GEL"),
-        OVERVIEW("OVERVIEW_GEL");
+        HOME("home"),
+        OVERVIEW("overview");
 
         public final String id;
 
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/WindowTransformSwipeHandler.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/WindowTransformSwipeHandler.java
index d927d83..404cfe6 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/WindowTransformSwipeHandler.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/WindowTransformSwipeHandler.java
@@ -1077,8 +1077,7 @@
 
         final View floatingView = homeAnimationFactory.getFloatingView();
         final boolean isFloatingIconView = floatingView instanceof FloatingIconView;
-
-        RectFSpringAnim anim = new RectFSpringAnim(startRect, targetRect);
+        RectFSpringAnim anim = new RectFSpringAnim(startRect, targetRect, mActivity.getResources());
         if (isFloatingIconView) {
             FloatingIconView fiv = (FloatingIconView) floatingView;
             anim.addAnimatorListener(fiv);
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/util/RectFSpringAnim.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/util/RectFSpringAnim.java
index 09db695..3f4ad58 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/util/RectFSpringAnim.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/util/RectFSpringAnim.java
@@ -19,6 +19,7 @@
 import android.animation.ObjectAnimator;
 import android.animation.PropertyValuesHolder;
 import android.animation.ValueAnimator;
+import android.content.res.Resources;
 import android.graphics.PointF;
 import android.graphics.RectF;
 import android.util.FloatProperty;
@@ -26,6 +27,7 @@
 import androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener;
 import androidx.dynamicanimation.animation.FloatPropertyCompat;
 
+import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.anim.AnimationSuccessListener;
 import com.android.launcher3.anim.FlingSpringAnim;
@@ -63,16 +65,16 @@
                 }
             };
 
-    private static final FloatPropertyCompat<RectFSpringAnim> RECT_CENTER_Y =
-            new FloatPropertyCompat<RectFSpringAnim>("rectCenterYSpring") {
+    private static final FloatPropertyCompat<RectFSpringAnim> RECT_Y =
+            new FloatPropertyCompat<RectFSpringAnim>("rectYSpring") {
                 @Override
                 public float getValue(RectFSpringAnim anim) {
-                    return anim.mCurrentCenterY;
+                    return anim.mCurrentY;
                 }
 
                 @Override
-                public void setValue(RectFSpringAnim anim, float currentCenterY) {
-                    anim.mCurrentCenterY = currentCenterY;
+                public void setValue(RectFSpringAnim anim, float y) {
+                    anim.mCurrentY = y;
                     anim.onUpdate();
                 }
             };
@@ -98,7 +100,9 @@
     private final List<Animator.AnimatorListener> mAnimatorListeners = new ArrayList<>();
 
     private float mCurrentCenterX;
-    private float mCurrentCenterY;
+    private float mCurrentY;
+    // If true, tracking the bottom of the rects, else tracking the top.
+    private boolean mTrackingBottomY;
     private float mCurrentScaleProgress;
     private FlingSpringAnim mRectXAnim;
     private FlingSpringAnim mRectYAnim;
@@ -108,19 +112,32 @@
     private boolean mRectYAnimEnded;
     private boolean mRectScaleAnimEnded;
 
-    public RectFSpringAnim(RectF startRect, RectF targetRect) {
+    private float mMinVisChange;
+    private float mYOvershoot;
+
+    public RectFSpringAnim(RectF startRect, RectF targetRect, Resources resources) {
         mStartRect = startRect;
         mTargetRect = targetRect;
         mCurrentCenterX = mStartRect.centerX();
-        mCurrentCenterY = mStartRect.centerY();
+
+        mTrackingBottomY = startRect.bottom < targetRect.bottom;
+        mCurrentY = mTrackingBottomY ? mStartRect.bottom : mStartRect.top;
+
+        mMinVisChange = resources.getDimensionPixelSize(R.dimen.swipe_up_fling_min_visible_change);
+        mYOvershoot = resources.getDimensionPixelSize(R.dimen.swipe_up_y_overshoot);
     }
 
     public void onTargetPositionChanged() {
         if (mRectXAnim != null && mRectXAnim.getTargetPosition() != mTargetRect.centerX()) {
             mRectXAnim.updatePosition(mCurrentCenterX, mTargetRect.centerX());
         }
-        if (mRectYAnim != null && mRectYAnim.getTargetPosition() != mTargetRect.centerY()) {
-            mRectYAnim.updatePosition(mCurrentCenterY, mTargetRect.centerY());
+
+        if (mRectYAnim != null) {
+            if (mTrackingBottomY && mRectYAnim.getTargetPosition() != mTargetRect.bottom) {
+                mRectYAnim.updatePosition(mCurrentY, mTargetRect.bottom);
+            } else if (!mTrackingBottomY && mRectYAnim.getTargetPosition() != mTargetRect.top) {
+                mRectYAnim.updatePosition(mCurrentY, mTargetRect.top);
+            }
         }
     }
 
@@ -142,10 +159,23 @@
             mRectYAnimEnded = true;
             maybeOnEnd();
         });
-        mRectXAnim = new FlingSpringAnim(this, RECT_CENTER_X, mCurrentCenterX,
-                mTargetRect.centerX(), velocityPxPerMs.x * 1000, onXEndListener);
-        mRectYAnim = new FlingSpringAnim(this, RECT_CENTER_Y, mCurrentCenterY,
-                mTargetRect.centerY(), velocityPxPerMs.y * 1000, onYEndListener);
+
+        float startX = mCurrentCenterX;
+        float endX = mTargetRect.centerX();
+        float minXValue = Math.min(startX, endX);
+        float maxXValue = Math.max(startX, endX);
+        mRectXAnim = new FlingSpringAnim(this, RECT_CENTER_X, startX, endX,
+                velocityPxPerMs.x * 1000, mMinVisChange, minXValue, maxXValue, 1f, onXEndListener);
+
+        float startVelocityY = velocityPxPerMs.y * 1000;
+        // Scale the Y velocity based on the initial velocity to tune the curves.
+        float springVelocityFactor = 0.1f + 0.9f * Math.abs(startVelocityY) / 20000.0f;
+        float startY = mCurrentY;
+        float endY = mTrackingBottomY ? mTargetRect.bottom : mTargetRect.top;
+        float minYValue = Math.min(startY, endY - mYOvershoot);
+        float maxYValue = Math.max(startY, endY);
+        mRectYAnim = new FlingSpringAnim(this, RECT_Y, startY, endY, startVelocityY,
+                mMinVisChange, minYValue, maxYValue, springVelocityFactor, onYEndListener);
 
         mRectScaleAnim = ObjectAnimator.ofPropertyValuesHolder(this,
                 PropertyValuesHolder.ofFloat(RECT_SCALE_PROGRESS, 1))
@@ -182,8 +212,13 @@
                     mTargetRect.width());
             float currentHeight = Utilities.mapRange(mCurrentScaleProgress, mStartRect.height(),
                     mTargetRect.height());
-            mCurrentRect.set(mCurrentCenterX - currentWidth / 2, mCurrentCenterY - currentHeight / 2,
-                    mCurrentCenterX + currentWidth / 2, mCurrentCenterY + currentHeight / 2);
+            if (mTrackingBottomY) {
+                mCurrentRect.set(mCurrentCenterX - currentWidth / 2, mCurrentY - currentHeight,
+                        mCurrentCenterX + currentWidth / 2, mCurrentY);
+            } else {
+                mCurrentRect.set(mCurrentCenterX - currentWidth / 2, mCurrentY,
+                        mCurrentCenterX + currentWidth / 2, mCurrentY + currentHeight);
+            }
             for (OnUpdateListener onUpdateListener : mOnUpdateListeners) {
                 onUpdateListener.onUpdate(mCurrentRect, mCurrentScaleProgress);
             }
diff --git a/src/com/android/launcher3/anim/FlingSpringAnim.java b/src/com/android/launcher3/anim/FlingSpringAnim.java
index 3d981c1..f53ea51 100644
--- a/src/com/android/launcher3/anim/FlingSpringAnim.java
+++ b/src/com/android/launcher3/anim/FlingSpringAnim.java
@@ -28,8 +28,6 @@
 public class FlingSpringAnim {
 
     private static final float FLING_FRICTION = 1.5f;
-    // Have the spring pull towards the target if we've slowed down too much before reaching it.
-    private static final float FLING_END_THRESHOLD_PX = 50f;
     private static final float SPRING_STIFFNESS = 200;
     private static final float SPRING_DAMPING = 0.85f;
 
@@ -39,23 +37,27 @@
     private float mTargetPosition;
 
     public <K> FlingSpringAnim(K object, FloatPropertyCompat<K> property, float startPosition,
-            float targetPosition, float startVelocity, OnAnimationEndListener onEndListener) {
+            float targetPosition, float startVelocity, float minVisChange, float minValue,
+            float maxValue, float springVelocityFactor, OnAnimationEndListener onEndListener) {
         mFlingAnim = new FlingAnimation(object, property)
                 .setFriction(FLING_FRICTION)
-                .setMinimumVisibleChange(FLING_END_THRESHOLD_PX)
+                // Have the spring pull towards the target if we've slowed down too much before
+                // reaching it.
+                .setMinimumVisibleChange(minVisChange)
                 .setStartVelocity(startVelocity)
-                .setMinValue(Math.min(startPosition, targetPosition))
-                .setMaxValue(Math.max(startPosition, targetPosition));
+                .setMinValue(minValue)
+                .setMaxValue(maxValue);
         mTargetPosition = targetPosition;
 
         mFlingAnim.addEndListener(((animation, canceled, value, velocity) -> {
             mSpringAnim = new SpringAnimation(object, property)
-                    .setStartVelocity(velocity)
+                    .setStartValue(value)
+                    .setStartVelocity(velocity * springVelocityFactor)
                     .setSpring(new SpringForce(mTargetPosition)
                             .setStiffness(SPRING_STIFFNESS)
                             .setDampingRatio(SPRING_DAMPING));
             mSpringAnim.addEndListener(onEndListener);
-            mSpringAnim.start();
+            mSpringAnim.animateToFinalPosition(mTargetPosition);
         }));
     }
 
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompat.java b/src/com/android/launcher3/compat/LauncherAppsCompat.java
index 4275f31..58fc73d 100644
--- a/src/com/android/launcher3/compat/LauncherAppsCompat.java
+++ b/src/com/android/launcher3/compat/LauncherAppsCompat.java
@@ -21,6 +21,7 @@
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.LauncherActivityInfo;
+import android.content.pm.PackageInstaller;
 import android.content.pm.ShortcutInfo;
 import android.graphics.Rect;
 import android.os.Bundle;
@@ -56,7 +57,9 @@
     public static LauncherAppsCompat getInstance(Context context) {
         synchronized (sInstanceLock) {
             if (sInstance == null) {
-                if (Utilities.ATLEAST_OREO) {
+                if (Utilities.ATLEAST_Q) {
+                    sInstance = new LauncherAppsCompatVQ(context.getApplicationContext());
+                } else if (Utilities.ATLEAST_OREO) {
                     sInstance = new LauncherAppsCompatVO(context.getApplicationContext());
                 } else {
                     sInstance = new LauncherAppsCompatVL(context.getApplicationContext());
@@ -83,4 +86,6 @@
             UserHandle user);
     public abstract List<ShortcutConfigActivityInfo> getCustomShortcutActivityList(
             @Nullable PackageUserKey packageUser);
+
+    public abstract List<PackageInstaller.SessionInfo> getAllPackageInstallerSessions();
 }
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVL.java b/src/com/android/launcher3/compat/LauncherAppsCompatVL.java
index fc48ba7..1d19b53 100644
--- a/src/com/android/launcher3/compat/LauncherAppsCompatVL.java
+++ b/src/com/android/launcher3/compat/LauncherAppsCompatVL.java
@@ -22,6 +22,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.LauncherActivityInfo;
 import android.content.pm.LauncherApps;
+import android.content.pm.PackageInstaller;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ShortcutInfo;
@@ -199,5 +200,10 @@
         }
         return result;
     }
+
+    @Override
+    public List<PackageInstaller.SessionInfo> getAllPackageInstallerSessions() {
+        return mContext.getPackageManager().getPackageInstaller().getAllSessions();
+    }
 }
 
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVQ.java b/src/com/android/launcher3/compat/LauncherAppsCompatVQ.java
new file mode 100644
index 0000000..0a1811e
--- /dev/null
+++ b/src/com/android/launcher3/compat/LauncherAppsCompatVQ.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.compat;
+
+import android.annotation.TargetApi;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageInstaller;
+
+import java.util.List;
+
+@TargetApi(29)
+public class LauncherAppsCompatVQ extends LauncherAppsCompatVO {
+
+    LauncherAppsCompatVQ(Context context) {
+        super(context);
+    }
+
+    public List<PackageInstaller.SessionInfo> getAllPackageInstallerSessions() {
+        return mLauncherApps.getAllPackageInstallerSessions();
+    }
+}
diff --git a/src/com/android/launcher3/compat/PackageInstallerCompatVL.java b/src/com/android/launcher3/compat/PackageInstallerCompatVL.java
index fe7b4e5..a34ca50 100644
--- a/src/com/android/launcher3/compat/PackageInstallerCompatVL.java
+++ b/src/com/android/launcher3/compat/PackageInstallerCompatVL.java
@@ -27,6 +27,7 @@
 import android.text.TextUtils;
 import android.util.SparseArray;
 
+import com.android.launcher3.Utilities;
 import com.android.launcher3.icons.IconCache;
 import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.LauncherModel;
@@ -49,6 +50,7 @@
     private final Handler mWorker;
     private final Context mAppContext;
     private final HashMap<String,Boolean> mSessionVerifiedMap = new HashMap<>();
+    private final LauncherAppsCompat mLauncherApps;
 
     PackageInstallerCompatVL(Context context) {
         mAppContext = context.getApplicationContext();
@@ -56,6 +58,7 @@
         mCache = LauncherAppState.getInstance(context).getIconCache();
         mWorker = new Handler(LauncherModel.getWorkerLooper());
         mInstaller.registerSessionCallback(mCallback, mWorker);
+        mLauncherApps = LauncherAppsCompat.getInstance(context);
     }
 
     @Override
@@ -171,7 +174,9 @@
 
     @Override
     public List<SessionInfo> getAllVerifiedSessions() {
-        List<SessionInfo> list = new ArrayList<>(mInstaller.getAllSessions());
+        List<SessionInfo> list = new ArrayList<>(Utilities.ATLEAST_Q
+                ? mLauncherApps.getAllPackageInstallerSessions()
+                : mInstaller.getAllSessions());
         Iterator<SessionInfo> it = list.iterator();
         while (it.hasNext()) {
             if (verify(it.next()) == null) {