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 getPackageName()33 public String getPackageName() { 34 return componentName.getPackageName(); 35 } 36 37 /** 38 * Creates a {@link ShortcutRequest} for this key 39 */ buildRequest(Context context)40 public ShortcutRequest buildRequest(Context context) { 41 return new ShortcutRequest(context, user) 42 .forPackage(componentName.getPackageName(), getId()); 43 } 44 fromInfo(ShortcutInfo shortcutInfo)45 public static ShortcutKey fromInfo(ShortcutInfo shortcutInfo) { 46 return new ShortcutKey(shortcutInfo.getPackage(), shortcutInfo.getUserHandle(), 47 shortcutInfo.getId()); 48 } 49 fromIntent(Intent intent, UserHandle user)50 public static ShortcutKey fromIntent(Intent intent, UserHandle user) { 51 String shortcutId = intent.getStringExtra(EXTRA_SHORTCUT_ID); 52 return new ShortcutKey(intent.getPackage(), user, shortcutId); 53 } 54 fromItemInfo(ItemInfo info)55 public static ShortcutKey fromItemInfo(ItemInfo info) { 56 return fromIntent(info.getIntent(), info.user); 57 } 58 makeIntent(ShortcutInfo si)59 public static Intent makeIntent(ShortcutInfo si) { 60 return new Intent(Intent.ACTION_MAIN) 61 .addCategory(INTENT_CATEGORY) 62 .setComponent(si.getActivity()) 63 .setPackage(si.getPackage()) 64 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) 65 .putExtra(EXTRA_SHORTCUT_ID, si.getId()); 66 } 67 } 68