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

Commit 08f7261d authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding a rank column for itemInfo

> Rank is used to determine position of an item
in a folder.

Bug: 18590192
Change-Id: I2826a7c570b4cc58e719d685f17a24ec6133804f
parent e87e6abc
Loading
Loading
Loading
Loading
+16 −55
Original line number Diff line number Diff line
@@ -71,8 +71,6 @@ public class Folder extends LinearLayout implements DragSource, View.OnClickList
    static final int STATE_ANIMATING = 1;
    static final int STATE_OPEN = 2;

    private static final int CLOSE_FOLDER_DELAY_MS = 150;

    private int mExpandDuration;
    private int mMaterialExpandDuration;
    private int mMaterialExpandStagger;
@@ -342,60 +340,33 @@ public class Folder extends LinearLayout implements DragSource, View.OnClickList
        return mInfo;
    }

    private class GridComparator implements Comparator<ShortcutInfo> {
        int mNumCols;
        public GridComparator(int numCols) {
            mNumCols = numCols;
        }

        @Override
        public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
            int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
            int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
            return (lhIndex - rhIndex);
        }
    }

    private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
        int maxX = 0;
        int count = items.size();
        for (int i = 0; i < count; i++) {
            ShortcutInfo item = items.get(i);
            if (item.cellX > maxX) {
                maxX = item.cellX;
            }
        }

        GridComparator gridComparator = new GridComparator(maxX + 1);
        Collections.sort(items, gridComparator);
        final int countX = mContent.getCountX();
        for (int i = 0; i < count; i++) {
            int x = i % countX;
            int y = i / countX;
            ShortcutInfo item = items.get(i);
            item.cellX = x;
            item.cellY = y;
        }
    }

    void bind(FolderInfo info) {
        mInfo = info;
        ArrayList<ShortcutInfo> children = info.contents;
        ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
        setupContentForNumItems(children.size());
        placeInReadingOrder(children);
        int count = 0;

        final int totalChildren = children.size();
        setupContentForNumItems(totalChildren);

        // Arrange children in the grid based on the rank.
        Collections.sort(children, Utilities.RANK_COMPARATOR);
        final int countX = mContent.getCountX();

        int visibleChildren = 0;
        for (int i = 0; i < children.size(); i++) {
            ShortcutInfo child = (ShortcutInfo) children.get(i);
            ShortcutInfo child = children.get(i);
            child.cellX = i % countX;
            child.cellY = i / countX;

            if (createAndAddShortcut(child) == null) {
                overflow.add(child);
            } else {
                count++;
                visibleChildren++;
            }
        }

        // We rearrange the items in case there are any empty gaps
        setupContentForNumItems(count);
        setupContentForNumItems(visibleChildren);

        // If our folder has too many items we prune them from the list. This is an issue
        // when upgrading from the old Folders implementation which could contain an unlimited
@@ -414,7 +385,6 @@ public class Folder extends LinearLayout implements DragSource, View.OnClickList
        } else {
            mFolderName.setText("");
        }
        updateItemLocationsInDatabase();

        // In case any children didn't come across during loading, clean up the folder accordingly
        mFolderIcon.post(new Runnable() {
@@ -914,22 +884,13 @@ public class Folder extends LinearLayout implements DragSource, View.OnClickList
        // Do nothing
    }

    private void updateItemLocationsInDatabase() {
        ArrayList<View> list = getItemsInReadingOrder();
        for (int i = 0; i < list.size(); i++) {
            View v = list.get(i);
            ItemInfo info = (ItemInfo) v.getTag();
            LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
                    info.cellX, info.cellY);
        }
    }

    private void updateItemLocationsInDatabaseBatch() {
        ArrayList<View> list = getItemsInReadingOrder();
        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
        for (int i = 0; i < list.size(); i++) {
            View v = list.get(i);
            ItemInfo info = (ItemInfo) v.getTag();
            info.rank = i;
            items.add(info);
        }

+7 −5
Original line number Diff line number Diff line
@@ -97,6 +97,11 @@ public class ItemInfo {
     */
    public int minSpanY = 1;

    /**
     * Indicates the position in an ordered list.
     */
    public int rank = 0;

    /**
     * Indicates that this item needs to be updated in the db
     */
@@ -135,6 +140,7 @@ public class ItemInfo {
        cellY = info.cellY;
        spanX = info.spanX;
        spanY = info.spanY;
        rank = info.rank;
        screenId = info.screenId;
        itemType = info.itemType;
        container = info.container;
@@ -161,6 +167,7 @@ public class ItemInfo {
        values.put(LauncherSettings.Favorites.CELLY, cellY);
        values.put(LauncherSettings.Favorites.SPANX, spanX);
        values.put(LauncherSettings.Favorites.SPANY, spanY);
        values.put(LauncherSettings.Favorites.RANK, rank);
        long serialNumber = UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
        values.put(LauncherSettings.Favorites.PROFILE_ID, serialNumber);

@@ -170,11 +177,6 @@ public class ItemInfo {
        }
    }

    void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) {
        values.put(LauncherSettings.Favorites.CELLX, cellX);
        values.put(LauncherSettings.Favorites.CELLY, cellY);
    }

    static byte[] flattenBitmap(Bitmap bitmap) {
        // Try go guesstimate how much space the icon will take when serialized
        // to avoid unnecessary allocations/copies during the write.
+5 −0
Original line number Diff line number Diff line
@@ -90,6 +90,11 @@ public class LauncherBackupAgentHelper extends BackupAgentHelper {
        if (hasData && mHelper.restoreSuccessful) {
            LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
            LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);

            // TODO: Update the backup set to include rank.
            if (mHelper.restoredBackupVersion <= 2) {
                LauncherAppState.getLauncherProvider().updateFolderItemsRank();
            }
        } else {
            if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
            LauncherAppState.getLauncherProvider().createEmptyDB();
+2 −2
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ import android.app.backup.BackupDataInputStream;
import android.app.backup.BackupDataOutput;
import android.app.backup.BackupHelper;
import android.app.backup.BackupManager;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.ContentResolver;
@@ -60,7 +59,6 @@ import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.zip.CRC32;

/**
@@ -148,6 +146,7 @@ public class LauncherBackupHelper implements BackupHelper {

    private DeviceProfieData mCurrentProfile;
    boolean restoreSuccessful;
    int restoredBackupVersion = 1;

    public LauncherBackupHelper(Context context) {
        mContext = context;
@@ -299,6 +298,7 @@ public class LauncherBackupHelper implements BackupHelper {
                MessageNano.mergeFrom(journal, readCheckedBytes(mBuffer, dataSize));
                applyJournal(journal);
                restoreSuccessful = isBackupCompatible(journal);
                restoredBackupVersion = journal.backupVersion;
                return;
            }

+8 −2
Original line number Diff line number Diff line
@@ -768,6 +768,7 @@ public class LauncherModel extends BroadcastReceiver
        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
        values.put(LauncherSettings.Favorites.RANK, item.rank);
        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);

        updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
@@ -801,6 +802,7 @@ public class LauncherModel extends BroadcastReceiver
            values.put(LauncherSettings.Favorites.CONTAINER, item.container);
            values.put(LauncherSettings.Favorites.CELLX, item.cellX);
            values.put(LauncherSettings.Favorites.CELLY, item.cellY);
            values.put(LauncherSettings.Favorites.RANK, item.rank);
            values.put(LauncherSettings.Favorites.SCREEN, item.screenId);

            contentValues.add(values);
@@ -832,6 +834,7 @@ public class LauncherModel extends BroadcastReceiver
        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
        values.put(LauncherSettings.Favorites.RANK, item.rank);
        values.put(LauncherSettings.Favorites.SPANX, item.spanX);
        values.put(LauncherSettings.Favorites.SPANY, item.spanY);
        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
@@ -845,7 +848,6 @@ public class LauncherModel extends BroadcastReceiver
    static void updateItemInDatabase(Context context, final ItemInfo item) {
        final ContentValues values = new ContentValues();
        item.onAddToDatabase(context, values);
        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
        updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
    }

@@ -906,6 +908,7 @@ public class LauncherModel extends BroadcastReceiver
        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
        final int rankIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.RANK);
        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
        final int profileIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
@@ -915,6 +918,7 @@ public class LauncherModel extends BroadcastReceiver
                ItemInfo item = new ItemInfo();
                item.cellX = c.getInt(cellXIndex);
                item.cellY = c.getInt(cellYIndex);
                item.rank = c.getInt(rankIndex);
                item.spanX = Math.max(1, c.getInt(spanXIndex));
                item.spanY = Math.max(1, c.getInt(spanYIndex));
                item.container = c.getInt(containerIndex);
@@ -1002,7 +1006,6 @@ public class LauncherModel extends BroadcastReceiver

        item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
        values.put(LauncherSettings.Favorites._ID, item.id);
        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);

        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        Runnable r = new Runnable() {
@@ -1917,6 +1920,8 @@ public class LauncherModel extends BroadcastReceiver
                            (LauncherSettings.Favorites.SPANX);
                    final int spanYIndex = c.getColumnIndexOrThrow(
                            LauncherSettings.Favorites.SPANY);
                    final int rankIndex = c.getColumnIndexOrThrow(
                            LauncherSettings.Favorites.RANK);
                    final int restoredIndex = c.getColumnIndexOrThrow(
                            LauncherSettings.Favorites.RESTORED);
                    final int profileIdIndex = c.getColumnIndexOrThrow(
@@ -2102,6 +2107,7 @@ public class LauncherModel extends BroadcastReceiver
                                    info.screenId = c.getInt(screenIndex);
                                    info.cellX = c.getInt(cellXIndex);
                                    info.cellY = c.getInt(cellYIndex);
                                    info.rank = c.getInt(rankIndex);
                                    info.spanX = 1;
                                    info.spanY = 1;
                                    info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Loading