• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.stream.IntStream;
28 
29 /**
30  * Utils class for {@link com.android.launcher3.LauncherModel}.
31  */
32 public class ModelUtils {
33 
34     /**
35      * Filters the set of items who are directly or indirectly (via another container) on the
36      * specified screen.
37      */
filterCurrentWorkspaceItems( final IntSet currentScreenIds, ArrayList<T> allWorkspaceItems, ArrayList<T> currentScreenItems, ArrayList<T> otherScreenItems)38     public static <T extends ItemInfo> void filterCurrentWorkspaceItems(
39             final IntSet currentScreenIds,
40             ArrayList<T> allWorkspaceItems,
41             ArrayList<T> currentScreenItems,
42             ArrayList<T> otherScreenItems) {
43         // Purge any null ItemInfos
44         allWorkspaceItems.removeIf(Objects::isNull);
45         // Order the set of items by their containers first, this allows use to walk through the
46         // list sequentially, build up a list of containers that are in the specified screen,
47         // as well as all items in those containers.
48         IntSet itemsOnScreen = new IntSet();
49         Collections.sort(allWorkspaceItems,
50                 (lhs, rhs) -> Integer.compare(lhs.container, rhs.container));
51         for (T info : allWorkspaceItems) {
52             if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
53                 if (currentScreenIds.contains(info.screenId)) {
54                     currentScreenItems.add(info);
55                     itemsOnScreen.add(info.id);
56                 } else {
57                     otherScreenItems.add(info);
58                 }
59             } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
60                 currentScreenItems.add(info);
61                 itemsOnScreen.add(info.id);
62             } else {
63                 if (itemsOnScreen.contains(info.container)) {
64                     currentScreenItems.add(info);
65                     itemsOnScreen.add(info.id);
66                 } else {
67                     otherScreenItems.add(info);
68                 }
69             }
70         }
71     }
72 
73     /**
74      * Iterates though current workspace items and returns available hotseat ranks for prediction.
75      */
getMissingHotseatRanks(List<ItemInfo> items, int len)76     public static IntArray getMissingHotseatRanks(List<ItemInfo> items, int len) {
77         IntSet seen = new IntSet();
78         items.stream().filter(
79                 info -> info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)
80                 .forEach(i -> seen.add(i.screenId));
81         IntArray result = new IntArray(len);
82         IntStream.range(0, len).filter(i -> !seen.contains(i)).forEach(result::add);
83         return result;
84     }
85 }
86