Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 48d6c62f authored by Saalim Quadri's avatar Saalim Quadri
Browse files

(fix): Revert back to AOSP icon push mechanism

* Partial revert of [1]

[1]: ea5ff7b6



Signed-off-by: default avatarSaalim Quadri <danascape@gmail.com>
parent 6d7bb2b1
Loading
Loading
Loading
Loading
Loading
+40 −140
Original line number Diff line number Diff line
@@ -15,25 +15,19 @@
 */
package com.android.launcher3.celllayout;

import static com.android.launcher3.CellLayout.MODE_ON_DROP;

import android.graphics.Rect;
import android.view.View;

import com.android.launcher3.CellLayout;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.Workspace;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.util.CellAndSpan;
import com.android.launcher3.util.GridOccupancy;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.widget.LauncherAppWidgetHostView;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
@@ -50,10 +44,6 @@ public class ReorderAlgorithm {
        mCellLayout = cellLayout;
    }

    private final ArrayList<View> mIntersectingViews = new ArrayList<>();

    private final Rect mOccupiedRect = new Rect();

    /**
     * This method differs from closestEmptySpaceReorder and dropInPlaceSolution because this method
     * will move items around and will change the shape of the item if possible to try to find a
@@ -110,30 +100,8 @@ public class ReorderAlgorithm {
        boolean success;
        // First we try the exact nearest position of the item being dragged,
        // we will then want to try to move this around to other neighbouring positions
        if (intersectingViewsExists(result[0], result[1], spanX, spanY, direction, dragView, solution)) {
            int[] nearestResult = new int[2];
            mCellLayout.markCellsAsOccupiedForView(dragView);
            mCellLayout.findCellForSpan(nearestResult, spanX, spanY);
            if (nearestResult[1] <= result[1]) {
                result = nearestResult;
                if (result[0] == 0) {
                    result[0] = mCellLayout.getCountX() - 1;
                    result[1] = result[1] - 1;
                } else {
                    result[0] = result[0] - 1;
                }
            }
            if ((result[0] >= 0 && result[1] >= 0) && solution.map.containsKey(dragView)) {
                intersectingViewsExists(result[0], result[1], spanX, spanY, direction, dragView, solution);
            } else {
                mCellLayout.findCellForSpan(nearestResult, spanX, spanY);
                result = nearestResult;
                intersectingViewsExists(result[0], result[1], spanX, spanY, direction, dragView, solution);
            }
            mCellLayout.markCellsAsUnoccupiedForView(dragView);
        }
        success = rearrangementExists(direction, dragView, solution);

        success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
                solution);

        if (!success) {
            // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
@@ -156,12 +124,13 @@ public class ReorderAlgorithm {
        return solution;
    }

    private boolean intersectingViewsExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
    private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
            View ignoreView, ItemConfiguration solution) {
        // Return early if get invalid cell positions
        if (cellX < 0 || cellY < 0) return false;
        mIntersectingViews.clear();
        mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);

        ArrayList<View> intersectingViews = new ArrayList<>();
        Rect occupiedRect = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);

        // Mark the desired location of the view currently being dragged.
        if (ignoreView != null) {
@@ -192,40 +161,30 @@ public class ReorderAlgorithm {
                if (!lp.canReorder) {
                    return false;
                }
                if (!isWidget() && child instanceof LauncherAppWidgetHostView) {
                    return false;
                }
                mIntersectingViews.add(child);
                intersectingViews.add(child);
            }
        }

        solution.intersectingViews = new ArrayList<>(mIntersectingViews);

        return !mIntersectingViews.isEmpty();

    }
        solution.intersectingViews = intersectingViews;

    public boolean rearrangementExists(int[] direction, View ignoreView, ItemConfiguration solution) {
        // First we try to find a solution which respects the push mechanic. That is,
        // we try to find a solution such that no displaced item travels through another item
        // without also displacing that item.
        if (mIntersectingViews.size() == 1  || mIntersectingViews.isEmpty()) {
            if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
        if (attemptPushInDirection(intersectingViews, occupiedRect, direction, ignoreView,
                solution)) {
            return true;
        }
        }

        // Next we try moving the views as a block, but without requiring the push mechanic.
        //if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
        //       solution)) {
        //   return true;
        // }
        if (addViewsToTempLocation(intersectingViews, occupiedRect, direction, ignoreView,
                solution)) {
            return true;
        }

        // Ok, they couldn't move as a block, let's move them individually
        boolean success = false;
        for (View v : mIntersectingViews) {
            if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
        for (View v : intersectingViews) {
            if (!addViewToTempLocation(v, occupiedRect, direction, solution)) {
                return false;
            } else {
                success = true;
@@ -234,7 +193,6 @@ public class ReorderAlgorithm {
        return success;
    }


    private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop, int[] direction,
            ItemConfiguration currentState) {
        CellAndSpan c = currentState.map.get(v);
@@ -343,6 +301,7 @@ public class ReorderAlgorithm {
            }
        }


        while (pushDistance > 0 && !fail) {
            for (View v : currentState.sortedViews) {
                // For each view that isn't in the cluster, we see if the leading edge of the
@@ -413,89 +372,32 @@ public class ReorderAlgorithm {
        if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
            // If the direction vector has two non-zero components, we try pushing
            // separately in each of the components.
            int temp = direction[1];
            direction[1] = 0;

            if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                    ignoreView, solution)) {
            int temp;
            for (int j = 0; j < 2; j++) {
                for (int i = 1; i >= 0; i--) {
                    temp = direction[i];
                    direction[i] = 0;
                    if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                            solution)) {
                        return true;
                    }
            direction[1] = temp;
            temp = direction[0];
            direction[0] = 0;

            if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                    ignoreView, solution)) {
                return true;
                    direction[i] = temp;
                }
            // Revert the direction
            direction[0] = temp;

            // Now we try pushing in each component of the opposite direction
            direction[0] *= -1;
            direction[1] *= -1;
            temp = direction[1];
            direction[1] = 0;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                    ignoreView, solution)) {
                return true;
            }

            direction[1] = temp;
            temp = direction[0];
            direction[0] = 0;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                    ignoreView, solution)) {
                return true;
                revertDir(direction);
            }
            // revert the direction
            direction[0] = temp;
            direction[0] *= -1;
            direction[1] *= -1;

        } else {
            // If the direction vector has a single non-zero component, we push first in the
            // direction of the vector
            if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                    ignoreView, solution)) {
            int temp;
            for (int j = 0; j < 2; j++) {
                for (int i = 0; i < 2; i++) {
                    if (pushViewsToTempLocation(intersectingViews, occupied, direction, ignoreView,
                            solution)) {
                        return true;
                    }
            // Then we try the opposite direction
            direction[0] *= -1;
            direction[1] *= -1;
            if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                    ignoreView, solution)) {
                return true;
                    revertDir(direction);
                }
            // Switch the direction back
            direction[0] *= -1;
            direction[1] *= -1;

            // If we have failed to find a push solution with the above, then we try
            // to find a solution by pushing along the perpendicular axis.

                // Swap the components
            if (isWidget()) {
                int temp = direction[1];
                direction[1] = direction[0];
                direction[0] = temp;
                if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                        ignoreView, solution)) {
                    return true;
                }

                // Then we try the opposite direction
                direction[0] *= -1;
                direction[1] *= -1;
                if (pushViewsToTempLocation(intersectingViews, occupied, direction,
                        ignoreView, solution)) {
                    return true;
                }
                // Switch the direction back
                direction[0] *= -1;
                direction[1] *= -1;

                // Swap the components back
                temp = direction[1];
                direction[1] = direction[0];
                direction[0] = temp;
@@ -822,8 +724,6 @@ public class ReorderAlgorithm {
        return Workspace.isWidget;
    }



    public Workspace<?> getWorkspace() {
        return Launcher.cast(mCellLayout.mActivity).getWorkspace();
    }