From 9e24c7c37d640455f999152ffffe89a91a45d3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Fri, 22 Sep 2023 11:28:35 +0200 Subject: [PATCH] Prevent crashes when dragging widgets after resizing grid If grid is resized to a size too small to fit a widget, it should not be displayed. It was still possible to drag it in some situations, causing a crash, since it can not fit the screen. --- .../launcher/views/HomeScreenGrid.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index c13e540..5117dd4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -76,7 +76,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel private var draggedItem: HomeScreenGridItem? = null private var resizedWidget: HomeScreenGridItem? = null private var isFirstDraw = true - private var redrawWidgets = false private var iconSize = 0 private val pager = AnimatedGridPager( @@ -212,7 +211,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel cells.clear() gridCenters.clear() iconMargin = (context.resources.getDimension(R.dimen.icon_side_margin) * 5 / columnCount).toInt() - redrawWidgets = true + isFirstDraw = true + gridItems.filter { it.type == ITEM_TYPE_WIDGET }.forEach { + appWidgetHost.deleteAppWidgetId(it.widgetId) + } + widgetViews.forEach { removeView(it) } + widgetViews.clear() redrawGrid() } } @@ -835,6 +839,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel } private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) { + if (item.outOfBounds()) { + return + } + val activity = context as MainActivity val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { it.provider.className == item.className } if (appWidgetProviderInfo != null) { @@ -1298,7 +1306,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel } private fun HomeScreenGridItem.outOfBounds(): Boolean { - return (left >= columnCount || right >= columnCount || (!docked && (top >= rowCount - 1 || bottom >= rowCount - 1))) + return (left >= columnCount + || right >= columnCount + || (!docked && (top >= rowCount - 1 || bottom >= rowCount - 1)) + || (type == ITEM_TYPE_WIDGET && (bottom - top > rowCount - 1 || right - left > columnCount - 1)) + ) } private inner class HomeScreenGridTouchHelper(host: View) : ExploreByTouchHelper(host) {