• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.util.SparseLongArray;
22 
23 import com.android.launcher3.FolderInfo;
24 import com.android.launcher3.ItemInfo;
25 import com.android.launcher3.LauncherAppWidgetInfo;
26 import com.android.launcher3.LauncherSettings.Favorites;
27 import com.android.launcher3.ShortcutInfo;
28 import com.android.launcher3.shortcuts.ShortcutKey;
29 
30 import java.util.HashSet;
31 
32 /**
33  * A utility class to check for {@link ItemInfo}
34  */
35 public abstract class ItemInfoMatcher {
36 
matches(ItemInfo info, ComponentName cn)37     public abstract boolean matches(ItemInfo info, ComponentName cn);
38 
39     /**
40      * Filters {@param infos} to those satisfying the {@link #matches(ItemInfo, ComponentName)}.
41      */
filterItemInfos(Iterable<ItemInfo> infos)42     public final HashSet<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos) {
43         HashSet<ItemInfo> filtered = new HashSet<>();
44         for (ItemInfo i : infos) {
45             if (i instanceof ShortcutInfo) {
46                 ShortcutInfo info = (ShortcutInfo) i;
47                 ComponentName cn = info.getTargetComponent();
48                 if (cn != null && matches(info, cn)) {
49                     filtered.add(info);
50                 }
51             } else if (i instanceof FolderInfo) {
52                 FolderInfo info = (FolderInfo) i;
53                 for (ShortcutInfo s : info.contents) {
54                     ComponentName cn = s.getTargetComponent();
55                     if (cn != null && matches(s, cn)) {
56                         filtered.add(s);
57                     }
58                 }
59             } else if (i instanceof LauncherAppWidgetInfo) {
60                 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
61                 ComponentName cn = info.providerName;
62                 if (cn != null && matches(info, cn)) {
63                     filtered.add(info);
64                 }
65             }
66         }
67         return filtered;
68     }
69 
70     /**
71      * Returns a new matcher with returns true if either this or {@param matcher} returns true.
72      */
or(final ItemInfoMatcher matcher)73     public ItemInfoMatcher or(final ItemInfoMatcher matcher) {
74        final ItemInfoMatcher that = this;
75         return new ItemInfoMatcher() {
76             @Override
77             public boolean matches(ItemInfo info, ComponentName cn) {
78                 return that.matches(info, cn) || matcher.matches(info, cn);
79             }
80         };
81     }
82 
83     /**
84      * Returns a new matcher with returns true if both this and {@param matcher} returns true.
85      */
86     public ItemInfoMatcher and(final ItemInfoMatcher matcher) {
87         final ItemInfoMatcher that = this;
88         return new ItemInfoMatcher() {
89             @Override
90             public boolean matches(ItemInfo info, ComponentName cn) {
91                 return that.matches(info, cn) && matcher.matches(info, cn);
92             }
93         };
94     }
95 
96     /**
97      * Returns a new matcher which returns the opposite boolean value of the provided
98      * {@param matcher}.
99      */
100     public static ItemInfoMatcher not(final ItemInfoMatcher matcher) {
101         return new ItemInfoMatcher() {
102             @Override
103             public boolean matches(ItemInfo info, ComponentName cn) {
104                 return !matcher.matches(info, cn);
105             }
106         };
107     }
108 
109     public static ItemInfoMatcher ofUser(final UserHandle user) {
110         return new ItemInfoMatcher() {
111             @Override
112             public boolean matches(ItemInfo info, ComponentName cn) {
113                 return info.user.equals(user);
114             }
115         };
116     }
117 
118     public static ItemInfoMatcher ofComponents(
119             final HashSet<ComponentName> components, final UserHandle user) {
120         return new ItemInfoMatcher() {
121             @Override
122             public boolean matches(ItemInfo info, ComponentName cn) {
123                 return components.contains(cn) && info.user.equals(user);
124             }
125         };
126     }
127 
128     public static ItemInfoMatcher ofPackages(
129             final HashSet<String> packageNames, final UserHandle user) {
130         return new ItemInfoMatcher() {
131             @Override
132             public boolean matches(ItemInfo info, ComponentName cn) {
133                 return packageNames.contains(cn.getPackageName()) && info.user.equals(user);
134             }
135         };
136     }
137 
138     public static ItemInfoMatcher ofShortcutKeys(final HashSet<ShortcutKey> keys) {
139         return new ItemInfoMatcher() {
140             @Override
141             public boolean matches(ItemInfo info, ComponentName cn) {
142                 return info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
143                         keys.contains(ShortcutKey.fromItemInfo(info));
144             }
145         };
146     }
147 
148     public static ItemInfoMatcher ofItemIds(
149             final LongArrayMap<Boolean> ids, final Boolean matchDefault) {
150         return new ItemInfoMatcher() {
151             @Override
152             public boolean matches(ItemInfo info, ComponentName cn) {
153                 return ids.get(info.id, matchDefault);
154             }
155         };
156     }
157 }
158