Add support for app shortcuts

This closes #49
This commit is contained in:
Ensar Sarajčić 2023-09-11 12:19:52 +02:00
parent 60a4c31a88
commit 1698c18d7c
3 changed files with 79 additions and 30 deletions

View File

@ -2,15 +2,23 @@ package com.simplemobiletools.launcher.extensions
import android.app.Activity import android.app.Activity
import android.content.ComponentName import android.content.ComponentName
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import android.content.pm.LauncherApps
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.Rect
import android.net.Uri import android.net.Uri
import android.os.Process
import android.provider.Settings import android.provider.Settings
import android.view.ContextThemeWrapper import android.view.ContextThemeWrapper
import android.view.Gravity import android.view.Gravity
import android.view.Menu
import android.view.View import android.view.View
import android.widget.PopupMenu 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 androidx.core.view.forEach
import com.google.android.material.color.MaterialColors import com.google.android.material.color.MaterialColors
import com.simplemobiletools.commons.extensions.getPopupMenuTheme 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.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.uninstall).isVisible = gridItem.type == ITEM_TYPE_ICON && canAppBeUninstalled(gridItem.packageName)
menu.findItem(R.id.remove).isVisible = !isOnAllAppsFragment 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 -> setOnMenuItemClickListener { item ->
listener.onAnyClick() listener.onAnyClick()
when (item.itemId) { when (item.itemId) {

View File

@ -1,34 +1,37 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item <group android:id="@+id/group_main">
android:id="@+id/app_info" <item
android:icon="@drawable/ic_info_vector" android:id="@+id/app_info"
android:title="@string/app_info" android:icon="@drawable/ic_info_vector"
app:showAsAction="always" /> android:title="@string/app_info"
<item app:showAsAction="always" />
android:id="@+id/hide_icon" <item
android:icon="@drawable/ic_hide_vector" android:id="@+id/hide_icon"
android:title="@string/hide" android:icon="@drawable/ic_hide_vector"
app:showAsAction="always" /> android:title="@string/hide"
<item app:showAsAction="always" />
android:id="@+id/resize" <item
android:icon="@drawable/ic_resize_vector" android:id="@+id/resize"
android:title="@string/resize" android:icon="@drawable/ic_resize_vector"
app:showAsAction="always" /> android:title="@string/resize"
<item app:showAsAction="always" />
android:id="@+id/rename" <item
android:icon="@drawable/ic_rename_vector" android:id="@+id/rename"
android:title="@string/rename" android:icon="@drawable/ic_rename_vector"
app:showAsAction="always" /> android:title="@string/rename"
<item app:showAsAction="always" />
android:id="@+id/remove" <item
android:icon="@drawable/ic_cross_vector" android:id="@+id/remove"
android:title="@string/remove" android:icon="@drawable/ic_cross_vector"
app:showAsAction="always" /> android:title="@string/remove"
<item app:showAsAction="always" />
android:id="@+id/uninstall" <item
android:icon="@drawable/ic_delete_vector" android:id="@+id/uninstall"
android:title="@string/uninstall" android:icon="@drawable/ic_delete_vector"
app:showAsAction="always" /> android:title="@string/uninstall"
app:showAsAction="always" />
</group>
<group android:id="@+id/group_shortcuts" />
</menu> </menu>

View File

@ -11,4 +11,5 @@
<dimen name="page_indicator_margin">6dp</dimen> <dimen name="page_indicator_margin">6dp</dimen>
<dimen name="min_folder_view_height">200dp</dimen> <dimen name="min_folder_view_height">200dp</dimen>
<dimen name="max_folder_view_height">500dp</dimen> <dimen name="max_folder_view_height">500dp</dimen>
<dimen name="menu_icon_size">24dp</dimen>
</resources> </resources>