From 2ff37d27d417c24f28c3eae895aeb4e9bdd2d822 Mon Sep 17 00:00:00 2001 From: Suphon Thanakornpakapong Date: Mon, 25 Apr 2022 11:08:34 +0700 Subject: [PATCH] Fix IndexOutOfBoundsException while loading items --- .../core/database/model/LauncherItem.java | 2 +- .../features/launcher/AppProvider.java | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/foundation/e/blisslauncher/core/database/model/LauncherItem.java b/app/src/main/java/foundation/e/blisslauncher/core/database/model/LauncherItem.java index 15860ca805..6c4ca0d544 100644 --- a/app/src/main/java/foundation/e/blisslauncher/core/database/model/LauncherItem.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/database/model/LauncherItem.java @@ -140,7 +140,7 @@ public class LauncherItem { @Override public String toString() { - return "[id: " + id + "item_type: " + itemType + ", container: " + container + ", screen: " + screenId + return "[id: " + id + ", item_type: " + itemType + ", container: " + container + ", screen: " + screenId + ", cell: " + cell + "]"; } diff --git a/app/src/main/java/foundation/e/blisslauncher/features/launcher/AppProvider.java b/app/src/main/java/foundation/e/blisslauncher/features/launcher/AppProvider.java index 27f897a97e..1f56e86ed9 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/launcher/AppProvider.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/launcher/AppProvider.java @@ -337,6 +337,8 @@ public class AppProvider { if (index != null) { FolderItem folderItem = (FolderItem) mLauncherItems.get(index); folderItem.items.add(applicationItem); + } else { + Log.e("AppProvider", "folder not found for item: " + applicationItem.id); } } } else if (databaseItem.itemType == Constants.ITEM_TYPE_SHORTCUT) { @@ -377,17 +379,22 @@ public class AppProvider { } } - if (foldersIndex.size() > 0) { - for (int i = 0; i < foldersIndex.size(); i++) { - FolderItem folderItem = - (FolderItem) mLauncherItems.get(foldersIndex.get(foldersIndex.keyAt(i))); - if (folderItem.items == null || folderItem.items.size() == 0) { - DatabaseManager.getManager(mContext).removeLauncherItem(folderItem.id); - mLauncherItems.remove((int) foldersIndex.get(foldersIndex.keyAt(i))); - } else { - folderItem.icon = new GraphicsUtil(mContext).generateFolderIcon(mContext, - folderItem); - } + List folderItemsIndex = new ArrayList<>(); + for (int i = 0; i < foldersIndex.size(); i++) { + int itemIndex = foldersIndex.get(foldersIndex.keyAt(i)); + folderItemsIndex.add(itemIndex); + } + Collections.sort(folderItemsIndex); + for (int i = folderItemsIndex.size() - 1; i >= 0; i--) { + int itemIndex = folderItemsIndex.get(i); + FolderItem folderItem = + (FolderItem) mLauncherItems.get(itemIndex); + if (folderItem.items == null || folderItem.items.size() == 0) { + DatabaseManager.getManager(mContext).removeLauncherItem(folderItem.id); + mLauncherItems.remove(itemIndex); + } else { + folderItem.icon = new GraphicsUtil(mContext).generateFolderIcon(mContext, + folderItem); } } -- GitLab