From 012f56cec05882d15206228ce7c73fc5b52d637f Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 20 Sep 2022 15:58:57 +0200 Subject: [PATCH] give more info about long pressed item to the activity --- .../launcher/activities/MainActivity.kt | 20 ++++--- .../launcher/extensions/Activity.kt | 2 +- .../launcher/fragments/AllAppsFragment.kt | 4 +- .../launcher/views/HomeScreenGrid.kt | 55 +++++++++++-------- 4 files changed, 47 insertions(+), 34 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 9517d8b..8ad3158 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -77,7 +77,7 @@ class MainActivity : SimpleActivity(), FlingListener { ensureBackgroundThread { getDefaultAppPackages() config.wasHomeScreenInit = true - home_screen_grid.fetchAppIcons() + home_screen_grid.fetchGridItems() } } } @@ -136,6 +136,7 @@ class MainActivity : SimpleActivity(), FlingListener { if (mIsIconLongPressed && mOpenPopupMenu != null) { mOpenPopupMenu?.dismiss() mOpenPopupMenu = null + home_screen_grid.itemDraggingStarted() } if (mTouchDownY != -1 && !mIgnoreMoveEvents) { @@ -150,6 +151,7 @@ class MainActivity : SimpleActivity(), FlingListener { mTouchDownY = -1 mIgnoreMoveEvents = false mIsIconLongPressed = false + home_screen_grid.itemDraggingStopped() if (!mIgnoreUpEvent) { if (all_apps_fragment.y < mScreenHeight * 0.7) { showFragment(all_apps_fragment) @@ -178,7 +180,7 @@ class MainActivity : SimpleActivity(), FlingListener { } if (hasDeletedAnything) { - home_screen_grid.fetchAppIcons() + home_screen_grid.fetchGridItems() } mCachedLaunchers = launchers @@ -229,10 +231,10 @@ class MainActivity : SimpleActivity(), FlingListener { mIgnoreMoveEvents = true main_holder.performHapticFeedback() - val clickedPackageName = home_screen_grid.isClickingIcon(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) - if (clickedPackageName.isNotEmpty()) { + val clickedGridItem = home_screen_grid.isClickingGridItem(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) + if (clickedGridItem != null) { mIsIconLongPressed = true - showHomeIconMenu(x, y, clickedPackageName) + showHomeIconMenu(x, y, clickedGridItem.packageName) return } @@ -241,9 +243,9 @@ class MainActivity : SimpleActivity(), FlingListener { fun homeScreenClicked(x: Float, y: Float) { if (x >= home_screen_grid.left && x <= home_screen_grid.right && y >= home_screen_grid.top && y <= home_screen_grid.bottom) { - val clickedPackageName = home_screen_grid.isClickingIcon(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) - if (clickedPackageName.isNotEmpty()) { - launchApp(clickedPackageName) + val clickedGridItem = home_screen_grid.isClickingGridItem(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) + if (clickedGridItem != null) { + launchApp(clickedGridItem.packageName) } } } @@ -251,7 +253,7 @@ class MainActivity : SimpleActivity(), FlingListener { private fun showHomeIconMenu(x: Float, y: Float, clickedPackageName: String) { home_screen_popup_menu_anchor.x = x home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_offset_y) - mOpenPopupMenu = handleAppIconPopupMenu(home_screen_popup_menu_anchor, clickedPackageName) + mOpenPopupMenu = handleGridItemPopupMenu(home_screen_popup_menu_anchor, clickedPackageName) } private fun showMainLongPressMenu(x: Float, y: Float) { diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt index dfce44d..479cbae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt @@ -36,7 +36,7 @@ fun Activity.uninstallApp(packageName: String) { } } -fun Activity.handleAppIconPopupMenu(anchorView: View, appPackageName: String): PopupMenu { +fun Activity.handleGridItemPopupMenu(anchorView: View, appPackageName: String): PopupMenu { val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply { inflate(R.menu.menu_app_icon) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt index c699c73..102240f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -13,7 +13,7 @@ import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.activities.MainActivity import com.simplemobiletools.launcher.adapters.LaunchersAdapter import com.simplemobiletools.launcher.extensions.getColumnCount -import com.simplemobiletools.launcher.extensions.handleAppIconPopupMenu +import com.simplemobiletools.launcher.extensions.handleGridItemPopupMenu import com.simplemobiletools.launcher.extensions.launchApp import com.simplemobiletools.launcher.interfaces.AllAppsListener import com.simplemobiletools.launcher.models.AppLauncher @@ -124,6 +124,6 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment override fun onIconLongPressed(x: Float, y: Float, packageName: String) { all_apps_popup_menu_anchor.x = x all_apps_popup_menu_anchor.y = y - activity?.handleAppIconPopupMenu(all_apps_popup_menu_anchor, packageName) + activity?.handleGridItemPopupMenu(all_apps_popup_menu_anchor, packageName) } } 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 6947390..f7222c3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -26,6 +26,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie private var iconMargin = context.resources.getDimension(R.dimen.icon_side_margin).toInt() private var labelSideMargin = context.resources.getDimension(R.dimen.small_margin).toInt() private var textPaint: TextPaint + private var isDraggingItem = false // let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps private var rowXCoords = ArrayList(COLUMN_COUNT) @@ -34,8 +35,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie private var rowHeight = 0 private var iconSize = 0 - private var appIcons = ArrayList() - private var appIconDrawables = HashMap() + private var gridItems = ArrayList() + private var gridItemDrawables = HashMap() init { textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { @@ -44,17 +45,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie setShadowLayer(.5f, 0f, 0f, Color.BLACK) } - fetchAppIcons() + fetchGridItems() } - fun fetchAppIcons() { + fun fetchGridItems() { ensureBackgroundThread { - appIconDrawables.clear() - appIcons = context.homeScreenGridItemsDB.getAllItems() as ArrayList - appIcons.forEach { item -> + gridItemDrawables.clear() + gridItems = context.homeScreenGridItemsDB.getAllItems() as ArrayList + gridItems.forEach { item -> val drawable = context.getDrawableForPackageName(item.packageName) if (drawable != null) { - appIconDrawables[item.packageName] = drawable + gridItemDrawables[item.packageName] = drawable } } @@ -62,6 +63,16 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie } } + fun itemDraggingStarted() { + isDraggingItem = true + invalidate() + } + + fun itemDraggingStopped() { + isDraggingItem = false + invalidate() + } + @SuppressLint("DrawAllocation") override fun onDraw(canvas: Canvas) { super.onDraw(canvas) @@ -78,29 +89,29 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie } } - appIcons.forEach { icon -> - val drawable = appIconDrawables[icon.packageName] + gridItems.forEach { item -> + val drawable = gridItemDrawables[item.packageName] if (drawable != null) { - val drawableX = rowXCoords[icon.left] + iconMargin + val drawableX = rowXCoords[item.left] + iconMargin // icons at the bottom are drawn at the bottom of the grid and they have no label - if (icon.top == ROW_COUNT - 1) { - val drawableY = rowYCoords[icon.top] + rowHeight - iconSize - iconMargin * 2 + if (item.top == ROW_COUNT - 1) { + val drawableY = rowYCoords[item.top] + rowHeight - iconSize - iconMargin * 2 drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) } else { - val drawableY = rowYCoords[icon.top] + iconSize / 2 + val drawableY = rowYCoords[item.top] + iconSize / 2 drawable.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) - val textY = rowYCoords[icon.top] + iconSize * 1.5f + labelSideMargin + val textY = rowYCoords[item.top] + iconSize * 1.5f + labelSideMargin val staticLayout = StaticLayout.Builder - .obtain(icon.title, 0, icon.title.length, textPaint, rowWidth - 2 * labelSideMargin) + .obtain(item.title, 0, item.title.length, textPaint, rowWidth - 2 * labelSideMargin) .setMaxLines(2) .setEllipsize(TextUtils.TruncateAt.END) .setAlignment(Layout.Alignment.ALIGN_CENTER) .build() canvas.save() - canvas.translate(rowXCoords[icon.left].toFloat() + labelSideMargin, textY) + canvas.translate(rowXCoords[item.left].toFloat() + labelSideMargin, textY) staticLayout.draw(canvas) canvas.restore() } @@ -110,13 +121,13 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie } } - fun isClickingIcon(x: Float, y: Float): String { - for (appIcon in appIcons) { - if (x >= appIcon.left * rowWidth && x <= appIcon.right * rowWidth && y >= appIcon.top * rowHeight && y <= appIcon.bottom * rowHeight) { - return appIcon.packageName + fun isClickingGridItem(x: Float, y: Float): HomeScreenGridItem? { + for (gridItem in gridItems) { + if (x >= gridItem.left * rowWidth && x <= gridItem.right * rowWidth && y >= gridItem.top * rowHeight && y <= gridItem.bottom * rowHeight) { + return gridItem } } - return "" + return null } }