Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix widgets getting stuck or disappearing #83

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ interface HomeScreenGridItemsDao {
@Query("UPDATE home_screen_grid_items SET `left` = :left, `top` = :top, `right` = :right, `bottom` = :bottom, `page` = :page, `docked` = :docked , `parent_id` = :parentId WHERE id = :id")
fun updateItemPosition(left: Int, top: Int, right: Int, bottom: Int, page: Int, docked: Boolean, parentId: Long?, id: Long)

@Query("UPDATE home_screen_grid_items SET widget_id = :widgetId WHERE id = :id")
fun updateWidgetId(widgetId: Int, id: Long): Int

@Query("DELETE FROM home_screen_grid_items WHERE id = :id")
fun deleteItemById(id: Long)

Expand Down
34 changes: 19 additions & 15 deletions app/src/main/kotlin/org/fossify/home/views/HomeScreenGrid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val itemId = context.homeScreenGridItemsDB.insert(widgetItem)
widgetItem.id = itemId
post {
bindWidget(widgetItem, false)
bindWidget(widgetItem)
}
} else {
context.homeScreenGridItemsDB.updateItemPosition(
Expand Down Expand Up @@ -880,27 +880,31 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
redrawGrid()
}

private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) {
private fun bindWidget(item: HomeScreenGridItem) {
if (item.outOfBounds()) {
return
}

val activity = context as MainActivity
val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { it.provider.className == item.className }
if (appWidgetProviderInfo != null) {
val appWidgetId = appWidgetHost.allocateAppWidgetId()
activity.handleWidgetBinding(appWidgetManager, appWidgetId, appWidgetProviderInfo) { canBind ->
item.widgetId = appWidgetHost.allocateAppWidgetId()
ensureBackgroundThread {
context.homeScreenGridItemsDB.updateWidgetId(item.widgetId, item.id!!)
}

activity.handleWidgetBinding(appWidgetManager, item.widgetId, appWidgetProviderInfo) { canBind ->
if (canBind) {
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
activity.handleWidgetConfigureScreen(appWidgetHost, appWidgetId) { success ->
if (appWidgetProviderInfo.configure != null) {
activity.handleWidgetConfigureScreen(appWidgetHost, item.widgetId) { success ->
if (success) {
placeAppWidget(appWidgetId, appWidgetProviderInfo, item)
placeAppWidget(appWidgetProviderInfo, item)
} else {
removeItemFromHomeScreen(item)
}
}
} else {
placeAppWidget(appWidgetId, appWidgetProviderInfo, item)
placeAppWidget(appWidgetProviderInfo, item)
}
} else {
removeItemFromHomeScreen(item)
Expand All @@ -913,12 +917,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
}

private fun placeAppWidget(appWidgetId: Int, appWidgetProviderInfo: AppWidgetProviderInfo, item: HomeScreenGridItem) {
item.widgetId = appWidgetId
private fun placeAppWidget(appWidgetProviderInfo: AppWidgetProviderInfo, item: HomeScreenGridItem) {
// we have to pass the base context here, else there will be errors with the themes
val widgetView = appWidgetHost.createView((context as MainActivity).baseContext, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.tag = appWidgetId
widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo)
val widgetView = appWidgetHost.createView((context as MainActivity).baseContext, item.widgetId, appWidgetProviderInfo) as MyAppWidgetHostView
widgetView.tag = item.widgetId
widgetView.setAppWidget(item.widgetId, appWidgetProviderInfo)
widgetView.longPressListener = { x, y ->
val activity = context as? MainActivity
if (activity?.isAllAppsFragmentExpanded() == false) {
Expand All @@ -937,7 +940,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel

// remove the drawable so that it gets refreshed on long press
item.drawable = null
// Delete existing windget if it has already been loaded to the list
// Delete existing widget if it has already been loaded to the list
gridItems.removeIf { it.id == item.id }
gridItems.add(item)
}
Expand Down Expand Up @@ -1051,7 +1054,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel

if (isFirstDraw) {
gridItems.filter { it.type == ITEM_TYPE_WIDGET && !it.outOfBounds() }.forEach { item ->
bindWidget(item, true)
val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { it.provider.className == item.className }
placeAppWidget(appWidgetProviderInfo!!, item)
}
} else {
gridItems.filter { it.type == ITEM_TYPE_WIDGET && !it.outOfBounds() }.forEach { item ->
Expand Down