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