From 6ac3d0f86c0a7d4d41e98add55497a197b81140c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Wed, 19 Jul 2023 16:30:21 +0200 Subject: [PATCH] Fix shortcut pin intent handling --- .../launcher/activities/MainActivity.kt | 34 +++++++++++++------ .../launcher/views/HomeScreenGrid.kt | 9 ++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index 7a72b2c..2dc524d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -101,6 +101,25 @@ class MainActivity : SimpleActivity(), FlingListener { fragment.beVisible() } + handleIntentAction(intent) + + home_screen_grid.itemClickListener = { + performItemClick(it) + } + + home_screen_grid.itemLongClickListener = { + performItemLongClick(home_screen_grid.getClickableRect(it).left.toFloat(), it) + } + } + + override fun onNewIntent(intent: Intent?) { + super.onNewIntent(intent) + if (intent != null) { + handleIntentAction(intent) + } + } + + private fun handleIntentAction(intent: Intent) { if (intent.action == LauncherApps.ACTION_CONFIRM_PIN_SHORTCUT) { val launcherApps = applicationContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps val item = launcherApps.getPinItemRequest(intent) @@ -132,24 +151,19 @@ class MainActivity : SimpleActivity(), FlingListener { icon ) + runOnUiThread { + home_screen_grid.skipToPage(page) + } // delay showing the shortcut both to let the user see adding it in realtime and hackily avoid concurrent modification exception at HomeScreenGrid Thread.sleep(2000) try { item.accept() - home_screen_grid.storeAndShowGridItem(gridItem, navigateToPage = true) + home_screen_grid.storeAndShowGridItem(gridItem) } catch (ignored: IllegalStateException) { } } } - - home_screen_grid.itemClickListener = { - performItemClick(it) - } - - home_screen_grid.itemLongClickListener = { - performItemLongClick(home_screen_grid.getClickableRect(it).left.toFloat(), it) - } } private fun findFirstEmptyCell(): Pair { @@ -169,7 +183,7 @@ class MainActivity : SimpleActivity(), FlingListener { for (checkedXCell in 0 until ROW_COUNT - 1) { val wantedCell = Triple(page, checkedXCell, checkedYCell) if (!occupiedCells.contains(wantedCell)) { - return Pair(page, Rect(wantedCell.first, wantedCell.second, wantedCell.first, wantedCell.second)) + return Pair(page, Rect(wantedCell.second, wantedCell.third, wantedCell.second, wantedCell.third)) } } } 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 4ab808f..ddb38ac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -368,13 +368,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel } } - fun storeAndShowGridItem(item: HomeScreenGridItem, navigateToPage: Boolean = false) { + fun storeAndShowGridItem(item: HomeScreenGridItem) { val newId = context.homeScreenGridItemsDB.insert(item) item.id = newId gridItems.add(item) - if (navigateToPage) { - skipToPage(item.page) - } redrawGrid() } @@ -903,8 +900,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel return false } - private fun skipToPage(targetPage: Int): Boolean { - if (targetPage < getMaxPage() + 1) { + fun skipToPage(targetPage: Int): Boolean { + if (currentPage != targetPage && targetPage < getMaxPage() + 1) { lastPage = currentPage currentPage = targetPage handlePageChange()