1 /* 2 * Copyright (C) 2019 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 package com.android.launcher3.util; 17 18 import static com.android.launcher3.BuildConfig.WIDGETS_ENABLED; 19 20 import com.android.launcher3.LauncherSettings; 21 import com.android.launcher3.Utilities; 22 import com.android.launcher3.model.data.ItemInfo; 23 import com.android.launcher3.model.data.WorkspaceItemInfo; 24 import com.android.launcher3.shortcuts.ShortcutKey; 25 26 public class ShortcutUtil { 27 /** 28 * Returns true when we should show shortcut menu for the item. 29 */ supportsShortcuts(ItemInfo info)30 public static boolean supportsShortcuts(ItemInfo info) { 31 return isActive(info) && (isApp(info) || isPinnedShortcut(info)); 32 } 33 34 /** 35 * Returns true when we should show depp shortcuts in shortcut menu for the item. 36 */ supportsDeepShortcuts(ItemInfo info)37 public static boolean supportsDeepShortcuts(ItemInfo info) { 38 return isActive(info) && isApp(info) && !!WIDGETS_ENABLED; 39 } 40 41 /** 42 * Returns the shortcut id if the item is a pinned shortcut. 43 */ getShortcutIdIfPinnedShortcut(ItemInfo info)44 public static String getShortcutIdIfPinnedShortcut(ItemInfo info) { 45 return isActive(info) && isPinnedShortcut(info) 46 ? ShortcutKey.fromItemInfo(info).getId() : null; 47 } 48 49 /** 50 * Returns the person keys associated with the item. (Has no function right now.) 51 */ getPersonKeysIfPinnedShortcut(ItemInfo info)52 public static String[] getPersonKeysIfPinnedShortcut(ItemInfo info) { 53 return isActive(info) && isPinnedShortcut(info) 54 ? ((WorkspaceItemInfo) info).getPersonKeys() : Utilities.EMPTY_STRING_ARRAY; 55 } 56 57 /** 58 * Returns true if the item is a deep shortcut. 59 */ isDeepShortcut(ItemInfo info)60 public static boolean isDeepShortcut(ItemInfo info) { 61 return info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT 62 && info instanceof WorkspaceItemInfo; 63 } 64 isActive(ItemInfo info)65 private static boolean isActive(ItemInfo info) { 66 boolean isLoading = info instanceof WorkspaceItemInfo 67 && ((WorkspaceItemInfo) info).hasPromiseIconUi(); 68 return !isLoading && !info.isDisabled(); 69 } 70 isApp(ItemInfo info)71 private static boolean isApp(ItemInfo info) { 72 return info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; 73 } 74 isPinnedShortcut(ItemInfo info)75 private static boolean isPinnedShortcut(ItemInfo info) { 76 return info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT 77 && info.container != ItemInfo.NO_ID 78 && info instanceof WorkspaceItemInfo; 79 } 80 } 81