Merge pull request #143 from esensar/fix/widget-grid-resize-crash

Prevent crashes when dragging widgets after resizing grid
This commit is contained in:
Tibor Kaputa 2023-09-22 11:40:04 +02:00 committed by GitHub
commit d383f371e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 3 deletions

View File

@ -76,7 +76,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private var draggedItem: HomeScreenGridItem? = null private var draggedItem: HomeScreenGridItem? = null
private var resizedWidget: HomeScreenGridItem? = null private var resizedWidget: HomeScreenGridItem? = null
private var isFirstDraw = true private var isFirstDraw = true
private var redrawWidgets = false
private var iconSize = 0 private var iconSize = 0
private val pager = AnimatedGridPager( private val pager = AnimatedGridPager(
@ -212,7 +211,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
cells.clear() cells.clear()
gridCenters.clear() gridCenters.clear()
iconMargin = (context.resources.getDimension(R.dimen.icon_side_margin) * 5 / columnCount).toInt() 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() redrawGrid()
} }
} }
@ -835,6 +839,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) { private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) {
if (item.outOfBounds()) {
return
}
val activity = context as MainActivity val activity = context as MainActivity
val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { it.provider.className == item.className } val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { it.provider.className == item.className }
if (appWidgetProviderInfo != null) { if (appWidgetProviderInfo != null) {
@ -1298,7 +1306,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
private fun HomeScreenGridItem.outOfBounds(): Boolean { 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) { private inner class HomeScreenGridTouchHelper(host: View) : ExploreByTouchHelper(host) {