1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.shortcuts; 18 19 import android.annotation.TargetApi; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ShortcutInfo; 24 import android.os.Build; 25 26 import com.android.launcher3.ItemInfo; 27 import com.android.launcher3.compat.DeferredLauncherActivityInfo; 28 import com.android.launcher3.compat.LauncherActivityInfoCompat; 29 import com.android.launcher3.compat.UserHandleCompat; 30 import com.android.launcher3.compat.UserManagerCompat; 31 32 /** 33 * Wrapper class for {@link android.content.pm.ShortcutInfo}, representing deep shortcuts into apps. 34 * 35 * Not to be confused with {@link com.android.launcher3.ShortcutInfo}. 36 */ 37 @TargetApi(Build.VERSION_CODES.N) 38 public class ShortcutInfoCompat { 39 private static final String INTENT_CATEGORY = "com.android.launcher3.DEEP_SHORTCUT"; 40 public static final String EXTRA_SHORTCUT_ID = "shortcut_id"; 41 42 private ShortcutInfo mShortcutInfo; 43 ShortcutInfoCompat(ShortcutInfo shortcutInfo)44 public ShortcutInfoCompat(ShortcutInfo shortcutInfo) { 45 mShortcutInfo = shortcutInfo; 46 } 47 48 @TargetApi(Build.VERSION_CODES.N) makeIntent(Context context)49 public Intent makeIntent(Context context) { 50 long serialNumber = UserManagerCompat.getInstance(context) 51 .getSerialNumberForUser(getUserHandle()); 52 return new Intent(Intent.ACTION_MAIN) 53 .addCategory(INTENT_CATEGORY) 54 .setComponent(getActivity()) 55 .setPackage(getPackage()) 56 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) 57 .putExtra(ItemInfo.EXTRA_PROFILE, serialNumber) 58 .putExtra(EXTRA_SHORTCUT_ID, getId()); 59 } 60 getShortcutInfo()61 public ShortcutInfo getShortcutInfo() { 62 return mShortcutInfo; 63 } 64 getPackage()65 public String getPackage() { 66 return mShortcutInfo.getPackage(); 67 } 68 getId()69 public String getId() { 70 return mShortcutInfo.getId(); 71 } 72 getShortLabel()73 public CharSequence getShortLabel() { 74 return mShortcutInfo.getShortLabel(); 75 } 76 getLongLabel()77 public CharSequence getLongLabel() { 78 return mShortcutInfo.getLongLabel(); 79 } 80 getLastChangedTimestamp()81 public long getLastChangedTimestamp() { 82 return mShortcutInfo.getLastChangedTimestamp(); 83 } 84 getActivity()85 public ComponentName getActivity() { 86 return mShortcutInfo.getActivity(); 87 } 88 getUserHandle()89 public UserHandleCompat getUserHandle() { 90 return UserHandleCompat.fromUser(mShortcutInfo.getUserHandle()); 91 } 92 hasKeyFieldsOnly()93 public boolean hasKeyFieldsOnly() { 94 return mShortcutInfo.hasKeyFieldsOnly(); 95 } 96 isPinned()97 public boolean isPinned() { 98 return mShortcutInfo.isPinned(); 99 } 100 isDeclaredInManifest()101 public boolean isDeclaredInManifest() { 102 return mShortcutInfo.isDeclaredInManifest(); 103 } 104 isEnabled()105 public boolean isEnabled() { 106 return mShortcutInfo.isEnabled(); 107 } 108 isDynamic()109 public boolean isDynamic() { 110 return mShortcutInfo.isDynamic(); 111 } 112 getRank()113 public int getRank() { 114 return mShortcutInfo.getRank(); 115 } 116 getDisabledMessage()117 public CharSequence getDisabledMessage() { 118 return mShortcutInfo.getDisabledMessage(); 119 } 120 121 @Override toString()122 public String toString() { 123 return mShortcutInfo.toString(); 124 } 125 getActivityInfo(Context context)126 public LauncherActivityInfoCompat getActivityInfo(Context context) { 127 return new DeferredLauncherActivityInfo(getActivity(), getUserHandle(), context); 128 } 129 } 130