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 androidx.annotation.NonNull;
23 
24 import com.android.launcher3.LauncherSettings.Favorites;
25 import com.android.launcher3.model.data.FolderInfo;
26 import com.android.launcher3.model.data.ItemInfo;
27 import com.android.launcher3.shortcuts.ShortcutKey;
28 
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.Set;
32 import java.util.function.Predicate;
33 
34 /**
35  * A utility class to check for {@link ItemInfo}
36  */
37 public abstract class ItemInfoMatcher {
38 
39     /**
40      * Empty component used for match testing
41      */
42     private static final ComponentName EMPTY_COMPONENT = new ComponentName("", "");
43 
ofUser(UserHandle user)44     public static Predicate<ItemInfo> ofUser(UserHandle user) {
45         return info -> info != null && info.user.equals(user);
46     }
47 
ofComponents( HashSet<ComponentName> components, UserHandle user)48     public static Predicate<ItemInfo> ofComponents(
49             HashSet<ComponentName> components, UserHandle user) {
50         return info -> info != null && info.user.equals(user)
51                 && components.contains(getNonNullComponent(info));
52     }
53 
ofPackages(Set<String> packageNames, UserHandle user)54     public static Predicate<ItemInfo> ofPackages(Set<String> packageNames, UserHandle user) {
55         return info -> info != null && info.user.equals(user)
56                 && packageNames.contains(getNonNullComponent(info).getPackageName());
57     }
58 
ofShortcutKeys(Set<ShortcutKey> keys)59     public static Predicate<ItemInfo> ofShortcutKeys(Set<ShortcutKey> keys) {
60         return info -> info != null && info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT
61                 && keys.contains(ShortcutKey.fromItemInfo(info));
62     }
63 
64     /**
65      * Returns a matcher for items within folders.
66      */
forFolderMatch(Predicate<ItemInfo> childOperator)67     public static Predicate<ItemInfo> forFolderMatch(Predicate<ItemInfo> childOperator) {
68         return info -> info instanceof FolderInfo && ((FolderInfo) info).getContents().stream()
69                 .anyMatch(childOperator);
70     }
71 
72     /**
73      * Returns a matcher for items with provided ids
74      */
ofItemIds(IntSet ids)75     public static Predicate<ItemInfo> ofItemIds(IntSet ids) {
76         return info -> info != null && ids.contains(info.id);
77     }
78 
79     /**
80      * Returns a matcher for items with provided items
81      */
ofItems(Collection<? extends ItemInfo> items)82     public static Predicate<ItemInfo> ofItems(Collection<? extends ItemInfo> items) {
83         IntSet ids = new IntSet();
84         items.forEach(item -> ids.add(item.id));
85         return ofItemIds(ids);
86     }
87 
getNonNullComponent(@onNull ItemInfo info)88     private static ComponentName getNonNullComponent(@NonNull ItemInfo info) {
89         ComponentName cn = info.getTargetComponent();
90         return cn != null ? cn : EMPTY_COMPONENT;
91     }
92 }
93