2016-07-28 12:11:54 -07:00
|
|
|
/*
|
|
|
|
|
* 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;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.UserHandle;
|
2023-05-11 10:39:20 -07:00
|
|
|
import android.util.Log;
|
2016-07-28 12:11:54 -07:00
|
|
|
|
2022-05-02 10:00:07 -07:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
2016-07-28 12:11:54 -07:00
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2021-11-04 16:53:45 -07:00
|
|
|
import com.android.launcher3.model.data.FolderInfo;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
2016-07-28 12:11:54 -07:00
|
|
|
import com.android.launcher3.shortcuts.ShortcutKey;
|
2023-05-11 10:39:20 -07:00
|
|
|
import com.android.launcher3.testing.shared.TestProtocol;
|
2016-07-28 12:11:54 -07:00
|
|
|
|
2021-06-23 12:36:18 -07:00
|
|
|
import java.util.Collection;
|
2016-07-28 12:11:54 -07:00
|
|
|
import java.util.HashSet;
|
2020-08-22 02:09:42 -07:00
|
|
|
import java.util.Set;
|
2022-05-02 10:00:07 -07:00
|
|
|
import java.util.function.Predicate;
|
2016-07-28 12:11:54 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A utility class to check for {@link ItemInfo}
|
|
|
|
|
*/
|
2022-05-02 10:00:07 -07:00
|
|
|
public abstract class ItemInfoMatcher {
|
2016-07-28 12:11:54 -07:00
|
|
|
|
2021-09-08 12:28:25 -04:00
|
|
|
/**
|
|
|
|
|
* Empty component used for match testing
|
|
|
|
|
*/
|
2022-05-02 10:00:07 -07:00
|
|
|
private static final ComponentName EMPTY_COMPONENT = new ComponentName("", "");
|
2017-10-18 10:31:36 -07:00
|
|
|
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> ofUser(UserHandle user) {
|
2023-05-11 10:39:20 -07:00
|
|
|
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);
|
|
|
|
|
};
|
2016-09-01 15:17:46 -07:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> ofComponents(
|
|
|
|
|
HashSet<ComponentName> components, UserHandle user) {
|
|
|
|
|
return info -> info != null && info.user.equals(user)
|
|
|
|
|
&& components.contains(getNonNullComponent(info));
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> ofPackages(Set<String> packageNames, UserHandle user) {
|
|
|
|
|
return info -> info != null && info.user.equals(user)
|
|
|
|
|
&& packageNames.contains(getNonNullComponent(info).getPackageName());
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> ofShortcutKeys(Set<ShortcutKey> keys) {
|
|
|
|
|
return info -> info != null && info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT
|
2021-11-04 16:53:45 -07:00
|
|
|
&& keys.contains(ShortcutKey.fromItemInfo(info));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a matcher for items within folders.
|
|
|
|
|
*/
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> forFolderMatch(Predicate<ItemInfo> childOperator) {
|
|
|
|
|
return info -> info instanceof FolderInfo && ((FolderInfo) info).contents.stream()
|
|
|
|
|
.anyMatch(childOperator);
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
2017-08-17 03:01:19 -07:00
|
|
|
|
2020-07-09 19:31:40 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a matcher for items with provided ids
|
|
|
|
|
*/
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> ofItemIds(IntSet ids) {
|
|
|
|
|
return info -> info != null && ids.contains(info.id);
|
2017-08-17 03:01:19 -07:00
|
|
|
}
|
2021-06-23 12:36:18 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a matcher for items with provided items
|
|
|
|
|
*/
|
2022-05-02 10:00:07 -07:00
|
|
|
public static Predicate<ItemInfo> ofItems(Collection<? extends ItemInfo> items) {
|
2021-06-23 12:36:18 -07:00
|
|
|
IntSet ids = new IntSet();
|
|
|
|
|
items.forEach(item -> ids.add(item.id));
|
|
|
|
|
return ofItemIds(ids);
|
|
|
|
|
}
|
2022-05-02 10:00:07 -07:00
|
|
|
|
|
|
|
|
private static ComponentName getNonNullComponent(@NonNull ItemInfo info) {
|
|
|
|
|
ComponentName cn = info.getTargetComponent();
|
|
|
|
|
return cn != null ? cn : EMPTY_COMPONENT;
|
|
|
|
|
}
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|