mirror of
				https://github.com/SimpleMobileTools/Simple-Launcher.git
				synced 2025-06-05 21:59:15 +02:00 
			
		
		
		
	handle long pressing icons at the desktop too
This commit is contained in:
		| @@ -22,6 +22,7 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread | ||||
| import com.simplemobiletools.launcher.BuildConfig | ||||
| import com.simplemobiletools.launcher.R | ||||
| import com.simplemobiletools.launcher.extensions.config | ||||
| import com.simplemobiletools.launcher.extensions.handleAppIconPopupMenu | ||||
| import com.simplemobiletools.launcher.extensions.homeScreenGridItemsDB | ||||
| import com.simplemobiletools.launcher.extensions.launchApp | ||||
| import com.simplemobiletools.launcher.fragments.AllAppsFragment | ||||
| @@ -159,7 +160,31 @@ class MainActivity : SimpleActivity(), FlingListener { | ||||
|         } | ||||
|  | ||||
|         main_holder.performHapticFeedback() | ||||
|         val clickedPackageName = home_screen_grid.gridClicked(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) | ||||
|         if (clickedPackageName.isNotEmpty()) { | ||||
|             showHomeIconMenu(x, y, clickedPackageName) | ||||
|             return | ||||
|         } | ||||
|  | ||||
|         showMainLongPressMenu(x, y) | ||||
|     } | ||||
|  | ||||
|     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.gridClicked(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) | ||||
|             if (clickedPackageName.isNotEmpty()) { | ||||
|                 launchApp(clickedPackageName) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     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) | ||||
|         handleAppIconPopupMenu(home_screen_popup_menu_anchor, clickedPackageName) | ||||
|     } | ||||
|  | ||||
|     private fun showMainLongPressMenu(x: Float, y: Float) { | ||||
|         home_screen_popup_menu_anchor.x = x | ||||
|         home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_offset_y) | ||||
|         val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) | ||||
| @@ -177,14 +202,6 @@ 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) { | ||||
|             home_screen_grid.gridClicked(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) { packageName -> | ||||
|                 launchApp(packageName) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun showWidgetsFragment() { | ||||
|         showFragment(widgets_fragment) | ||||
|     } | ||||
|   | ||||
| @@ -1,7 +1,16 @@ | ||||
| package com.simplemobiletools.launcher.extensions | ||||
|  | ||||
| import android.app.Activity | ||||
| import android.content.Intent | ||||
| import android.net.Uri | ||||
| import android.provider.Settings | ||||
| import android.view.ContextThemeWrapper | ||||
| import android.view.Gravity | ||||
| import android.view.View | ||||
| import android.widget.PopupMenu | ||||
| import com.simplemobiletools.commons.extensions.getPopupMenuTheme | ||||
| import com.simplemobiletools.commons.extensions.showErrorToast | ||||
| import com.simplemobiletools.launcher.R | ||||
|  | ||||
| fun Activity.launchApp(packageName: String) { | ||||
|     val launchIntent = packageManager.getLaunchIntentForPackage(packageName) | ||||
| @@ -11,3 +20,26 @@ fun Activity.launchApp(packageName: String) { | ||||
|         showErrorToast(e) | ||||
|     } | ||||
| } | ||||
|  | ||||
| fun Activity.launchAppInfo(packageName: String) { | ||||
|     Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||||
|         data = Uri.fromParts("package", packageName, null) | ||||
|         startActivity(this) | ||||
|     } | ||||
| } | ||||
|  | ||||
| fun Activity.handleAppIconPopupMenu(anchorView: View, appPackageName: String) { | ||||
|     val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) | ||||
|     PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply { | ||||
|         inflate(R.menu.menu_app_icon) | ||||
|         setOnMenuItemClickListener { item -> | ||||
|             when (item.itemId) { | ||||
|                 R.id.app_info -> { | ||||
|                     launchAppInfo(appPackageName) | ||||
|                 } | ||||
|             } | ||||
|             true | ||||
|         } | ||||
|         show() | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -6,11 +6,10 @@ import android.content.Intent | ||||
| import android.content.pm.PackageManager | ||||
| import android.graphics.Bitmap | ||||
| import android.graphics.Color | ||||
| import android.net.Uri | ||||
| import android.provider.Settings | ||||
| import android.util.AttributeSet | ||||
| import android.view.* | ||||
| import android.widget.PopupMenu | ||||
| import android.view.MotionEvent | ||||
| import android.view.Surface | ||||
| import android.view.WindowManager | ||||
| import androidx.core.graphics.drawable.toBitmap | ||||
| import com.simplemobiletools.commons.extensions.* | ||||
| import com.simplemobiletools.commons.helpers.ensureBackgroundThread | ||||
| @@ -19,10 +18,7 @@ import com.simplemobiletools.commons.views.MyGridLayoutManager | ||||
| 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.getDrawableForPackageName | ||||
| import com.simplemobiletools.launcher.extensions.launchApp | ||||
| import com.simplemobiletools.launcher.extensions.launchersDB | ||||
| import com.simplemobiletools.launcher.extensions.* | ||||
| import com.simplemobiletools.launcher.interfaces.AllAppsListener | ||||
| import com.simplemobiletools.launcher.models.AppLauncher | ||||
| import kotlinx.android.synthetic.main.all_apps_fragment.view.* | ||||
| @@ -163,26 +159,7 @@ 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 | ||||
|         val contextTheme = ContextThemeWrapper(activity, activity!!.getPopupMenuTheme()) | ||||
|         PopupMenu(contextTheme, all_apps_popup_menu_anchor, Gravity.TOP or Gravity.END).apply { | ||||
|             inflate(R.menu.menu_app_icon) | ||||
|             setOnMenuItemClickListener { item -> | ||||
|                 when (item.itemId) { | ||||
|                     R.id.app_info -> { | ||||
|                         launchAppInfo(packageName) | ||||
|                     } | ||||
|                 } | ||||
|                 true | ||||
|             } | ||||
|             show() | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun launchAppInfo(packageName: String) { | ||||
|         Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||||
|             data = Uri.fromParts("package", packageName, null) | ||||
|             activity?.startActivity(this) | ||||
|         } | ||||
|         activity?.handleAppIconPopupMenu(all_apps_popup_menu_anchor, packageName) | ||||
|     } | ||||
|  | ||||
|     // taken from https://gist.github.com/maxjvh/a6ab15cbba9c82a5065d | ||||
|   | ||||
| @@ -83,12 +83,13 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun gridClicked(x: Float, y: Float, callback: (packageName: String) -> Unit) { | ||||
|         for (gridItem in appIcons) { | ||||
|             if (x >= gridItem.left * rowWidth && x <= gridItem.right * rowWidth && y >= gridItem.top * rowHeight && y <= gridItem.bottom * rowHeight) { | ||||
|                 callback(gridItem.packageName) | ||||
|                 break | ||||
|     fun gridClicked(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 | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return "" | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user