From e63401e711176e2e5cc1f4ec3ba4fd960c4f6144 Mon Sep 17 00:00:00 2001 From: Brandon Dayauon Date: Fri, 21 Apr 2023 11:09:33 -0700 Subject: [PATCH] Separate animations and add dragOffSet points in dragOptions For 1, when animateShift() is called shiftAnimation() is the only thing needed. 2, by having point saved in preDragCondition, we can use that to update the drag layers and not have to modify registrationX/Y later Removed dragVisualizedOffset since it does not do anything. There is an issue that also needs to be fixed if user decides to long click on a search result and let go, the icon flashes at the touch point when you let go.. so we check when mContent can be shown in DragView depending on if there is dragOffset. If there is dragOffset, set mContent to invisible. If there is no dragOffset, set mContent to visible because it doesn't matter as the original content is at the same spot. bug: 245659929 test: manual: video: https://drive.google.com/file/d/1JQ0pud31HU0WlrqecX0v1cdPKQ37jQCf/view?usp=sharing Change-Id: I4d2276b9c43e1e92c45d8538b8dde70baa84a5e8 --- .../taskbar/TaskbarDragController.java | 5 +- src/com/android/launcher3/Workspace.java | 14 +++-- .../launcher3/dragndrop/DragController.java | 13 ++-- .../launcher3/dragndrop/DragOptions.java | 7 +++ .../android/launcher3/dragndrop/DragView.java | 59 +++++++++++-------- .../dragndrop/LauncherDragController.java | 8 +-- .../SecondaryDisplayLauncher.java | 17 ++++-- .../SecondaryDragController.java | 9 +-- .../secondarydisplay/SecondaryDragLayer.java | 2 +- .../widget/PendingItemDragHelper.java | 9 +-- 10 files changed, 81 insertions(+), 62 deletions(-) diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java index 72add4fc77..88fea31543 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarDragController.java @@ -198,7 +198,7 @@ public class TaskbarDragController extends DragController im @Override public boolean shouldStartDrag(double distanceDragged) { - return mDragView != null && mDragView.isAnimationFinished(); + return mDragView != null && mDragView.isScaleAnimationFinished(); } @Override @@ -231,7 +231,6 @@ public class TaskbarDragController extends DragController im dragLayerY, (View target, DropTarget.DragObject d, boolean success) -> {} /* DragSource */, (ItemInfo) btv.getTag(), - /* dragVisualizeOffset = */ null, dragRect, scale * iconScale, scale, @@ -241,7 +240,7 @@ public class TaskbarDragController extends DragController im @Override protected DragView startDrag(@Nullable Drawable drawable, @Nullable View view, DraggableView originalView, int dragLayerX, int dragLayerY, DragSource source, - ItemInfo dragInfo, Point dragOffset, Rect dragRegion, float initialDragViewScale, + ItemInfo dragInfo, Rect dragRegion, float initialDragViewScale, float dragViewScaleOnDrop, DragOptions options) { mOptions = options; diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java index 98016f67fa..2fa1a57666 100644 --- a/src/com/android/launcher3/Workspace.java +++ b/src/com/android/launcher3/Workspace.java @@ -1661,17 +1661,14 @@ public class Workspace extends PagedView scale = previewProvider.getScaleAndPosition(contentView, mTempXY); } - int halfPadding = previewProvider.previewPadding / 2; int dragLayerX = mTempXY[0]; int dragLayerY = mTempXY[1]; - Point dragVisualizeOffset = null; Rect dragRect = new Rect(); if (draggableView != null) { draggableView.getSourceVisualDragBounds(dragRect); dragLayerY += dragRect.top; - dragVisualizeOffset = new Point(-halfPadding, halfPadding); } @@ -1689,6 +1686,15 @@ public class Workspace extends PagedView } } + if (dragOptions.preDragCondition != null) { + int xDragOffSet = dragOptions.preDragCondition.getDragOffset().x; + int yDragOffSet = dragOptions.preDragCondition.getDragOffset().y; + if (xDragOffSet != 0 || yDragOffSet != 0) { + dragLayerX += xDragOffSet; + dragLayerY += yDragOffSet; + } + } + final DragView dv; if (contentView instanceof View) { if (contentView instanceof LauncherAppWidgetHostView) { @@ -1701,7 +1707,6 @@ public class Workspace extends PagedView dragLayerY, source, dragObject, - dragVisualizeOffset, dragRect, scale * iconScale, scale, @@ -1714,7 +1719,6 @@ public class Workspace extends PagedView dragLayerY, source, dragObject, - dragVisualizeOffset, dragRect, scale * iconScale, scale, diff --git a/src/com/android/launcher3/dragndrop/DragController.java b/src/com/android/launcher3/dragndrop/DragController.java index 328d7699ed..98672685e9 100644 --- a/src/com/android/launcher3/dragndrop/DragController.java +++ b/src/com/android/launcher3/dragndrop/DragController.java @@ -143,14 +143,12 @@ public abstract class DragController int dragLayerY, DragSource source, ItemInfo dragInfo, - Point dragOffset, Rect dragRegion, float initialDragViewScale, float dragViewScaleOnDrop, DragOptions options) { - return startDrag(drawable, /* view= */ null, originalView, dragLayerX, dragLayerY, - source, dragInfo, dragOffset, dragRegion, initialDragViewScale, dragViewScaleOnDrop, - options); + return startDrag(drawable, /* view= */ null, originalView, dragLayerX, dragLayerY, source, + dragInfo, dragRegion, initialDragViewScale, dragViewScaleOnDrop, options); } /** @@ -180,14 +178,12 @@ public abstract class DragController int dragLayerY, DragSource source, ItemInfo dragInfo, - Point dragOffset, Rect dragRegion, float initialDragViewScale, float dragViewScaleOnDrop, DragOptions options) { - return startDrag(/* drawable= */ null, view, originalView, dragLayerX, dragLayerY, - source, dragInfo, dragOffset, dragRegion, initialDragViewScale, dragViewScaleOnDrop, - options); + return startDrag(/* drawable= */ null, view, originalView, dragLayerX, dragLayerY, source, + dragInfo, dragRegion, initialDragViewScale, dragViewScaleOnDrop, options); } protected abstract DragView startDrag( @@ -198,7 +194,6 @@ public abstract class DragController int dragLayerY, DragSource source, ItemInfo dragInfo, - Point dragOffset, Rect dragRegion, float initialDragViewScale, float dragViewScaleOnDrop, diff --git a/src/com/android/launcher3/dragndrop/DragOptions.java b/src/com/android/launcher3/dragndrop/DragOptions.java index 1ff433541e..2af81db294 100644 --- a/src/com/android/launcher3/dragndrop/DragOptions.java +++ b/src/com/android/launcher3/dragndrop/DragOptions.java @@ -78,5 +78,12 @@ public class DragOptions { * This will be true if the condition was met, otherwise false. */ void onPreDragEnd(DropTarget.DragObject dragObject, boolean dragStarted); + + /** + * The offset points that should be overridden to update the dragLayer. + */ + default Point getDragOffset() { + return new Point(0,0); + } } } diff --git a/src/com/android/launcher3/dragndrop/DragView.java b/src/com/android/launcher3/dragndrop/DragView.java index 46c8e8114b..0d0717e4f6 100644 --- a/src/com/android/launcher3/dragndrop/DragView.java +++ b/src/com/android/launcher3/dragndrop/DragView.java @@ -37,7 +37,6 @@ import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Path; import android.graphics.Picture; -import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.AdaptiveIconDrawable; import android.graphics.drawable.ColorDrawable; @@ -89,15 +88,17 @@ public abstract class DragView extends Fram private final RunnableList mOnDragStartCallback = new RunnableList(); - private Point mDragVisualizeOffset = null; + private boolean mHasDragOffset; private Rect mDragRegion = null; protected final T mActivity; private final BaseDragLayer mDragLayer; private boolean mHasDrawn = false; - final ValueAnimator mAnim; + final ValueAnimator mScaleAnim; + final ValueAnimator mShiftAnim; + // Whether mAnim has started. Unlike mAnim.isStarted(), this is true even after mAnim ends. - private boolean mAnimStarted; + private boolean mScaleAnimStarted; private Runnable mOnAnimEndCallback = null; private int mLastTouchX; @@ -166,9 +167,9 @@ public abstract class DragView extends Fram setScaleY(initialScale); // Animate the view into the correct position - mAnim = ValueAnimator.ofFloat(0f, 1f); - mAnim.setDuration(VIEW_ZOOM_DURATION); - mAnim.addUpdateListener(animation -> { + mScaleAnim = ValueAnimator.ofFloat(0f, 1f); + mScaleAnim.setDuration(VIEW_ZOOM_DURATION); + mScaleAnim.addUpdateListener(animation -> { final float value = (Float) animation.getAnimatedValue(); setScaleX(Utilities.mapRange(value, initialScale, mEndScale)); setScaleY(Utilities.mapRange(value, initialScale, mEndScale)); @@ -176,10 +177,10 @@ public abstract class DragView extends Fram animation.cancel(); } }); - mAnim.addListener(new AnimatorListenerAdapter() { + mScaleAnim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { - mAnimStarted = true; + mScaleAnimStarted = true; } @Override @@ -190,6 +191,8 @@ public abstract class DragView extends Fram } } }); + // Set up the shift animator. + mShiftAnim = ValueAnimator.ofFloat(0f, 1f); setDragRegion(new Rect(0, 0, width, height)); @@ -319,12 +322,12 @@ public abstract class DragView extends Fram return mDragRegion.height(); } - public void setDragVisualizeOffset(Point p) { - mDragVisualizeOffset = p; + public void setHasDragOffset(boolean hasDragOffset) { + mHasDragOffset = hasDragOffset; } - public Point getDragVisualizeOffset() { - return mDragVisualizeOffset; + public boolean getHasDragOffset() { + return mHasDragOffset; } public void setDragRegion(Rect r) { @@ -392,22 +395,29 @@ public abstract class DragView extends Fram if (mContent != null) { // At the drag start, the source view visibility is set to invisible. - mContent.setVisibility(VISIBLE); + if (getHasDragOffset()) { + // If there is any dragOffset, this means the content will show away of the original + // icon location, otherwise it's fine since original content would just show at the + // same spot. + mContent.setVisibility(INVISIBLE); + } else { + mContent.setVisibility(VISIBLE); + } } move(touchX, touchY); // Post the animation to skip other expensive work happening on the first frame - post(mAnim::start); + post(mScaleAnim::start); } public void cancelAnimation() { - if (mAnim != null && mAnim.isRunning()) { - mAnim.cancel(); + if (mScaleAnim != null && mScaleAnim.isRunning()) { + mScaleAnim.cancel(); } } - public boolean isAnimationFinished() { - return mAnimStarted && !mAnim.isRunning(); + public boolean isScaleAnimationFinished() { + return mScaleAnimStarted && !mScaleAnim.isRunning(); } /** @@ -434,13 +444,15 @@ public abstract class DragView extends Fram int duration); public void animateShift(final int shiftX, final int shiftY) { - if (mAnim.isStarted()) { - return; - } + if (mShiftAnim.isStarted()) return; + + // Set mContent visibility to visible to show icon regardless in case it is INVISIBLE. + if (mContent != null) mContent.setVisibility(VISIBLE); + mAnimatedShiftX = shiftX; mAnimatedShiftY = shiftY; applyTranslation(); - mAnim.addUpdateListener(new AnimatorUpdateListener() { + mShiftAnim.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float fraction = 1 - animation.getAnimatedFraction(); @@ -449,6 +461,7 @@ public abstract class DragView extends Fram applyTranslation(); } }); + mShiftAnim.start(); } private void applyTranslation() { diff --git a/src/com/android/launcher3/dragndrop/LauncherDragController.java b/src/com/android/launcher3/dragndrop/LauncherDragController.java index 0e8b0a5ba6..aaa5b1ace7 100644 --- a/src/com/android/launcher3/dragndrop/LauncherDragController.java +++ b/src/com/android/launcher3/dragndrop/LauncherDragController.java @@ -21,7 +21,6 @@ import static com.android.launcher3.LauncherState.NORMAL; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import android.content.res.Resources; -import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.view.HapticFeedbackConstants; @@ -60,7 +59,6 @@ public class LauncherDragController extends DragController { int dragLayerY, DragSource source, ItemInfo dragInfo, - Point dragOffset, Rect dragRegion, float initialDragViewScale, float dragViewScaleOnDrop, @@ -129,9 +127,11 @@ public class LauncherDragController extends DragController { mDragObject.dragInfo = dragInfo; mDragObject.originalDragInfo = mDragObject.dragInfo.makeShallowCopy(); - if (dragOffset != null) { - dragView.setDragVisualizeOffset(new Point(dragOffset)); + if (mOptions.preDragCondition != null) { + dragView.setHasDragOffset(mOptions.preDragCondition.getDragOffset().x != 0 || + mOptions.preDragCondition.getDragOffset().y != 0); } + if (dragRegion != null) { dragView.setDragRegion(new Rect(dragRegion)); } diff --git a/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java b/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java index 56dffa9b64..fcc62a705f 100644 --- a/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java +++ b/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java @@ -18,7 +18,6 @@ package com.android.launcher3.secondarydisplay; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.content.Intent; -import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -405,17 +404,25 @@ public class SecondaryDisplayLauncher extends BaseDraggingActivity drawable = null; scale = previewProvider.getScaleAndPosition(contentView, mTempXY); } - int halfPadding = previewProvider.previewPadding / 2; + int dragLayerX = mTempXY[0]; int dragLayerY = mTempXY[1]; - Point dragVisualizeOffset = null; Rect dragRect = new Rect(); if (draggableView != null) { draggableView.getSourceVisualDragBounds(dragRect); dragLayerY += dragRect.top; - dragVisualizeOffset = new Point(-halfPadding, halfPadding); } + + if (options.preDragCondition != null) { + int xOffSet = options.preDragCondition.getDragOffset().x; + int yOffSet = options.preDragCondition.getDragOffset().y; + if (xOffSet != 0 && yOffSet != 0) { + dragLayerX += xOffSet; + dragLayerY += yOffSet; + } + } + if (contentView != null) { mDragController.startDrag( contentView, @@ -424,7 +431,6 @@ public class SecondaryDisplayLauncher extends BaseDraggingActivity dragLayerY, source, dragObject, - dragVisualizeOffset, dragRect, scale * iconScale, scale, @@ -437,7 +443,6 @@ public class SecondaryDisplayLauncher extends BaseDraggingActivity dragLayerY, source, dragObject, - dragVisualizeOffset, dragRect, scale * iconScale, scale, diff --git a/src/com/android/launcher3/secondarydisplay/SecondaryDragController.java b/src/com/android/launcher3/secondarydisplay/SecondaryDragController.java index b1a9b868c3..8d1d96b649 100644 --- a/src/com/android/launcher3/secondarydisplay/SecondaryDragController.java +++ b/src/com/android/launcher3/secondarydisplay/SecondaryDragController.java @@ -17,7 +17,6 @@ package com.android.launcher3.secondarydisplay; import android.content.res.Resources; -import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.view.HapticFeedbackConstants; @@ -51,7 +50,7 @@ public class SecondaryDragController extends DragController @Override public boolean shouldStartDrag(double distanceDragged) { - return mDragView != null && mDragView.isAnimationFinished(); + return mDragView != null && mDragView.isScaleAnimationFinished(); } @Override diff --git a/src/com/android/launcher3/widget/PendingItemDragHelper.java b/src/com/android/launcher3/widget/PendingItemDragHelper.java index f2694349ea..faad307b35 100644 --- a/src/com/android/launcher3/widget/PendingItemDragHelper.java +++ b/src/com/android/launcher3/widget/PendingItemDragHelper.java @@ -103,7 +103,6 @@ public class PendingItemDragHelper extends DragPreviewProvider { final int previewWidth; final int previewHeight; final float scale; - final Point dragOffset; final Rect dragRegion; mEstimatedCellSize = launcher.getWorkspace().estimateItemSize(mAddInfo); @@ -173,7 +172,6 @@ public class PendingItemDragHelper extends DragPreviewProvider { scale = previewBounds.width() / (float) previewWidth; launcher.getDragController().addDragListener(new WidgetHostViewLoader(launcher, mView)); - dragOffset = null; dragRegion = null; draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_WIDGET); } else { @@ -188,8 +186,6 @@ public class PendingItemDragHelper extends DragPreviewProvider { li.recycle(); scale = ((float) launcher.getDeviceProfile().iconSizePx) / previewWidth; - dragOffset = new Point(previewPadding / 2, previewPadding / 2); - // Create a preview same as the workspace cell size and draw the icon at the // appropriate position. DeviceProfile dp = launcher.getDeviceProfile(); @@ -217,11 +213,10 @@ public class PendingItemDragHelper extends DragPreviewProvider { // Start the drag if (mAppWidgetHostViewPreview != null) { launcher.getDragController().startDrag(mAppWidgetHostViewPreview, draggableView, - dragLayerX, dragLayerY, source, mAddInfo, dragOffset, dragRegion, scale, scale, - options); + dragLayerX, dragLayerY, source, mAddInfo, dragRegion, scale, scale, options); } else { launcher.getDragController().startDrag(preview, draggableView, dragLayerX, dragLayerY, - source, mAddInfo, dragOffset, dragRegion, scale, scale, options); + source, mAddInfo, dragRegion, scale, scale, options); } }