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.LauncherSettings.Favorites; 23 import com.android.launcher3.model.data.ItemInfo; 24 import com.android.launcher3.shortcuts.ShortcutKey; 25 26 import java.util.HashSet; 27 import java.util.Set; 28 29 /** 30 * A utility class to check for {@link ItemInfo} 31 */ 32 public interface ItemInfoMatcher { 33 matches(ItemInfo info, ComponentName cn)34 boolean matches(ItemInfo info, ComponentName cn); 35 36 /** 37 * Returns true if the itemInfo matches this check 38 */ matchesInfo(ItemInfo info)39 default boolean matchesInfo(ItemInfo info) { 40 if (info != null) { 41 ComponentName cn = info.getTargetComponent(); 42 return cn != null && matches(info, cn); 43 } else { 44 return false; 45 } 46 } 47 48 /** 49 * Returns a new matcher with returns true if either this or {@param matcher} returns true. 50 */ or(ItemInfoMatcher matcher)51 default ItemInfoMatcher or(ItemInfoMatcher matcher) { 52 return (info, cn) -> matches(info, cn) || matcher.matches(info, cn); 53 } 54 55 /** 56 * Returns a new matcher with returns true if both this and {@param matcher} returns true. 57 */ and(ItemInfoMatcher matcher)58 default ItemInfoMatcher and(ItemInfoMatcher matcher) { 59 return (info, cn) -> matches(info, cn) && matcher.matches(info, cn); 60 } 61 62 /** 63 * Returns a new matcher with returns the opposite value of this. 64 */ negate()65 default ItemInfoMatcher negate() { 66 return (info, cn) -> !matches(info, cn); 67 } 68 ofUser(UserHandle user)69 static ItemInfoMatcher ofUser(UserHandle user) { 70 return (info, cn) -> info.user.equals(user); 71 } 72 ofComponents(HashSet<ComponentName> components, UserHandle user)73 static ItemInfoMatcher ofComponents(HashSet<ComponentName> components, UserHandle user) { 74 return (info, cn) -> components.contains(cn) && info.user.equals(user); 75 } 76 ofPackages(Set<String> packageNames, UserHandle user)77 static ItemInfoMatcher ofPackages(Set<String> packageNames, UserHandle user) { 78 return (info, cn) -> packageNames.contains(cn.getPackageName()) && info.user.equals(user); 79 } 80 ofShortcutKeys(Set<ShortcutKey> keys)81 static ItemInfoMatcher ofShortcutKeys(Set<ShortcutKey> keys) { 82 return (info, cn) -> info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT && 83 keys.contains(ShortcutKey.fromItemInfo(info)); 84 } 85 86 /** 87 * Returns a matcher for items with provided ids 88 */ ofItemIds(IntSet ids)89 static ItemInfoMatcher ofItemIds(IntSet ids) { 90 return (info, cn) -> ids.contains(info.id); 91 } 92 } 93