• 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     public static ItemInfoMatcher ofUser(final UserHandle user) {
97         return new ItemInfoMatcher() {
98             @Override
99             public boolean matches(ItemInfo info, ComponentName cn) {
100                 return info.user.equals(user);
101             }
102         };
103     }
104 
105     public static ItemInfoMatcher ofComponents(
106             final HashSet<ComponentName> components, final UserHandle user) {
107         return new ItemInfoMatcher() {
108             @Override
109             public boolean matches(ItemInfo info, ComponentName cn) {
110                 return components.contains(cn) && info.user.equals(user);
111             }
112         };
113     }
114 
115     public static ItemInfoMatcher ofPackages(
116             final HashSet<String> packageNames, final UserHandle user) {
117         return new ItemInfoMatcher() {
118             @Override
119             public boolean matches(ItemInfo info, ComponentName cn) {
120                 return packageNames.contains(cn.getPackageName()) && info.user.equals(user);
121             }
122         };
123     }
124 
125     public static ItemInfoMatcher ofShortcutKeys(final HashSet<ShortcutKey> keys) {
126         return new ItemInfoMatcher() {
127             @Override
128             public boolean matches(ItemInfo info, ComponentName cn) {
129                 return info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
130                         keys.contains(ShortcutKey.fromItemInfo(info));
131             }
132         };
133     }
134 
135     public static ItemInfoMatcher ofItemIds(
136             final LongArrayMap<Boolean> ids, final Boolean matchDefault) {
137         return new ItemInfoMatcher() {
138             @Override
139             public boolean matches(ItemInfo info, ComponentName cn) {
140                 return ids.get(info.id, matchDefault);
141             }
142         };
143     }
144 }
145