add a new menu button for removing icons from the home screen

This commit is contained in:
tibbi 2022-09-22 22:19:18 +02:00
parent 06738b595c
commit 3441a84d02
6 changed files with 30 additions and 12 deletions

View File

@ -177,7 +177,7 @@ class MainActivity : SimpleActivity(), FlingListener {
if (!launchers.map { it.packageName }.contains(packageName)) {
hasDeletedAnything = true
launchersDB.deleteApp(packageName)
homeScreenGridItemsDB.deleteItem(packageName)
homeScreenGridItemsDB.deleteByPackageName(packageName)
}
}
@ -235,7 +235,7 @@ class MainActivity : SimpleActivity(), FlingListener {
main_holder.performHapticFeedback()
val clickedGridItem = home_screen_grid.isClickingGridItem(getGridTouchedX(x), getGridTouchedY(y))
if (clickedGridItem != null) {
showHomeIconMenu(x, y - resources.getDimension(R.dimen.icon_long_press_anchor_offset_y), clickedGridItem)
showHomeIconMenu(x, y - resources.getDimension(R.dimen.icon_long_press_anchor_offset_y), clickedGridItem, false)
return
}
@ -251,15 +251,15 @@ class MainActivity : SimpleActivity(), FlingListener {
}
}
private fun getGridTouchedX(x: Float) = Math.min(Math.max(x.toInt() - home_screen_grid.marginLeft, 0), home_screen_grid.width).toInt()
private fun getGridTouchedX(x: Float) = Math.min(Math.max(x.toInt() - home_screen_grid.marginLeft, 0), home_screen_grid.width)
private fun getGridTouchedY(y: Float) = Math.min(Math.max(y.toInt() - home_screen_grid.marginTop, 0), home_screen_grid.height).toInt()
private fun getGridTouchedY(y: Float) = Math.min(Math.max(y.toInt() - home_screen_grid.marginTop, 0), home_screen_grid.height)
fun showHomeIconMenu(x: Float, y: Float, gridItem: HomeScreenGridItem) {
fun showHomeIconMenu(x: Float, y: Float, gridItem: HomeScreenGridItem, isOnAllAppsFragment: Boolean) {
mLongPressedIcon = gridItem
home_screen_popup_menu_anchor.x = x
home_screen_popup_menu_anchor.y = y
mOpenPopupMenu = handleGridItemPopupMenu(home_screen_popup_menu_anchor, gridItem.packageName)
mOpenPopupMenu = handleGridItemPopupMenu(home_screen_popup_menu_anchor, gridItem, isOnAllAppsFragment)
}
private fun showMainLongPressMenu(x: Float, y: Float) {
@ -278,15 +278,17 @@ class MainActivity : SimpleActivity(), FlingListener {
}
}
private fun handleGridItemPopupMenu(anchorView: View, appPackageName: String): PopupMenu {
private fun handleGridItemPopupMenu(anchorView: View, gridItem: HomeScreenGridItem, isOnAllAppsFragment: Boolean): PopupMenu {
val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme())
return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply {
inflate(R.menu.menu_app_icon)
menu.findItem(R.id.remove).isVisible = !isOnAllAppsFragment
setOnMenuItemClickListener { item ->
(all_apps_fragment as AllAppsFragment).ignoreTouches = false
when (item.itemId) {
R.id.app_info -> launchAppInfo(appPackageName)
R.id.uninstall -> uninstallApp(appPackageName)
R.id.app_info -> launchAppInfo(gridItem.packageName)
R.id.remove -> home_screen_grid.removeAppIcon(gridItem.id!!)
R.id.uninstall -> uninstallApp(gridItem.packageName)
}
true
}

View File

@ -128,7 +128,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) {
val gridItem = HomeScreenGridItem(null, -1, -1, -1, 1, appLauncher.packageName, appLauncher.title)
activity?.showHomeIconMenu(x, y, gridItem)
activity?.showHomeIconMenu(x, y, gridItem, true)
ignoreTouches = true
}
}

View File

@ -23,6 +23,9 @@ interface HomeScreenGridItemsDao {
@Query("UPDATE home_screen_grid_items SET `left` = :left, `top` = :top, `right` = :right, `bottom` = :bottom WHERE id = :id")
fun updateAppPosition(left: Int, top: Int, right: Int, bottom: Int, id: Long)
@Query("DELETE FROM home_screen_grid_items WHERE id = :id")
fun deleteById(id: Long)
@Query("DELETE FROM home_screen_grid_items WHERE package_name = :packageName")
fun deleteItem(packageName: String)
fun deleteByPackageName(packageName: String)
}

View File

@ -64,6 +64,14 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
}
}
fun removeAppIcon(iconId: Long) {
ensureBackgroundThread {
context.homeScreenGridItemsDB.deleteById(iconId)
gridItems.removeIf { it.id == iconId }
invalidate()
}
}
fun itemDraggingStarted(draggedGridItem: HomeScreenGridItem) {
draggedItem = draggedGridItem
invalidate()

View File

@ -6,6 +6,11 @@
android:icon="@drawable/ic_info_vector"
android:title="@string/app_info"
app:showAsAction="always" />
<item
android:id="@+id/remove"
android:icon="@drawable/ic_cross_vector"
android:title="@string/remove"
app:showAsAction="always" />
<item
android:id="@+id/uninstall"
android:icon="@drawable/ic_delete_vector"

View File

@ -1,6 +1,6 @@
<resources>
<dimen name="launcher_icon_size">55dp</dimen>
<dimen name="icon_long_press_anchor_offset_y">100dp</dimen>
<dimen name="icon_long_press_anchor_offset_y">150dp</dimen>
<dimen name="home_long_press_anchor_offset_y">50dp</dimen>
<dimen name="widget_preview_size">140dp</dimen>
<dimen name="icon_side_margin">10dp</dimen>