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 18 import com.android.launcher3.LauncherSettings; 19 import com.android.launcher3.model.data.ItemInfo; 20 import com.android.launcher3.util.IntArray; 21 import com.android.launcher3.util.IntSet; 22 23 import java.util.Collections; 24 import java.util.List; 25 import java.util.Objects; 26 import java.util.stream.IntStream; 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 */ filterCurrentWorkspaceItems( final IntSet currentScreenIds, List<? extends T> allWorkspaceItems, List<T> currentScreenItems, List<T> otherScreenItems)37 public static <T extends ItemInfo> void filterCurrentWorkspaceItems( 38 final IntSet currentScreenIds, 39 List<? extends T> allWorkspaceItems, 40 List<T> currentScreenItems, 41 List<T> otherScreenItems) { 42 // Purge any null ItemInfos 43 allWorkspaceItems.removeIf(Objects::isNull); 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) { 52 if (currentScreenIds.contains(info.screenId)) { 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 /** 73 * Iterates though current workspace items and returns available hotseat ranks for prediction. 74 */ getMissingHotseatRanks(List<ItemInfo> items, int len)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 } 84 } 85