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.FolderInfo;
24 import com.android.launcher3.model.data.ItemInfo;
25 import com.android.launcher3.shortcuts.ShortcutKey;
26 
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 /**
32  * A utility class to check for {@link ItemInfo}
33  */
34 public interface ItemInfoMatcher {
35 
36     /**
37      * Empty component used for match testing
38      */
39     ComponentName EMPTY_COMPONENT = new ComponentName("", "");
40 
matches(ItemInfo info, ComponentName cn)41     boolean matches(ItemInfo info, ComponentName cn);
42 
43     /**
44      * Returns true if the itemInfo matches this check
45      */
matchesInfo(ItemInfo info)46     default boolean matchesInfo(ItemInfo info) {
47         if (info != null) {
48             ComponentName cn = info.getTargetComponent();
49             return matches(info, cn != null ? cn : EMPTY_COMPONENT);
50         } else {
51             return false;
52         }
53     }
54 
55     /**
56      * Returns a new matcher with returns true if either this or {@param matcher} returns true.
57      */
or(ItemInfoMatcher matcher)58     default ItemInfoMatcher or(ItemInfoMatcher matcher) {
59         return (info, cn) -> matches(info, cn) || matcher.matches(info, cn);
60     }
61 
62     /**
63      * Returns a new matcher with returns true if both this and {@param matcher} returns true.
64      */
and(ItemInfoMatcher matcher)65     default ItemInfoMatcher and(ItemInfoMatcher matcher) {
66         return (info, cn) -> matches(info, cn) && matcher.matches(info, cn);
67     }
68 
69     /**
70      * Returns a new matcher with returns the opposite value of this.
71      */
negate()72     default ItemInfoMatcher negate() {
73         return (info, cn) -> !matches(info, cn);
74     }
75 
ofUser(UserHandle user)76     static ItemInfoMatcher ofUser(UserHandle user) {
77         return (info, cn) -> info.user.equals(user);
78     }
79 
ofComponents(HashSet<ComponentName> components, UserHandle user)80     static ItemInfoMatcher ofComponents(HashSet<ComponentName> components, UserHandle user) {
81         return (info, cn) -> components.contains(cn) && info.user.equals(user);
82     }
83 
ofPackages(Set<String> packageNames, UserHandle user)84     static ItemInfoMatcher ofPackages(Set<String> packageNames, UserHandle user) {
85         return (info, cn) -> packageNames.contains(cn.getPackageName()) && info.user.equals(user);
86     }
87 
ofShortcutKeys(Set<ShortcutKey> keys)88     static ItemInfoMatcher ofShortcutKeys(Set<ShortcutKey> keys) {
89         return (info, cn) -> info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT
90                 && keys.contains(ShortcutKey.fromItemInfo(info));
91     }
92 
93     /**
94      * Returns a matcher for items within folders.
95      */
forFolderMatch(ItemInfoMatcher childOperator)96     static ItemInfoMatcher forFolderMatch(ItemInfoMatcher childOperator) {
97         return (info, cn) -> info instanceof FolderInfo && ((FolderInfo) info).contents.stream()
98                 .anyMatch(childOperator::matchesInfo);
99     }
100 
101     /**
102      * Returns a matcher for items with provided ids
103      */
ofItemIds(IntSet ids)104     static ItemInfoMatcher ofItemIds(IntSet ids) {
105         return (info, cn) -> ids.contains(info.id);
106     }
107 
108     /**
109      * Returns a matcher for items with provided items
110      */
ofItems(Collection<? extends ItemInfo> items)111     static ItemInfoMatcher ofItems(Collection<? extends ItemInfo> items) {
112         IntSet ids = new IntSet();
113         items.forEach(item -> ids.add(item.id));
114         return ofItemIds(ids);
115     }
116 }
117