Duplicate pinned shortcut using Shortcut Manager

1.7k Views Asked by At

I am trying to create a pinned shortcut on the homescreen of my using ShortcutManager. I am able to create the pinned shortcut using the following code:

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.google.com"));
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)){
    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
    .setIntent(i)                
    .setShortLabel("label")                  
    .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
    .build();

   ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}else{
    L.v("Shortcut", "Pinned shortcuts are not supported!");
}

I am facing two issues:

  1. There is no check to handle duplicate shortcuts. Every time I click on the button to create a shortcut, it creates a shortcut every single time and the home screen is getting filled by these shortcuts. Is there any way to check whether the shortcut already exists like:-
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.google.com"));

Intent installer = new Intent();        installer.putExtra("android.intent.extra.shortcut.INTENT", i);          installer.putExtra("android.intent.extra.shortcut.NAME", "Shortcut name");          installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(getApplicationContext() , R.drawable.ic_launcher));
installer.putExtra("duplicate", false);
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer);

The problem with this piece of code is that it is not working in android 8.0 and above but it handles duplication of shortcut correctly using the following code :-

installer.putExtra("duplicate", false);

I want to achieve the same using Shortcut Manager

  1. When a shortcut is created using Shortcut Manager, the icon is duplicated like

duplicate icon

I have looked at the solution provided here but no luck so far:-

Strange app icon duplication in pinned shortcut (Android O)

Any ideas??

2

There are 2 best solutions below

0
On
fun isPinnedShortcutsExits(context: Context, id: String): Boolean {
    return when {
        Build.VERSION.SDK_INT >= 30 -> {
            context.getSystemService(ShortcutManager::class.java)
                .getShortcuts(ShortcutManager.FLAG_MATCH_PINNED)
                .any { it.id == id }
        }
        Build.VERSION.SDK_INT >= 25 -> {
            context.getSystemService(ShortcutManager::class.java)
                .pinnedShortcuts
                .any { it.id == id }
        }
        else -> false
    }
}

or

ShortcutManagerCompat.getShortcuts(this, ShortcutManagerCompat.FLAG_MATCH_PINNED)
    .any { it.id == "xxx" }
0
On

You can get all current shortcuts by calling

List<ShortcutInfo> currPinned = shortcutManager.getPinnedShortcuts();

then add to Map or Set and iterate over them and if its already exist dont add it again

if (currPinned != null) { for (ShortcutInfo shortcut: currPinned) { currPinnedMap.put(shortcut.getId(), shortcut); } } .... //iterate over you "new shortcuts" and check if the present already if (currPinnedMap.containsKey(id)) { continue; } // add really new ones