summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/util/ItemInfoMatcher.java
blob: 8a27381a0bc8c20bae93278936af3f68a2245ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.util;

import android.content.ComponentName;
import android.os.UserHandle;
import android.util.Log;

import androidx.annotation.NonNull;

import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.model.data.FolderInfo;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.shortcuts.ShortcutKey;
import com.android.launcher3.testing.shared.TestProtocol;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

/**
 * A utility class to check for {@link ItemInfo}
 */
public abstract class ItemInfoMatcher {

    /**
     * Empty component used for match testing
     */
    private static final ComponentName EMPTY_COMPONENT = new ComponentName("", "");

    public static Predicate<ItemInfo> ofUser(UserHandle user) {
        return info -> {
            if (TestProtocol.sDebugTracing) {
                Log.d(TestProtocol.WORK_TAB_MISSING, "userHandle: " + user
                        + ", itemUserHandle: " + info.user
                        + " package: " + info.getTargetPackage());
            }
            return info != null && info.user.equals(user);
        };
    }

    public static Predicate<ItemInfo> ofComponents(
            HashSet<ComponentName> components, UserHandle user) {
        return info -> info != null && info.user.equals(user)
                && components.contains(getNonNullComponent(info));
    }

    public static Predicate<ItemInfo> ofPackages(Set<String> packageNames, UserHandle user) {
        return info -> info != null && info.user.equals(user)
                && packageNames.contains(getNonNullComponent(info).getPackageName());
    }

    public static Predicate<ItemInfo> ofShortcutKeys(Set<ShortcutKey> keys) {
        return info -> info != null && info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT
                && keys.contains(ShortcutKey.fromItemInfo(info));
    }

    /**
     * Returns a matcher for items within folders.
     */
    public static Predicate<ItemInfo> forFolderMatch(Predicate<ItemInfo> childOperator) {
        return info -> info instanceof FolderInfo && ((FolderInfo) info).contents.stream()
                .anyMatch(childOperator);
    }

    /**
     * Returns a matcher for items with provided ids
     */
    public static Predicate<ItemInfo> ofItemIds(IntSet ids) {
        return info -> info != null && ids.contains(info.id);
    }

    /**
     * Returns a matcher for items with provided items
     */
    public static Predicate<ItemInfo> ofItems(Collection<? extends ItemInfo> items) {
        IntSet ids = new IntSet();
        items.forEach(item -> ids.add(item.id));
        return ofItemIds(ids);
    }

    private static ComponentName getNonNullComponent(@NonNull ItemInfo info) {
        ComponentName cn = info.getTargetComponent();
        return cn != null ? cn : EMPTY_COMPONENT;
    }
}