Merge "First pass at using hardware bitmaps" into ub-launcher3-master
diff --git a/quickstep/src/com/android/launcher3/uioverrides/TwoStepSwipeController.java b/quickstep/src/com/android/launcher3/uioverrides/TwoStepSwipeController.java
index 20cda1c..435d57e 100644
--- a/quickstep/src/com/android/launcher3/uioverrides/TwoStepSwipeController.java
+++ b/quickstep/src/com/android/launcher3/uioverrides/TwoStepSwipeController.java
@@ -367,7 +367,7 @@
         mCurrentAnimation.setEndAction(() -> {
             // TODO: Add logging
             clearState();
-            mLauncher.getStateManager().goToState(OVERVIEW, false /* animated */);
+            mLauncher.getStateManager().goToState(OVERVIEW, true /* animated */);
         });
 
         if (mTwoStateAnimationController != null) {
diff --git a/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java b/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
index 965696f..43a01a7 100644
--- a/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
+++ b/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
@@ -199,9 +199,9 @@
     private void executeFrameUpdate() {
         if (mLauncherReady) {
             final float displacement = -mCurrentDisplacement;
-            int hotseatHeight = mHotseat.getHeight();
-            float translation = Utilities.boundToRange(displacement, 0, hotseatHeight);
-            float shift = hotseatHeight == 0 ? 0 : translation / hotseatHeight;
+            int hotseatSize = getHotseatSize();
+            float translation = Utilities.boundToRange(displacement, 0, hotseatSize);
+            float shift = hotseatSize == 0 ? 0 : translation / hotseatSize;
             mCurrentShift.updateValue(shift);
         }
     }
@@ -215,13 +215,14 @@
         if (mTargetRect.isEmpty()) {
             RecentsView.getPageRect(mLauncher, mTargetRect);
             DragLayer dl = mLauncher.getDragLayer();
-            mSourceRect.set(0, 0, dl.getWidth(), dl.getHeight());
+            mSourceRect.set(0, 0, dl.getWidth() - mStableInsets.left - mStableInsets.right,
+                    dl.getHeight() - mStableInsets.top - mStableInsets.bottom);
         }
 
         float shift = mCurrentShift.value * mActivityMultiplier.value;
-        int hotseatHeight = mHotseat.getHeight();
+        int hotseatSize = getHotseatSize();
 
-        mHotseat.setTranslationY((1 - shift) * hotseatHeight);
+        mHotseat.setTranslationY((1 - shift) * hotseatSize);
 
         mRectEvaluator.evaluate(shift, mSourceRect, mTargetRect);
 
@@ -230,10 +231,17 @@
         mDragView.setTranslationY(mCurrentRect.top - mStableInsets.top * scale * shift);
         mDragView.setScaleX(scale);
         mDragView.setScaleY(scale);
+        //  TODO: mDragView.getViewBounds().setClipLeft((int) (mStableInsets.left * shift));
         mDragView.getViewBounds().setClipTop((int) (mStableInsets.top * shift));
+        // TODO: mDragView.getViewBounds().setClipRight((int) (mStableInsets.right * shift));
         mDragView.getViewBounds().setClipBottom((int) (mStableInsets.bottom * shift));
     }
 
