1 package com.android.launcher3.shortcuts;
2 
3 import android.content.ComponentName;
4 import android.content.Intent;
5 import android.content.pm.ShortcutInfo;
6 import android.os.UserHandle;
7 
8 import com.android.launcher3.ItemInfo;
9 import com.android.launcher3.util.ComponentKey;
10 
11 /**
12  * A key that uniquely identifies a shortcut using its package, id, and user handle.
13  */
14 public class ShortcutKey extends ComponentKey {
15 
16     public static final String EXTRA_SHORTCUT_ID = "shortcut_id";
17     private static final String INTENT_CATEGORY = "com.android.launcher3.DEEP_SHORTCUT";
18 
ShortcutKey(String packageName, UserHandle user, String id)19     public ShortcutKey(String packageName, UserHandle user, String id) {
20         // Use the id as the class name.
21         super(new ComponentName(packageName, id), user);
22     }
23 
ShortcutKey(ComponentName componentName, UserHandle user)24     public ShortcutKey(ComponentName componentName, UserHandle user) {
25         super(componentName, user);
26     }
27 
getId()28     public String getId() {
29         return componentName.getClassName();
30     }
31 
fromInfo(ShortcutInfo shortcutInfo)32     public static ShortcutKey fromInfo(ShortcutInfo shortcutInfo) {
33         return new ShortcutKey(shortcutInfo.getPackage(), shortcutInfo.getUserHandle(),
34                 shortcutInfo.getId());
35     }
36 
fromIntent(Intent intent, UserHandle user)37     public static ShortcutKey fromIntent(Intent intent, UserHandle user) {
38         String shortcutId = intent.getStringExtra(EXTRA_SHORTCUT_ID);
39         return new ShortcutKey(intent.getPackage(), user, shortcutId);
40     }
41 
fromItemInfo(ItemInfo info)42     public static ShortcutKey fromItemInfo(ItemInfo info) {
43         return fromIntent(info.getIntent(), info.user);
44     }
45 
makeIntent(ShortcutInfo si)46     public static Intent makeIntent(ShortcutInfo si) {
47         return new Intent(Intent.ACTION_MAIN)
48                 .addCategory(INTENT_CATEGORY)
49                 .setComponent(si.getActivity())
50                 .setPackage(si.getPackage())
51                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
52                 .putExtra(EXTRA_SHORTCUT_ID, si.getId());
53     }
54 }
55