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

Commit 641637f9 authored by Stephen Bird's avatar Stephen Bird Committed by Matt Garnes
Browse files

Move icons to fit within Dynamic grid upon resize.

If the dynamic grid is made smaller, currently icons will simply be
hidden from view. Instead, create extra screens and squeeze the icons in
so that they will all be visible upon resize.

In some cases, icons can end up creating screens when it's not necessary
we should revisit this at a later time and look into making these icons
show up in a more expected place.

Change-Id: I9a46f1ac45c1a04e252ed8943afcfe952df1392d
(cherry picked from commit e25ab7bd)
parent 3e0d08d7
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -112,6 +112,12 @@ public class ItemInfo {
     */
    CharSequence contentDescription;

    /**
     * Indicates that this item has had it's position changed
     * because the grid size was made smaller and it could no longer fit.
     */
    public boolean wasMovedDueToReducedSpace = false;

    /**
     * The position of the item in a drag-and-drop operation.
     */
+9 −4
Original line number Diff line number Diff line
@@ -481,7 +481,7 @@ public class Launcher extends Activity

        super.onCreate(savedInstanceState);

        initializeDynamicGrid();
        initializeDynamicGrid(false);
        mHideIconLabels = SettingsProvider.getBoolean(this,
                SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
                R.bool.preferences_interface_homescreen_hide_icon_labels_default);
@@ -566,8 +566,10 @@ public class Launcher extends Activity
    @Override
    public void onLauncherProviderChange() { }

    private void initializeDynamicGrid() {
    private void initializeDynamicGrid(boolean updateGrid) {
        if (!updateGrid) {
            LauncherAppState.setApplicationContext(getApplicationContext());
        }
        LauncherAppState app = LauncherAppState.getInstance();

        mHideIconLabels = SettingsProvider.getBoolean(this,
@@ -5167,6 +5169,9 @@ public class Launcher extends Activity
        PackageInstallerCompat.getInstance(this).onFinishBind();
        mModel.recheckRestoredItems(this);
        mWorkspace.stripEmptyScreens();
        if (mWorkspace.isInOverviewMode()) {
            mWorkspace.resetOverviewMode();
        }
    }

    private void sendLoadingCompleteBroadcastIfNecessary() {
@@ -5814,7 +5819,7 @@ public class Launcher extends Activity
    public void updateDynamicGrid(int page) {
        mSearchDropTargetBar.setupQSB(Launcher.this);

        initializeDynamicGrid();
        initializeDynamicGrid(true);

        mGrid.layout(Launcher.this);

+109 −42
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
@@ -48,8 +47,8 @@ import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

@@ -60,7 +59,6 @@ import com.android.launcher3.compat.PackageInstallerCompat;
import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.compat.UserManagerCompat;

import com.android.launcher3.settings.SettingsProvider;

import java.lang.ref.WeakReference;
@@ -1511,11 +1509,7 @@ public class LauncherModel extends BroadcastReceiver
    }

    public void stopLoader() {
        synchronized (mLock) {
            if (mLoaderTask != null) {
                mLoaderTask.stopLocked();
            }
        }
        resetLoadedState(true, true);
    }

    /** Loads the workspace screens db into a map of Rank -> ScreenId */
@@ -1883,38 +1877,114 @@ public class LauncherModel extends BroadcastReceiver
                return true;
            }

            if (!occupied.containsKey(item.screenId)) {
                ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
                occupied.put(item.screenId, items);
            // If the current item's position lies outside of the bounds
            // of the current grid size, attempt to place it in the next
            // available position.
            if (item.cellX < 0 || item.cellY < 0 || item.cellX + item.spanX > countX
                    || item.cellY + item.spanY > countY) {
                if (item.itemType != LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET) {
                    // Place the item at 0 0 of screen 0
                    // if items overlap here, they will be moved later on
                    item.cellX = 0;
                    item.cellY = 0;
                    item.screenId = 0;
                    item.wasMovedDueToReducedSpace = true;
                    item.requiresDbUpdate = true;
                } else {
                    // see if widget can be shrunk to fit a screen, if not, just remove it
                    if (item.minSpanX > countX || item.minSpanY > countY) {
                        deleteOnInvalidPlacement.set(true);
                        return false;
                    }
                    // if the widget is larger than the grid, shrink it down
                    if (item.cellX + item.spanX > countX) {
                        item.cellX = 0;
                        item.spanY = (item.spanY / 2) > 0 ? item.spanY / 2 : 1;
                        item.spanX = item.minSpanX;
                        item.requiresDbUpdate = true;
                        item.wasMovedDueToReducedSpace = true;
                    }
                    if (item.cellY + item.spanY > countY) {
                        item.cellY = 0;
                        item.spanY = countY;
                        item.requiresDbUpdate = true;
                        item.wasMovedDueToReducedSpace = true;
                    }
                    if (item.cellY + item.spanY == countY && item.cellX + item.spanX == countX) {
                        // if the widget is the size of the grid, make a screen all it's own.
                        item.screenId = sBgWorkspaceScreens.size() + 1;
                    }
                }
            } else {
                item.wasMovedDueToReducedSpace = false;
                item.requiresDbUpdate = true;
            }

            final ItemInfo[][] screens = occupied.get(item.screenId);
            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
                    item.cellX < 0 || item.cellY < 0 ||
                    item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
                Log.e(TAG, "Error loading shortcut " + item
                        + " into cell (" + containerIndex + "-" + item.screenId + ":"
                        + item.cellX + "," + item.cellY
                        + ") out of screen bounds ( " + countX + "x" + countY + ")");
                return false;
            if (!occupied.containsKey(item.screenId)) {
                ItemInfo[][] items = new ItemInfo[countX][countY];
                occupied.put(item.screenId, items);
            }
            ItemInfo[][] screens = occupied.get(item.screenId);

            // Check if any workspace icons overlap with each other
            for (int x = item.cellX; x < (item.cellX + item.spanX); x++) {
                for (int y = item.cellY; y < (item.cellY + item.spanY); y++) {
                    if (screens[x][y] != null) {
                        Log.e(TAG, "Error loading shortcut " + item
                            + " into cell (" + containerIndex + "-" + item.screenId + ":"
                            + x + "," + y
                            + ") occupied by "
                            + screens[x][y]);
                        return false;
                        ItemInfo occupiedItem = screens[x][y];
                        // If an item is overlapping another because one of them
                        // was moved due to the size of the grid changing, 
                        // move the current item to a free spot past this one.
                        if (occupiedItem.wasMovedDueToReducedSpace
                                || item.wasMovedDueToReducedSpace) {
                            // overlapping icon exists here
                            // we must find a free space.
                            boolean freeFound = false;
                            int nextX = 0;
                            int nextY = 0;
                            while (!freeFound) {
                                if (screens[nextX][nextY] == null) {
                                    item.cellX = nextX;
                                    item.cellY = nextY;
                                    freeFound = true;
                                } else {
                                    if (nextX + item.spanX == countX) {
                                        if (nextY + item.spanY == countY) {
                                            // If we've reached the bottom of the page and are still
                                            // searching, add a new page to place this item.
                                            item.screenId += 1;
                                            nextY = 0;
                                            nextX = 0;
                                            if (!occupied.containsKey(item.screenId)) {
                                                ItemInfo[][] items = new ItemInfo[countX][countY];
                                                occupied.put(item.screenId, items);
                                            }
                                            screens = occupied.get(item.screenId);
                                        } else {
                                            nextX = 0;
                                            nextY++;
                                        }
                                    } else {
                                        nextX++;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            for (int x = item.cellX; x < (item.cellX + item.spanX); x++) {
                for (int y = item.cellY; y < (item.cellY + item.spanY); y++) {
                    screens[x][y] = item;
                    if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET) {
                        // fill up the entire grid where the widget technically is
                        for (int spanX = x; spanX < item.spanX; spanX++) {
                            screens[spanX][y] = item;
                            for (int spanY = y; spanY < item.spanX; spanY++) {
                                screens[spanX][spanY] = item;
                            }
                        }
                    }
                }
            }

@@ -2489,6 +2559,10 @@ public class LauncherModel extends BroadcastReceiver
                    }
                    LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
                } else {




                    TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
                    for (Integer i : orderedScreens.keySet()) {
                        sBgWorkspaceScreens.add(orderedScreens.get(i));
@@ -2497,26 +2571,19 @@ public class LauncherModel extends BroadcastReceiver
                    Launcher.addDumpLog(TAG, "11683562 -   sBgWorkspaceScreens: " +
                            TextUtils.join(", ", sBgWorkspaceScreens), true);

                    // Remove any empty screens
                    ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
                    // Make sure that we have all the screens we will need that may have been added
                    for (ItemInfo item: sBgItemsIdMap.values()) {
                        long screenId = item.screenId;
                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
                                unusedScreens.contains(screenId)) {
                            unusedScreens.remove(screenId);
                                !sBgWorkspaceScreens.contains(screenId)) {
                                sBgWorkspaceScreens.add(screenId);
                        }
                    }

                    // If there are any empty screens remove them, and update.
                    if (unusedScreens.size() != 0) {
                        // Log to disk
                        Launcher.addDumpLog(TAG, "11683562 -   unusedScreens (to be removed): " +
                                TextUtils.join(", ", unusedScreens), true);

                        sBgWorkspaceScreens.removeAll(unusedScreens);

                    updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
                }
                }

                if (DEBUG_LOADERS) {
                    Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
+7 −0
Original line number Diff line number Diff line
@@ -2173,6 +2173,13 @@ public class Workspace extends SmoothPagedView
        return true;
    }

    public boolean resetOverviewMode() {
        //reset overviewmode without any animations
        exitOverviewMode(-1, false);
        enableOverviewMode(true, -1, false);
        return true;
    }

    public void exitOverviewMode(boolean animated) {
        exitOverviewMode(-1, animated);
    }