Merge pull request #122 from esensar/feature/49-app-shortcuts

Add support for app shortcuts
This commit is contained in:
Tibor Kaputa 2023-09-11 14:48:53 +02:00 committed by GitHub
commit 8202e5dde7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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.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.longLabel)
.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) {

View file

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

View file

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