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.model.data.LauncherAppWidgetInfo;
26 import com.android.launcher3.model.data.WorkspaceItemInfo;
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 interface ItemInfoMatcher {
35 
matches(ItemInfo info, ComponentName cn)36     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     default HashSet<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos) {
42         HashSet<ItemInfo> filtered = new HashSet<>();
43         for (ItemInfo i : infos) {
44             if (i instanceof WorkspaceItemInfo) {
45                 WorkspaceItemInfo info = (WorkspaceItemInfo) 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 (WorkspaceItemInfo 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 
69     /**
70      * Returns a new matcher with returns true if either this or {@param matcher} returns true.
71      */
or(ItemInfoMatcher matcher)72     default ItemInfoMatcher or(ItemInfoMatcher matcher) {
73         return (info, cn) -> matches(info, cn) || matcher.matches(info, cn);
74     }
75 
76     /**
77      * Returns a new matcher with returns true if both this and {@param matcher} returns true.
78      */
and(ItemInfoMatcher matcher)79     default ItemInfoMatcher and(ItemInfoMatcher matcher) {
80         return (info, cn) -> matches(info, cn) && matcher.matches(info, cn);
81     }
82 
83     /**
84      * Returns a new matcher which returns the opposite boolean value of the provided
85      * {@param matcher}.
86      */
not(ItemInfoMatcher matcher)87     static ItemInfoMatcher not(ItemInfoMatcher matcher) {
88         return (info, cn) -> !matcher.matches(info, cn);
89     }
90 
ofUser(UserHandle user)91     static ItemInfoMatcher ofUser(UserHandle user) {
92         return (info, cn) -> info.user.equals(user);
93     }
94 
ofComponents(HashSet<ComponentName> components, UserHandle user)95     static ItemInfoMatcher ofComponents(HashSet<ComponentName> components, UserHandle user) {
96         return (info, cn) -> components.contains(cn) && info.user.equals(user);
97     }
98 
ofPackages(HashSet<String> packageNames, UserHandle user)99     static ItemInfoMatcher ofPackages(HashSet<String> packageNames, UserHandle user) {
100         return (info, cn) -> packageNames.contains(cn.getPackageName()) && info.user.equals(user);
101     }
102 
ofShortcutKeys(HashSet<ShortcutKey> keys)103     static ItemInfoMatcher ofShortcutKeys(HashSet<ShortcutKey> keys) {
104         return  (info, cn) -> info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
105                         keys.contains(ShortcutKey.fromItemInfo(info));
106     }
107 
ofItemIds(IntSparseArrayMap<Boolean> ids, Boolean matchDefault)108     static ItemInfoMatcher ofItemIds(IntSparseArrayMap<Boolean> ids, Boolean matchDefault) {
109         return (info, cn) -> ids.get(info.id, matchDefault);
110     }
111 }
112