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;
|
2016-07-28 12:11:54 -07:00
|
|
|
|
|
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.FolderInfo;
|
|
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
|
|
|
|
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
|
|
|
|
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
2016-07-28 12:11:54 -07:00
|
|
|
import com.android.launcher3.shortcuts.ShortcutKey;
|
|
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A utility class to check for {@link ItemInfo}
|
|
|
|
|
*/
|
2018-07-09 16:47:01 -07:00
|
|
|
public interface ItemInfoMatcher {
|
2016-07-28 12:11:54 -07:00
|
|
|
|
2018-07-09 16:47:01 -07:00
|
|
|
boolean matches(ItemInfo info, ComponentName cn);
|
2016-07-28 12:11:54 -07:00
|
|
|
|
2016-09-01 15:17:46 -07:00
|
|
|
/**
|
|
|
|
|
* Filters {@param infos} to those satisfying the {@link #matches(ItemInfo, ComponentName)}.
|
|
|
|
|
*/
|
2018-07-09 16:47:01 -07:00
|
|
|
default HashSet<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos) {
|
2016-09-01 15:17:46 -07:00
|
|
|
HashSet<ItemInfo> filtered = new HashSet<>();
|
|
|
|
|
for (ItemInfo i : infos) {
|
2019-03-27 16:03:06 -07:00
|
|
|
if (i instanceof WorkspaceItemInfo) {
|
|
|
|
|
WorkspaceItemInfo info = (WorkspaceItemInfo) i;
|
2016-09-01 15:17:46 -07:00
|
|
|
ComponentName cn = info.getTargetComponent();
|
|
|
|
|
if (cn != null && matches(info, cn)) {
|
|
|
|
|
filtered.add(info);
|
|
|
|
|
}
|
|
|
|
|
} else if (i instanceof FolderInfo) {
|
|
|
|
|
FolderInfo info = (FolderInfo) i;
|
2019-03-27 16:03:06 -07:00
|
|
|
for (WorkspaceItemInfo s : info.contents) {
|
2016-09-01 15:17:46 -07:00
|
|
|
ComponentName cn = s.getTargetComponent();
|
|
|
|
|
if (cn != null && matches(s, cn)) {
|
|
|
|
|
filtered.add(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (i instanceof LauncherAppWidgetInfo) {
|
|
|
|
|
LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
|
|
|
|
|
ComponentName cn = info.providerName;
|
|
|
|
|
if (cn != null && matches(info, cn)) {
|
|
|
|
|
filtered.add(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return filtered;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-17 03:01:19 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a new matcher with returns true if either this or {@param matcher} returns true.
|
|
|
|
|
*/
|
2018-07-09 16:47:01 -07:00
|
|
|
default ItemInfoMatcher or(ItemInfoMatcher matcher) {
|
|
|
|
|
return (info, cn) -> matches(info, cn) || matcher.matches(info, cn);
|
2017-08-17 03:01:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a new matcher with returns true if both this and {@param matcher} returns true.
|
|
|
|
|
*/
|
2018-07-09 16:47:01 -07:00
|
|
|
default ItemInfoMatcher and(ItemInfoMatcher matcher) {
|
|
|
|
|
return (info, cn) -> matches(info, cn) && matcher.matches(info, cn);
|
2017-08-17 03:01:19 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-18 10:31:36 -07:00
|
|
|
/**
|
2020-07-09 19:31:40 -07:00
|
|
|
* Returns a new matcher with returns the opposite value of this.
|
2017-10-18 10:31:36 -07:00
|
|
|
*/
|
2020-07-09 19:31:40 -07:00
|
|
|
default ItemInfoMatcher negate() {
|
|
|
|
|
return (info, cn) -> !matches(info, cn);
|
2017-10-18 10:31:36 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 16:47:01 -07:00
|
|
|
static ItemInfoMatcher ofUser(UserHandle user) {
|
|
|
|
|
return (info, cn) -> info.user.equals(user);
|
2016-09-01 15:17:46 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 16:47:01 -07:00
|
|
|
static ItemInfoMatcher ofComponents(HashSet<ComponentName> components, UserHandle user) {
|
|
|
|
|
return (info, cn) -> components.contains(cn) && info.user.equals(user);
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 16:47:01 -07:00
|
|
|
static ItemInfoMatcher ofPackages(HashSet<String> packageNames, UserHandle user) {
|
|
|
|
|
return (info, cn) -> packageNames.contains(cn.getPackageName()) && info.user.equals(user);
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-09 16:47:01 -07:00
|
|
|
static ItemInfoMatcher ofShortcutKeys(HashSet<ShortcutKey> keys) {
|
|
|
|
|
return (info, cn) -> info.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
|
2016-12-15 17:40:07 -08:00
|
|
|
keys.contains(ShortcutKey.fromItemInfo(info));
|
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
|
|
|
|
|
*/
|
|
|
|
|
static ItemInfoMatcher ofItemIds(IntSet ids) {
|
|
|
|
|
return (info, cn) -> ids.contains(info.id);
|
2017-08-17 03:01:19 -07:00
|
|
|
}
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|