+    private int getHotseatSize() {
+        return mLauncher.getDeviceProfile().isVerticalBarLayout()
+                ? mHotseat.getWidth() : mHotseat.getHeight();
+    }
+
     @UiThread
     public void setRecentsTaskLoadPlan(RecentsTaskLoadPlan loadPlan) {
         mLoadPlan = loadPlan;
@@ -272,7 +280,7 @@
             endShift = endVelocity < 0 ? 1 : 0;
             float minFlingVelocity = res.getDimension(R.dimen.quickstep_fling_min_velocity);
             if (Math.abs(endVelocity) > minFlingVelocity && mLauncherReady) {
-                float distanceToTravel = (endShift - mCurrentShift.value) * mHotseat.getHeight();
+                float distanceToTravel = (endShift - mCurrentShift.value) * getHotseatSize();
 
                 // we want the page's snap velocity to approximately match the velocity at
                 // which the user flings, so we scale the duration by a value near to the
diff --git a/quickstep/src/com/android/quickstep/TaskThumbnailView.java b/quickstep/src/com/android/quickstep/TaskThumbnailView.java
index 6e8bbeb..4a9bfea 100644
--- a/quickstep/src/com/android/quickstep/TaskThumbnailView.java
+++ b/quickstep/src/com/android/quickstep/TaskThumbnailView.java
@@ -25,7 +25,6 @@
 import android.graphics.LightingColorFilter;
 import android.graphics.Matrix;
 import android.graphics.Paint;
-import android.graphics.Point;
 import android.graphics.Rect;
 import android.graphics.Shader;
 import android.util.AttributeSet;
@@ -154,17 +153,15 @@
                 final Configuration configuration =
                         getContext().getApplicationContext().getResources().getConfiguration();
                 final DeviceProfile profile = Launcher.getLauncher(getContext()).getDeviceProfile();
-                if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
-                    if (mThumbnailData.orientation == Configuration.ORIENTATION_PORTRAIT) {
-                        // If we are in the same orientation as the screenshot, just scale it to the
-                        // width of the task view
-                        mThumbnailScale = (float) getMeasuredWidth() / mThumbnailRect.width();
-                    } else {
-                        // Scale the landscape thumbnail up to app size, then scale that to the task
-                        // view size to match other portrait screenshots
-                        mThumbnailScale = invThumbnailScale *
-                                ((float) getMeasuredWidth() / profile.getCurrentWidth());
-                    }
+                if (configuration.orientation == mThumbnailData.orientation) {
+                    // If we are in the same orientation as the screenshot, just scale it to the
+                    // width of the task view
+                    mThumbnailScale = (float) getMeasuredWidth() / mThumbnailRect.width();
+                } else if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
+                    // Scale the landscape thumbnail up to app size, then scale that to the task
+                    // view size to match other portrait screenshots
+                    mThumbnailScale = invThumbnailScale *
+                            ((float) getMeasuredWidth() / profile.getCurrentWidth());
                 } else {
                     // Otherwise, scale the screenshot to fit 1:1 in the current orientation
                     mThumbnailScale = invThumbnailScale;
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index 7b220d8..f457a59 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -44,6 +44,7 @@
 import android.view.Choreographer;
 import android.view.Display;
 import android.view.MotionEvent;
+import android.view.Surface;
 import android.view.VelocityTracker;
 import android.view.View;
 import android.view.ViewConfiguration;
@@ -61,6 +62,7 @@
 import com.android.systemui.shared.recents.model.RecentsTaskLoader;
 import com.android.systemui.shared.system.ActivityManagerWrapper;
 import com.android.systemui.shared.system.BackgroundExecutor;
+import com.android.systemui.shared.system.WindowManagerWrapper;
 
 import java.util.function.Consumer;
 
@@ -105,6 +107,8 @@
     private int mTouchSlop;
     private float mStartDisplacement;
     private NavBarSwipeInteractionHandler mInteractionHandler;
+    private int mDisplayRotation;
+    private Rect mStableInsets = new Rect();
 
     private ISystemUiProxy mISystemUiProxy;
     private Consumer<MotionEvent> mCurrentConsumer = mNoOpTouchConsumer;
@@ -181,6 +185,10 @@
                     mInteractionHandler.endTouch(0);
                     mInteractionHandler = null;
                 }
+
+                Display display = getSystemService(WindowManager.class).getDefaultDisplay();
+                mDisplayRotation = display.getRotation();
+                WindowManagerWrapper.getInstance().getStableInsets(mStableInsets);
                 break;
             }
             case ACTION_POINTER_UP: {
@@ -206,6 +214,11 @@
                 mLastPos.set(ev.getX(pointerIndex), ev.getY(pointerIndex));
 
                 float displacement = ev.getY(pointerIndex) - mDownPos.y;
+                if (isNavBarOnRight()) {
+                    displacement = ev.getX(pointerIndex) - mDownPos.x;
+                } else if (isNavBarOnLeft()) {
+                    displacement = mDownPos.x - ev.getX(pointerIndex);
+                }
                 if (mInteractionHandler == null) {
                     if (Math.abs(displacement) >= mTouchSlop) {
                         mStartDisplacement = Math.signum(displacement) * mTouchSlop;
@@ -229,6 +242,13 @@
         }
     }
 
+    private boolean isNavBarOnRight() {
+        return mDisplayRotation == Surface.ROTATION_90 && mStableInsets.right > 0;
+    }
+
+    private boolean isNavBarOnLeft() {
+        return mDisplayRotation == Surface.ROTATION_270 && mStableInsets.left > 0;
+    }
 
     private void startTouchTracking() {
         // Create the shared handler
@@ -272,7 +292,10 @@
             mVelocityTracker.computeCurrentVelocity(1000,
                     ViewConfiguration.get(this).getScaledMaximumFlingVelocity());
 
-            mInteractionHandler.endTouch(mVelocityTracker.getYVelocity(mActivePointerId));
+            float velocity = isNavBarOnRight() ? mVelocityTracker.getXVelocity(mActivePointerId)
+                    : isNavBarOnLeft() ? -mVelocityTracker.getXVelocity(mActivePointerId)
+                    : mVelocityTracker.getYVelocity(mActivePointerId);
+            mInteractionHandler.endTouch(velocity);
             mInteractionHandler = null;
         }
         mVelocityTracker.recycle();
@@ -290,9 +313,16 @@
         Point displaySize = new Point();
         Display display = getSystemService(WindowManager.class).getDefaultDisplay();
         display.getRealSize(displaySize);
+        int rotation = display.getRotation();
+        // The rotation is backwards in landscape, so flip it.
+        if (rotation == Surface.ROTATION_270) {
+            rotation = Surface.ROTATION_90;
+        } else if (rotation == Surface.ROTATION_90) {
+            rotation = Surface.ROTATION_270;
+        }
         try {
             return mISystemUiProxy.screenshot(new Rect(), displaySize.x, displaySize.y, 0, 100000,
-                    false, display.getRotation()).toBitmap();
+                    false, rotation).toBitmap();
         } catch (RemoteException e) {
             Log.e(TAG, "Error capturing snapshot", e);
             return null;