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.util; 18 19 import android.content.ComponentName; 20 import android.os.UserHandle; 21 22 import com.android.launcher3.FolderInfo; 23 import com.android.launcher3.ItemInfo; 24 import com.android.launcher3.LauncherAppWidgetInfo; 25 import com.android.launcher3.LauncherSettings.Favorites; 26 import com.android.launcher3.ShortcutInfo; 27 import com.android.launcher3.shortcuts.ShortcutKey; 28 29 import java.util.HashSet; 30 31 /** 32 * A utility class to check for {@link ItemInfo} 33 */ 34 public abstract class ItemInfoMatcher { 35 matches(ItemInfo info, ComponentName cn)36 public abstract boolean matches(ItemInfo info, ComponentName cn); 37 38 /** 39 * Filters {@param infos} to those satisfying the {@link #matches(ItemInfo, ComponentName)}. 40 */ filterItemInfos(Iterable<ItemInfo> infos)41 public final HashSet<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos) { 42 HashSet<ItemInfo> filtered = new HashSet<>(); 43 for (ItemInfo i : infos) { 44 if (i instanceof ShortcutInfo) { 45 ShortcutInfo info = (ShortcutInfo) i; 46 ComponentName cn = info.getTargetComponent(); 47 if (cn != null && matches(info, cn)) { 48 filtered.add(info); 49 } 50 } else if (i instanceof FolderInfo) { 51 FolderInfo info = (FolderInfo) i; 52 for (ShortcutInfo s : info.contents) { 53 ComponentName cn = s.getTargetComponent(); 54 if (cn != null && matches(s, cn)) { 55 filtered.add(s); 56 } 57 } 58 } else if (i instanceof LauncherAppWidgetInfo) { 59 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i; 60 ComponentName cn = info.providerName; 61 if (cn != null && matches(info, cn)) { 62 filtered.add(info); 63 } 64 } 65 } 66 return filtered; 67 } 68 ofUser(final UserHandle user)69 public static ItemInfoMatcher ofUser(final UserHandle user) { 70 return new ItemInfoMatcher() { 71 @Override 72 public boolean matches(ItemInfo info, ComponentName cn) { 73 return info.user.equals(user); 74 } 75 }; 76 } 77 78 public static ItemInfoMatcher ofComponents( 79 final HashSet<ComponentName> components, final UserHandle user) { 80 return new ItemInfoMatcher() { 81 @Override 82 public boolean matches(ItemInfo info, ComponentName cn) { 83 return components.contains(cn) && info.user.equals(user); 84 } 85 }; 86 } 87 88 public static ItemInfoMatcher ofPackages( 89 final HashSet<String> packageNames, final UserHandle user) { 90 return new ItemInfoMatcher() { 91 @Override 92 public boolean matches(ItemInfo info, ComponentName cn) { 93 return packageNames.contains(cn.getPackageName()) && info.user.equals(user); 94 } 95 }; 96 } 97 98 public static ItemInfoMatcher ofShortcutKeys(final HashSet<ShortcutKey> keys) { 99 return new ItemInfoMatcher() { 100 @Override 101 public boolean matches(ItemInfo info, ComponentName cn) { 102 return info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT && 103 keys.contains(ShortcutKey.fromItemInfo(info)); 104 } 105 }; 106 } 107 } 108