Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.launcher3.model; |
| 17 | |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 18 | import com.android.launcher3.LauncherSettings; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 19 | import com.android.launcher3.model.data.ItemInfo; |
Samuel Fufa | 385e932 | 2020-05-04 11:14:52 -0700 | [diff] [blame] | 20 | import com.android.launcher3.util.IntArray; |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 21 | import com.android.launcher3.util.IntSet; |
| 22 | |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 23 | import java.util.Collections; |
Samuel Fufa | 385e932 | 2020-05-04 11:14:52 -0700 | [diff] [blame] | 24 | import java.util.List; |
Sunny Goyal | d4d2a73 | 2020-08-22 02:09:42 -0700 | [diff] [blame] | 25 | import java.util.Objects; |
Samuel Fufa | 385e932 | 2020-05-04 11:14:52 -0700 | [diff] [blame] | 26 | import java.util.stream.IntStream; |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Utils class for {@link com.android.launcher3.LauncherModel}. |
| 30 | */ |
| 31 | public 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 Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 37 | public static <T extends ItemInfo> void filterCurrentWorkspaceItems( |
Sunny Goyal | 3c35442 | 2022-05-13 09:01:14 -0700 | [diff] [blame] | 38 | final IntSet currentScreenIds, |
Sunny Goyal | 72a7466 | 2024-02-14 12:32:59 -0800 | [diff] [blame] | 39 | List<? extends T> allWorkspaceItems, |
| 40 | List<T> currentScreenItems, |
| 41 | List<T> otherScreenItems) { |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 42 | // Purge any null ItemInfos |
Sunny Goyal | d4d2a73 | 2020-08-22 02:09:42 -0700 | [diff] [blame] | 43 | allWorkspaceItems.removeIf(Objects::isNull); |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 44 | // 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 Kloczl | 55edfe5 | 2021-05-14 12:21:30 +0200 | [diff] [blame] | 52 | if (currentScreenIds.contains(info.screenId)) { |
Tracy Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 53 | 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 Fufa | 385e932 | 2020-05-04 11:14:52 -0700 | [diff] [blame] | 73 | * 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 Zhou | 44fa294 | 2019-11-29 21:47:29 -0800 | [diff] [blame] | 84 | } |