From 1698c18d7c13ca2b1328b97d1376dbc4e7e80199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 11 Sep 2023 12:19:52 +0200 Subject: [PATCH] Add support for app shortcuts This closes #49 --- .../launcher/extensions/Activity.kt | 45 +++++++++++++ app/src/main/res/menu/menu_app_icon.xml | 63 ++++++++++--------- app/src/main/res/values/dimens.xml | 1 + 3 files changed, 79 insertions(+), 30 deletions(-) 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 88abffd..e1e0d75 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt @@ -2,15 +2,23 @@ package com.simplemobiletools.launcher.extensions import android.app.Activity import android.content.ComponentName +import android.content.Context import android.content.Intent import android.content.pm.ApplicationInfo +import android.content.pm.LauncherApps import android.content.res.ColorStateList +import android.graphics.Rect import android.net.Uri +import android.os.Process import android.provider.Settings import android.view.ContextThemeWrapper import android.view.Gravity +import android.view.Menu import android.view.View import android.widget.PopupMenu +import androidx.core.graphics.drawable.toBitmap +import androidx.core.graphics.drawable.toDrawable +import androidx.core.view.MenuCompat import androidx.core.view.forEach import com.google.android.material.color.MaterialColors import com.simplemobiletools.commons.extensions.getPopupMenuTheme @@ -91,6 +99,43 @@ fun Activity.handleGridItemPopupMenu(anchorView: View, gridItem: HomeScreenGridI menu.findItem(R.id.app_info).isVisible = gridItem.type == ITEM_TYPE_ICON menu.findItem(R.id.uninstall).isVisible = gridItem.type == ITEM_TYPE_ICON && canAppBeUninstalled(gridItem.packageName) menu.findItem(R.id.remove).isVisible = !isOnAllAppsFragment + + val launcherApps = applicationContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + val shortcuts = if (launcherApps.hasShortcutHostPermission()) { + try { + val query = LauncherApps.ShortcutQuery().setQueryFlags( + LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC or LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST or LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED + ).setPackage(gridItem.packageName) + launcherApps.getShortcuts(query, Process.myUserHandle()) + } catch (e: Exception) { + null + } + } else { + null + } + + val hasShortcuts = !shortcuts.isNullOrEmpty() + MenuCompat.setGroupDividerEnabled(menu, hasShortcuts) + menu.setGroupVisible(R.id.group_shortcuts, hasShortcuts) + if (hasShortcuts) { + val iconSize = resources.getDimensionPixelSize(R.dimen.menu_icon_size) + shortcuts?.forEach { + menu.add(R.id.group_shortcuts, Menu.NONE, Menu.NONE, it.shortLabel) + .setIcon( + launcherApps.getShortcutIconDrawable(it, resources.displayMetrics.densityDpi).toBitmap(width = iconSize, height = iconSize) + .toDrawable(resources) + ) + .setOnMenuItemClickListener { _ -> + listener.onAnyClick() + val id = it.id + val packageName = it.`package` + val userHandle = Process.myUserHandle() + launcherApps.startShortcut(packageName, id, Rect(), null, userHandle) + true + } + } + } + setOnMenuItemClickListener { item -> listener.onAnyClick() when (item.itemId) { diff --git a/app/src/main/res/menu/menu_app_icon.xml b/app/src/main/res/menu/menu_app_icon.xml index aeccbb3..119a47c 100644 --- a/app/src/main/res/menu/menu_app_icon.xml +++ b/app/src/main/res/menu/menu_app_icon.xml @@ -1,34 +1,37 @@ - - - - - - + + + + + + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index d8beb5c..1b5275d 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -11,4 +11,5 @@ 6dp 200dp 500dp + 24dp