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 import android.os.UserHandle; 26 27 import com.android.launcher3.R; 28 29 /** 30 * Wrapper class for {@link android.content.pm.ShortcutInfo}, representing deep shortcuts into apps. 31 * 32 * Not to be confused with {@link com.android.launcher3.ShortcutInfo}. 33 */ 34 @TargetApi(Build.VERSION_CODES.N) 35 public class ShortcutInfoCompat { 36 private static final String INTENT_CATEGORY = "com.android.launcher3.DEEP_SHORTCUT"; 37 private static final String EXTRA_BADGEPKG = "badge_package"; 38 public static final String EXTRA_SHORTCUT_ID = "shortcut_id"; 39 private ShortcutInfo mShortcutInfo; 40 41 public ShortcutInfoCompat(ShortcutInfo shortcutInfo) { 42 mShortcutInfo = shortcutInfo; 43 } 44 45 @TargetApi(Build.VERSION_CODES.N) 46 public Intent makeIntent() { 47 return new Intent(Intent.ACTION_MAIN) 48 .addCategory(INTENT_CATEGORY) 49 .setComponent(getActivity()) 50 .setPackage(getPackage()) 51 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) 52 .putExtra(EXTRA_SHORTCUT_ID, getId()); 53 } 54 55 public ShortcutInfo getShortcutInfo() { 56 return mShortcutInfo; 57 } 58 59 public String getPackage() { 60 return mShortcutInfo.getPackage(); 61 } 62 63 public String getBadgePackage(Context context) { 64 String whitelistedPkg = context.getString(R.string.shortcutinfocompat_badgepkg_whitelist); 65 if (whitelistedPkg.equals(getPackage()) 66 && mShortcutInfo.getExtras().containsKey(EXTRA_BADGEPKG)) { 67 return mShortcutInfo.getExtras().getString(EXTRA_BADGEPKG); 68 } 69 return getPackage(); 70 } 71 72 public String getId() { 73 return mShortcutInfo.getId(); 74 } 75 76 public CharSequence getShortLabel() { 77 return mShortcutInfo.getShortLabel(); 78 } 79 80 public CharSequence getLongLabel() { 81 return mShortcutInfo.getLongLabel(); 82 } 83 84 public long getLastChangedTimestamp() { 85 return mShortcutInfo.getLastChangedTimestamp(); 86 } 87 88 public ComponentName getActivity() { 89 return mShortcutInfo.getActivity(); 90 } 91 92 public UserHandle getUserHandle() { 93 return mShortcutInfo.getUserHandle(); 94 } 95 96 public boolean hasKeyFieldsOnly() { 97 return mShortcutInfo.hasKeyFieldsOnly(); 98 } 99 100 public boolean isPinned() { 101 return mShortcutInfo.isPinned(); 102 } 103 104 public boolean isDeclaredInManifest() { 105 return mShortcutInfo.isDeclaredInManifest(); 106 } 107 108 public boolean isEnabled() { 109 return mShortcutInfo.isEnabled(); 110 } 111 112 public boolean isDynamic() { 113 return mShortcutInfo.isDynamic(); 114 } 115 116 public int getRank() { 117 return mShortcutInfo.getRank(); 118 } 119 120 public CharSequence getDisabledMessage() { 121 return mShortcutInfo.getDisabledMessage(); 122 } 123 124 @Override 125 public String toString() { 126 return mShortcutInfo.toString(); 127 } 128 } 129