1d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal/*
2d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * Copyright (C) 2016 The Android Open Source Project
3d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal *
4d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * you may not use this file except in compliance with the License.
6d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * You may obtain a copy of the License at
7d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal *
8d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal *
10d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * Unless required by applicable law or agreed to in writing, software
11d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * See the License for the specific language governing permissions and
14d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * limitations under the License.
15d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal */
16d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
17d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalpackage com.android.launcher3.util;
18d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
19d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalimport android.content.ComponentName;
207c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyalimport android.os.UserHandle;
216e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyalimport android.util.SparseLongArray;
22d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
2340452cf468b44a4173338cc83000b4ad84860ebcSunny Goyalimport com.android.launcher3.FolderInfo;
24d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalimport com.android.launcher3.ItemInfo;
2540452cf468b44a4173338cc83000b4ad84860ebcSunny Goyalimport com.android.launcher3.LauncherAppWidgetInfo;
26d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalimport com.android.launcher3.LauncherSettings.Favorites;
27fc02c1b446ee54561ac7351fb6ff0f8294785f0eTony Wickhamimport com.android.launcher3.ShortcutInfo;
28d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalimport com.android.launcher3.shortcuts.ShortcutKey;
29d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
30d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalimport java.util.HashSet;
31d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
32d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal/**
33d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal * A utility class to check for {@link ItemInfo}
34d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal */
35d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyalpublic abstract class ItemInfoMatcher {
36d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
37d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public abstract boolean matches(ItemInfo info, ComponentName cn);
38d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
3940452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal    /**
4040452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal     * Filters {@param infos} to those satisfying the {@link #matches(ItemInfo, ComponentName)}.
4140452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal     */
4240452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal    public final HashSet<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos) {
4340452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal        HashSet<ItemInfo> filtered = new HashSet<>();
4440452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal        for (ItemInfo i : infos) {
4540452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            if (i instanceof ShortcutInfo) {
4640452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                ShortcutInfo info = (ShortcutInfo) i;
4740452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                ComponentName cn = info.getTargetComponent();
4840452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                if (cn != null && matches(info, cn)) {
4940452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                    filtered.add(info);
5040452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                }
5140452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            } else if (i instanceof FolderInfo) {
5240452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                FolderInfo info = (FolderInfo) i;
5340452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                for (ShortcutInfo s : info.contents) {
5440452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                    ComponentName cn = s.getTargetComponent();
5540452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                    if (cn != null && matches(s, cn)) {
5640452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                        filtered.add(s);
5740452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                    }
5840452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                }
5940452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            } else if (i instanceof LauncherAppWidgetInfo) {
6040452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
6140452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                ComponentName cn = info.providerName;
6240452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                if (cn != null && matches(info, cn)) {
6340452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                    filtered.add(info);
6440452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                }
6540452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            }
6640452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal        }
6740452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal        return filtered;
6840452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal    }
6940452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal
706e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    /**
716e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal     * Returns a new matcher with returns true if either this or {@param matcher} returns true.
726e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal     */
736e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    public ItemInfoMatcher or(final ItemInfoMatcher matcher) {
746e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal       final ItemInfoMatcher that = this;
756e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        return new ItemInfoMatcher() {
766e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            @Override
776e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
786e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal                return that.matches(info, cn) || matcher.matches(info, cn);
796e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            }
806e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        };
816e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    }
826e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal
836e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    /**
846e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal     * Returns a new matcher with returns true if both this and {@param matcher} returns true.
856e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal     */
866e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    public ItemInfoMatcher and(final ItemInfoMatcher matcher) {
876e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        final ItemInfoMatcher that = this;
886e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        return new ItemInfoMatcher() {
896e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            @Override
906e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
916e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal                return that.matches(info, cn) && matcher.matches(info, cn);
926e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            }
936e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        };
946e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    }
956e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal
967c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal    public static ItemInfoMatcher ofUser(final UserHandle user) {
9740452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal        return new ItemInfoMatcher() {
9840452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            @Override
9940452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
10040452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal                return info.user.equals(user);
10140452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal            }
10240452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal        };
10340452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal    }
10440452cf468b44a4173338cc83000b4ad84860ebcSunny Goyal
105d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public static ItemInfoMatcher ofComponents(
1067c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal            final HashSet<ComponentName> components, final UserHandle user) {
107d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        return new ItemInfoMatcher() {
108d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            @Override
109d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
110d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal                return components.contains(cn) && info.user.equals(user);
111d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            }
112d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        };
113d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    }
114d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
115d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public static ItemInfoMatcher ofPackages(
1167c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal            final HashSet<String> packageNames, final UserHandle user) {
117d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        return new ItemInfoMatcher() {
118d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            @Override
119d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
120d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal                return packageNames.contains(cn.getPackageName()) && info.user.equals(user);
121d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            }
122d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        };
123d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    }
124d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
125d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public static ItemInfoMatcher ofShortcutKeys(final HashSet<ShortcutKey> keys) {
126d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        return new ItemInfoMatcher() {
127d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            @Override
128d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
129d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal                return info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
1303fe4a146cf774261ab3552dd8ab392439c771e54Sunny Goyal                        keys.contains(ShortcutKey.fromItemInfo(info));
131d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            }
132d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        };
133d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    }
1346e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal
1356e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    public static ItemInfoMatcher ofItemIds(
1366e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            final LongArrayMap<Boolean> ids, final Boolean matchDefault) {
1376e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        return new ItemInfoMatcher() {
1386e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            @Override
1396e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            public boolean matches(ItemInfo info, ComponentName cn) {
1406e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal                return ids.get(info.id, matchDefault);
1416e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal            }
1426e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal        };
1436e13dd3aa867d01f3f1d196ac82eca184328577cSunny Goyal    }
144d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal}
145