Home » Android » android – How to remove pinned shortcuts?

android – How to remove pinned shortcuts?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Background

Starting from Android O, it’s possible to create pinned shortcuts, which (on supported launchers) show a dialog to confirm the creation of them:

            ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(context, uniqueShortcutId)
                    .setShortLabel(label)
                    .setIntent(shortcutIntent)
                    .setLongLabel(label)
                    .setIcon(IconCompat.createWithBitmap(bitmap))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(this, pinShortcutInfo , null);

Docs:

https://developer.android.com/reference/android/content/pm/ShortcutManager.html
https://developer.android.com/guide/topics/ui/shortcuts.html

The problem

Sometimes, the pinned shortcut is not relevant anymore. For example, it points to something that doesn’t exist anymore.

In this case, I want to be able to remove it.

What I’ve tried

I thought this is possible by the next code, but it doesn’t, because it is probably about dynamic shortcuts, which is something else :

ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
final ArrayList<String> shortcutIdsToRemove = new ArrayList<>();
for (ShortcutInfo pinnedShortcut : pinnedShortcuts) {
    final Intent pinnedShortcutIntent = pinnedShortcut.getIntent();
    shortcutIdsToRemove.add(pinnedShortcut.getId());
}
shortcutManager.removeDynamicShortcuts(shortcutIdsToRemove);
// this also doesn't work : shortcutManager.disableShortcuts(shortcutIdsToRemove);

The question

How can I remove pinned shortcuts? Is it even possible?


Update: seems it isn’t possible, as Google mentioned here.

So far, this API seems very restricting to me, as it has so many disadvantages:

  1. The created icon image cannot be created from a resource ID.
  2. On the launcher, the created icon has a tiny icon of the app that created it.
  3. The icon cannot be removed via the API (only disabled), so if you created one that points to another app, and this app was removed, you won’t be able to remove it.
  4. The creation cannot be in the background, as it requires a confirmation dialog. It also cannot be created in a batch. Only one after another.
  5. All pinned shortcuts your app has created will be removed if your app was removed.

So, my question now is:

  1. Is there any way to create and remove a shortcut using the previous API, using an adb command, with root if needed ?

  2. Is it possible to remove specific pinned shortcuts using adb command , with root if needed?

How to&Answers:

disableShortcuts() is the best you can do. It will show the user a custom error message when they try to select the disabled shortcut.

From the official documentation:

Shortcut limitations
Although you can publish up to five shortcuts (static and dynamic shortcuts combined) at a time for your app, most launchers can only display four.

However, there is no limit to the number of pinned shortcuts to your app that users can create. Even though your app cannot remove pinned shortcuts, it can still disable them.

Another section:

Because your app and its users can pin shortcuts to the device’s launcher, it’s possible that these pinned shortcuts could direct users to actions within your app that are out of date or no longer exist. To manage this situation, you can disable the shortcuts that you don’t want users to select by calling disableShortcuts(), which removes the specified shortcuts from the static and dynamic shortcuts list and disables any pinned copies of these shortcuts. You can also use an overloaded version of this method, which accepts a CharSequence as a custom error message. That error message then appears when users attempt to launch any disabled shortcut.

Note: If you remove some of your app’s static shortcuts when you update your app, the system disables these shortcuts automatically.