blob: 9e72e2823e144b9b9947d685093f71ca8650a00a [file] [log] [blame]
Tracy Zhou44fa2942019-11-29 21:47:29 -08001/*
2 * Copyright (C) 2019 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 */
16package com.android.launcher3.model;
17
Tracy Zhou44fa2942019-11-29 21:47:29 -080018import com.android.launcher3.LauncherSettings;
Sunny Goyale396abf2020-04-06 15:11:17 -070019import com.android.launcher3.model.data.ItemInfo;
Samuel Fufa385e9322020-05-04 11:14:52 -070020import com.android.launcher3.util.IntArray;
Tracy Zhou44fa2942019-11-29 21:47:29 -080021import com.android.launcher3.util.IntSet;
22
Tracy Zhou44fa2942019-11-29 21:47:29 -080023import java.util.Collections;
Samuel Fufa385e9322020-05-04 11:14:52 -070024import java.util.List;
Sunny Goyald4d2a732020-08-22 02:09:42 -070025import java.util.Objects;
Samuel Fufa385e9322020-05-04 11:14:52 -070026import java.util.stream.IntStream;
Tracy Zhou44fa2942019-11-29 21:47:29 -080027
28/**
29 * Utils class for {@link com.android.launcher3.LauncherModel}.
30 */
31public class ModelUtils {
32
33 /**
34 * Filters the set of items who are directly or indirectly (via another container) on the
35 * specified screen.
36 */
Andras Kloczl55edfe52021-05-14 12:21:30 +020037 public static <T extends ItemInfo> void filterCurrentWorkspaceItems(
Sunny Goyal3c354422022-05-13 09:01:14 -070038 final IntSet currentScreenIds,
Sunny Goyal72a74662024-02-14 12:32:59 -080039 List<? extends T> allWorkspaceItems,
40 List<T> currentScreenItems,
41 List<T> otherScreenItems) {
Tracy Zhou44fa2942019-11-29 21:47:29 -080042 // Purge any null ItemInfos
Sunny Goyald4d2a732020-08-22 02:09:42 -070043 allWorkspaceItems.removeIf(Objects::isNull);
Tracy Zhou44fa2942019-11-29 21:47:29 -080044 // Order the set of items by their containers first, this allows use to walk through the
45 // list sequentially, build up a list of containers that are in the specified screen,
46 // as well as all items in those containers.
47 IntSet itemsOnScreen = new IntSet();
48 Collections.sort(allWorkspaceItems,
49 (lhs, rhs) -> Integer.compare(lhs.container, rhs.container));
50 for (T info : allWorkspaceItems) {
51 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Andras Kloczl55edfe52021-05-14 12:21:30 +020052 if (currentScreenIds.contains(info.screenId)) {
Tracy Zhou44fa2942019-11-29 21:47:29 -080053 currentScreenItems.add(info);
54 itemsOnScreen.add(info.id);
55 } else {
56 otherScreenItems.add(info);
57 }
58 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
59 currentScreenItems.add(info);
60 itemsOnScreen.add(info.id);
61 } else {
62 if (itemsOnScreen.contains(info.container)) {
63 currentScreenItems.add(info);
64 itemsOnScreen.add(info.id);
65 } else {
66 otherScreenItems.add(info);
67 }
68 }
69 }
70 }
71
72 /**
Samuel Fufa385e9322020-05-04 11:14:52 -070073 * Iterates though current workspace items and returns available hotseat ranks for prediction.
74 */
75 public static IntArray getMissingHotseatRanks(List<ItemInfo> items, int len) {
76 IntSet seen = new IntSet();
77 items.stream().filter(
78 info -> info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
79 .forEach(i -> seen.add(i.screenId));
80 IntArray result = new IntArray(len);
81 IntStream.range(0, len).filter(i -> !seen.contains(i)).forEach(result::add);
82 return result;
83 }
Tracy Zhou44fa2942019-11-29 21:47:29 -080084}